aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp/src/timer.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-07-12 02:45:42 +0200
committerDario Nieuwenhuis <[email protected]>2021-07-12 03:45:57 +0200
commit7547c8d8d6800dd661dcda9f62ba988eefadd38e (patch)
tree078e4238487043136b4054e00862e4116f0bdbcc /embassy-rp/src/timer.rs
parentc210a6efd1f77c4dd6c5df7b31e49c771ceb0cff (diff)
rp/timer: add
Diffstat (limited to 'embassy-rp/src/timer.rs')
-rw-r--r--embassy-rp/src/timer.rs187
1 files changed, 187 insertions, 0 deletions
diff --git a/embassy-rp/src/timer.rs b/embassy-rp/src/timer.rs
new file mode 100644
index 000000000..5baa02607
--- /dev/null
+++ b/embassy-rp/src/timer.rs
@@ -0,0 +1,187 @@
1use core::cell::Cell;
2use critical_section::CriticalSection;
3use embassy::interrupt::{Interrupt, InterruptExt};
4use embassy::util::CriticalSectionMutex as Mutex;
5
6use crate::{interrupt, pac};
7
8struct AlarmState {
9 timestamp: Cell<u64>,
10 callback: Cell<Option<(fn(*mut ()), *mut ())>>,
11}
12unsafe impl Send for AlarmState {}
13
14const ALARM_COUNT: usize = 4;
15const DUMMY_ALARM: AlarmState = AlarmState {
16 timestamp: Cell::new(0),
17 callback: Cell::new(None),
18};
19
20static ALARMS: Mutex<[AlarmState; ALARM_COUNT]> = Mutex::new([DUMMY_ALARM; ALARM_COUNT]);
21
22fn now() -> u64 {
23 loop {
24 unsafe {
25 let hi = pac::TIMER.timerawh().read();
26 let lo = pac::TIMER.timerawl().read();
27 let hi2 = pac::TIMER.timerawh().read();
28 if hi == hi2 {
29 return (hi as u64) << 32 | (lo as u64);
30 }
31 }
32 }
33}
34
35struct Timer;
36impl embassy::time::Clock for Timer {
37 fn now(&self) -> u64 {
38 now()
39 }
40}
41
42pub trait AlarmInstance {
43 fn alarm_num(&self) -> usize;
44}
45
46impl AlarmInstance for crate::peripherals::TIMER_ALARM0 {
47 fn alarm_num(&self) -> usize {
48 0
49 }
50}
51impl AlarmInstance for crate::peripherals::TIMER_ALARM1 {
52 fn alarm_num(&self) -> usize {
53 1
54 }
55}
56impl AlarmInstance for crate::peripherals::TIMER_ALARM2 {
57 fn alarm_num(&self) -> usize {
58 2
59 }
60}
61impl AlarmInstance for crate::peripherals::TIMER_ALARM3 {
62 fn alarm_num(&self) -> usize {
63 3
64 }
65}
66
67pub struct Alarm<T: AlarmInstance> {
68 inner: T,
69}
70
71impl<T: AlarmInstance> Alarm<T> {
72 pub fn new(inner: T) -> Self {
73 Self { inner }
74 }
75}
76
77impl<T: AlarmInstance> embassy::time::Alarm for Alarm<T> {
78 fn set_callback(&self, callback: fn(*mut ()), ctx: *mut ()) {
79 let n = self.inner.alarm_num();
80 critical_section::with(|cs| {
81 let alarm = &ALARMS.borrow(cs)[n];
82 alarm.callback.set(Some((callback, ctx)));
83 })
84 }
85
86 fn set(&self, timestamp: u64) {
87 let n = self.inner.alarm_num();
88
89 critical_section::with(|cs| {
90 let alarm = &ALARMS.borrow(cs)[n];
91 alarm.timestamp.set(timestamp);
92
93 // Arm it.
94 // Note that we're not checking the high bits at all. This means the irq may fire early
95 // if the alarm is more than 72 minutes (2^32 us) in the future. This is OK, since on irq fire
96 // it is checked if the alarm time has passed.
97 unsafe { pac::TIMER.alarm(n).write_value(timestamp as u32) };
98
99 let now = now();
100
101 // If alarm timestamp has passed, trigger it instantly.
102 // This disarms it.
103 if timestamp <= now {
104 trigger_alarm(n, cs);
105 }
106 })
107 }
108
109 fn clear(&self) {
110 self.set(u64::MAX);
111 }
112}
113
114fn check_alarm(n: usize) {
115 critical_section::with(|cs| {
116 let alarm = &ALARMS.borrow(cs)[n];
117 let timestamp = alarm.timestamp.get();
118 if timestamp <= now() {
119 trigger_alarm(n, cs)
120 } else {
121 // Not elapsed, arm it again.
122 // This can happen if it was set more than 2^32 us in the future.
123 unsafe { pac::TIMER.alarm(n).write_value(timestamp as u32) };
124 }
125 });
126
127 // clear the irq
128 unsafe { pac::TIMER.intr().write(|w| w.set_alarm(n, true)) }
129}
130
131fn trigger_alarm(n: usize, cs: CriticalSection) {
132 // disarm
133 unsafe { pac::TIMER.armed().write(|w| w.set_armed(1 << n)) }
134
135 let alarm = &ALARMS.borrow(cs)[n];
136 alarm.timestamp.set(u64::MAX);
137
138 // Call after clearing alarm, so the callback can set another alarm.
139 if let Some((f, ctx)) = alarm.callback.get() {
140 f(ctx);
141 }
142}
143
144/// safety: must be called exactly once at bootup
145pub unsafe fn init() {
146 // init alarms
147 critical_section::with(|cs| {
148 let alarms = ALARMS.borrow(cs);
149 for a in alarms {
150 a.timestamp.set(u64::MAX);
151 }
152 });
153
154 // enable all irqs
155 pac::TIMER.inte().write(|w| {
156 w.set_alarm(0, true);
157 w.set_alarm(1, true);
158 w.set_alarm(2, true);
159 w.set_alarm(3, true);
160 });
161 interrupt::TIMER_IRQ_0::steal().enable();
162 interrupt::TIMER_IRQ_1::steal().enable();
163 interrupt::TIMER_IRQ_2::steal().enable();
164 interrupt::TIMER_IRQ_3::steal().enable();
165
166 embassy::time::set_clock(&Timer);
167}
168
169#[interrupt]
170unsafe fn TIMER_IRQ_0() {
171 check_alarm(0)
172}
173
174#[interrupt]
175unsafe fn TIMER_IRQ_1() {
176 check_alarm(1)
177}
178
179#[interrupt]
180unsafe fn TIMER_IRQ_2() {
181 check_alarm(2)
182}
183
184#[interrupt]
185unsafe fn TIMER_IRQ_3() {
186 check_alarm(3)
187}