From 952f525af5bfbb800ab4593b77e69b8b13f95b16 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Fri, 11 Jun 2021 09:19:02 +0200 Subject: [PATCH 1/9] Provide a way for a peripheral to query its clock frequency Currently this looks up the frequency in the global singleton that must be initialized by the per-chip RCC implementation. At present, this is only done for the L0 family of chips. --- embassy-macros/src/chip/stm32.rs | 4 +--- embassy-stm32/src/clock.rs | 4 +++- embassy-stm32/src/rcc/l0/mod.rs | 24 ++++++++++-------------- embassy-stm32/src/rcc/mod.rs | 23 ++++++++++++++--------- embassy-stm32/src/spi/v2.rs | 2 +- stm32-metapac/gen/src/lib.rs | 15 +++++++++++++++ 6 files changed, 44 insertions(+), 28 deletions(-) diff --git a/embassy-macros/src/chip/stm32.rs b/embassy-macros/src/chip/stm32.rs index 0a3a5abb9..9cd0111c9 100644 --- a/embassy-macros/src/chip/stm32.rs +++ b/embassy-macros/src/chip/stm32.rs @@ -17,9 +17,7 @@ pub fn generate(embassy_prefix: &ModulePrefix, config: syn::Expr) -> TokenStream ); let clock = unsafe { make_static(&mut c) }; - // TODO: Is TIM2 always APB1? - let timer_freq = unsafe { #embassy_stm32_path::rcc::get_freqs().apb1_clk }; - clock.start(timer_freq); + clock.start(); let mut alarm = clock.alarm1(); unsafe { #embassy_path::time::set_clock(clock) }; diff --git a/embassy-stm32/src/clock.rs b/embassy-stm32/src/clock.rs index 39a96402a..6c3175789 100644 --- a/embassy-stm32/src/clock.rs +++ b/embassy-stm32/src/clock.rs @@ -77,12 +77,14 @@ impl Clock { } } - pub fn start(&'static self, timer_freq: Hertz) { + pub fn start(&'static self) { let inner = T::inner(); T::enable(); T::reset(); + let timer_freq = T::frequency(); + // NOTE(unsafe) Critical section to use the unsafe methods critical_section::with(|_| { unsafe { diff --git a/embassy-stm32/src/rcc/l0/mod.rs b/embassy-stm32/src/rcc/l0/mod.rs index d08c32b17..978d373a2 100644 --- a/embassy-stm32/src/rcc/l0/mod.rs +++ b/embassy-stm32/src/rcc/l0/mod.rs @@ -469,35 +469,31 @@ impl RccExt for RCC { } }; - let (apb1_freq, apb1_tim_freq, apb1_pre) = match cfgr.apb1_pre { - APBPrescaler::NotDivided => (ahb_freq, ahb_freq, 1), + let apb1_freq = match cfgr.apb1_pre { + APBPrescaler::NotDivided => ahb_freq, pre => { let pre: Ppre = pre.into(); let pre: u8 = 1 << (pre.0 - 3); let freq = ahb_freq / pre as u32; - (freq, freq * 2, pre as u8) + freq } }; - let (apb2_freq, apb2_tim_freq, apb2_pre) = match cfgr.apb2_pre { - APBPrescaler::NotDivided => (ahb_freq, ahb_freq, 1), + let apb2_freq = match cfgr.apb2_pre { + APBPrescaler::NotDivided => ahb_freq, pre => { let pre: Ppre = pre.into(); let pre: u8 = 1 << (pre.0 - 3); let freq = ahb_freq / (1 << (pre as u8 - 3)); - (freq, freq * 2, pre as u8) + freq } }; Clocks { - sys_clk: sys_clk.hz(), - ahb_clk: ahb_freq.hz(), - apb1_clk: apb1_freq.hz(), - apb2_clk: apb2_freq.hz(), - apb1_tim_clk: apb1_tim_freq.hz(), - apb2_tim_clk: apb2_tim_freq.hz(), - apb1_pre, - apb2_pre, + sys: sys_clk.hz(), + ahb: ahb_freq.hz(), + apb1: apb1_freq.hz(), + apb2: apb2_freq.hz(), } } } diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs index 3c5b53b05..132b50b02 100644 --- a/embassy-stm32/src/rcc/mod.rs +++ b/embassy-stm32/src/rcc/mod.rs @@ -9,14 +9,10 @@ use core::mem::MaybeUninit; /// The existence of this value indicates that the clock configuration can no longer be changed #[derive(Clone, Copy)] pub struct Clocks { - pub sys_clk: Hertz, - pub ahb_clk: Hertz, - pub apb1_clk: Hertz, - pub apb1_tim_clk: Hertz, - pub apb2_clk: Hertz, - pub apb2_tim_clk: Hertz, - pub apb1_pre: u8, - pub apb2_pre: u8, + pub sys: Hertz, + pub ahb: Hertz, + pub apb1: Hertz, + pub apb2: Hertz, } static mut CLOCK_FREQS: MaybeUninit = MaybeUninit::uninit(); @@ -50,6 +46,7 @@ cfg_if::cfg_if! { pub(crate) mod sealed { pub trait RccPeripheral { + fn frequency() -> crate::time::Hertz; fn reset(); fn enable(); fn disable(); @@ -59,8 +56,16 @@ pub(crate) mod sealed { pub trait RccPeripheral: sealed::RccPeripheral + 'static {} crate::pac::peripheral_rcc!( - ($inst:ident, $enable:ident, $reset:ident, $perien:ident, $perirst:ident) => { + ($inst:ident, $clk:ident, $enable:ident, $reset:ident, $perien:ident, $perirst:ident) => { impl sealed::RccPeripheral for peripherals::$inst { + fn frequency() -> crate::time::Hertz { + critical_section::with(|_| { + unsafe { + let freqs = get_freqs(); + freqs.$clk + } + }) + } fn enable() { critical_section::with(|_| { unsafe { diff --git a/embassy-stm32/src/spi/v2.rs b/embassy-stm32/src/spi/v2.rs index a7ac54cdd..4e135e9df 100644 --- a/embassy-stm32/src/spi/v2.rs +++ b/embassy-stm32/src/spi/v2.rs @@ -37,7 +37,6 @@ pub struct Spi<'d, T: Instance> { impl<'d, T: Instance> Spi<'d, T> { pub fn new( - pclk: Hertz, _peri: impl Unborrow + 'd, sck: impl Unborrow>, mosi: impl Unborrow>, @@ -60,6 +59,7 @@ impl<'d, T: Instance> Spi<'d, T> { let mosi = mosi.degrade(); let miso = miso.degrade(); + let pclk = T::frequency(); let br = Self::compute_baud_rate(pclk, freq.into()); unsafe { diff --git a/stm32-metapac/gen/src/lib.rs b/stm32-metapac/gen/src/lib.rs index 399840c5b..ae01f1e85 100644 --- a/stm32-metapac/gen/src/lib.rs +++ b/stm32-metapac/gen/src/lib.rs @@ -287,8 +287,23 @@ pub fn gen(options: Options) { match (en, rst) { (Some((enable_reg, enable_field)), Some((reset_reg, reset_field))) => { + let clock = if clock_prefix == "" { + let re = Regex::new("([A-Z]+\\d*).*").unwrap(); + if !re.is_match(enable_reg) { + panic!( + "unable to derive clock name from register name {}", + enable_reg + ); + } else { + let caps = re.captures(enable_reg).unwrap(); + caps.get(1).unwrap().as_str() + } + } else { + clock_prefix + }; peripheral_rcc_table.push(vec![ name.clone(), + clock.to_ascii_lowercase(), enable_reg.to_ascii_lowercase(), reset_reg.to_ascii_lowercase(), format!("set_{}", enable_field.to_ascii_lowercase()), From 2c63393c9e274cc2e8955c5d84e35f4e89dc886d Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Fri, 11 Jun 2021 17:45:07 +0200 Subject: [PATCH 2/9] Add Clock type per RCC family --- embassy-stm32/src/rcc/l0/mod.rs | 10 ++++++++- embassy-stm32/src/rcc/mod.rs | 40 +++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/embassy-stm32/src/rcc/l0/mod.rs b/embassy-stm32/src/rcc/l0/mod.rs index 978d373a2..fc8a94883 100644 --- a/embassy-stm32/src/rcc/l0/mod.rs +++ b/embassy-stm32/src/rcc/l0/mod.rs @@ -1,6 +1,6 @@ use crate::pac; use crate::peripherals::{self, CRS, RCC, SYSCFG}; -use crate::rcc::{get_freqs, set_freqs, Clocks}; +use crate::rcc::{get_freqs, set_freqs}; use crate::time::Hertz; use crate::time::U32Ext; use core::marker::PhantomData; @@ -12,6 +12,14 @@ use pac::rcc::vals::{Hpre, Msirange, Plldiv, Pllmul, Pllsrc, Ppre, Sw}; /// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, /// and with the addition of the init function to configure a system clock. +#[derive(Clone, Copy)] +pub struct Clocks { + pub sys: Hertz, + pub ahb: Hertz, + pub apb1: Hertz, + pub apb2: Hertz, +} + /// System clock mux source #[derive(Clone, Copy)] pub enum ClockSrc { diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs index 132b50b02..7271030aa 100644 --- a/embassy-stm32/src/rcc/mod.rs +++ b/embassy-stm32/src/rcc/mod.rs @@ -1,20 +1,11 @@ #![macro_use] use crate::peripherals; -use crate::time::Hertz; use core::mem::MaybeUninit; /// Frozen clock frequencies /// /// The existence of this value indicates that the clock configuration can no longer be changed -#[derive(Clone, Copy)] -pub struct Clocks { - pub sys: Hertz, - pub ahb: Hertz, - pub apb1: Hertz, - pub apb2: Hertz, -} - static mut CLOCK_FREQS: MaybeUninit = MaybeUninit::uninit(); /// Sets the clock frequencies @@ -36,6 +27,37 @@ cfg_if::cfg_if! { } else if #[cfg(rcc_l0)] { mod l0; pub use l0::*; + } else if #[cfg(rcc_l4)] { + // TODO: Implement + use crate::time::Hertz; + + #[derive(Clone, Copy)] + pub struct Clocks { + pub apb1: Hertz, + pub apb2: Hertz, + pub ahb2: Hertz, + } + + #[derive(Default)] + pub struct Config {} + pub unsafe fn init(_config: Config) { + } + } else if #[cfg(rcc_f4)] { + // TODO: Implement + use crate::time::Hertz; + + #[derive(Clone, Copy)] + pub struct Clocks { + pub apb1: Hertz, + pub apb2: Hertz, + pub ahb2: Hertz, + } + + #[derive(Default)] + pub struct Config {} + pub unsafe fn init(_config: Config) { + } + } else { #[derive(Default)] pub struct Config {} From 0b52731897322ed7e002a713bd6eab6a59c15ebc Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Fri, 11 Jun 2021 17:53:05 +0200 Subject: [PATCH 3/9] Add clocks for h7 --- embassy-stm32/src/rcc/h7/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/embassy-stm32/src/rcc/h7/mod.rs b/embassy-stm32/src/rcc/h7/mod.rs index f15dbe075..bd133dc7e 100644 --- a/embassy-stm32/src/rcc/h7/mod.rs +++ b/embassy-stm32/src/rcc/h7/mod.rs @@ -12,6 +12,15 @@ mod pll; use pll::pll_setup; pub use pll::PllConfig; +// Clock type used by peripherals +#[derive(Clone, Copy)] +pub struct Clocks { + pub apb1: Hertz, + pub apb2: Hertz, + pub apb4: Hertz, + pub ahb2: Hertz, +} + const HSI: Hertz = Hertz(64_000_000); const CSI: Hertz = Hertz(4_000_000); const HSI48: Hertz = Hertz(48_000_000); From a13e07625fb191145d4dcb13b1dac9f4ef86bb8c Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Fri, 11 Jun 2021 17:58:58 +0200 Subject: [PATCH 4/9] Add ... c1? --- embassy-stm32/src/rcc/h7/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/embassy-stm32/src/rcc/h7/mod.rs b/embassy-stm32/src/rcc/h7/mod.rs index bd133dc7e..11f8ec898 100644 --- a/embassy-stm32/src/rcc/h7/mod.rs +++ b/embassy-stm32/src/rcc/h7/mod.rs @@ -19,6 +19,7 @@ pub struct Clocks { pub apb2: Hertz, pub apb4: Hertz, pub ahb2: Hertz, + pub c1: Hertz, } const HSI: Hertz = Hertz(64_000_000); From 95532726b2fe38c3510b8ba3e56c3cb8f4e3a914 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 14 Jun 2021 10:48:14 +0200 Subject: [PATCH 5/9] Add minimal RCC impls for L4 and F4 --- embassy-stm32/src/rcc/f4/mod.rs | 204 ++++++++++++++++++++++++++++++++ embassy-stm32/src/rcc/h7/mod.rs | 26 ++-- embassy-stm32/src/rcc/l0/mod.rs | 96 +-------------- embassy-stm32/src/rcc/l4/mod.rs | 203 +++++++++++++++++++++++++++++++ embassy-stm32/src/rcc/mod.rs | 54 ++++----- examples/stm32f4/src/bin/spi.rs | 1 - examples/stm32l0/src/bin/spi.rs | 1 - examples/stm32l4/src/bin/spi.rs | 1 - stm32-metapac/gen/src/lib.rs | 6 +- 9 files changed, 455 insertions(+), 137 deletions(-) create mode 100644 embassy-stm32/src/rcc/f4/mod.rs create mode 100644 embassy-stm32/src/rcc/l4/mod.rs diff --git a/embassy-stm32/src/rcc/f4/mod.rs b/embassy-stm32/src/rcc/f4/mod.rs new file mode 100644 index 000000000..abd631944 --- /dev/null +++ b/embassy-stm32/src/rcc/f4/mod.rs @@ -0,0 +1,204 @@ +pub use super::common::*; +use crate::pac; +use crate::peripherals::{self, RCC}; +use crate::rcc::{get_freqs, set_freqs, Clocks}; +use crate::time::Hertz; +use crate::time::U32Ext; +use core::marker::PhantomData; +use embassy::util::Unborrow; +use embassy_extras::unborrow; +use pac::rcc::vals::{Hpre, Ppre, Sw}; + +/// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, +/// and with the addition of the init function to configure a system clock. + +/// Only the basic setup using the HSE and HSI clocks are supported as of now. + +/// HSI speed +pub const HSI_FREQ: u32 = 16_000_000; + +/// System clock mux source +#[derive(Clone, Copy)] +pub enum ClockSrc { + HSE(Hertz), + HSI16, +} + +impl Into for APBPrescaler { + fn into(self) -> Ppre { + match self { + APBPrescaler::NotDivided => Ppre::DIV1, + APBPrescaler::Div2 => Ppre::DIV2, + APBPrescaler::Div4 => Ppre::DIV4, + APBPrescaler::Div8 => Ppre::DIV8, + APBPrescaler::Div16 => Ppre::DIV16, + } + } +} + +impl Into for AHBPrescaler { + fn into(self) -> Hpre { + match self { + AHBPrescaler::NotDivided => Hpre::DIV1, + AHBPrescaler::Div2 => Hpre::DIV2, + AHBPrescaler::Div4 => Hpre::DIV4, + AHBPrescaler::Div8 => Hpre::DIV8, + AHBPrescaler::Div16 => Hpre::DIV16, + AHBPrescaler::Div64 => Hpre::DIV64, + AHBPrescaler::Div128 => Hpre::DIV128, + AHBPrescaler::Div256 => Hpre::DIV256, + AHBPrescaler::Div512 => Hpre::DIV512, + } + } +} + +/// Clocks configutation +pub struct Config { + mux: ClockSrc, + ahb_pre: AHBPrescaler, + apb1_pre: APBPrescaler, + apb2_pre: APBPrescaler, +} + +impl Default for Config { + #[inline] + fn default() -> Config { + Config { + mux: ClockSrc::HSI16, + ahb_pre: AHBPrescaler::NotDivided, + apb1_pre: APBPrescaler::NotDivided, + apb2_pre: APBPrescaler::NotDivided, + } + } +} + +impl Config { + #[inline] + pub fn clock_src(mut self, mux: ClockSrc) -> Self { + self.mux = mux; + self + } + + #[inline] + pub fn ahb_pre(mut self, pre: AHBPrescaler) -> Self { + self.ahb_pre = pre; + self + } + + #[inline] + pub fn apb1_pre(mut self, pre: APBPrescaler) -> Self { + self.apb1_pre = pre; + self + } + + #[inline] + pub fn apb2_pre(mut self, pre: APBPrescaler) -> Self { + self.apb2_pre = pre; + self + } +} + +/// RCC peripheral +pub struct Rcc<'d> { + _rb: peripherals::RCC, + phantom: PhantomData<&'d mut peripherals::RCC>, +} + +impl<'d> Rcc<'d> { + pub fn new(rcc: impl Unborrow + 'd) -> Self { + unborrow!(rcc); + Self { + _rb: rcc, + phantom: PhantomData, + } + } + + // Safety: RCC init must have been called + pub fn clocks(&self) -> &'static Clocks { + unsafe { get_freqs() } + } +} + +/// Extension trait that freezes the `RCC` peripheral with provided clocks configuration +pub trait RccExt { + fn freeze(self, config: Config) -> Clocks; +} + +impl RccExt for RCC { + #[inline] + fn freeze(self, cfgr: Config) -> Clocks { + let rcc = pac::RCC; + let (sys_clk, sw) = match cfgr.mux { + ClockSrc::HSI16 => { + // Enable HSI16 + unsafe { + rcc.cr().write(|w| w.set_hsion(true)); + while !rcc.cr().read().hsirdy() {} + } + + (HSI_FREQ, Sw::HSI) + } + ClockSrc::HSE(freq) => { + // Enable HSE + unsafe { + rcc.cr().write(|w| w.set_hseon(true)); + while !rcc.cr().read().hserdy() {} + } + + (freq.0, Sw::HSE) + } + }; + + unsafe { + rcc.cfgr().modify(|w| { + w.set_sw(sw.into()); + w.set_hpre(cfgr.ahb_pre.into()); + w.set_ppre1(cfgr.apb1_pre.into()); + w.set_ppre2(cfgr.apb2_pre.into()); + }); + } + + let ahb_freq: u32 = match cfgr.ahb_pre { + AHBPrescaler::NotDivided => sys_clk, + pre => { + let pre: Hpre = pre.into(); + let pre = 1 << (pre.0 as u32 - 7); + sys_clk / pre + } + }; + + let apb1_freq = match cfgr.apb1_pre { + APBPrescaler::NotDivided => ahb_freq, + pre => { + let pre: Ppre = pre.into(); + let pre: u8 = 1 << (pre.0 - 3); + let freq = ahb_freq / pre as u32; + freq + } + }; + + let apb2_freq = match cfgr.apb2_pre { + APBPrescaler::NotDivided => ahb_freq, + pre => { + let pre: Ppre = pre.into(); + let pre: u8 = 1 << (pre.0 - 3); + let freq = ahb_freq / (1 << (pre as u8 - 3)); + freq + } + }; + + Clocks { + sys: sys_clk.hz(), + ahb1: ahb_freq.hz(), + ahb2: ahb_freq.hz(), + apb1: apb1_freq.hz(), + apb2: apb2_freq.hz(), + } + } +} + +pub unsafe fn init(config: Config) { + let r = ::steal(); + let clocks = r.freeze(config); + set_freqs(clocks); +} diff --git a/embassy-stm32/src/rcc/h7/mod.rs b/embassy-stm32/src/rcc/h7/mod.rs index 11f8ec898..f51ea9d71 100644 --- a/embassy-stm32/src/rcc/h7/mod.rs +++ b/embassy-stm32/src/rcc/h7/mod.rs @@ -6,22 +6,13 @@ use crate::pac::rcc::vals::Timpre; use crate::pac::{DBGMCU, RCC, SYSCFG}; use crate::peripherals; use crate::pwr::{Power, VoltageScale}; +use crate::rcc::{set_freqs, Clocks}; use crate::time::Hertz; mod pll; use pll::pll_setup; pub use pll::PllConfig; -// Clock type used by peripherals -#[derive(Clone, Copy)] -pub struct Clocks { - pub apb1: Hertz, - pub apb2: Hertz, - pub apb4: Hertz, - pub ahb2: Hertz, - pub c1: Hertz, -} - const HSI: Hertz = Hertz(64_000_000); const CSI: Hertz = Hertz(4_000_000); const HSI48: Hertz = Hertz(48_000_000); @@ -532,5 +523,16 @@ impl<'d> Rcc<'d> { } } -// TODO -pub unsafe fn init(_config: Config) {} +pub unsafe fn init(config: Config) { + let mut power = Power::new(::steal(), false); + let rcc = Rcc::new(::steal(), config); + let core_clocks = rcc.freeze(&mut power); + set_freqs(Clocks { + sys: core_clocks.c_ck, + ahb1: core_clocks.hclk, + ahb2: core_clocks.hclk, + apb1: core_clocks.pclk1, + apb2: core_clocks.pclk2, + apb4: core_clocks.pclk4, + }); +} diff --git a/embassy-stm32/src/rcc/l0/mod.rs b/embassy-stm32/src/rcc/l0/mod.rs index fc8a94883..8f056d51c 100644 --- a/embassy-stm32/src/rcc/l0/mod.rs +++ b/embassy-stm32/src/rcc/l0/mod.rs @@ -1,6 +1,7 @@ +pub use super::common::*; use crate::pac; use crate::peripherals::{self, CRS, RCC, SYSCFG}; -use crate::rcc::{get_freqs, set_freqs}; +use crate::rcc::{get_freqs, set_freqs, Clocks}; use crate::time::Hertz; use crate::time::U32Ext; use core::marker::PhantomData; @@ -12,13 +13,8 @@ use pac::rcc::vals::{Hpre, Msirange, Plldiv, Pllmul, Pllsrc, Ppre, Sw}; /// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, /// and with the addition of the init function to configure a system clock. -#[derive(Clone, Copy)] -pub struct Clocks { - pub sys: Hertz, - pub ahb: Hertz, - pub apb1: Hertz, - pub apb2: Hertz, -} +/// HSI speed +pub const HSI_FREQ: u32 = 16_000_000; /// System clock mux source #[derive(Clone, Copy)] @@ -29,90 +25,6 @@ pub enum ClockSrc { HSI16, } -/// MSI Clock Range -/// -/// These ranges control the frequency of the MSI. Internally, these ranges map -/// to the `MSIRANGE` bits in the `RCC_ICSCR` register. -#[derive(Clone, Copy)] -pub enum MSIRange { - /// Around 65.536 kHz - Range0, - /// Around 131.072 kHz - Range1, - /// Around 262.144 kHz - Range2, - /// Around 524.288 kHz - Range3, - /// Around 1.048 MHz - Range4, - /// Around 2.097 MHz (reset value) - Range5, - /// Around 4.194 MHz - Range6, -} - -impl Default for MSIRange { - fn default() -> MSIRange { - MSIRange::Range5 - } -} - -/// PLL divider -#[derive(Clone, Copy)] -pub enum PLLDiv { - Div2, - Div3, - Div4, -} - -/// PLL multiplier -#[derive(Clone, Copy)] -pub enum PLLMul { - Mul3, - Mul4, - Mul6, - Mul8, - Mul12, - Mul16, - Mul24, - Mul32, - Mul48, -} - -/// AHB prescaler -#[derive(Clone, Copy)] -pub enum AHBPrescaler { - NotDivided, - Div2, - Div4, - Div8, - Div16, - Div64, - Div128, - Div256, - Div512, -} - -/// APB prescaler -#[derive(Clone, Copy)] -pub enum APBPrescaler { - NotDivided, - Div2, - Div4, - Div8, - Div16, -} - -/// PLL clock input source -#[derive(Clone, Copy)] -pub enum PLLSource { - HSI16, - HSE(Hertz), -} - -/// HSI speed -pub const HSI_FREQ: u32 = 16_000_000; - impl Into for PLLMul { fn into(self) -> Pllmul { match self { diff --git a/embassy-stm32/src/rcc/l4/mod.rs b/embassy-stm32/src/rcc/l4/mod.rs new file mode 100644 index 000000000..e8c488e06 --- /dev/null +++ b/embassy-stm32/src/rcc/l4/mod.rs @@ -0,0 +1,203 @@ +pub use super::common::*; +use crate::pac; +use crate::peripherals::{self, RCC}; +use crate::rcc::{get_freqs, set_freqs, Clocks}; +use crate::time::Hertz; +use crate::time::U32Ext; +use core::marker::PhantomData; +use embassy::util::Unborrow; +use embassy_extras::unborrow; + +/// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, +/// and with the addition of the init function to configure a system clock. + +/// Only the basic setup using the HSE and HSI clocks are supported as of now. + +/// HSI speed +pub const HSI_FREQ: u32 = 16_000_000; + +/// System clock mux source +#[derive(Clone, Copy)] +pub enum ClockSrc { + HSE(Hertz), + HSI16, +} + +impl Into for APBPrescaler { + fn into(self) -> u8 { + match self { + APBPrescaler::NotDivided => 1, + APBPrescaler::Div2 => 0x04, + APBPrescaler::Div4 => 0x05, + APBPrescaler::Div8 => 0x06, + APBPrescaler::Div16 => 0x07, + } + } +} + +impl Into for AHBPrescaler { + fn into(self) -> u8 { + match self { + AHBPrescaler::NotDivided => 1, + AHBPrescaler::Div2 => 0x08, + AHBPrescaler::Div4 => 0x09, + AHBPrescaler::Div8 => 0x0a, + AHBPrescaler::Div16 => 0x0b, + AHBPrescaler::Div64 => 0x0c, + AHBPrescaler::Div128 => 0x0d, + AHBPrescaler::Div256 => 0x0e, + AHBPrescaler::Div512 => 0x0f, + } + } +} + +/// Clocks configutation +pub struct Config { + mux: ClockSrc, + ahb_pre: AHBPrescaler, + apb1_pre: APBPrescaler, + apb2_pre: APBPrescaler, +} + +impl Default for Config { + #[inline] + fn default() -> Config { + Config { + mux: ClockSrc::HSI16, + ahb_pre: AHBPrescaler::NotDivided, + apb1_pre: APBPrescaler::NotDivided, + apb2_pre: APBPrescaler::NotDivided, + } + } +} + +impl Config { + #[inline] + pub fn clock_src(mut self, mux: ClockSrc) -> Self { + self.mux = mux; + self + } + + #[inline] + pub fn ahb_pre(mut self, pre: AHBPrescaler) -> Self { + self.ahb_pre = pre; + self + } + + #[inline] + pub fn apb1_pre(mut self, pre: APBPrescaler) -> Self { + self.apb1_pre = pre; + self + } + + #[inline] + pub fn apb2_pre(mut self, pre: APBPrescaler) -> Self { + self.apb2_pre = pre; + self + } +} + +/// RCC peripheral +pub struct Rcc<'d> { + _rb: peripherals::RCC, + phantom: PhantomData<&'d mut peripherals::RCC>, +} + +impl<'d> Rcc<'d> { + pub fn new(rcc: impl Unborrow + 'd) -> Self { + unborrow!(rcc); + Self { + _rb: rcc, + phantom: PhantomData, + } + } + + // Safety: RCC init must have been called + pub fn clocks(&self) -> &'static Clocks { + unsafe { get_freqs() } + } +} + +/// Extension trait that freezes the `RCC` peripheral with provided clocks configuration +pub trait RccExt { + fn freeze(self, config: Config) -> Clocks; +} + +impl RccExt for RCC { + #[inline] + fn freeze(self, cfgr: Config) -> Clocks { + let rcc = pac::RCC; + let (sys_clk, sw) = match cfgr.mux { + ClockSrc::HSI16 => { + // Enable HSI16 + unsafe { + rcc.cr().write(|w| w.set_hsion(true)); + while !rcc.cr().read().hsirdy() {} + } + + (HSI_FREQ, 0x01) + } + ClockSrc::HSE(freq) => { + // Enable HSE + unsafe { + rcc.cr().write(|w| w.set_hseon(true)); + while !rcc.cr().read().hserdy() {} + } + + (freq.0, 0x02) + } + }; + + unsafe { + rcc.cfgr().modify(|w| { + w.set_sw(sw.into()); + w.set_hpre(cfgr.ahb_pre.into()); + w.set_ppre1(cfgr.apb1_pre.into()); + w.set_ppre2(cfgr.apb2_pre.into()); + }); + } + + let ahb_freq: u32 = match cfgr.ahb_pre { + AHBPrescaler::NotDivided => sys_clk, + pre => { + let pre: u8 = pre.into(); + let pre = 1 << (pre as u32 - 7); + sys_clk / pre + } + }; + + let apb1_freq = match cfgr.apb1_pre { + APBPrescaler::NotDivided => ahb_freq, + pre => { + let pre: u8 = pre.into(); + let pre: u8 = 1 << (pre - 3); + let freq = ahb_freq / pre as u32; + freq + } + }; + + let apb2_freq = match cfgr.apb2_pre { + APBPrescaler::NotDivided => ahb_freq, + pre => { + let pre: u8 = pre.into(); + let pre: u8 = 1 << (pre - 3); + let freq = ahb_freq / (1 << (pre as u8 - 3)); + freq + } + }; + + Clocks { + sys: sys_clk.hz(), + ahb1: ahb_freq.hz(), + ahb2: ahb_freq.hz(), + apb1: apb1_freq.hz(), + apb2: apb2_freq.hz(), + } + } +} + +pub unsafe fn init(config: Config) { + let r = ::steal(); + let clocks = r.freeze(config); + set_freqs(clocks); +} diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs index 7271030aa..b5ca7f40a 100644 --- a/embassy-stm32/src/rcc/mod.rs +++ b/embassy-stm32/src/rcc/mod.rs @@ -1,7 +1,28 @@ #![macro_use] use crate::peripherals; +use crate::time::Hertz; use core::mem::MaybeUninit; +mod common; + +#[derive(Clone, Copy)] +pub struct Clocks { + pub sys: Hertz, + pub apb1: Hertz, + pub apb2: Hertz, + + #[cfg(any(rcc_l0))] + pub ahb: Hertz, + + #[cfg(any(rcc_l4, rcc_f4, rcc_h7))] + pub ahb1: Hertz, + + #[cfg(any(rcc_l4, rcc_f4, rcc_h7))] + pub ahb2: Hertz, + + #[cfg(any(rcc_h7))] + pub apb4: Hertz, +} /// Frozen clock frequencies /// @@ -28,36 +49,11 @@ cfg_if::cfg_if! { mod l0; pub use l0::*; } else if #[cfg(rcc_l4)] { - // TODO: Implement - use crate::time::Hertz; - - #[derive(Clone, Copy)] - pub struct Clocks { - pub apb1: Hertz, - pub apb2: Hertz, - pub ahb2: Hertz, - } - - #[derive(Default)] - pub struct Config {} - pub unsafe fn init(_config: Config) { - } + mod l4; + pub use l4::*; } else if #[cfg(rcc_f4)] { - // TODO: Implement - use crate::time::Hertz; - - #[derive(Clone, Copy)] - pub struct Clocks { - pub apb1: Hertz, - pub apb2: Hertz, - pub ahb2: Hertz, - } - - #[derive(Default)] - pub struct Config {} - pub unsafe fn init(_config: Config) { - } - + mod f4; + pub use f4::*; } else { #[derive(Default)] pub struct Config {} diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs index af0d57412..610bb64e9 100644 --- a/examples/stm32f4/src/bin/spi.rs +++ b/examples/stm32f4/src/bin/spi.rs @@ -50,7 +50,6 @@ fn main() -> ! { let p = embassy_stm32::init(Default::default()); let mut spi = Spi::new( - Hertz(16_000_000), p.SPI3, p.PC10, p.PC12, diff --git a/examples/stm32l0/src/bin/spi.rs b/examples/stm32l0/src/bin/spi.rs index 4eb9bfdd2..0e828c436 100644 --- a/examples/stm32l0/src/bin/spi.rs +++ b/examples/stm32l0/src/bin/spi.rs @@ -28,7 +28,6 @@ fn main() -> ! { rcc.enable_debug_wfe(&mut p.DBGMCU, true); let mut spi = Spi::new( - Hertz(16_000_000), p.SPI1, p.PB3, p.PA7, diff --git a/examples/stm32l4/src/bin/spi.rs b/examples/stm32l4/src/bin/spi.rs index 9db854dc3..7c672b70d 100644 --- a/examples/stm32l4/src/bin/spi.rs +++ b/examples/stm32l4/src/bin/spi.rs @@ -44,7 +44,6 @@ fn main() -> ! { let p = embassy_stm32::init(Default::default()); let mut spi = Spi::new( - Hertz(16_000_000), p.SPI3, p.PC10, p.PC12, diff --git a/stm32-metapac/gen/src/lib.rs b/stm32-metapac/gen/src/lib.rs index ae01f1e85..eab4ebe23 100644 --- a/stm32-metapac/gen/src/lib.rs +++ b/stm32-metapac/gen/src/lib.rs @@ -84,7 +84,11 @@ fn find_reg_for_field<'c>( field_name: &str, ) -> Option<(&'c str, &'c str)> { rcc.fieldsets.iter().find_map(|(name, fieldset)| { - if name.starts_with(reg_prefix) { + // Workaround for some families that prefix register aliases with C1_, which does + // not help matching for clock name. + if name.starts_with("C1") { + None + } else if name.starts_with(reg_prefix) { fieldset .fields .iter() From a2da2a6db2e2bbef6d1031dd7a8461e5643ba711 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 14 Jun 2021 10:49:47 +0200 Subject: [PATCH 6/9] Remove unused l0 code --- embassy-stm32/src/rcc/l0/mod.rs | 48 --------------------------------- 1 file changed, 48 deletions(-) diff --git a/embassy-stm32/src/rcc/l0/mod.rs b/embassy-stm32/src/rcc/l0/mod.rs index 8f056d51c..ce9c58657 100644 --- a/embassy-stm32/src/rcc/l0/mod.rs +++ b/embassy-stm32/src/rcc/l0/mod.rs @@ -168,18 +168,6 @@ impl<'d> Rcc<'d> { unsafe { get_freqs() } } - /* - pub fn enable_lse(&mut self, _: &PWR) -> LSE { - self.rb.csr.modify(|_, w| { - // Enable LSE clock - w.lseon().set_bit() - }); - while self.rb.csr.read().lserdy().bit_is_clear() {} - LSE(()) - } - } - */ - pub fn enable_debug_wfe(&mut self, _dbg: &mut peripherals::DBGMCU, enable_dma: bool) { // NOTE(unsafe) We have exclusive access to the RCC and DBGMCU unsafe { @@ -239,30 +227,6 @@ impl<'d> Rcc<'d> { HSI48(()) } } -/* - -impl Rcc { - /// Configure MCO (Microcontroller Clock Output). - pub fn configure_mco

( - &mut self, - source: MCOSEL_A, - prescaler: MCOPRE_A, - output_pin: P, - ) -> MCOEnabled - where - P: mco::Pin, - { - output_pin.into_mco(); - - self.rb.cfgr.modify(|_, w| { - w.mcosel().variant(source); - w.mcopre().variant(prescaler) - }); - - MCOEnabled(()) - } -} -*/ /// Extension trait that freezes the `RCC` peripheral with provided clocks configuration pub trait RccExt { @@ -424,18 +388,6 @@ impl RccExt for RCC { #[derive(Clone, Copy)] pub struct HSI48(()); -/// Token that exists only if MCO (Microcontroller Clock Out) has been enabled. -/// -/// You can get an instance of this struct by calling [`Rcc::configure_mco`]. -#[derive(Clone, Copy)] -pub struct MCOEnabled(()); - -/// Token that exists only, if the LSE clock has been enabled -/// -/// You can get an instance of this struct by calling [`Rcc::enable_lse`]. -#[derive(Clone, Copy)] -pub struct LSE(()); - pub unsafe fn init(config: Config) { let rcc = pac::RCC; rcc.iopenr().write(|w| { From ee9f67fa013c703396ddbd76d3fe33b51bd6a8bb Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 14 Jun 2021 11:24:09 +0200 Subject: [PATCH 7/9] Add common types --- embassy-stm32/src/rcc/f4/mod.rs | 2 +- embassy-stm32/src/rcc/l0/mod.rs | 2 +- embassy-stm32/src/rcc/l4/mod.rs | 2 +- embassy-stm32/src/rcc/mod.rs | 2 +- embassy-stm32/src/rcc/types.rs | 94 +++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 embassy-stm32/src/rcc/types.rs diff --git a/embassy-stm32/src/rcc/f4/mod.rs b/embassy-stm32/src/rcc/f4/mod.rs index abd631944..a9e63ccc2 100644 --- a/embassy-stm32/src/rcc/f4/mod.rs +++ b/embassy-stm32/src/rcc/f4/mod.rs @@ -1,4 +1,4 @@ -pub use super::common::*; +pub use super::types::*; use crate::pac; use crate::peripherals::{self, RCC}; use crate::rcc::{get_freqs, set_freqs, Clocks}; diff --git a/embassy-stm32/src/rcc/l0/mod.rs b/embassy-stm32/src/rcc/l0/mod.rs index ce9c58657..2d51c690f 100644 --- a/embassy-stm32/src/rcc/l0/mod.rs +++ b/embassy-stm32/src/rcc/l0/mod.rs @@ -1,4 +1,4 @@ -pub use super::common::*; +pub use super::types::*; use crate::pac; use crate::peripherals::{self, CRS, RCC, SYSCFG}; use crate::rcc::{get_freqs, set_freqs, Clocks}; diff --git a/embassy-stm32/src/rcc/l4/mod.rs b/embassy-stm32/src/rcc/l4/mod.rs index e8c488e06..c33d9501e 100644 --- a/embassy-stm32/src/rcc/l4/mod.rs +++ b/embassy-stm32/src/rcc/l4/mod.rs @@ -1,4 +1,4 @@ -pub use super::common::*; +pub use super::types::*; use crate::pac; use crate::peripherals::{self, RCC}; use crate::rcc::{get_freqs, set_freqs, Clocks}; diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs index b5ca7f40a..9252c4da9 100644 --- a/embassy-stm32/src/rcc/mod.rs +++ b/embassy-stm32/src/rcc/mod.rs @@ -3,7 +3,7 @@ use crate::peripherals; use crate::time::Hertz; use core::mem::MaybeUninit; -mod common; +mod types; #[derive(Clone, Copy)] pub struct Clocks { diff --git a/embassy-stm32/src/rcc/types.rs b/embassy-stm32/src/rcc/types.rs new file mode 100644 index 000000000..df7917ab3 --- /dev/null +++ b/embassy-stm32/src/rcc/types.rs @@ -0,0 +1,94 @@ +#![allow(dead_code)] +/// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, +/// and with the addition of the init function to configure a system clock. +use crate::time::Hertz; + +/// System clock mux source +#[derive(Clone, Copy)] +pub enum ClockSrc { + MSI(MSIRange), + PLL(PLLSource, PLLMul, PLLDiv), + HSE(Hertz), + HSI16, +} + +/// MSI Clock Range +/// +/// These ranges control the frequency of the MSI. Internally, these ranges map +/// to the `MSIRANGE` bits in the `RCC_ICSCR` register. +#[derive(Clone, Copy)] +pub enum MSIRange { + /// Around 65.536 kHz + Range0, + /// Around 131.072 kHz + Range1, + /// Around 262.144 kHz + Range2, + /// Around 524.288 kHz + Range3, + /// Around 1.048 MHz + Range4, + /// Around 2.097 MHz (reset value) + Range5, + /// Around 4.194 MHz + Range6, +} + +impl Default for MSIRange { + fn default() -> MSIRange { + MSIRange::Range5 + } +} + +/// PLL divider +#[derive(Clone, Copy)] +pub enum PLLDiv { + Div2, + Div3, + Div4, +} + +/// PLL multiplier +#[derive(Clone, Copy)] +pub enum PLLMul { + Mul3, + Mul4, + Mul6, + Mul8, + Mul12, + Mul16, + Mul24, + Mul32, + Mul48, +} + +/// AHB prescaler +#[derive(Clone, Copy)] +pub enum AHBPrescaler { + NotDivided, + Div2, + Div4, + Div8, + Div16, + Div64, + Div128, + Div256, + Div512, +} + +/// APB prescaler +#[derive(Clone, Copy)] +pub enum APBPrescaler { + NotDivided, + Div2, + Div4, + Div8, + Div16, +} + +/// PLL clock input source +#[derive(Clone, Copy)] +pub enum PLLSource { + HSI16, + HSE(Hertz), +} From 5e1b0a539876f498ac11cb2d4c42424a499d7087 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 14 Jun 2021 11:41:02 +0200 Subject: [PATCH 8/9] Add wb55 clocks --- embassy-stm32/src/rcc/f4/mod.rs | 1 + embassy-stm32/src/rcc/h7/mod.rs | 1 + embassy-stm32/src/rcc/l4/mod.rs | 1 + embassy-stm32/src/rcc/mod.rs | 10 +- embassy-stm32/src/rcc/wb55/mod.rs | 204 ++++++++++++++++++++++++++++++ stm32-metapac/gen/src/lib.rs | 2 +- 6 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 embassy-stm32/src/rcc/wb55/mod.rs diff --git a/embassy-stm32/src/rcc/f4/mod.rs b/embassy-stm32/src/rcc/f4/mod.rs index a9e63ccc2..e8709b5fc 100644 --- a/embassy-stm32/src/rcc/f4/mod.rs +++ b/embassy-stm32/src/rcc/f4/mod.rs @@ -191,6 +191,7 @@ impl RccExt for RCC { sys: sys_clk.hz(), ahb1: ahb_freq.hz(), ahb2: ahb_freq.hz(), + ahb3: ahb_freq.hz(), apb1: apb1_freq.hz(), apb2: apb2_freq.hz(), } diff --git a/embassy-stm32/src/rcc/h7/mod.rs b/embassy-stm32/src/rcc/h7/mod.rs index f51ea9d71..4bb50be3e 100644 --- a/embassy-stm32/src/rcc/h7/mod.rs +++ b/embassy-stm32/src/rcc/h7/mod.rs @@ -531,6 +531,7 @@ pub unsafe fn init(config: Config) { sys: core_clocks.c_ck, ahb1: core_clocks.hclk, ahb2: core_clocks.hclk, + ahb3: core_clocks.hclk, apb1: core_clocks.pclk1, apb2: core_clocks.pclk2, apb4: core_clocks.pclk4, diff --git a/embassy-stm32/src/rcc/l4/mod.rs b/embassy-stm32/src/rcc/l4/mod.rs index c33d9501e..9ae6d62b9 100644 --- a/embassy-stm32/src/rcc/l4/mod.rs +++ b/embassy-stm32/src/rcc/l4/mod.rs @@ -190,6 +190,7 @@ impl RccExt for RCC { sys: sys_clk.hz(), ahb1: ahb_freq.hz(), ahb2: ahb_freq.hz(), + ahb3: ahb_freq.hz(), apb1: apb1_freq.hz(), apb2: apb2_freq.hz(), } diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs index 9252c4da9..48bdb3de6 100644 --- a/embassy-stm32/src/rcc/mod.rs +++ b/embassy-stm32/src/rcc/mod.rs @@ -14,12 +14,15 @@ pub struct Clocks { #[cfg(any(rcc_l0))] pub ahb: Hertz, - #[cfg(any(rcc_l4, rcc_f4, rcc_h7))] + #[cfg(any(rcc_l4, rcc_f4, rcc_h7, rcc_wb55))] pub ahb1: Hertz, - #[cfg(any(rcc_l4, rcc_f4, rcc_h7))] + #[cfg(any(rcc_l4, rcc_f4, rcc_h7, rcc_wb55))] pub ahb2: Hertz, + #[cfg(any(rcc_l4, rcc_f4, rcc_h7, rcc_wb55))] + pub ahb3: Hertz, + #[cfg(any(rcc_h7))] pub apb4: Hertz, } @@ -54,6 +57,9 @@ cfg_if::cfg_if! { } else if #[cfg(rcc_f4)] { mod f4; pub use f4::*; + } else if #[cfg(rcc_wb55)] { + mod wb55; + pub use wb55::*; } else { #[derive(Default)] pub struct Config {} diff --git a/embassy-stm32/src/rcc/wb55/mod.rs b/embassy-stm32/src/rcc/wb55/mod.rs new file mode 100644 index 000000000..9ae6d62b9 --- /dev/null +++ b/embassy-stm32/src/rcc/wb55/mod.rs @@ -0,0 +1,204 @@ +pub use super::types::*; +use crate::pac; +use crate::peripherals::{self, RCC}; +use crate::rcc::{get_freqs, set_freqs, Clocks}; +use crate::time::Hertz; +use crate::time::U32Ext; +use core::marker::PhantomData; +use embassy::util::Unborrow; +use embassy_extras::unborrow; + +/// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, +/// and with the addition of the init function to configure a system clock. + +/// Only the basic setup using the HSE and HSI clocks are supported as of now. + +/// HSI speed +pub const HSI_FREQ: u32 = 16_000_000; + +/// System clock mux source +#[derive(Clone, Copy)] +pub enum ClockSrc { + HSE(Hertz), + HSI16, +} + +impl Into for APBPrescaler { + fn into(self) -> u8 { + match self { + APBPrescaler::NotDivided => 1, + APBPrescaler::Div2 => 0x04, + APBPrescaler::Div4 => 0x05, + APBPrescaler::Div8 => 0x06, + APBPrescaler::Div16 => 0x07, + } + } +} + +impl Into for AHBPrescaler { + fn into(self) -> u8 { + match self { + AHBPrescaler::NotDivided => 1, + AHBPrescaler::Div2 => 0x08, + AHBPrescaler::Div4 => 0x09, + AHBPrescaler::Div8 => 0x0a, + AHBPrescaler::Div16 => 0x0b, + AHBPrescaler::Div64 => 0x0c, + AHBPrescaler::Div128 => 0x0d, + AHBPrescaler::Div256 => 0x0e, + AHBPrescaler::Div512 => 0x0f, + } + } +} + +/// Clocks configutation +pub struct Config { + mux: ClockSrc, + ahb_pre: AHBPrescaler, + apb1_pre: APBPrescaler, + apb2_pre: APBPrescaler, +} + +impl Default for Config { + #[inline] + fn default() -> Config { + Config { + mux: ClockSrc::HSI16, + ahb_pre: AHBPrescaler::NotDivided, + apb1_pre: APBPrescaler::NotDivided, + apb2_pre: APBPrescaler::NotDivided, + } + } +} + +impl Config { + #[inline] + pub fn clock_src(mut self, mux: ClockSrc) -> Self { + self.mux = mux; + self + } + + #[inline] + pub fn ahb_pre(mut self, pre: AHBPrescaler) -> Self { + self.ahb_pre = pre; + self + } + + #[inline] + pub fn apb1_pre(mut self, pre: APBPrescaler) -> Self { + self.apb1_pre = pre; + self + } + + #[inline] + pub fn apb2_pre(mut self, pre: APBPrescaler) -> Self { + self.apb2_pre = pre; + self + } +} + +/// RCC peripheral +pub struct Rcc<'d> { + _rb: peripherals::RCC, + phantom: PhantomData<&'d mut peripherals::RCC>, +} + +impl<'d> Rcc<'d> { + pub fn new(rcc: impl Unborrow + 'd) -> Self { + unborrow!(rcc); + Self { + _rb: rcc, + phantom: PhantomData, + } + } + + // Safety: RCC init must have been called + pub fn clocks(&self) -> &'static Clocks { + unsafe { get_freqs() } + } +} + +/// Extension trait that freezes the `RCC` peripheral with provided clocks configuration +pub trait RccExt { + fn freeze(self, config: Config) -> Clocks; +} + +impl RccExt for RCC { + #[inline] + fn freeze(self, cfgr: Config) -> Clocks { + let rcc = pac::RCC; + let (sys_clk, sw) = match cfgr.mux { + ClockSrc::HSI16 => { + // Enable HSI16 + unsafe { + rcc.cr().write(|w| w.set_hsion(true)); + while !rcc.cr().read().hsirdy() {} + } + + (HSI_FREQ, 0x01) + } + ClockSrc::HSE(freq) => { + // Enable HSE + unsafe { + rcc.cr().write(|w| w.set_hseon(true)); + while !rcc.cr().read().hserdy() {} + } + + (freq.0, 0x02) + } + }; + + unsafe { + rcc.cfgr().modify(|w| { + w.set_sw(sw.into()); + w.set_hpre(cfgr.ahb_pre.into()); + w.set_ppre1(cfgr.apb1_pre.into()); + w.set_ppre2(cfgr.apb2_pre.into()); + }); + } + + let ahb_freq: u32 = match cfgr.ahb_pre { + AHBPrescaler::NotDivided => sys_clk, + pre => { + let pre: u8 = pre.into(); + let pre = 1 << (pre as u32 - 7); + sys_clk / pre + } + }; + + let apb1_freq = match cfgr.apb1_pre { + APBPrescaler::NotDivided => ahb_freq, + pre => { + let pre: u8 = pre.into(); + let pre: u8 = 1 << (pre - 3); + let freq = ahb_freq / pre as u32; + freq + } + }; + + let apb2_freq = match cfgr.apb2_pre { + APBPrescaler::NotDivided => ahb_freq, + pre => { + let pre: u8 = pre.into(); + let pre: u8 = 1 << (pre - 3); + let freq = ahb_freq / (1 << (pre as u8 - 3)); + freq + } + }; + + Clocks { + sys: sys_clk.hz(), + ahb1: ahb_freq.hz(), + ahb2: ahb_freq.hz(), + ahb3: ahb_freq.hz(), + apb1: apb1_freq.hz(), + apb2: apb2_freq.hz(), + } + } +} + +pub unsafe fn init(config: Config) { + let r = ::steal(); + let clocks = r.freeze(config); + set_freqs(clocks); +} diff --git a/stm32-metapac/gen/src/lib.rs b/stm32-metapac/gen/src/lib.rs index eab4ebe23..5133aef9a 100644 --- a/stm32-metapac/gen/src/lib.rs +++ b/stm32-metapac/gen/src/lib.rs @@ -86,7 +86,7 @@ fn find_reg_for_field<'c>( rcc.fieldsets.iter().find_map(|(name, fieldset)| { // Workaround for some families that prefix register aliases with C1_, which does // not help matching for clock name. - if name.starts_with("C1") { + if name.starts_with("C1") || name.starts_with("C2") { None } else if name.starts_with(reg_prefix) { fieldset From 531093f28189573818d5ebcfd4ccf56389d3972f Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 14 Jun 2021 11:58:16 +0200 Subject: [PATCH 9/9] Derive SPI v1 and v3 clocks automatically --- embassy-stm32/src/spi/v1.rs | 2 +- embassy-stm32/src/spi/v3.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-stm32/src/spi/v1.rs b/embassy-stm32/src/spi/v1.rs index 227a36a89..01cbf86b6 100644 --- a/embassy-stm32/src/spi/v1.rs +++ b/embassy-stm32/src/spi/v1.rs @@ -29,7 +29,6 @@ pub struct Spi<'d, T: Instance> { impl<'d, T: Instance> Spi<'d, T> { pub fn new( - pclk: Hertz, _peri: impl Unborrow + 'd, sck: impl Unborrow>, mosi: impl Unborrow>, @@ -58,6 +57,7 @@ impl<'d, T: Instance> Spi<'d, T> { }); } + let pclk = T::frequency(); let br = Self::compute_baud_rate(pclk, freq.into()); unsafe { diff --git a/embassy-stm32/src/spi/v3.rs b/embassy-stm32/src/spi/v3.rs index 6073616bd..0b4a71457 100644 --- a/embassy-stm32/src/spi/v3.rs +++ b/embassy-stm32/src/spi/v3.rs @@ -37,7 +37,6 @@ pub struct Spi<'d, T: Instance> { impl<'d, T: Instance> Spi<'d, T> { pub fn new( - pclk: Hertz, _peri: impl Unborrow + 'd, sck: impl Unborrow>, mosi: impl Unborrow>, @@ -62,6 +61,7 @@ impl<'d, T: Instance> Spi<'d, T> { let mosi = mosi.degrade(); let miso = miso.degrade(); + let pclk = T::frequency(); let br = Self::compute_baud_rate(pclk, freq.into()); unsafe { T::enable();