Skip to content

Commit 1ffc6c4

Browse files
committed
both timers support up to 4 alarms
1 parent 0ccaf3d commit 1ffc6c4

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

rp235x-hal/src/timer.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ use crate::{
2222
/// Instant type used by the Timer & Alarm methods.
2323
pub type Instant = TimerInstantU64<1_000_000>;
2424

25-
static ALARMS: AtomicU8 = AtomicU8::new(0x0F);
26-
fn take_alarm(mask: u8) -> bool {
25+
static ALARMS_TIMER0: AtomicU8 = AtomicU8::new(0x0F);
26+
static ALARMS_TIMER1: AtomicU8 = AtomicU8::new(0x0F);
27+
fn take_alarm(mask: u8, alarms: &'static AtomicU8) -> bool {
2728
critical_section::with(|_| {
28-
let alarms = ALARMS.load(Ordering::Relaxed);
29-
ALARMS.store(alarms & !mask, Ordering::Relaxed);
30-
(alarms & mask) != 0
29+
let current_alarms = alarms.load(Ordering::Relaxed);
30+
alarms.store(current_alarms & !mask, Ordering::Relaxed);
31+
(current_alarms & mask) != 0
3132
})
3233
}
33-
fn release_alarm(mask: u8) {
34+
fn release_alarm(mask: u8, alarms: &'static AtomicU8) {
3435
critical_section::with(|_| {
35-
let alarms = ALARMS.load(Ordering::Relaxed);
36-
ALARMS.store(alarms | mask, Ordering::Relaxed);
36+
let current_alarms = alarms.load(Ordering::Relaxed);
37+
alarms.store(current_alarms | mask, Ordering::Relaxed);
3738
});
3839
}
3940

@@ -66,6 +67,15 @@ pub trait TimerDevice: Sealed + Clone + Copy + 'static {
6667
unsafe { &*pac::TIMER1::ptr() }
6768
}
6869
}
70+
71+
/// Get the static AtomicU8 for managing alarms.
72+
fn get_alarms_tracker() -> &'static AtomicU8 {
73+
if Self::ID == 0 {
74+
&ALARMS_TIMER0
75+
} else {
76+
&ALARMS_TIMER1
77+
}
78+
}
6979
}
7080

7181
impl TimerDevice for CopyableTimer0 {
@@ -168,22 +178,22 @@ where
168178
}
169179
/// Retrieve a reference to alarm 0. Will only return a value the first time this is called
170180
pub fn alarm_0(&mut self) -> Option<Alarm0<D>> {
171-
take_alarm(1 << 0).then_some(Alarm0(*self))
181+
take_alarm(1 << 0, <D>::get_alarms_tracker()).then_some(Alarm0(*self))
172182
}
173183

174184
/// Retrieve a reference to alarm 1. Will only return a value the first time this is called
175185
pub fn alarm_1(&mut self) -> Option<Alarm1<D>> {
176-
take_alarm(1 << 1).then_some(Alarm1(*self))
186+
take_alarm(1 << 1, <D>::get_alarms_tracker()).then_some(Alarm1(*self))
177187
}
178188

179189
/// Retrieve a reference to alarm 2. Will only return a value the first time this is called
180190
pub fn alarm_2(&mut self) -> Option<Alarm2<D>> {
181-
take_alarm(1 << 2).then_some(Alarm2(*self))
191+
take_alarm(1 << 2, <D>::get_alarms_tracker()).then_some(Alarm2(*self))
182192
}
183193

184194
/// Retrieve a reference to alarm 3. Will only return a value the first time this is called
185195
pub fn alarm_3(&mut self) -> Option<Alarm3<D>> {
186-
take_alarm(1 << 3).then_some(Alarm3(*self))
196+
take_alarm(1 << 3, <D>::get_alarms_tracker()).then_some(Alarm3(*self))
187197
}
188198

189199
/// Pauses execution for at minimum `us` microseconds.
@@ -549,7 +559,7 @@ macro_rules! impl_alarm {
549559
{
550560
fn drop(&mut self) {
551561
self.disable_interrupt();
552-
release_alarm($armed_bit_mask)
562+
release_alarm($armed_bit_mask, D::get_alarms_tracker());
553563
}
554564
}
555565
};
@@ -636,4 +646,4 @@ pub mod monotonic {
636646

637647
unsafe fn reset(&mut self) {}
638648
}
639-
}
649+
}

0 commit comments

Comments
 (0)