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:
Ulf Lilleengen
2021-09-21 13:42:27 +02:00
parent 14aa4265db
commit c79485c286
19 changed files with 560 additions and 12 deletions

View File

@ -625,6 +625,93 @@ stm32l083rz = [ "stm32-metapac/stm32l083rz" ]
stm32l083v8 = [ "stm32-metapac/stm32l083v8" ]
stm32l083vb = [ "stm32-metapac/stm32l083vb" ]
stm32l083vz = [ "stm32-metapac/stm32l083vz" ]
stm32l100c6-a = [ "stm32-metapac/stm32l100c6-a" ]
stm32l100c6 = [ "stm32-metapac/stm32l100c6" ]
stm32l100r8-a = [ "stm32-metapac/stm32l100r8-a" ]
stm32l100r8 = [ "stm32-metapac/stm32l100r8" ]
stm32l100rb-a = [ "stm32-metapac/stm32l100rb-a" ]
stm32l100rb = [ "stm32-metapac/stm32l100rb" ]
stm32l100rc = [ "stm32-metapac/stm32l100rc" ]
stm32l151c6-a = [ "stm32-metapac/stm32l151c6-a" ]
stm32l151c6 = [ "stm32-metapac/stm32l151c6" ]
stm32l151c8-a = [ "stm32-metapac/stm32l151c8-a" ]
stm32l151c8 = [ "stm32-metapac/stm32l151c8" ]
stm32l151cb-a = [ "stm32-metapac/stm32l151cb-a" ]
stm32l151cb = [ "stm32-metapac/stm32l151cb" ]
stm32l151cc = [ "stm32-metapac/stm32l151cc" ]
stm32l151qc = [ "stm32-metapac/stm32l151qc" ]
stm32l151qd = [ "stm32-metapac/stm32l151qd" ]
stm32l151qe = [ "stm32-metapac/stm32l151qe" ]
stm32l151r6-a = [ "stm32-metapac/stm32l151r6-a" ]
stm32l151r6 = [ "stm32-metapac/stm32l151r6" ]
stm32l151r8-a = [ "stm32-metapac/stm32l151r8-a" ]
stm32l151r8 = [ "stm32-metapac/stm32l151r8" ]
stm32l151rb-a = [ "stm32-metapac/stm32l151rb-a" ]
stm32l151rb = [ "stm32-metapac/stm32l151rb" ]
stm32l151rc-a = [ "stm32-metapac/stm32l151rc-a" ]
stm32l151rc = [ "stm32-metapac/stm32l151rc" ]
stm32l151rd = [ "stm32-metapac/stm32l151rd" ]
stm32l151re = [ "stm32-metapac/stm32l151re" ]
stm32l151uc = [ "stm32-metapac/stm32l151uc" ]
stm32l151v8-a = [ "stm32-metapac/stm32l151v8-a" ]
stm32l151v8 = [ "stm32-metapac/stm32l151v8" ]
stm32l151vb-a = [ "stm32-metapac/stm32l151vb-a" ]
stm32l151vb = [ "stm32-metapac/stm32l151vb" ]
stm32l151vc-a = [ "stm32-metapac/stm32l151vc-a" ]
stm32l151vc = [ "stm32-metapac/stm32l151vc" ]
stm32l151vd-x = [ "stm32-metapac/stm32l151vd-x" ]
stm32l151vd = [ "stm32-metapac/stm32l151vd" ]
stm32l151ve = [ "stm32-metapac/stm32l151ve" ]
stm32l151zc = [ "stm32-metapac/stm32l151zc" ]
stm32l151zd = [ "stm32-metapac/stm32l151zd" ]
stm32l151ze = [ "stm32-metapac/stm32l151ze" ]
stm32l152c6-a = [ "stm32-metapac/stm32l152c6-a" ]
stm32l152c6 = [ "stm32-metapac/stm32l152c6" ]
stm32l152c8-a = [ "stm32-metapac/stm32l152c8-a" ]
stm32l152c8 = [ "stm32-metapac/stm32l152c8" ]
stm32l152cb-a = [ "stm32-metapac/stm32l152cb-a" ]
stm32l152cb = [ "stm32-metapac/stm32l152cb" ]
stm32l152cc = [ "stm32-metapac/stm32l152cc" ]
stm32l152qc = [ "stm32-metapac/stm32l152qc" ]
stm32l152qd = [ "stm32-metapac/stm32l152qd" ]
stm32l152qe = [ "stm32-metapac/stm32l152qe" ]
stm32l152r6-a = [ "stm32-metapac/stm32l152r6-a" ]
stm32l152r6 = [ "stm32-metapac/stm32l152r6" ]
stm32l152r8-a = [ "stm32-metapac/stm32l152r8-a" ]
stm32l152r8 = [ "stm32-metapac/stm32l152r8" ]
stm32l152rb-a = [ "stm32-metapac/stm32l152rb-a" ]
stm32l152rb = [ "stm32-metapac/stm32l152rb" ]
stm32l152rc-a = [ "stm32-metapac/stm32l152rc-a" ]
stm32l152rc = [ "stm32-metapac/stm32l152rc" ]
stm32l152rd = [ "stm32-metapac/stm32l152rd" ]
stm32l152re = [ "stm32-metapac/stm32l152re" ]
stm32l152uc = [ "stm32-metapac/stm32l152uc" ]
stm32l152v8-a = [ "stm32-metapac/stm32l152v8-a" ]
stm32l152v8 = [ "stm32-metapac/stm32l152v8" ]
stm32l152vb-a = [ "stm32-metapac/stm32l152vb-a" ]
stm32l152vb = [ "stm32-metapac/stm32l152vb" ]
stm32l152vc-a = [ "stm32-metapac/stm32l152vc-a" ]
stm32l152vc = [ "stm32-metapac/stm32l152vc" ]
stm32l152vd-x = [ "stm32-metapac/stm32l152vd-x" ]
stm32l152vd = [ "stm32-metapac/stm32l152vd" ]
stm32l152ve = [ "stm32-metapac/stm32l152ve" ]
stm32l152zc = [ "stm32-metapac/stm32l152zc" ]
stm32l152zd = [ "stm32-metapac/stm32l152zd" ]
stm32l152ze = [ "stm32-metapac/stm32l152ze" ]
stm32l162qc = [ "stm32-metapac/stm32l162qc" ]
stm32l162qd = [ "stm32-metapac/stm32l162qd" ]
stm32l162rc-a = [ "stm32-metapac/stm32l162rc-a" ]
stm32l162rc = [ "stm32-metapac/stm32l162rc" ]
stm32l162rd = [ "stm32-metapac/stm32l162rd" ]
stm32l162re = [ "stm32-metapac/stm32l162re" ]
stm32l162vc-a = [ "stm32-metapac/stm32l162vc-a" ]
stm32l162vc = [ "stm32-metapac/stm32l162vc" ]
stm32l162vd-x = [ "stm32-metapac/stm32l162vd-x" ]
stm32l162vd = [ "stm32-metapac/stm32l162vd" ]
stm32l162ve = [ "stm32-metapac/stm32l162ve" ]
stm32l162zc = [ "stm32-metapac/stm32l162zc" ]
stm32l162zd = [ "stm32-metapac/stm32l162zd" ]
stm32l162ze = [ "stm32-metapac/stm32l162ze" ]
stm32l412c8 = [ "stm32-metapac/stm32l412c8" ]
stm32l412cb = [ "stm32-metapac/stm32l412cb" ]
stm32l412k8 = [ "stm32-metapac/stm32l412k8" ]

View File

@ -85,13 +85,28 @@ fn main() {
};
);
let mut chip_and_core = chip_name.split('_');
let chip = chip_and_core.next().expect("Unexpected stm32xx feature");
if let Some(core) = chip_and_core.next() {
println!("cargo:rustc-cfg={}_{}", &chip[..(chip.len() - 2)], core);
let mut s = chip_name.split('_');
let mut chip_name: String = s.next().unwrap().to_string();
let core_name = if let Some(c) = s.next() {
if !c.starts_with("CM") {
chip_name.push('_');
chip_name.push_str(c);
None
} else {
Some(c)
}
} else {
println!("cargo:rustc-cfg={}", &chip[..(chip.len() - 2)]);
None
};
if let Some(core) = core_name {
println!(
"cargo:rustc-cfg={}_{}",
&chip_name[..chip_name.len() - 2],
core
);
} else {
println!("cargo:rustc-cfg={}", &chip_name[..chip_name.len() - 2]);
}
println!("cargo:rerun-if-changed=build.rs");

View 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);
}

View File

@ -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::*;