Removed incorret unsafe impl Sync, added interrupt_free around obtaining an alarm

This commit is contained in:
Victor Koenders 2021-11-20 08:52:38 +01:00
parent 1fd04d3384
commit a9d96f352e
No known key found for this signature in database
GPG key ID: 2E441540865B8A1C

View file

@ -13,9 +13,6 @@ pub struct Timer {
alarms: [bool; 4], alarms: [bool; 4],
} }
// Safety: All access is read-only.
unsafe impl Sync for Timer {}
impl Timer { impl Timer {
/// Create a new [`Timer`] /// Create a new [`Timer`]
pub fn new(timer: TIMER, resets: &mut RESETS) -> Self { pub fn new(timer: TIMER, resets: &mut RESETS) -> Self {
@ -55,42 +52,50 @@ impl Timer {
/// Retrieve a reference to alarm 0. Will only return a value the first time this is called /// Retrieve a reference to alarm 0. Will only return a value the first time this is called
pub fn alarm_0(&mut self) -> Option<Alarm0> { pub fn alarm_0(&mut self) -> Option<Alarm0> {
if self.alarms[0] { cortex_m::interrupt::free(|_| {
self.alarms[0] = false; if self.alarms[0] {
Some(Alarm0(PhantomData)) self.alarms[0] = false;
} else { Some(Alarm0(PhantomData))
None } else {
} None
}
})
} }
/// Retrieve a reference to alarm 1. Will only return a value the first time this is called /// Retrieve a reference to alarm 1. Will only return a value the first time this is called
pub fn alarm_1(&mut self) -> Option<Alarm1> { pub fn alarm_1(&mut self) -> Option<Alarm1> {
if self.alarms[1] { cortex_m::interrupt::free(|_| {
self.alarms[1] = false; if self.alarms[1] {
Some(Alarm1(PhantomData)) self.alarms[1] = false;
} else { Some(Alarm1(PhantomData))
None } else {
} None
}
})
} }
/// Retrieve a reference to alarm 2. Will only return a value the first time this is called /// Retrieve a reference to alarm 2. Will only return a value the first time this is called
pub fn alarm_2(&mut self) -> Option<Alarm2> { pub fn alarm_2(&mut self) -> Option<Alarm2> {
if self.alarms[2] { cortex_m::interrupt::free(|_| {
self.alarms[2] = false; if self.alarms[2] {
Some(Alarm2(PhantomData)) self.alarms[2] = false;
} else { Some(Alarm2(PhantomData))
None } else {
} None
}
})
} }
/// Retrieve a reference to alarm 3. Will only return a value the first time this is called /// Retrieve a reference to alarm 3. Will only return a value the first time this is called
pub fn alarm_3(&mut self) -> Option<Alarm3> { pub fn alarm_3(&mut self) -> Option<Alarm3> {
if self.alarms[3] { cortex_m::interrupt::free(|_| {
self.alarms[3] = false; if self.alarms[3] {
Some(Alarm3(PhantomData)) self.alarms[3] = false;
} else { Some(Alarm3(PhantomData))
None } else {
} None
}
})
} }
} }