--- /dev/null
+/*
+ * 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 <stdint.h>
+
+static volatile uint32_t jiffies;
+
+void
+SysTick_Handler (void)
+{
+ jiffies++;
+}
+
+uint32_t
+get_ticks ()
+{
+ return jiffies;
+}
--- /dev/null
+/*
+ * 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<u32>;
+
+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
+}