Use atomic operations for altering Timer interrupts

This commit is contained in:
9names 2022-01-30 22:44:19 +11:00
parent 5771f872f2
commit b19f2cc8e5

View file

@ -3,6 +3,7 @@
use embedded_time::duration::Microseconds; use embedded_time::duration::Microseconds;
use crate::atomic_register_access::{write_bitmask_clear, write_bitmask_set};
use crate::pac::{RESETS, TIMER}; use crate::pac::{RESETS, TIMER};
use crate::resets::SubsystemReset; use crate::resets::SubsystemReset;
use core::marker::PhantomData; use core::marker::PhantomData;
@ -200,12 +201,15 @@ macro_rules! impl_alarm {
/// ` is called. /// ` is called.
/// ///
/// The interrupt is unable to trigger a 2nd time until this interrupt is cleared. /// The interrupt is unable to trigger a 2nd time until this interrupt is cleared.
pub fn clear_interrupt(&mut self, timer: &mut Timer) { pub fn clear_interrupt(&mut self) {
// safety: Because we have a mutable reference on `timer`, we have exclusive access to `TIMER::ptr()` // safety: TIMER.intr is a write-clear register, so we can atomically clear our interrupt
let _ = timer; // by writing its value to this field
let timer = unsafe { &*TIMER::ptr() }; // Only one instance of this alarm index can exist, and only this alarm interacts with this bit
// of the TIMER.inte register
timer.intr.modify(|_, w| w.$int_alarm().set_bit()); unsafe {
let timer = &(*pac::TIMER::ptr());
timer.intr.write_with_zero(|w| w.$int_alarm().set_bit());
}
} }
/// Enable this alarm to trigger an interrupt. This alarm will trigger ` /// Enable this alarm to trigger an interrupt. This alarm will trigger `
@ -215,21 +219,27 @@ macro_rules! impl_alarm {
/// After this interrupt is triggered, make sure to clear the interrupt with [clear_interrupt]. /// After this interrupt is triggered, make sure to clear the interrupt with [clear_interrupt].
/// ///
/// [clear_interrupt]: #method.clear_interrupt /// [clear_interrupt]: #method.clear_interrupt
pub fn enable_interrupt(&mut self, timer: &mut Timer) { pub fn enable_interrupt(&mut self) {
// safety: Because we have a mutable reference on `timer`, we have exclusive access to `TIMER::ptr()` // safety: using the atomic set alias means we can atomically set our interrupt enable bit.
let _ = timer; // Only one instance of this alarm can exist, and only this alarm interacts with this bit
let timer = unsafe { &*TIMER::ptr() }; // of the TIMER.inte register
unsafe {
timer.inte.modify(|_, w| w.$int_alarm().set_bit()); let timer = &(*pac::TIMER::ptr());
let reg = (&timer.inte).as_ptr();
write_bitmask_set(reg, $armed_bit_mask);
}
} }
/// Disable this alarm, preventing it from triggering an interrupt. /// Disable this alarm, preventing it from triggering an interrupt.
pub fn disable_interrupt(&mut self, timer: &mut Timer) { pub fn disable_interrupt(&mut self) {
// safety: Because we have a mutable reference on `timer`, we have exclusive access to `TIMER::ptr()` // safety: using the atomic set alias means we can atomically clear our interrupt enable bit.
let _ = timer; // Only one instance of this alarm can exist, and only this alarm interacts with this bit
let timer = unsafe { &*TIMER::ptr() }; // of the TIMER.inte register
unsafe {
timer.inte.modify(|_, w| w.$int_alarm().clear_bit()); let timer = &(*pac::TIMER::ptr());
let reg = (&timer.inte).as_ptr();
write_bitmask_clear(reg, $armed_bit_mask);
}
} }
/// Schedule the alarm to be finished after `countdown`. If [enable_interrupt] is called, this will trigger interrupt ` /// Schedule the alarm to be finished after `countdown`. If [enable_interrupt] is called, this will trigger interrupt `