diff options
| author | Phil Markgraf <[email protected]> | 2023-07-15 04:40:23 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-15 13:40:23 +0200 |
| commit | 3bae53306683a57020ba751afaf631ec169deeed (patch) | |
| tree | 529e02e65c7186d1e26f514f36bd349f8e331ca3 | |
| parent | 7b36fe049dbb2b7c816a80d2238c2169bbc716d0 (diff) | |
Enable RTC on STM32WL chips (#1645)
* Add clippy allow to not report if same then branch
* Support enabling RTC clock on STM32WL
* Add clippy allow to not report if same then branch
* Support enabling RTC clock on STM32WL
* Add rtc example for stm32wl
* Address code review feedback
| -rw-r--r-- | embassy-stm32/src/rcc/wl.rs | 61 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v3.rs | 1 | ||||
| -rw-r--r-- | examples/stm32wl/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/stm32wl/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/stm32wl/src/bin/rtc.rs | 43 |
5 files changed, 108 insertions, 4 deletions
diff --git a/embassy-stm32/src/rcc/wl.rs b/embassy-stm32/src/rcc/wl.rs index 7072db984..6b69bb1cb 100644 --- a/embassy-stm32/src/rcc/wl.rs +++ b/embassy-stm32/src/rcc/wl.rs | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | use crate::pac::{FLASH, RCC}; | 1 | use crate::pac::pwr::vals::Dbp; |
| 2 | use crate::pac::{FLASH, PWR, RCC}; | ||
| 2 | use crate::rcc::{set_freqs, Clocks}; | 3 | use crate::rcc::{set_freqs, Clocks}; |
| 3 | use crate::time::Hertz; | 4 | use crate::time::Hertz; |
| 4 | 5 | ||
| @@ -184,6 +185,8 @@ pub struct Config { | |||
| 184 | pub apb1_pre: APBPrescaler, | 185 | pub apb1_pre: APBPrescaler, |
| 185 | pub apb2_pre: APBPrescaler, | 186 | pub apb2_pre: APBPrescaler, |
| 186 | pub enable_lsi: bool, | 187 | pub enable_lsi: bool, |
| 188 | pub enable_rtc_apb: bool, | ||
| 189 | pub rtc_mux: RtcClockSource, | ||
| 187 | } | 190 | } |
| 188 | 191 | ||
| 189 | impl Default for Config { | 192 | impl Default for Config { |
| @@ -196,10 +199,25 @@ impl Default for Config { | |||
| 196 | apb1_pre: APBPrescaler::NotDivided, | 199 | apb1_pre: APBPrescaler::NotDivided, |
| 197 | apb2_pre: APBPrescaler::NotDivided, | 200 | apb2_pre: APBPrescaler::NotDivided, |
| 198 | enable_lsi: false, | 201 | enable_lsi: false, |
| 202 | enable_rtc_apb: false, | ||
| 203 | rtc_mux: RtcClockSource::LSI32, | ||
| 199 | } | 204 | } |
| 200 | } | 205 | } |
| 201 | } | 206 | } |
| 202 | 207 | ||
| 208 | pub enum RtcClockSource { | ||
| 209 | LSE32, | ||
| 210 | LSI32, | ||
| 211 | } | ||
| 212 | |||
| 213 | #[repr(u8)] | ||
| 214 | pub enum Lsedrv { | ||
| 215 | Low = 0, | ||
| 216 | MediumLow = 1, | ||
| 217 | MediumHigh = 2, | ||
| 218 | High = 3, | ||
| 219 | } | ||
| 220 | |||
| 203 | pub(crate) unsafe fn init(config: Config) { | 221 | pub(crate) unsafe fn init(config: Config) { |
| 204 | let (sys_clk, sw, vos) = match config.mux { | 222 | let (sys_clk, sw, vos) = match config.mux { |
| 205 | ClockSrc::HSI16 => (HSI_FREQ.0, 0x01, VoltageScale::Range2), | 223 | ClockSrc::HSI16 => (HSI_FREQ.0, 0x01, VoltageScale::Range2), |
| @@ -266,6 +284,32 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 266 | 284 | ||
| 267 | while FLASH.acr().read().latency() != ws {} | 285 | while FLASH.acr().read().latency() != ws {} |
| 268 | 286 | ||
| 287 | match config.rtc_mux { | ||
| 288 | RtcClockSource::LSE32 => { | ||
| 289 | // 1. Unlock the backup domain | ||
| 290 | PWR.cr1().modify(|w| w.set_dbp(Dbp::ENABLED)); | ||
| 291 | |||
| 292 | // 2. Setup the LSE | ||
| 293 | RCC.bdcr().modify(|w| { | ||
| 294 | // Enable LSE | ||
| 295 | w.set_lseon(true); | ||
| 296 | // Max drive strength | ||
| 297 | // TODO: should probably be settable | ||
| 298 | w.set_lsedrv(Lsedrv::High as u8); //---// PAM - should not be commented | ||
| 299 | }); | ||
| 300 | |||
| 301 | // Wait until LSE is running | ||
| 302 | while !RCC.bdcr().read().lserdy() {} | ||
| 303 | } | ||
| 304 | RtcClockSource::LSI32 => { | ||
| 305 | // Turn on the internal 32 kHz LSI oscillator | ||
| 306 | RCC.csr().modify(|w| w.set_lsion(true)); | ||
| 307 | |||
| 308 | // Wait until LSI is running | ||
| 309 | while !RCC.csr().read().lsirdy() {} | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 269 | match config.mux { | 313 | match config.mux { |
| 270 | ClockSrc::HSI16 => { | 314 | ClockSrc::HSI16 => { |
| 271 | // Enable HSI16 | 315 | // Enable HSI16 |
| @@ -287,11 +331,26 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 287 | w.set_msirgsel(true); | 331 | w.set_msirgsel(true); |
| 288 | w.set_msirange(range.into()); | 332 | w.set_msirange(range.into()); |
| 289 | w.set_msion(true); | 333 | w.set_msion(true); |
| 334 | |||
| 335 | if let RtcClockSource::LSE32 = config.rtc_mux { | ||
| 336 | // If LSE is enabled, enable calibration of MSI | ||
| 337 | w.set_msipllen(true); | ||
| 338 | } else { | ||
| 339 | w.set_msipllen(false); | ||
| 340 | } | ||
| 290 | }); | 341 | }); |
| 291 | while !RCC.cr().read().msirdy() {} | 342 | while !RCC.cr().read().msirdy() {} |
| 292 | } | 343 | } |
| 293 | } | 344 | } |
| 294 | 345 | ||
| 346 | if config.enable_rtc_apb { | ||
| 347 | // enable peripheral clock for communication | ||
| 348 | crate::pac::RCC.apb1enr1().modify(|w| w.set_rtcapben(true)); | ||
| 349 | |||
| 350 | // read to allow the pwr clock to enable | ||
| 351 | crate::pac::PWR.cr1().read(); | ||
| 352 | } | ||
| 353 | |||
| 295 | RCC.extcfgr().modify(|w| { | 354 | RCC.extcfgr().modify(|w| { |
| 296 | if config.shd_ahb_pre == AHBPrescaler::NotDivided { | 355 | if config.shd_ahb_pre == AHBPrescaler::NotDivided { |
| 297 | w.set_shdhpre(0); | 356 | w.set_shdhpre(0); |
diff --git a/embassy-stm32/src/rtc/v3.rs b/embassy-stm32/src/rtc/v3.rs index 7e5c64d90..8ef0ec51d 100644 --- a/embassy-stm32/src/rtc/v3.rs +++ b/embassy-stm32/src/rtc/v3.rs | |||
| @@ -172,6 +172,7 @@ impl sealed::Instance for crate::peripherals::RTC { | |||
| 172 | const BACKUP_REGISTER_COUNT: usize = 32; | 172 | const BACKUP_REGISTER_COUNT: usize = 32; |
| 173 | 173 | ||
| 174 | fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { | 174 | fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { |
| 175 | #[allow(clippy::if_same_then_else)] | ||
| 175 | if register < Self::BACKUP_REGISTER_COUNT { | 176 | if register < Self::BACKUP_REGISTER_COUNT { |
| 176 | //Some(rtc.bkpr()[register].read().bits()) | 177 | //Some(rtc.bkpr()[register].read().bits()) |
| 177 | None // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC | 178 | None // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC |
diff --git a/examples/stm32wl/.cargo/config.toml b/examples/stm32wl/.cargo/config.toml index 4f8094ff2..ee416fcbc 100644 --- a/examples/stm32wl/.cargo/config.toml +++ b/examples/stm32wl/.cargo/config.toml | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | runner = "probe-rs run --chip STM32WLE5JCIx" | 3 | runner = "probe-rs run --chip STM32WLE5JCIx" |
| 4 | 4 | ||
| 5 | [build] | 5 | [build] |
| 6 | target = "thumbv7em-none-eabihf" | 6 | target = "thumbv7em-none-eabi" |
| 7 | 7 | ||
| 8 | [env] | 8 | [env] |
| 9 | DEFMT_LOG = "trace" | 9 | DEFMT_LOG = "trace" |
diff --git a/examples/stm32wl/Cargo.toml b/examples/stm32wl/Cargo.toml index 99f68387f..e2c66f456 100644 --- a/examples/stm32wl/Cargo.toml +++ b/examples/stm32wl/Cargo.toml | |||
| @@ -8,8 +8,8 @@ license = "MIT OR Apache-2.0" | |||
| 8 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 8 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 9 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 9 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 10 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 10 | embassy-time = { version = "0.1.2", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti", "chrono"] } |
| 12 | embassy-embedded-hal = {version = "0.1.0", path = "../../embassy-embedded-hal" } | 12 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } |
| 13 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] } | 13 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] } |
| 14 | lora-phy = { version = "1" } | 14 | lora-phy = { version = "1" } |
| 15 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } | 15 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } |
| @@ -25,6 +25,7 @@ embedded-storage = "0.3.0" | |||
| 25 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 25 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 26 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 26 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 27 | heapless = { version = "0.7.5", default-features = false } | 27 | heapless = { version = "0.7.5", default-features = false } |
| 28 | chrono = { version = "^0.4", default-features = false } | ||
| 28 | 29 | ||
| 29 | [patch.crates-io] | 30 | [patch.crates-io] |
| 30 | lora-phy = { git = "https://github.com/embassy-rs/lora-phy", rev = "ad289428fd44b02788e2fa2116445cc8f640a265" } | 31 | lora-phy = { git = "https://github.com/embassy-rs/lora-phy", rev = "ad289428fd44b02788e2fa2116445cc8f640a265" } |
diff --git a/examples/stm32wl/src/bin/rtc.rs b/examples/stm32wl/src/bin/rtc.rs new file mode 100644 index 000000000..e11825499 --- /dev/null +++ b/examples/stm32wl/src/bin/rtc.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use chrono::{NaiveDate, NaiveDateTime}; | ||
| 6 | use defmt::*; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_stm32::rcc::{self, ClockSrc}; | ||
| 9 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | ||
| 10 | use embassy_stm32::Config; | ||
| 11 | use embassy_time::{Duration, Timer}; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | #[embassy_executor::main] | ||
| 15 | async fn main(_spawner: Spawner) { | ||
| 16 | let p = { | ||
| 17 | let mut config = Config::default(); | ||
| 18 | config.rcc.mux = ClockSrc::HSE32; | ||
| 19 | config.rcc.rtc_mux = rcc::RtcClockSource::LSE32; | ||
| 20 | config.rcc.enable_rtc_apb = true; | ||
| 21 | embassy_stm32::init(config) | ||
| 22 | }; | ||
| 23 | info!("Hello World!"); | ||
| 24 | |||
| 25 | let now = NaiveDate::from_ymd_opt(2020, 5, 15) | ||
| 26 | .unwrap() | ||
| 27 | .and_hms_opt(10, 30, 15) | ||
| 28 | .unwrap(); | ||
| 29 | |||
| 30 | let mut rtc = Rtc::new( | ||
| 31 | p.RTC, | ||
| 32 | RtcConfig::default().clock_config(embassy_stm32::rtc::RtcClockSource::LSE), | ||
| 33 | ); | ||
| 34 | info!("Got RTC! {:?}", now.timestamp()); | ||
| 35 | |||
| 36 | rtc.set_datetime(now.into()).expect("datetime not set"); | ||
| 37 | |||
| 38 | // In reality the delay would be much longer | ||
| 39 | Timer::after(Duration::from_millis(20000)).await; | ||
| 40 | |||
| 41 | let then: NaiveDateTime = rtc.now().unwrap().into(); | ||
| 42 | info!("Got RTC! {:?}", then.timestamp()); | ||
| 43 | } | ||
