Merge pull request #1921 from xoviat/rtc

stm32: update configure_ls and misc cleanup
This commit is contained in:
xoviat 2023-09-18 00:04:32 +00:00 committed by GitHub
commit 0da793e5de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 104 additions and 61 deletions

View file

@ -1,4 +1,3 @@
use core::marker::PhantomData;
use core::ptr; use core::ptr;
use embassy_stm32::ipcc::Ipcc; use embassy_stm32::ipcc::Ipcc;
@ -13,7 +12,7 @@ use crate::unsafe_linked_list::LinkedListNode;
use crate::{channels, evt}; use crate::{channels, evt};
pub struct Ble { pub struct Ble {
phantom: PhantomData<Ble>, _private: (),
} }
impl Ble { impl Ble {
@ -29,7 +28,7 @@ impl Ble {
}); });
} }
Self { phantom: PhantomData } Self { _private: () }
} }
/// `HW_IPCC_BLE_EvtNot` /// `HW_IPCC_BLE_EvtNot`
pub async fn tl_read(&self) -> EvtBox<Self> { pub async fn tl_read(&self) -> EvtBox<Self> {

View file

@ -1,5 +1,4 @@
use core::future::poll_fn; use core::future::poll_fn;
use core::marker::PhantomData;
use core::ptr; use core::ptr;
use core::sync::atomic::{AtomicBool, Ordering}; use core::sync::atomic::{AtomicBool, Ordering};
use core::task::Poll; use core::task::Poll;
@ -21,12 +20,12 @@ static MAC_WAKER: AtomicWaker = AtomicWaker::new();
static MAC_EVT_OUT: AtomicBool = AtomicBool::new(false); static MAC_EVT_OUT: AtomicBool = AtomicBool::new(false);
pub struct Mac { pub struct Mac {
phantom: PhantomData<Mac>, _private: (),
} }
impl Mac { impl Mac {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
Self { phantom: PhantomData } Self { _private: () }
} }
/// `HW_IPCC_MAC_802_15_4_EvtNot` /// `HW_IPCC_MAC_802_15_4_EvtNot`

View file

@ -1,6 +1,5 @@
//! Memory manager routines //! Memory manager routines
use core::future::poll_fn; use core::future::poll_fn;
use core::marker::PhantomData;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use core::task::Poll; use core::task::Poll;
@ -21,7 +20,7 @@ static MM_WAKER: AtomicWaker = AtomicWaker::new();
static mut LOCAL_FREE_BUF_QUEUE: Aligned<A4, MaybeUninit<LinkedListNode>> = Aligned(MaybeUninit::uninit()); static mut LOCAL_FREE_BUF_QUEUE: Aligned<A4, MaybeUninit<LinkedListNode>> = Aligned(MaybeUninit::uninit());
pub struct MemoryManager { pub struct MemoryManager {
phantom: PhantomData<MemoryManager>, _private: (),
} }
impl MemoryManager { impl MemoryManager {
@ -44,7 +43,7 @@ impl MemoryManager {
}); });
} }
Self { phantom: PhantomData } Self { _private: () }
} }
pub async fn run_queue(&self) { pub async fn run_queue(&self) {

View file

@ -1,4 +1,3 @@
use core::marker::PhantomData;
use core::ptr; use core::ptr;
use crate::cmd::CmdPacket; use crate::cmd::CmdPacket;
@ -12,7 +11,7 @@ use crate::unsafe_linked_list::LinkedListNode;
use crate::{channels, Ipcc, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_DEVICE_INFO_TABLE, TL_SYS_TABLE}; use crate::{channels, Ipcc, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_DEVICE_INFO_TABLE, TL_SYS_TABLE};
pub struct Sys { pub struct Sys {
phantom: PhantomData<Sys>, _private: (),
} }
impl Sys { impl Sys {
@ -27,7 +26,7 @@ impl Sys {
}); });
} }
Self { phantom: PhantomData } Self { _private: () }
} }
/// Returns CPU2 wireless firmware information (if present). /// Returns CPU2 wireless firmware information (if present).

View file

@ -1,5 +1,5 @@
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Default)] #[derive(Default, Clone, Copy)]
pub enum LseDrive { pub enum LseDrive {
#[cfg(any(rtc_v2f7, rtc_v2l4))] #[cfg(any(rtc_v2f7, rtc_v2l4))]
Low = 0, Low = 0,
@ -87,40 +87,42 @@ impl BackupDomain {
rtc_v3u5 rtc_v3u5
))] ))]
#[allow(dead_code, unused_variables)] #[allow(dead_code, unused_variables)]
pub fn configure_ls(clock_source: RtcClockSource, lse_drive: Option<LseDrive>) { pub fn configure_ls(clock_source: RtcClockSource, lsi: bool, lse: Option<LseDrive>) {
match clock_source { if lsi {
RtcClockSource::LSI => { #[cfg(rtc_v3u5)]
#[cfg(rtc_v3u5)] let csr = crate::pac::RCC.bdcr();
let csr = crate::pac::RCC.bdcr();
#[cfg(not(rtc_v3u5))] #[cfg(not(rtc_v3u5))]
let csr = crate::pac::RCC.csr(); let csr = crate::pac::RCC.csr();
Self::modify(|_| {
#[cfg(not(any(rcc_wb, rcc_wba)))]
csr.modify(|w| w.set_lsion(true));
#[cfg(any(rcc_wb, rcc_wba))]
csr.modify(|w| w.set_lsi1on(true));
});
Self::modify(|_| {
#[cfg(not(any(rcc_wb, rcc_wba)))] #[cfg(not(any(rcc_wb, rcc_wba)))]
while !csr.read().lsirdy() {} csr.modify(|w| w.set_lsion(true));
#[cfg(any(rcc_wb, rcc_wba))] #[cfg(any(rcc_wb, rcc_wba))]
while !csr.read().lsi1rdy() {} csr.modify(|w| w.set_lsi1on(true));
} });
RtcClockSource::LSE => {
let lse_drive = lse_drive.unwrap_or_default();
Self::modify(|w| { #[cfg(not(any(rcc_wb, rcc_wba)))]
#[cfg(any(rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l4))] while !csr.read().lsirdy() {}
w.set_lsedrv(lse_drive.into());
w.set_lseon(true);
});
while !Self::read().lserdy() {} #[cfg(any(rcc_wb, rcc_wba))]
} while !csr.read().lsi1rdy() {}
}
if let Some(lse_drive) = lse {
Self::modify(|w| {
#[cfg(any(rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l4))]
w.set_lsedrv(lse_drive.into());
w.set_lseon(true);
});
while !Self::read().lserdy() {}
}
match clock_source {
RtcClockSource::LSI => assert!(lsi),
RtcClockSource::LSE => assert!(&lse.is_some()),
_ => {} _ => {}
}; };

View file

@ -291,6 +291,8 @@ pub struct Config {
pub pll: PLLConfig, pub pll: PLLConfig,
pub mux: ClockSrc, pub mux: ClockSrc,
pub rtc: Option<RtcClockSource>, pub rtc: Option<RtcClockSource>,
pub lsi: bool,
pub lse: Option<Hertz>,
pub voltage: VoltageScale, pub voltage: VoltageScale,
pub ahb_pre: AHBPrescaler, pub ahb_pre: AHBPrescaler,
pub apb1_pre: APBPrescaler, pub apb1_pre: APBPrescaler,
@ -308,6 +310,8 @@ impl Default for Config {
voltage: VoltageScale::Scale3, voltage: VoltageScale::Scale3,
mux: ClockSrc::HSI, mux: ClockSrc::HSI,
rtc: None, rtc: None,
lsi: false,
lse: None,
ahb_pre: AHBPrescaler::DIV1, ahb_pre: AHBPrescaler::DIV1,
apb1_pre: APBPrescaler::DIV1, apb1_pre: APBPrescaler::DIV1,
apb2_pre: APBPrescaler::DIV1, apb2_pre: APBPrescaler::DIV1,
@ -421,9 +425,11 @@ pub(crate) unsafe fn init(config: Config) {
RCC.apb1enr().modify(|w| w.set_pwren(true)); RCC.apb1enr().modify(|w| w.set_pwren(true));
PWR.cr().read(); PWR.cr().read();
config BackupDomain::configure_ls(
.rtc config.rtc.unwrap_or(RtcClockSource::NOCLOCK),
.map(|clock_source| BackupDomain::configure_ls(clock_source, None)); config.lsi,
config.lse.map(|_| Default::default()),
);
set_freqs(Clocks { set_freqs(Clocks {
sys: sys_clk, sys: sys_clk,

View file

@ -35,6 +35,8 @@ pub struct Config {
pub pll48: bool, pub pll48: bool,
pub rtc: Option<RtcClockSource>, pub rtc: Option<RtcClockSource>,
pub lsi: bool,
pub lse: Option<Hertz>,
} }
#[cfg(stm32f410)] #[cfg(stm32f410)]
@ -461,12 +463,15 @@ pub(crate) unsafe fn init(config: Config) {
}) })
}); });
config BackupDomain::configure_ls(
.rtc config.rtc.unwrap_or(RtcClockSource::NOCLOCK),
.map(|clock_source| BackupDomain::configure_ls(clock_source, None)); config.lsi,
config.lse.map(|_| Default::default()),
);
let rtc = match config.rtc { let rtc = match config.rtc {
Some(RtcClockSource::LSI) => Some(LSI_FREQ), Some(RtcClockSource::LSI) => Some(LSI_FREQ),
Some(RtcClockSource::LSE) => Some(config.lse.unwrap()),
_ => None, _ => None,
}; };

View file

@ -138,6 +138,8 @@ pub struct Config {
#[cfg(crs)] #[cfg(crs)]
pub enable_hsi48: bool, pub enable_hsi48: bool,
pub rtc: Option<RtcClockSource>, pub rtc: Option<RtcClockSource>,
pub lse: Option<Hertz>,
pub lsi: bool,
} }
impl Default for Config { impl Default for Config {
@ -151,6 +153,8 @@ impl Default for Config {
#[cfg(crs)] #[cfg(crs)]
enable_hsi48: false, enable_hsi48: false,
rtc: None, rtc: None,
lse: None,
lsi: false,
} }
} }
} }
@ -235,9 +239,11 @@ pub(crate) unsafe fn init(config: Config) {
} }
}; };
config.rtc.map(|rtc| { BackupDomain::configure_ls(
BackupDomain::configure_ls(rtc, None); config.rtc.unwrap_or(RtcClockSource::NOCLOCK),
}); config.lsi,
config.lse.map(|_| Default::default()),
);
RCC.cfgr().modify(|w| { RCC.cfgr().modify(|w| {
w.set_sw(sw); w.set_sw(sw);

View file

@ -241,6 +241,8 @@ pub struct Config {
#[cfg(not(any(stm32l471, stm32l475, stm32l476, stm32l486)))] #[cfg(not(any(stm32l471, stm32l475, stm32l476, stm32l486)))]
pub hsi48: bool, pub hsi48: bool,
pub rtc_mux: RtcClockSource, pub rtc_mux: RtcClockSource,
pub lse: Option<Hertz>,
pub lsi: bool,
} }
impl Default for Config { impl Default for Config {
@ -255,6 +257,8 @@ impl Default for Config {
#[cfg(not(any(stm32l471, stm32l475, stm32l476, stm32l486)))] #[cfg(not(any(stm32l471, stm32l475, stm32l476, stm32l486)))]
hsi48: false, hsi48: false,
rtc_mux: RtcClockSource::LSI, rtc_mux: RtcClockSource::LSI,
lsi: true,
lse: None,
} }
} }
} }
@ -407,7 +411,7 @@ pub(crate) unsafe fn init(config: Config) {
RCC.apb1enr1().modify(|w| w.set_pwren(true)); RCC.apb1enr1().modify(|w| w.set_pwren(true));
BackupDomain::configure_ls(config.rtc_mux, None); BackupDomain::configure_ls(config.rtc_mux, config.lsi, config.lse.map(|_| Default::default()));
let (sys_clk, sw) = match config.mux { let (sys_clk, sw) = match config.mux {
ClockSrc::MSI(range) => { ClockSrc::MSI(range) => {

View file

@ -31,6 +31,16 @@ pub use _version::*;
#[cfg(feature = "low-power")] #[cfg(feature = "low-power")]
use atomic_polyfill::{AtomicU32, Ordering}; use atomic_polyfill::{AtomicU32, Ordering};
// Model Clock Configuration
//
// pub struct Clocks {
// hse: Option<Hertz>,
// hsi: bool,
// lse: Option<Hertz>,
// lsi: bool,
// rtc: RtcSource,
// }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Clocks { pub struct Clocks {

View file

@ -108,6 +108,7 @@ pub struct Pll {
pub struct Config { pub struct Config {
pub hse: Option<Hse>, pub hse: Option<Hse>,
pub lse: Option<Hertz>, pub lse: Option<Hertz>,
pub lsi: bool,
pub sys: Sysclk, pub sys: Sysclk,
pub mux: Option<PllMux>, pub mux: Option<PllMux>,
pub pll48: Option<Pll48Source>, pub pll48: Option<Pll48Source>,
@ -136,6 +137,7 @@ pub const WPAN_DEFAULT: Config = Config {
}), }),
pll48: None, pll48: None,
rtc: Some(RtcClockSource::LSE), rtc: Some(RtcClockSource::LSE),
lsi: false,
pll: Some(Pll { pll: Some(Pll {
mul: 12, mul: 12,
@ -164,6 +166,7 @@ impl Default for Config {
pll: None, pll: None,
pllsai: None, pllsai: None,
rtc: None, rtc: None,
lsi: false,
ahb1_pre: AHBPrescaler::DIV1, ahb1_pre: AHBPrescaler::DIV1,
ahb2_pre: AHBPrescaler::DIV1, ahb2_pre: AHBPrescaler::DIV1,
@ -294,9 +297,11 @@ pub(crate) fn configure_clocks(config: &Config) {
rcc.cfgr().modify(|w| w.set_stopwuck(true)); rcc.cfgr().modify(|w| w.set_stopwuck(true));
config BackupDomain::configure_ls(
.rtc config.rtc.unwrap_or(RtcClockSource::NOCLOCK),
.map(|clock_source| BackupDomain::configure_ls(clock_source, None)); config.lsi,
config.lse.map(|_| Default::default()),
);
match &config.hse { match &config.hse {
Some(hse) => { Some(hse) => {

View file

@ -138,6 +138,8 @@ pub struct Config {
pub apb1_pre: APBPrescaler, pub apb1_pre: APBPrescaler,
pub apb2_pre: APBPrescaler, pub apb2_pre: APBPrescaler,
pub rtc_mux: RtcClockSource, pub rtc_mux: RtcClockSource,
pub lse: Option<Hertz>,
pub lsi: bool,
pub adc_clock_source: AdcClockSource, pub adc_clock_source: AdcClockSource,
} }
@ -151,6 +153,8 @@ impl Default for Config {
apb1_pre: APBPrescaler::DIV1, apb1_pre: APBPrescaler::DIV1,
apb2_pre: APBPrescaler::DIV1, apb2_pre: APBPrescaler::DIV1,
rtc_mux: RtcClockSource::LSI, rtc_mux: RtcClockSource::LSI,
lsi: true,
lse: None,
adc_clock_source: AdcClockSource::default(), adc_clock_source: AdcClockSource::default(),
} }
} }
@ -231,7 +235,7 @@ pub(crate) unsafe fn init(config: Config) {
while FLASH.acr().read().latency() != ws {} while FLASH.acr().read().latency() != ws {}
// Enables the LSI if configured // Enables the LSI if configured
BackupDomain::configure_ls(config.rtc_mux, None); BackupDomain::configure_ls(config.rtc_mux, config.lsi, config.lse.map(|_| Default::default()));
match config.mux { match config.mux {
ClockSrc::HSI16 => { ClockSrc::HSI16 => {

View file

@ -82,8 +82,9 @@ impl core::ops::Sub for RtcInstant {
} }
} }
#[non_exhaustive] pub struct RtcTimeProvider {
pub struct RtcTimeProvider; _private: (),
}
impl RtcTimeProvider { impl RtcTimeProvider {
/// Return the current datetime. /// Return the current datetime.
@ -186,8 +187,8 @@ impl Rtc {
} }
/// Acquire a [`RtcTimeProvider`] instance. /// Acquire a [`RtcTimeProvider`] instance.
pub fn time_provider(&self) -> RtcTimeProvider { pub const fn time_provider(&self) -> RtcTimeProvider {
RtcTimeProvider RtcTimeProvider { _private: () }
} }
/// Set the datetime to a new value. /// Set the datetime to a new value.
@ -222,7 +223,7 @@ impl Rtc {
/// ///
/// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`]. /// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`].
pub fn now(&self) -> Result<DateTime, RtcError> { pub fn now(&self) -> Result<DateTime, RtcError> {
RtcTimeProvider.now() self.time_provider().now()
} }
/// Check if daylight savings time is active. /// Check if daylight savings time is active.

View file

@ -12,13 +12,15 @@ use defmt::assert;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::rcc::RtcClockSource; use embassy_stm32::rcc::RtcClockSource;
use embassy_stm32::rtc::{Rtc, RtcConfig}; use embassy_stm32::rtc::{Rtc, RtcConfig};
use embassy_stm32::time::Hertz;
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let mut config = config(); let mut config = config();
config.rcc.rtc = Some(RtcClockSource::LSI); config.rcc.lse = Some(Hertz(32_768));
config.rcc.rtc = Some(RtcClockSource::LSE);
let p = embassy_stm32::init(config); let p = embassy_stm32::init(config);
info!("Hello World!"); info!("Hello World!");

View file

@ -13,6 +13,7 @@ use embassy_executor::Spawner;
use embassy_stm32::low_power::{stop_with_rtc, Executor}; use embassy_stm32::low_power::{stop_with_rtc, Executor};
use embassy_stm32::rcc::RtcClockSource; use embassy_stm32::rcc::RtcClockSource;
use embassy_stm32::rtc::{Rtc, RtcConfig}; use embassy_stm32::rtc::{Rtc, RtcConfig};
use embassy_stm32::time::Hertz;
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use static_cell::make_static; use static_cell::make_static;
@ -28,7 +29,8 @@ fn main() -> ! {
async fn async_main(_spawner: Spawner) { async fn async_main(_spawner: Spawner) {
let mut config = config(); let mut config = config();
config.rcc.rtc = Some(RtcClockSource::LSI); config.rcc.lse = Some(Hertz(32_768));
config.rcc.rtc = Some(RtcClockSource::LSE);
let p = embassy_stm32::init(config); let p = embassy_stm32::init(config);
info!("Hello World!"); info!("Hello World!");