From 2d5832af1ef5b1addfb9c989af71e031331b4efe Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Sun, 7 Jul 2019 18:45:19 +0200 Subject: [PATCH] common: Add the systick module. This implements various timer functions using the systick peripheral. --- SConscript.libcommon | 2 ++ src/common/lib.rs | 1 + src/common/systick.c | 38 ++++++++++++++++++++++ src/common/systick.rs | 76 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 src/common/systick.c create mode 100644 src/common/systick.rs diff --git a/SConscript.libcommon b/SConscript.libcommon index 54d1aaa..227c80a 100644 --- a/SConscript.libcommon +++ b/SConscript.libcommon @@ -6,6 +6,7 @@ source_files_rs = [ 'src/common/register.rs', 'src/common/nvic.rs', 'src/common/clock.rs', + 'src/common/systick.rs', 'src/common/crc32.rs', 'src/common/buffer.rs', 'src/common/usb_serial.rs', @@ -13,6 +14,7 @@ source_files_rs = [ source_files_c = [ 'src/common/startup.c', + 'src/common/systick.c', 'src/common/ringbuf.c', 'src/common/usb_device_ch9.c', 'src/common/usb_device_dci.c', diff --git a/src/common/lib.rs b/src/common/lib.rs index dfe7b40..e93861e 100644 --- a/src/common/lib.rs +++ b/src/common/lib.rs @@ -28,6 +28,7 @@ pub mod register; pub mod nvic; pub mod clock; +pub mod systick; pub mod crc32; pub mod buffer; pub mod usb_serial; diff --git a/src/common/systick.c b/src/common/systick.c new file mode 100644 index 0000000..649cd81 --- /dev/null +++ b/src/common/systick.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de) + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +static volatile uint32_t jiffies; + +void +SysTick_Handler (void) +{ + jiffies++; +} + +uint32_t +get_ticks () +{ + return jiffies; +} diff --git a/src/common/systick.rs b/src/common/systick.rs new file mode 100644 index 0000000..78730d3 --- /dev/null +++ b/src/common/systick.rs @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de) + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +use register; + +type Reg32 = register::Register; + +const SYSTICK_BASE: u32 = 0xe000e010; + +const SYSTICK_CTRL: u32 = SYSTICK_BASE + 0x00; +const SYSTICK_LOAD: u32 = SYSTICK_BASE + 0x04; +const SYSTICK_VAL : u32 = SYSTICK_BASE + 0x08; + +pub const RESOLUTION_MS : u32 = 10; + +extern { + fn get_ticks() -> u32; +} + +pub fn init() { + // Assuming a 48 MHz core clock. + let ticks = 48000000 / 1000 * RESOLUTION_MS; + + let mut load = Reg32::new(SYSTICK_LOAD); + load.write(ticks - 1); + + let mut val = Reg32::new(SYSTICK_VAL); + val.write(0); + + let mut ctrl = Reg32::new(SYSTICK_CTRL); + ctrl.write(7); +} + +pub fn now() -> u32 { + unsafe { + get_ticks() + } +} + +pub fn delay_ms(timeout_ms: u32) { + let timeout_ticks = timeout_ms / RESOLUTION_MS; + let start_ticks = now(); + + while (now() - start_ticks) < timeout_ticks { + } +} + +pub fn has_timeout_ms(start_ticks: u32, timeout_ms: u32) -> bool { + let timeout_ticks = timeout_ms / RESOLUTION_MS; + + (now() - start_ticks) >= timeout_ticks +} + +pub fn elapsed_ms(later_ticks: u32, earlier_ticks: u32) -> u32 { + (later_ticks - earlier_ticks) * RESOLUTION_MS +} -- 2.30.2