diff options
| author | xoviat <[email protected]> | 2023-08-06 11:58:28 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-08-06 11:58:28 -0500 |
| commit | 28618d12a11a73a5c0c4e2b176b7096796f79cdb (patch) | |
| tree | 7ea43543b60c1b1191f129ab0ecfbe1a453d499f /embassy-stm32/src | |
| parent | 66c171211822e9b680976f73f971ac27e5870934 (diff) | |
stm32/rtc: restructure
Diffstat (limited to 'embassy-stm32/src')
| -rw-r--r-- | embassy-stm32/src/rcc/wb.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/mod.rs | 33 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 21 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v3.rs | 19 |
4 files changed, 35 insertions, 40 deletions
diff --git a/embassy-stm32/src/rcc/wb.rs b/embassy-stm32/src/rcc/wb.rs index 97e6aa604..e9b7296d5 100644 --- a/embassy-stm32/src/rcc/wb.rs +++ b/embassy-stm32/src/rcc/wb.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | pub use super::common::{AHBPrescaler, APBPrescaler}; | 1 | pub use super::common::{AHBPrescaler, APBPrescaler}; |
| 2 | use crate::rcc::Clocks; | 2 | use crate::rcc::Clocks; |
| 3 | use crate::rtc::{enable_rtc, RtcClockSource}; | 3 | use crate::rtc::{enable as enable_rtc, RtcClockSource}; |
| 4 | use crate::time::{khz, mhz, Hertz}; | 4 | use crate::time::{khz, mhz, Hertz}; |
| 5 | 5 | ||
| 6 | /// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, | 6 | /// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, |
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index 551d80439..2d54c926d 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs | |||
| @@ -33,19 +33,15 @@ pub struct Rtc<'d, T: Instance> { | |||
| 33 | rtc_config: RtcConfig, | 33 | rtc_config: RtcConfig, |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | pub(crate) fn enable_rtc(clock_source: RtcClockSource) { | 36 | #[allow(dead_code)] |
| 37 | // TODO: rewrite the RTC module so that enable is separated from configure | 37 | pub(crate) fn enable(clock_source: RtcClockSource) { |
| 38 | 38 | Rtc::<crate::peripherals::RTC>::enable(clock_source); | |
| 39 | assert!(clock_source == RtcClockSource::LSI || clock_source == RtcClockSource::LSE); | 39 | } |
| 40 | 40 | ||
| 41 | let _ = Rtc::new( | 41 | #[cfg(feature = "time")] |
| 42 | unsafe { crate::Peripherals::steal().RTC }, | 42 | #[allow(dead_code)] |
| 43 | RtcConfig { | 43 | pub(crate) fn set_wakeup_timer(_duration: embassy_time::Duration) { |
| 44 | clock_config: clock_source, | 44 | todo!() |
| 45 | async_prescaler: 1, | ||
| 46 | sync_prescaler: 1, | ||
| 47 | }, | ||
| 48 | ); | ||
| 49 | } | 45 | } |
| 50 | 46 | ||
| 51 | #[derive(Copy, Clone, Debug, PartialEq)] | 47 | #[derive(Copy, Clone, Debug, PartialEq)] |
| @@ -64,7 +60,7 @@ pub enum RtcClockSource { | |||
| 64 | #[derive(Copy, Clone, PartialEq)] | 60 | #[derive(Copy, Clone, PartialEq)] |
| 65 | pub struct RtcConfig { | 61 | pub struct RtcConfig { |
| 66 | /// RTC clock source | 62 | /// RTC clock source |
| 67 | clock_config: RtcClockSource, | 63 | clock_source: RtcClockSource, |
| 68 | /// Asynchronous prescaler factor | 64 | /// Asynchronous prescaler factor |
| 69 | /// This is the asynchronous division factor: | 65 | /// This is the asynchronous division factor: |
| 70 | /// ck_apre frequency = RTCCLK frequency/(PREDIV_A+1) | 66 | /// ck_apre frequency = RTCCLK frequency/(PREDIV_A+1) |
| @@ -82,7 +78,7 @@ impl Default for RtcConfig { | |||
| 82 | /// Raw sub-seconds in 1/256. | 78 | /// Raw sub-seconds in 1/256. |
| 83 | fn default() -> Self { | 79 | fn default() -> Self { |
| 84 | RtcConfig { | 80 | RtcConfig { |
| 85 | clock_config: RtcClockSource::LSI, | 81 | clock_source: RtcClockSource::LSI, |
| 86 | async_prescaler: 127, | 82 | async_prescaler: 127, |
| 87 | sync_prescaler: 255, | 83 | sync_prescaler: 255, |
| 88 | } | 84 | } |
| @@ -91,8 +87,8 @@ impl Default for RtcConfig { | |||
| 91 | 87 | ||
| 92 | impl RtcConfig { | 88 | impl RtcConfig { |
| 93 | /// Sets the clock source of RTC config | 89 | /// Sets the clock source of RTC config |
| 94 | pub fn clock_config(mut self, cfg: RtcClockSource) -> Self { | 90 | pub fn clock_source(mut self, clock_source: RtcClockSource) -> Self { |
| 95 | self.clock_config = cfg; | 91 | self.clock_source = clock_source; |
| 96 | self | 92 | self |
| 97 | } | 93 | } |
| 98 | 94 | ||
| @@ -135,7 +131,10 @@ impl<'d, T: Instance> Rtc<'d, T> { | |||
| 135 | rtc_config, | 131 | rtc_config, |
| 136 | }; | 132 | }; |
| 137 | 133 | ||
| 138 | rtc_struct.apply_config(rtc_config); | 134 | Self::enable(rtc_config.clock_source); |
| 135 | |||
| 136 | rtc_struct.configure(rtc_config); | ||
| 137 | rtc_struct.rtc_config = rtc_config; | ||
| 139 | 138 | ||
| 140 | rtc_struct | 139 | rtc_struct |
| 141 | } | 140 | } |
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs index e3b9dfb8b..743a04d62 100644 --- a/embassy-stm32/src/rtc/v2.rs +++ b/embassy-stm32/src/rtc/v2.rs | |||
| @@ -1,15 +1,10 @@ | |||
| 1 | use stm32_metapac::rtc::vals::{Init, Osel, Pol}; | 1 | use stm32_metapac::rtc::vals::{Init, Osel, Pol}; |
| 2 | 2 | ||
| 3 | use super::{sealed, Instance, RtcConfig}; | 3 | use super::{sealed, Instance, RtcClockSource, RtcConfig}; |
| 4 | use crate::pac::rtc::Rtc; | 4 | use crate::pac::rtc::Rtc; |
| 5 | 5 | ||
| 6 | impl<'d, T: Instance> super::Rtc<'d, T> { | 6 | impl<'d, T: Instance> super::Rtc<'d, T> { |
| 7 | /// Applies the RTC config | 7 | pub(super) fn enable(clock_source: RtcClockSource) { |
| 8 | /// It this changes the RTC clock source the time will be reset | ||
| 9 | pub(super) fn apply_config(&mut self, rtc_config: RtcConfig) { | ||
| 10 | // Unlock the backup domain | ||
| 11 | let clock_config = rtc_config.clock_config as u8; | ||
| 12 | |||
| 13 | #[cfg(not(rtc_v2wb))] | 8 | #[cfg(not(rtc_v2wb))] |
| 14 | use stm32_metapac::rcc::vals::Rtcsel; | 9 | use stm32_metapac::rcc::vals::Rtcsel; |
| 15 | 10 | ||
| @@ -38,7 +33,7 @@ impl<'d, T: Instance> super::Rtc<'d, T> { | |||
| 38 | #[cfg(not(rtc_v2wb))] | 33 | #[cfg(not(rtc_v2wb))] |
| 39 | let rtcsel = reg.rtcsel().to_bits(); | 34 | let rtcsel = reg.rtcsel().to_bits(); |
| 40 | 35 | ||
| 41 | if !reg.rtcen() || rtcsel != clock_config { | 36 | if !reg.rtcen() || rtcsel != clock_source as u8 { |
| 42 | #[cfg(not(any(rtc_v2l0, rtc_v2l1, rtc_v2f2)))] | 37 | #[cfg(not(any(rtc_v2l0, rtc_v2l1, rtc_v2f2)))] |
| 43 | crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true)); | 38 | crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true)); |
| 44 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] | 39 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] |
| @@ -53,9 +48,9 @@ impl<'d, T: Instance> super::Rtc<'d, T> { | |||
| 53 | 48 | ||
| 54 | // Select RTC source | 49 | // Select RTC source |
| 55 | #[cfg(not(rtc_v2wb))] | 50 | #[cfg(not(rtc_v2wb))] |
| 56 | w.set_rtcsel(Rtcsel::from_bits(clock_config)); | 51 | w.set_rtcsel(Rtcsel::from_bits(clock_source as u8)); |
| 57 | #[cfg(rtc_v2wb)] | 52 | #[cfg(rtc_v2wb)] |
| 58 | w.set_rtcsel(clock_config); | 53 | w.set_rtcsel(clock_source as u8); |
| 59 | w.set_rtcen(true); | 54 | w.set_rtcen(true); |
| 60 | 55 | ||
| 61 | // Restore bcdr | 56 | // Restore bcdr |
| @@ -71,7 +66,11 @@ impl<'d, T: Instance> super::Rtc<'d, T> { | |||
| 71 | w.set_lsebyp(reg.lsebyp()); | 66 | w.set_lsebyp(reg.lsebyp()); |
| 72 | }); | 67 | }); |
| 73 | } | 68 | } |
| 69 | } | ||
| 74 | 70 | ||
| 71 | /// Applies the RTC config | ||
| 72 | /// It this changes the RTC clock source the time will be reset | ||
| 73 | pub(super) fn configure(&mut self, rtc_config: RtcConfig) { | ||
| 75 | self.write(true, |rtc| { | 74 | self.write(true, |rtc| { |
| 76 | rtc.cr().modify(|w| { | 75 | rtc.cr().modify(|w| { |
| 77 | #[cfg(rtc_v2f2)] | 76 | #[cfg(rtc_v2f2)] |
| @@ -87,8 +86,6 @@ impl<'d, T: Instance> super::Rtc<'d, T> { | |||
| 87 | w.set_prediv_a(rtc_config.async_prescaler); | 86 | w.set_prediv_a(rtc_config.async_prescaler); |
| 88 | }); | 87 | }); |
| 89 | }); | 88 | }); |
| 90 | |||
| 91 | self.rtc_config = rtc_config; | ||
| 92 | } | 89 | } |
| 93 | 90 | ||
| 94 | /// Calibrate the clock drift. | 91 | /// Calibrate the clock drift. |
diff --git a/embassy-stm32/src/rtc/v3.rs b/embassy-stm32/src/rtc/v3.rs index 8ef0ec51d..64ecf4fbd 100644 --- a/embassy-stm32/src/rtc/v3.rs +++ b/embassy-stm32/src/rtc/v3.rs | |||
| @@ -1,12 +1,10 @@ | |||
| 1 | use stm32_metapac::rtc::vals::{Calp, Calw16, Calw8, Fmt, Init, Key, Osel, Pol, TampalrmPu, TampalrmType}; | 1 | use stm32_metapac::rtc::vals::{Calp, Calw16, Calw8, Fmt, Init, Key, Osel, Pol, TampalrmPu, TampalrmType}; |
| 2 | 2 | ||
| 3 | use super::{sealed, Instance, RtcCalibrationCyclePeriod, RtcConfig}; | 3 | use super::{sealed, Instance, RtcCalibrationCyclePeriod, RtcClockSource, RtcConfig}; |
| 4 | use crate::pac::rtc::Rtc; | 4 | use crate::pac::rtc::Rtc; |
| 5 | 5 | ||
| 6 | impl<'d, T: Instance> super::Rtc<'d, T> { | 6 | impl<'d, T: Instance> super::Rtc<'d, T> { |
| 7 | /// Applies the RTC config | 7 | pub(super) fn enable(clock_source: RtcClockSource) { |
| 8 | /// It this changes the RTC clock source the time will be reset | ||
| 9 | pub(super) fn apply_config(&mut self, rtc_config: RtcConfig) { | ||
| 10 | // Unlock the backup domain | 8 | // Unlock the backup domain |
| 11 | #[cfg(not(any(rtc_v3u5, rcc_wl5, rcc_wle)))] | 9 | #[cfg(not(any(rtc_v3u5, rcc_wl5, rcc_wle)))] |
| 12 | { | 10 | { |
| @@ -24,11 +22,10 @@ impl<'d, T: Instance> super::Rtc<'d, T> { | |||
| 24 | let reg = crate::pac::RCC.bdcr().read(); | 22 | let reg = crate::pac::RCC.bdcr().read(); |
| 25 | assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet."); | 23 | assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet."); |
| 26 | 24 | ||
| 27 | let config_rtcsel = rtc_config.clock_config as u8; | ||
| 28 | #[cfg(not(any(rcc_wl5, rcc_wle)))] | 25 | #[cfg(not(any(rcc_wl5, rcc_wle)))] |
| 29 | let config_rtcsel = crate::pac::rcc::vals::Rtcsel::from_bits(config_rtcsel); | 26 | let config_rtcsel = crate::pac::rcc::vals::Rtcsel::from_bits(clock_source as u8); |
| 30 | 27 | ||
| 31 | if !reg.rtcen() || reg.rtcsel() != config_rtcsel { | 28 | if !reg.rtcen() || reg.rtcsel() != clock_source as u8 { |
| 32 | crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true)); | 29 | crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true)); |
| 33 | 30 | ||
| 34 | crate::pac::RCC.bdcr().modify(|w| { | 31 | crate::pac::RCC.bdcr().modify(|w| { |
| @@ -36,7 +33,7 @@ impl<'d, T: Instance> super::Rtc<'d, T> { | |||
| 36 | w.set_bdrst(false); | 33 | w.set_bdrst(false); |
| 37 | 34 | ||
| 38 | // Select RTC source | 35 | // Select RTC source |
| 39 | w.set_rtcsel(config_rtcsel); | 36 | w.set_rtcsel(clock_source as u8); |
| 40 | 37 | ||
| 41 | w.set_rtcen(true); | 38 | w.set_rtcen(true); |
| 42 | 39 | ||
| @@ -49,7 +46,11 @@ impl<'d, T: Instance> super::Rtc<'d, T> { | |||
| 49 | w.set_lsebyp(reg.lsebyp()); | 46 | w.set_lsebyp(reg.lsebyp()); |
| 50 | }); | 47 | }); |
| 51 | } | 48 | } |
| 49 | } | ||
| 52 | 50 | ||
| 51 | /// Applies the RTC config | ||
| 52 | /// It this changes the RTC clock source the time will be reset | ||
| 53 | pub(super) fn configure(&mut self, rtc_config: RtcConfig) { | ||
| 53 | self.write(true, |rtc| { | 54 | self.write(true, |rtc| { |
| 54 | rtc.cr().modify(|w| { | 55 | rtc.cr().modify(|w| { |
| 55 | w.set_fmt(Fmt::TWENTYFOURHOUR); | 56 | w.set_fmt(Fmt::TWENTYFOURHOUR); |
| @@ -69,8 +70,6 @@ impl<'d, T: Instance> super::Rtc<'d, T> { | |||
| 69 | w.set_tampalrm_pu(TampalrmPu::NOPULLUP); | 70 | w.set_tampalrm_pu(TampalrmPu::NOPULLUP); |
| 70 | }); | 71 | }); |
| 71 | }); | 72 | }); |
| 72 | |||
| 73 | self.rtc_config = rtc_config; | ||
| 74 | } | 73 | } |
| 75 | 74 | ||
| 76 | const RTC_CALR_MIN_PPM: f32 = -487.1; | 75 | const RTC_CALR_MIN_PPM: f32 = -487.1; |
