Refactoring system timer critical sections
@@ -56,8 +56,8 @@ impl Clock for SystemTimer {
// is taken when reading and modifying register values.
let regs = unsafe { &*hal::device::TIM15::ptr() };
- cortex_m::interrupt::free(|_cs| {
loop {
+ if let Some(instant) = cortex_m::interrupt::free(|_cs| {
// Checking for overflows of the current counter must be performed atomically. Any
// 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
@@ -78,14 +78,18 @@ impl Clock for SystemTimer {
if regs.sr.read().uif().bit_is_clear() {
// Note(unsafe): We are in a critical section, so it is safe to read the
// global variable.
- return unsafe {
- Ok(Instant::new(
+ unsafe {
+ Some(Instant::new(
((OVERFLOWS << 16) + current_value) as u32,
))
- };
+ }
+ } else {
+ None
+ }
+ }) {
+ return Ok(instant);
}
}
- })
}
}
@@ -102,9 +106,6 @@ impl Monotonic for SystemTimer {
fn set_compare(&mut self, instant: &Instant<Self>) {
let regs = unsafe { &*hal::device::TIM15::ptr() };
- // Note(cs): When operating on the timer registers, we must be atomic to prevent
- // pre-emption.
- cortex_m::interrupt::free(|_cs| {
let now = self.try_now().unwrap();
match now.checked_duration_until(instant) {
// If the scheduled instant is too far in the future, we can't set an exact
@@ -142,7 +143,6 @@ impl Monotonic for SystemTimer {
self.clear_compare_flag();
cortex_m::peripheral::NVIC::pend(hal::interrupt::TIM15);
}
- });
}
fn clear_compare_flag(&mut self) {
