aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/Cargo.toml2
-rw-r--r--embassy-stm32/src/rtc/mod.rs163
-rw-r--r--embassy-stm32/src/rtc/v2.rs138
-rw-r--r--embassy-stm32/src/rtc/v3.rs8
-rw-r--r--tests/stm32/Cargo.toml2
5 files changed, 172 insertions, 141 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index 01a9d482f..7b3a6c2bb 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -18,7 +18,7 @@ flavors = [
18 { regex_feature = "stm32f7.*", target = "thumbv7em-none-eabi" }, 18 { regex_feature = "stm32f7.*", target = "thumbv7em-none-eabi" },
19 { regex_feature = "stm32c0.*", target = "thumbv6m-none-eabi" }, 19 { regex_feature = "stm32c0.*", target = "thumbv6m-none-eabi" },
20 { regex_feature = "stm32g0.*", target = "thumbv6m-none-eabi" }, 20 { regex_feature = "stm32g0.*", target = "thumbv6m-none-eabi" },
21 { regex_feature = "stm32g4.*", target = "thumbv7em-none-eabi" }, 21 { regex_feature = "stm32g4.*", target = "thumbv7em-none-eabi", features = ["low-power"] },
22 { regex_feature = "stm32h5.*", target = "thumbv8m.main-none-eabihf" }, 22 { regex_feature = "stm32h5.*", target = "thumbv8m.main-none-eabihf" },
23 { regex_feature = "stm32h7.*", target = "thumbv7em-none-eabi" }, 23 { regex_feature = "stm32h7.*", target = "thumbv7em-none-eabi" },
24 { regex_feature = "stm32l0.*", target = "thumbv6m-none-eabi", features = ["low-power"] }, 24 { regex_feature = "stm32l0.*", target = "thumbv6m-none-eabi", features = ["low-power"] },
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs
index e94a58575..b4315f535 100644
--- a/embassy-stm32/src/rtc/mod.rs
+++ b/embassy-stm32/src/rtc/mod.rs
@@ -33,6 +33,61 @@ use embassy_hal_internal::Peripheral;
33use crate::peripherals::RTC; 33use crate::peripherals::RTC;
34use crate::rtc::sealed::Instance; 34use crate::rtc::sealed::Instance;
35 35
36#[allow(dead_code)]
37#[repr(u8)]
38#[derive(Clone, Copy, Debug)]
39pub(crate) enum WakeupPrescaler {
40 Div2 = 2,
41 Div4 = 4,
42 Div8 = 8,
43 Div16 = 16,
44}
45
46#[cfg(any(stm32wb, stm32f4, stm32l0, stm32g4))]
47impl From<WakeupPrescaler> for crate::pac::rtc::vals::Wucksel {
48 fn from(val: WakeupPrescaler) -> Self {
49 use crate::pac::rtc::vals::Wucksel;
50
51 match val {
52 WakeupPrescaler::Div2 => Wucksel::DIV2,
53 WakeupPrescaler::Div4 => Wucksel::DIV4,
54 WakeupPrescaler::Div8 => Wucksel::DIV8,
55 WakeupPrescaler::Div16 => Wucksel::DIV16,
56 }
57 }
58}
59
60#[cfg(any(stm32wb, stm32f4, stm32l0, stm32g4))]
61impl From<crate::pac::rtc::vals::Wucksel> for WakeupPrescaler {
62 fn from(val: crate::pac::rtc::vals::Wucksel) -> Self {
63 use crate::pac::rtc::vals::Wucksel;
64
65 match val {
66 Wucksel::DIV2 => WakeupPrescaler::Div2,
67 Wucksel::DIV4 => WakeupPrescaler::Div4,
68 Wucksel::DIV8 => WakeupPrescaler::Div8,
69 Wucksel::DIV16 => WakeupPrescaler::Div16,
70 _ => unreachable!(),
71 }
72 }
73}
74
75#[cfg(feature = "low-power")]
76impl WakeupPrescaler {
77 pub fn compute_min(val: u32) -> Self {
78 *[
79 WakeupPrescaler::Div2,
80 WakeupPrescaler::Div4,
81 WakeupPrescaler::Div8,
82 WakeupPrescaler::Div16,
83 ]
84 .iter()
85 .skip_while(|psc| **psc as u32 <= val)
86 .next()
87 .unwrap_or(&WakeupPrescaler::Div16)
88 }
89}
90
36/// Errors that can occur on methods on [RtcClock] 91/// Errors that can occur on methods on [RtcClock]
37#[non_exhaustive] 92#[non_exhaustive]
38#[derive(Clone, Debug, PartialEq, Eq)] 93#[derive(Clone, Debug, PartialEq, Eq)]
@@ -277,6 +332,114 @@ impl Rtc {
277 pub fn write_backup_register(&self, register: usize, value: u32) { 332 pub fn write_backup_register(&self, register: usize, value: u32) {
278 RTC::write_backup_register(&RTC::regs(), register, value) 333 RTC::write_backup_register(&RTC::regs(), register, value)
279 } 334 }
335
336 #[cfg(feature = "low-power")]
337 /// start the wakeup alarm and wtih a duration that is as close to but less than
338 /// the requested duration, and record the instant the wakeup alarm was started
339 pub(crate) fn start_wakeup_alarm(
340 &self,
341 requested_duration: embassy_time::Duration,
342 cs: critical_section::CriticalSection,
343 ) {
344 use embassy_time::{Duration, TICK_HZ};
345
346 #[cfg(any(rtc_v3, rtc_v3u5))]
347 use crate::pac::rtc::vals::Calrf;
348
349 // Panic if the rcc mod knows we're not using low-power rtc
350 #[cfg(any(rcc_wb, rcc_f4, rcc_f410))]
351 unsafe { crate::rcc::get_freqs() }.rtc.unwrap();
352
353 let requested_duration = requested_duration.as_ticks().clamp(0, u32::MAX as u64);
354 let rtc_hz = Self::frequency().0 as u64;
355 let rtc_ticks = requested_duration * rtc_hz / TICK_HZ;
356 let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32);
357
358 // adjust the rtc ticks to the prescaler and subtract one rtc tick
359 let rtc_ticks = rtc_ticks / prescaler as u64;
360 let rtc_ticks = rtc_ticks.clamp(0, (u16::MAX - 1) as u64).saturating_sub(1) as u16;
361
362 self.write(false, |regs| {
363 regs.cr().modify(|w| w.set_wute(false));
364
365 #[cfg(any(
366 rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb
367 ))]
368 {
369 regs.isr().modify(|w| w.set_wutf(false));
370 while !regs.isr().read().wutwf() {}
371 }
372
373 #[cfg(any(rtc_v3, rtc_v3u5))]
374 {
375 regs.scr().write(|w| w.set_cwutf(Calrf::CLEAR));
376 while !regs.icsr().read().wutwf() {}
377 }
378
379 regs.cr().modify(|w| w.set_wucksel(prescaler.into()));
380 regs.wutr().write(|w| w.set_wut(rtc_ticks));
381 regs.cr().modify(|w| w.set_wute(true));
382 regs.cr().modify(|w| w.set_wutie(true));
383 });
384
385 let instant = self.instant().unwrap();
386 trace!(
387 "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}",
388 Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(),
389 prescaler as u32,
390 rtc_ticks,
391 instant,
392 );
393
394 assert!(self.stop_time.borrow(cs).replace(Some(instant)).is_none())
395 }
396
397 #[cfg(feature = "low-power")]
398 /// stop the wakeup alarm and return the time elapsed since `start_wakeup_alarm`
399 /// was called, otherwise none
400 pub(crate) fn stop_wakeup_alarm(&self, cs: critical_section::CriticalSection) -> Option<embassy_time::Duration> {
401 use crate::interrupt::typelevel::Interrupt;
402 #[cfg(any(rtc_v3, rtc_v3u5))]
403 use crate::pac::rtc::vals::Calrf;
404
405 let instant = self.instant().unwrap();
406 if RTC::regs().cr().read().wute() {
407 trace!("rtc: stop wakeup alarm at {}", instant);
408
409 self.write(false, |regs| {
410 regs.cr().modify(|w| w.set_wutie(false));
411 regs.cr().modify(|w| w.set_wute(false));
412
413 #[cfg(any(
414 rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb
415 ))]
416 regs.isr().modify(|w| w.set_wutf(false));
417
418 #[cfg(any(rtc_v3, rtc_v3u5))]
419 regs.scr().write(|w| w.set_cwutf(Calrf::CLEAR));
420
421 crate::pac::EXTI
422 .pr(0)
423 .modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true));
424
425 <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend();
426 });
427 }
428
429 self.stop_time.borrow(cs).take().map(|stop_time| instant - stop_time)
430 }
431
432 #[cfg(feature = "low-power")]
433 pub(crate) fn enable_wakeup_line(&self) {
434 use crate::interrupt::typelevel::Interrupt;
435 use crate::pac::EXTI;
436
437 <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend();
438 unsafe { <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::enable() };
439
440 EXTI.rtsr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true));
441 EXTI.imr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true));
442 }
280} 443}
281 444
282pub(crate) fn byte_to_bcd2(byte: u8) -> (u8, u8) { 445pub(crate) fn byte_to_bcd2(byte: u8) -> (u8, u8) {
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs
index 006fd63f4..91f08fae4 100644
--- a/embassy-stm32/src/rtc/v2.rs
+++ b/embassy-stm32/src/rtc/v2.rs
@@ -6,145 +6,7 @@ use crate::peripherals::RTC;
6use crate::rtc::sealed::Instance; 6use crate::rtc::sealed::Instance;
7 7
8#[allow(dead_code)] 8#[allow(dead_code)]
9#[repr(u8)]
10#[derive(Clone, Copy, Debug)]
11pub(crate) enum WakeupPrescaler {
12 Div2 = 2,
13 Div4 = 4,
14 Div8 = 8,
15 Div16 = 16,
16}
17
18#[cfg(any(stm32wb, stm32f4, stm32l0))]
19impl From<WakeupPrescaler> for crate::pac::rtc::vals::Wucksel {
20 fn from(val: WakeupPrescaler) -> Self {
21 use crate::pac::rtc::vals::Wucksel;
22
23 match val {
24 WakeupPrescaler::Div2 => Wucksel::DIV2,
25 WakeupPrescaler::Div4 => Wucksel::DIV4,
26 WakeupPrescaler::Div8 => Wucksel::DIV8,
27 WakeupPrescaler::Div16 => Wucksel::DIV16,
28 }
29 }
30}
31
32#[cfg(any(stm32wb, stm32f4, stm32l0))]
33impl From<crate::pac::rtc::vals::Wucksel> for WakeupPrescaler {
34 fn from(val: crate::pac::rtc::vals::Wucksel) -> Self {
35 use crate::pac::rtc::vals::Wucksel;
36
37 match val {
38 Wucksel::DIV2 => WakeupPrescaler::Div2,
39 Wucksel::DIV4 => WakeupPrescaler::Div4,
40 Wucksel::DIV8 => WakeupPrescaler::Div8,
41 Wucksel::DIV16 => WakeupPrescaler::Div16,
42 _ => unreachable!(),
43 }
44 }
45}
46
47#[allow(dead_code)]
48impl WakeupPrescaler {
49 pub fn compute_min(val: u32) -> Self {
50 *[
51 WakeupPrescaler::Div2,
52 WakeupPrescaler::Div4,
53 WakeupPrescaler::Div8,
54 WakeupPrescaler::Div16,
55 ]
56 .iter()
57 .skip_while(|psc| **psc as u32 <= val)
58 .next()
59 .unwrap_or(&WakeupPrescaler::Div16)
60 }
61}
62
63impl super::Rtc { 9impl super::Rtc {
64 #[cfg(feature = "low-power")]
65 /// start the wakeup alarm and wtih a duration that is as close to but less than
66 /// the requested duration, and record the instant the wakeup alarm was started
67 pub(crate) fn start_wakeup_alarm(
68 &self,
69 requested_duration: embassy_time::Duration,
70 cs: critical_section::CriticalSection,
71 ) {
72 use embassy_time::{Duration, TICK_HZ};
73
74 // Panic if the rcc mod knows we're not using low-power rtc
75 #[cfg(any(rcc_wb, rcc_f4, rcc_f410))]
76 unsafe { crate::rcc::get_freqs() }.rtc.unwrap();
77
78 let requested_duration = requested_duration.as_ticks().clamp(0, u32::MAX as u64);
79 let rtc_hz = Self::frequency().0 as u64;
80 let rtc_ticks = requested_duration * rtc_hz / TICK_HZ;
81 let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32);
82
83 // adjust the rtc ticks to the prescaler and subtract one rtc tick
84 let rtc_ticks = rtc_ticks / prescaler as u64;
85 let rtc_ticks = rtc_ticks.clamp(0, (u16::MAX - 1) as u64).saturating_sub(1) as u16;
86
87 self.write(false, |regs| {
88 regs.cr().modify(|w| w.set_wute(false));
89 regs.isr().modify(|w| w.set_wutf(false));
90 while !regs.isr().read().wutwf() {}
91
92 regs.cr().modify(|w| w.set_wucksel(prescaler.into()));
93 regs.wutr().write(|w| w.set_wut(rtc_ticks));
94 regs.cr().modify(|w| w.set_wute(true));
95 regs.cr().modify(|w| w.set_wutie(true));
96 });
97
98 let instant = self.instant().unwrap();
99 trace!(
100 "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}",
101 Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(),
102 prescaler as u32,
103 rtc_ticks,
104 instant,
105 );
106
107 assert!(self.stop_time.borrow(cs).replace(Some(instant)).is_none())
108 }
109
110 #[cfg(feature = "low-power")]
111 /// stop the wakeup alarm and return the time elapsed since `start_wakeup_alarm`
112 /// was called, otherwise none
113 pub(crate) fn stop_wakeup_alarm(&self, cs: critical_section::CriticalSection) -> Option<embassy_time::Duration> {
114 use crate::interrupt::typelevel::Interrupt;
115
116 let instant = self.instant().unwrap();
117 if RTC::regs().cr().read().wute() {
118 trace!("rtc: stop wakeup alarm at {}", instant);
119
120 self.write(false, |regs| {
121 regs.cr().modify(|w| w.set_wutie(false));
122 regs.cr().modify(|w| w.set_wute(false));
123 regs.isr().modify(|w| w.set_wutf(false));
124
125 crate::pac::EXTI
126 .pr(0)
127 .modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true));
128
129 <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend();
130 });
131 }
132
133 self.stop_time.borrow(cs).take().map(|stop_time| instant - stop_time)
134 }
135
136 #[cfg(feature = "low-power")]
137 pub(crate) fn enable_wakeup_line(&self) {
138 use crate::interrupt::typelevel::Interrupt;
139 use crate::pac::EXTI;
140
141 <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend();
142 unsafe { <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::enable() };
143
144 EXTI.rtsr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true));
145 EXTI.imr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true));
146 }
147
148 /// Applies the RTC config 10 /// Applies the RTC config
149 /// It this changes the RTC clock source the time will be reset 11 /// It this changes the RTC clock source the time will be reset
150 pub(super) fn configure(&mut self, async_psc: u8, sync_psc: u16) { 12 pub(super) fn configure(&mut self, async_psc: u8, sync_psc: u16) {
diff --git a/embassy-stm32/src/rtc/v3.rs b/embassy-stm32/src/rtc/v3.rs
index 7bf757e7d..d2d0d9309 100644
--- a/embassy-stm32/src/rtc/v3.rs
+++ b/embassy-stm32/src/rtc/v3.rs
@@ -95,7 +95,7 @@ impl super::Rtc {
95 }) 95 })
96 } 96 }
97 97
98 pub(super) fn write<F, R>(&mut self, init_mode: bool, f: F) -> R 98 pub(super) fn write<F, R>(&self, init_mode: bool, f: F) -> R
99 where 99 where
100 F: FnOnce(&crate::pac::rtc::Rtc) -> R, 100 F: FnOnce(&crate::pac::rtc::Rtc) -> R,
101 { 101 {
@@ -129,6 +129,12 @@ impl super::Rtc {
129impl sealed::Instance for crate::peripherals::RTC { 129impl sealed::Instance for crate::peripherals::RTC {
130 const BACKUP_REGISTER_COUNT: usize = 32; 130 const BACKUP_REGISTER_COUNT: usize = 32;
131 131
132 #[cfg(all(feature = "low-power", stm32g4))]
133 const EXTI_WAKEUP_LINE: usize = 20;
134
135 #[cfg(all(feature = "low-power", stm32g4))]
136 type WakeupInterrupt = crate::interrupt::typelevel::RTC_WKUP;
137
132 fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { 138 fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> {
133 #[allow(clippy::if_same_then_else)] 139 #[allow(clippy::if_same_then_else)]
134 if register < Self::BACKUP_REGISTER_COUNT { 140 if register < Self::BACKUP_REGISTER_COUNT {
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml
index 4b769b1cb..9b9273a5c 100644
--- a/tests/stm32/Cargo.toml
+++ b/tests/stm32/Cargo.toml
@@ -10,7 +10,7 @@ stm32f103c8 = ["embassy-stm32/stm32f103c8", "not-gpdma"]
10stm32f429zi = ["embassy-stm32/stm32f429zi", "chrono", "eth", "stop", "can", "not-gpdma", "dac-adc-pin", "rng"] 10stm32f429zi = ["embassy-stm32/stm32f429zi", "chrono", "eth", "stop", "can", "not-gpdma", "dac-adc-pin", "rng"]
11stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac-adc-pin"] 11stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac-adc-pin"]
12stm32c031c6 = ["embassy-stm32/stm32c031c6", "cm0", "not-gpdma"] 12stm32c031c6 = ["embassy-stm32/stm32c031c6", "cm0", "not-gpdma"]
13stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "not-gpdma", "rng"] 13stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng"]
14stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac-adc-pin", "rng"] 14stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac-adc-pin", "rng"]
15stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng"] 15stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng"]
16stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng"] 16stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng"]