aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-08-23 19:52:32 -0500
committerxoviat <[email protected]>2023-08-23 19:52:32 -0500
commitfc04d2a33caa300bce38f7d0a46f1a0c814bb613 (patch)
treec2d4c3623bb080e1b7ab55b39fa435d29429d130 /embassy-stm32/src
parent1ea4c58c39d831408d7584be3f37b9f2d880d4ca (diff)
rtc: get wakup irq working
Diffstat (limited to 'embassy-stm32/src')
-rw-r--r--embassy-stm32/src/low_power.rs4
-rw-r--r--embassy-stm32/src/rtc/v2.rs48
2 files changed, 32 insertions, 20 deletions
diff --git a/embassy-stm32/src/low_power.rs b/embassy-stm32/src/low_power.rs
index 7d5093b61..f4bf7bbea 100644
--- a/embassy-stm32/src/low_power.rs
+++ b/embassy-stm32/src/low_power.rs
@@ -7,6 +7,7 @@ use embassy_time::Duration;
7 7
8use crate::interrupt; 8use crate::interrupt;
9use crate::interrupt::typelevel::Interrupt; 9use crate::interrupt::typelevel::Interrupt;
10use crate::pac::EXTI;
10 11
11const THREAD_PENDER: usize = usize::MAX; 12const THREAD_PENDER: usize = usize::MAX;
12const THRESHOLD: Duration = Duration::from_millis(500); 13const THRESHOLD: Duration = Duration::from_millis(500);
@@ -28,6 +29,9 @@ pub fn stop_with_rtc(rtc: &'static Rtc) {
28 crate::interrupt::typelevel::RTC_WKUP::unpend(); 29 crate::interrupt::typelevel::RTC_WKUP::unpend();
29 unsafe { crate::interrupt::typelevel::RTC_WKUP::enable() }; 30 unsafe { crate::interrupt::typelevel::RTC_WKUP::enable() };
30 31
32 EXTI.rtsr(0).modify(|w| w.set_line(22, true));
33 EXTI.imr(0).modify(|w| w.set_line(22, true));
34
31 unsafe { RTC = Some(rtc) }; 35 unsafe { RTC = Some(rtc) };
32} 36}
33 37
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs
index e03c7d2a0..1d4f4df30 100644
--- a/embassy-stm32/src/rtc/v2.rs
+++ b/embassy-stm32/src/rtc/v2.rs
@@ -1,3 +1,4 @@
1use defmt::Format;
1use stm32_metapac::rtc::vals::{Init, Osel, Pol}; 2use stm32_metapac::rtc::vals::{Init, Osel, Pol};
2 3
3use super::{sealed, RtcClockSource, RtcConfig}; 4use super::{sealed, RtcClockSource, RtcConfig};
@@ -73,7 +74,7 @@ impl core::ops::Sub for RtcInstant {
73} 74}
74 75
75#[allow(dead_code)] 76#[allow(dead_code)]
76#[derive(Clone, Copy)] 77#[derive(Clone, Copy, Debug, Format)]
77pub(crate) enum WakeupPrescaler { 78pub(crate) enum WakeupPrescaler {
78 Div2, 79 Div2,
79 Div4, 80 Div4,
@@ -185,17 +186,26 @@ impl super::Rtc {
185 ); 186 );
186 187
187 trace!("set wakeup timer for {} ms", duration.as_millis()); 188 trace!("set wakeup timer for {} ms", duration.as_millis());
189 trace!("set wakeup timer for {} ticks with pre {}", rtc_ticks, prescaler);
188 190
189 RTC::regs().wpr().write(|w| w.set_key(0xca)); 191 self.write(false, |regs| {
190 RTC::regs().wpr().write(|w| w.set_key(0x53)); 192 regs.cr().modify(|w| w.set_wutie(true));
191 193
192 RTC::regs().wutr().modify(|w| w.set_wut(rtc_ticks)); 194 trace!("clear wute");
195 regs.cr().modify(|w| w.set_wute(false));
196 regs.isr().modify(|w| w.set_wutf(false));
193 197
194 RTC::regs().cr().modify(|w| { 198 trace!("wait for wutwf...");
195 w.set_wucksel(prescaler.into()); 199 while !regs.isr().read().wutwf() {}
200 trace!("wait for wutwf...done");
196 201
197 w.set_wutie(true); 202 regs.cr().modify(|w| {
198 w.set_wute(true); 203 w.set_wucksel(prescaler.into());
204
205 w.set_wutie(true);
206 });
207
208 regs.cr().modify(|w| w.set_wute(true));
199 }); 209 });
200 210
201 if !RTC::regs().cr().read().wute() { 211 if !RTC::regs().cr().read().wute() {
@@ -204,6 +214,12 @@ impl super::Rtc {
204 trace!("wakeup timer enabled"); 214 trace!("wakeup timer enabled");
205 } 215 }
206 216
217 if !RTC::regs().cr().read().wutie() {
218 trace!("wakeup timer interrupt not enabled");
219 } else {
220 trace!("wakeup timer interrupt enabled");
221 }
222
207 RtcInstant::now() 223 RtcInstant::now()
208 } 224 }
209 225
@@ -216,19 +232,11 @@ impl super::Rtc {
216 pub(crate) fn stop_wakeup_alarm(&self) -> RtcInstant { 232 pub(crate) fn stop_wakeup_alarm(&self) -> RtcInstant {
217 trace!("disable wakeup timer..."); 233 trace!("disable wakeup timer...");
218 234
219 RTC::regs().cr().modify(|w| { 235 self.write(false, |regs| {
220 w.set_wute(false); 236 regs.cr().modify(|w| w.set_wute(false));
237 regs.isr().modify(|w| w.set_wutf(false));
221 }); 238 });
222 239
223 trace!("wait for wakeup timer stop...");
224
225 // Wait for the wakeup timer to stop
226 // while !RTC::regs().isr().read().wutf() {}
227 //
228 // RTC::regs().isr().modify(|w| w.set_wutf(false));
229
230 trace!("wait for wakeup timer stop...done");
231
232 RtcInstant::now() 240 RtcInstant::now()
233 } 241 }
234 242
@@ -380,7 +388,7 @@ impl super::Rtc {
380 }) 388 })
381 } 389 }
382 390
383 pub(super) fn write<F, R>(&mut self, init_mode: bool, f: F) -> R 391 pub(super) fn write<F, R>(&self, init_mode: bool, f: F) -> R
384 where 392 where
385 F: FnOnce(&crate::pac::rtc::Rtc) -> R, 393 F: FnOnce(&crate::pac::rtc::Rtc) -> R,
386 { 394 {