From 81a292a77edfd985e975eb5426abe62e498d3a08 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Thu, 6 May 2021 12:45:13 +0200 Subject: [PATCH] Fixing system timer --- src/hardware/system_timer.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/hardware/system_timer.rs b/src/hardware/system_timer.rs index 4b0ba0a..715cb9f 100644 --- a/src/hardware/system_timer.rs +++ b/src/hardware/system_timer.rs @@ -74,7 +74,7 @@ impl rtic::Monotonic for SystemTimer { // other task that is accessing the current time could potentially race for the // registers. Note that this is only required for writing to global state (e.g. timer // registers and overflow counter) - cortex_m::interrupt::free(|_cs| { + if let Some(time) = cortex_m::interrupt::free(|_cs| { // Check for overflows and clear the overflow bit atomically. This must be done in // a critical section to prevent race conditions on the status register. if regs.sr.read().uif().bit_is_set() { @@ -89,9 +89,15 @@ impl rtic::Monotonic for SystemTimer { // Check that an overflow didn't occur since we just cleared the overflow bit. If // it did, loop around and retry. if regs.sr.read().uif().bit_is_clear() { - return (overflows * 65535 + current_value) as i32; + // Note(unsafe): We are in a critical section, so it is safe to read the + // global variable. + unsafe { Some((OVERFLOWS * 65535 + current_value) as i32) } + } else { + None } - }) + }) { + return time; + } } }