Support for STM32L1
* Add RCC * Fix more issues with dash in chip names * Update stm32-data version * Add blinky and spi example
This commit is contained in:
235
embassy-stm32/src/rcc/l1/mod.rs
Normal file
235
embassy-stm32/src/rcc/l1/mod.rs
Normal file
@ -0,0 +1,235 @@
|
||||
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_hal_common::unborrow;
|
||||
use pac::rcc::vals::{Hpre, Ppre, Sw};
|
||||
|
||||
/// Most of clock setup is copied from rcc/l0
|
||||
|
||||
/// HSI speed
|
||||
pub const HSI_FREQ: u32 = 16_000_000;
|
||||
|
||||
/// System clock mux source
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum ClockSrc {
|
||||
MSI(MSIRange),
|
||||
HSE(Hertz),
|
||||
HSI,
|
||||
}
|
||||
|
||||
impl Into<Ppre> 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<Hpre> 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<u8> for MSIRange {
|
||||
fn into(self) -> u8 {
|
||||
match self {
|
||||
MSIRange::Range0 => 0b000,
|
||||
MSIRange::Range1 => 0b001,
|
||||
MSIRange::Range2 => 0b010,
|
||||
MSIRange::Range3 => 0b011,
|
||||
MSIRange::Range4 => 0b100,
|
||||
MSIRange::Range5 => 0b101,
|
||||
MSIRange::Range6 => 0b110,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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::MSI(MSIRange::default()),
|
||||
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<Target = peripherals::RCC> + '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 {
|
||||
// `cfgr` is almost always a constant, so make sure it can be constant-propagated properly by
|
||||
// marking this function and all `Config` constructors and setters as `#[inline]`.
|
||||
// This saves ~900 Bytes for the `pwr.rs` example.
|
||||
#[inline]
|
||||
fn freeze(self, cfgr: Config) -> Clocks {
|
||||
let rcc = pac::RCC;
|
||||
let (sys_clk, sw) = match cfgr.mux {
|
||||
ClockSrc::MSI(range) => {
|
||||
// Set MSI range
|
||||
unsafe {
|
||||
rcc.icscr().write(|w| w.set_msirange(range.into()));
|
||||
}
|
||||
|
||||
// Enable MSI
|
||||
unsafe {
|
||||
rcc.cr().write(|w| w.set_msion(true));
|
||||
while !rcc.cr().read().msirdy() {}
|
||||
}
|
||||
|
||||
let freq = 32_768 * (1 << (range as u8 + 1));
|
||||
(freq, Sw::MSI)
|
||||
}
|
||||
ClockSrc::HSI => {
|
||||
// Enable HSI
|
||||
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, apb1_tim_freq) = match cfgr.apb1_pre {
|
||||
APBPrescaler::NotDivided => (ahb_freq, 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)
|
||||
}
|
||||
};
|
||||
|
||||
let (apb2_freq, apb2_tim_freq) = match cfgr.apb2_pre {
|
||||
APBPrescaler::NotDivided => (ahb_freq, 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)
|
||||
}
|
||||
};
|
||||
|
||||
Clocks {
|
||||
sys: sys_clk.hz(),
|
||||
ahb: ahb_freq.hz(),
|
||||
apb1: apb1_freq.hz(),
|
||||
apb2: apb2_freq.hz(),
|
||||
apb1_tim: apb1_tim_freq.hz(),
|
||||
apb2_tim: apb2_tim_freq.hz(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn init(config: Config) {
|
||||
let r = <peripherals::RCC as embassy::util::Steal>::steal();
|
||||
let clocks = r.freeze(config);
|
||||
set_freqs(clocks);
|
||||
}
|
@ -27,7 +27,7 @@ pub struct Clocks {
|
||||
#[cfg(rcc_wl5)]
|
||||
pub apb3: Hertz,
|
||||
|
||||
#[cfg(any(rcc_l0, rcc_f0, rcc_f0x0, rcc_g0))]
|
||||
#[cfg(any(rcc_l0, rcc_l1, rcc_f0, rcc_f0x0, rcc_g0))]
|
||||
pub ahb: Hertz,
|
||||
|
||||
#[cfg(any(rcc_l4, rcc_f4, rcc_h7, rcc_wb, rcc_wl5))]
|
||||
@ -73,6 +73,9 @@ cfg_if::cfg_if! {
|
||||
} else if #[cfg(rcc_l0)] {
|
||||
mod l0;
|
||||
pub use l0::*;
|
||||
} else if #[cfg(rcc_l1)] {
|
||||
mod l1;
|
||||
pub use l1::*;
|
||||
} else if #[cfg(rcc_l4)] {
|
||||
mod l4;
|
||||
pub use l4::*;
|
||||
|
Reference in New Issue
Block a user