common: Silence compiler warnings about ignored Result return values.
[gps-watch.git] / src / common / systick.rs
1 /*
2  * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
3  *
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:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
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.
22  */
23
24 use register;
25
26 type Reg32 = register::Register<u32>;
27
28 const SYSTICK_BASE: u32 = 0xe000e010;
29
30 const SYSTICK_CTRL: u32 = SYSTICK_BASE + 0x00;
31 const SYSTICK_LOAD: u32 = SYSTICK_BASE + 0x04;
32 const SYSTICK_VAL : u32 = SYSTICK_BASE + 0x08;
33
34 pub const RESOLUTION_MS : u32 = 10;
35
36 extern {
37     fn get_ticks() -> u32;
38 }
39
40 pub fn init() {
41     // Assuming a 48 MHz core clock.
42     let ticks = 48000000 / 1000 * RESOLUTION_MS;
43
44     let mut load = Reg32::new(SYSTICK_LOAD);
45     load.write(ticks - 1);
46
47     let mut val = Reg32::new(SYSTICK_VAL);
48     val.write(0);
49
50     let mut ctrl = Reg32::new(SYSTICK_CTRL);
51     ctrl.write(7);
52 }
53
54 pub fn now() -> u32 {
55     unsafe {
56         get_ticks()
57     }
58 }
59
60 pub fn delay_ms(timeout_ms: u32) {
61     let timeout_ticks = timeout_ms / RESOLUTION_MS;
62     let start_ticks = now();
63
64     while (now() - start_ticks) < timeout_ticks {
65     }
66 }
67
68 pub fn has_timeout_ms(start_ticks: u32, timeout_ms: u32) -> bool {
69     let timeout_ticks = timeout_ms / RESOLUTION_MS;
70
71     (now() - start_ticks) >= timeout_ticks
72 }
73
74 pub fn elapsed_ms(later_ticks: u32, earlier_ticks: u32) -> u32 {
75     (later_ticks - earlier_ticks) * RESOLUTION_MS
76 }