2 * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 type Reg32 = register::Register<u32>;
28 const SYSTICK_BASE: u32 = 0xe000e010;
30 const SYSTICK_CTRL: u32 = SYSTICK_BASE + 0x00;
31 const SYSTICK_LOAD: u32 = SYSTICK_BASE + 0x04;
32 const SYSTICK_VAL : u32 = SYSTICK_BASE + 0x08;
34 pub const RESOLUTION_MS : u32 = 10;
37 fn get_ticks() -> u32;
41 // Assuming a 48 MHz core clock.
42 let ticks = 48000000 / 1000 * RESOLUTION_MS;
44 let mut load = Reg32::new(SYSTICK_LOAD);
45 load.write(ticks - 1);
47 let mut val = Reg32::new(SYSTICK_VAL);
50 let mut ctrl = Reg32::new(SYSTICK_CTRL);
60 pub fn delay_ms(timeout_ms: u32) {
61 let timeout_ticks = timeout_ms / RESOLUTION_MS;
62 let start_ticks = now();
64 while (now() - start_ticks) < timeout_ticks {
68 pub fn has_timeout_ms(start_ticks: u32, timeout_ms: u32) -> bool {
69 let timeout_ticks = timeout_ms / RESOLUTION_MS;
71 (now() - start_ticks) >= timeout_ticks
74 pub fn elapsed_ms(later_ticks: u32, earlier_ticks: u32) -> u32 {
75 (later_ticks - earlier_ticks) * RESOLUTION_MS