From b8b0bb6eee22827b4eb493c6b48b35e01b860aad Mon Sep 17 00:00:00 2001 From: Cesar Tamayo Claro Date: Tue, 9 Dec 2025 13:44:23 -0700 Subject: Add basic RTC support --- examples/stm32f3/Cargo.toml | 5 ++-- examples/stm32wba6/src/bin/rtc.rs | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 examples/stm32wba6/src/bin/rtc.rs (limited to 'examples') diff --git a/examples/stm32f3/Cargo.toml b/examples/stm32f3/Cargo.toml index 4349e8055..e624b4092 100644 --- a/examples/stm32f3/Cargo.toml +++ b/examples/stm32f3/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] # Change stm32f303ze to your chip name, if necessary. -embassy-stm32 = { version = "0.4.0", path = "../../embassy-stm32", features = [ "defmt", "stm32f303ze", "unstable-pac", "memory-x", "time-driver-tim2", "exti"] } +embassy-stm32 = { version = "0.4.0", path = "../../embassy-stm32", features = [ "defmt", "stm32f302r8", "unstable-pac", "memory-x", "time-driver-tim2", "exti"] } embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] } embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } @@ -27,7 +27,8 @@ embedded-storage = "0.3.1" static_cell = "2" [profile.release] -debug = 2 +debug = false +opt-level = 'z' [package.metadata.embassy] build = [ diff --git a/examples/stm32wba6/src/bin/rtc.rs b/examples/stm32wba6/src/bin/rtc.rs new file mode 100644 index 000000000..1e5634c60 --- /dev/null +++ b/examples/stm32wba6/src/bin/rtc.rs @@ -0,0 +1,63 @@ +#![no_std] +#![no_main] + +// use chrono::{NaiveDate, NaiveDateTime}; +use defmt::*; +use embassy_executor::Spawner; +use embassy_stm32::Config; +use embassy_stm32::rcc::*; +use embassy_stm32::rtc::{DateTime, DayOfWeek, Rtc, RtcConfig}; +use embassy_time::Timer; +use {defmt_rtt as _, panic_probe as _}; + +pub fn pll_init(config: &mut Config) { + config.rcc.pll1 = Some(embassy_stm32::rcc::Pll { + source: PllSource::HSI, + prediv: PllPreDiv::DIV1, // PLLM = 1 → HSI / 1 = 16 MHz + mul: PllMul::MUL30, // PLLN = 30 → 16 MHz * 30 = 480 MHz VCO + divr: Some(PllDiv::DIV5), // PLLR = 5 → 96 MHz (Sysclk) + // divq: Some(PllDiv::DIV10), // PLLQ = 10 → 48 MHz (NOT USED) + divq: None, + divp: Some(PllDiv::DIV30), // PLLP = 30 → 16 MHz (USBOTG) + frac: Some(0), // Fractional part (enabled) + }); + + config.rcc.ahb_pre = AHBPrescaler::DIV1; + config.rcc.apb1_pre = APBPrescaler::DIV1; + config.rcc.apb2_pre = APBPrescaler::DIV1; + config.rcc.apb7_pre = APBPrescaler::DIV1; + config.rcc.ahb5_pre = AHB5Prescaler::DIV4; + + // voltage scale for max performance + config.rcc.voltage_scale = VoltageScale::RANGE1; + // route PLL1_P into the USB‐OTG‐HS block + config.rcc.sys = Sysclk::PLL1_R; +} + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut config = Config::default(); + + pll_init(&mut config); + + let p = embassy_stm32::init(config); + + let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); + + // Setting datetime + let initial_datetime = DateTime::from(2025, 12, 9, DayOfWeek::Tuesday, 11, 00, 00, 0).unwrap(); + match rtc.0.set_datetime(initial_datetime) { + Ok(()) => info!("RTC set successfully."), + Err(e) => error!("Failed to set RTC date/time: {:?}", e), + } + + // Reading datetime every 1s + loop { + match rtc.1.now() { + Ok(result) => info!("{}", result), + Err(e) => error!("Failed to set RTC date/time: {:?}", e), + } + + Timer::after_millis(1000).await; + } +} -- cgit From e8a84adb8d5bf3f773dfccd88c97390f36b5605a Mon Sep 17 00:00:00 2001 From: Cesar Tamayo Claro Date: Tue, 9 Dec 2025 14:03:55 -0700 Subject: Undo smt32f3 accidental change --- examples/stm32f3/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stm32f3/Cargo.toml b/examples/stm32f3/Cargo.toml index e624b4092..a4cfe7ed8 100644 --- a/examples/stm32f3/Cargo.toml +++ b/examples/stm32f3/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] # Change stm32f303ze to your chip name, if necessary. -embassy-stm32 = { version = "0.4.0", path = "../../embassy-stm32", features = [ "defmt", "stm32f302r8", "unstable-pac", "memory-x", "time-driver-tim2", "exti"] } +embassy-stm32 = { version = "0.4.0", path = "../../embassy-stm32", features = [ "defmt", "stm32f303ze", "unstable-pac", "memory-x", "time-driver-tim2", "exti"] } embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] } embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt"] } embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } -- cgit From 351f300fa3be839dfddb409c764541746d19b939 Mon Sep 17 00:00:00 2001 From: Cesar Tamayo Claro Date: Tue, 9 Dec 2025 14:05:28 -0700 Subject: Undo smt32f3 accidental change 2.0 --- examples/stm32f3/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/stm32f3/Cargo.toml b/examples/stm32f3/Cargo.toml index a4cfe7ed8..4349e8055 100644 --- a/examples/stm32f3/Cargo.toml +++ b/examples/stm32f3/Cargo.toml @@ -27,8 +27,7 @@ embedded-storage = "0.3.1" static_cell = "2" [profile.release] -debug = false -opt-level = 'z' +debug = 2 [package.metadata.embassy] build = [ -- cgit From a48bd43f6ff39f9a98357a17d02ec03461b09351 Mon Sep 17 00:00:00 2001 From: Cesar Tamayo Claro Date: Tue, 9 Dec 2025 23:13:01 -0700 Subject: Update set date to Unix Epoch --- examples/stm32wba6/src/bin/rtc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stm32wba6/src/bin/rtc.rs b/examples/stm32wba6/src/bin/rtc.rs index 1e5634c60..f150df1c0 100644 --- a/examples/stm32wba6/src/bin/rtc.rs +++ b/examples/stm32wba6/src/bin/rtc.rs @@ -45,7 +45,7 @@ async fn main(_spawner: Spawner) { let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); // Setting datetime - let initial_datetime = DateTime::from(2025, 12, 9, DayOfWeek::Tuesday, 11, 00, 00, 0).unwrap(); + let initial_datetime = DateTime::from(1970, 1, 1, DayOfWeek::Thursday, 0, 00, 00, 0).unwrap(); match rtc.0.set_datetime(initial_datetime) { Ok(()) => info!("RTC set successfully."), Err(e) => error!("Failed to set RTC date/time: {:?}", e), -- cgit From 1d4fb4e39ef13f5d2b1c8979e7058acf6836e85b Mon Sep 17 00:00:00 2001 From: Cesar Tamayo Claro Date: Tue, 9 Dec 2025 23:16:34 -0700 Subject: Remove commented import --- examples/stm32wba6/src/bin/rtc.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'examples') diff --git a/examples/stm32wba6/src/bin/rtc.rs b/examples/stm32wba6/src/bin/rtc.rs index f150df1c0..cef8501e0 100644 --- a/examples/stm32wba6/src/bin/rtc.rs +++ b/examples/stm32wba6/src/bin/rtc.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] -// use chrono::{NaiveDate, NaiveDateTime}; use defmt::*; use embassy_executor::Spawner; use embassy_stm32::Config; -- cgit From 3892f575a0b4fc8ad2186020389d9923568a2534 Mon Sep 17 00:00:00 2001 From: Cesar Tamayo Claro Date: Tue, 9 Dec 2025 23:17:03 -0700 Subject: Copy rtc into stm32wba examples folder --- examples/stm32wba/src/bin/rtc.rs | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 examples/stm32wba/src/bin/rtc.rs (limited to 'examples') diff --git a/examples/stm32wba/src/bin/rtc.rs b/examples/stm32wba/src/bin/rtc.rs new file mode 100644 index 000000000..cef8501e0 --- /dev/null +++ b/examples/stm32wba/src/bin/rtc.rs @@ -0,0 +1,62 @@ +#![no_std] +#![no_main] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_stm32::Config; +use embassy_stm32::rcc::*; +use embassy_stm32::rtc::{DateTime, DayOfWeek, Rtc, RtcConfig}; +use embassy_time::Timer; +use {defmt_rtt as _, panic_probe as _}; + +pub fn pll_init(config: &mut Config) { + config.rcc.pll1 = Some(embassy_stm32::rcc::Pll { + source: PllSource::HSI, + prediv: PllPreDiv::DIV1, // PLLM = 1 → HSI / 1 = 16 MHz + mul: PllMul::MUL30, // PLLN = 30 → 16 MHz * 30 = 480 MHz VCO + divr: Some(PllDiv::DIV5), // PLLR = 5 → 96 MHz (Sysclk) + // divq: Some(PllDiv::DIV10), // PLLQ = 10 → 48 MHz (NOT USED) + divq: None, + divp: Some(PllDiv::DIV30), // PLLP = 30 → 16 MHz (USBOTG) + frac: Some(0), // Fractional part (enabled) + }); + + config.rcc.ahb_pre = AHBPrescaler::DIV1; + config.rcc.apb1_pre = APBPrescaler::DIV1; + config.rcc.apb2_pre = APBPrescaler::DIV1; + config.rcc.apb7_pre = APBPrescaler::DIV1; + config.rcc.ahb5_pre = AHB5Prescaler::DIV4; + + // voltage scale for max performance + config.rcc.voltage_scale = VoltageScale::RANGE1; + // route PLL1_P into the USB‐OTG‐HS block + config.rcc.sys = Sysclk::PLL1_R; +} + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut config = Config::default(); + + pll_init(&mut config); + + let p = embassy_stm32::init(config); + + let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); + + // Setting datetime + let initial_datetime = DateTime::from(1970, 1, 1, DayOfWeek::Thursday, 0, 00, 00, 0).unwrap(); + match rtc.0.set_datetime(initial_datetime) { + Ok(()) => info!("RTC set successfully."), + Err(e) => error!("Failed to set RTC date/time: {:?}", e), + } + + // Reading datetime every 1s + loop { + match rtc.1.now() { + Ok(result) => info!("{}", result), + Err(e) => error!("Failed to set RTC date/time: {:?}", e), + } + + Timer::after_millis(1000).await; + } +} -- cgit