Fixing system timer

This commit is contained in:
Ryan Summers 2021-05-06 12:45:13 +02:00
parent 949ea9c1b3
commit 81a292a77e
1 changed files with 9 additions and 3 deletions

View File

@ -74,7 +74,7 @@ impl rtic::Monotonic for SystemTimer {
// other task that is accessing the current time could potentially race for the // 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. Note that this is only required for writing to global state (e.g. timer
// registers and overflow counter) // 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 // 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. // a critical section to prevent race conditions on the status register.
if regs.sr.read().uif().bit_is_set() { 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 // Check that an overflow didn't occur since we just cleared the overflow bit. If
// it did, loop around and retry. // it did, loop around and retry.
if regs.sr.read().uif().bit_is_clear() { 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;
}
} }
} }