Merge pull request #230 from lulf/update-regen-peripherals

Update after RCC regen and register fix
This commit is contained in:
Dario Nieuwenhuis 2021-06-07 14:41:22 +02:00 committed by GitHub
commit 2b18440bec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 68 deletions

View File

@ -83,7 +83,7 @@ impl<T: Instance> Clock<T> {
unsafe { unsafe {
let rcc = crate::pac::RCC; let rcc = crate::pac::RCC;
rcc.apb1enr() rcc.apb1enr()
.modify(|w| w.set_tim2en(crate::pac::rcc::vals::Lptimen::ENABLED)); .modify(|w| w.set_tim2en(true));
rcc.apb1rstr().modify(|w| w.set_tim2rst(true)); rcc.apb1rstr().modify(|w| w.set_tim2rst(true));
rcc.apb1rstr().modify(|w| w.set_tim2rst(false)); rcc.apb1rstr().modify(|w| w.set_tim2rst(false));
} }

View File

@ -5,7 +5,8 @@ use core::marker::PhantomData;
use embassy::util::Unborrow; use embassy::util::Unborrow;
use embassy_extras::unborrow; use embassy_extras::unborrow;
#[derive(Debug, defmt::Format)] #[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error { pub enum Error {
UnconfiguredChannel, UnconfiguredChannel,
InvalidValue, InvalidValue,

View File

@ -28,8 +28,6 @@ pub struct Power {
impl Power { impl Power {
pub fn new(_peri: peripherals::PWR, enable_overdrive: bool) -> Self { pub fn new(_peri: peripherals::PWR, enable_overdrive: bool) -> Self {
use crate::pac::rcc::vals::Apb4enrSyscfgen;
// NOTE(unsafe) we have the PWR singleton // NOTE(unsafe) we have the PWR singleton
unsafe { unsafe {
// NB. The lower bytes of CR3 can only be written once after // NB. The lower bytes of CR3 can only be written once after
@ -57,8 +55,7 @@ impl Power {
VoltageScale::Scale1 VoltageScale::Scale1
} else { } else {
critical_section::with(|_| { critical_section::with(|_| {
RCC.apb4enr() RCC.apb4enr().modify(|w| w.set_syscfgen(true));
.modify(|w| w.set_syscfgen(Apb4enrSyscfgen::ENABLED));
SYSCFG.pwrcr().modify(|w| w.set_oden(1)); SYSCFG.pwrcr().modify(|w| w.set_oden(1));
}); });

View File

@ -101,10 +101,7 @@ impl<'d> Rcc<'d> {
/// achieved, but the mechanism for doing so is not yet /// achieved, but the mechanism for doing so is not yet
/// implemented here. /// implemented here.
pub fn freeze(mut self, pwr: &Power) -> CoreClocks { pub fn freeze(mut self, pwr: &Power) -> CoreClocks {
use crate::pac::rcc::vals::{ use crate::pac::rcc::vals::{Ckpersel, Dppre, Hpre, Hsebyp, Hsidiv, Pllsrc, Sw};
Apb4enrSyscfgen, Ckpersel, D1ppre, D2ppre1, D3ppre, Hpre, Hsebyp, Hsidiv, Hsion, Lsion,
Pllsrc, Sw,
};
let srcclk = self.config.hse.unwrap_or(HSI); // Available clocks let srcclk = self.config.hse.unwrap_or(HSI); // Available clocks
let (sys_ck, sys_use_pll1_p) = self.sys_ck_setup(srcclk); let (sys_ck, sys_use_pll1_p) = self.sys_ck_setup(srcclk);
@ -132,10 +129,10 @@ impl<'d> Rcc<'d> {
// do so it would need to ensure all PLLxON bits are clear // do so it would need to ensure all PLLxON bits are clear
// before changing the value of HSIDIV // before changing the value of HSIDIV
let cr = RCC.cr().read(); let cr = RCC.cr().read();
assert!(cr.hsion() == Hsion::ON); assert!(cr.hsion());
assert!(cr.hsidiv() == Hsidiv::DIV1); assert!(cr.hsidiv() == Hsidiv::DIV1);
RCC.csr().modify(|w| w.set_lsion(Lsion::ON)); RCC.csr().modify(|w| w.set_lsion(true));
while !RCC.csr().read().lsirdy() {} while !RCC.csr().read().lsirdy() {}
} }
@ -228,12 +225,12 @@ impl<'d> Rcc<'d> {
// NOTE(unsafe) We have the RCC singleton // NOTE(unsafe) We have the RCC singleton
unsafe { unsafe {
// Ensure CSI is on and stable // Ensure CSI is on and stable
RCC.cr().modify(|w| w.set_csion(Hsion::ON)); RCC.cr().modify(|w| w.set_csion(true));
while !RCC.cr().read().csirdy() {} while !RCC.cr().read().csirdy() {}
// Ensure HSI48 is on and stable // Ensure HSI48 is on and stable
RCC.cr().modify(|w| w.set_hsi48on(Hsion::ON)); RCC.cr().modify(|w| w.set_hsi48on(true));
while RCC.cr().read().hsi48on() == Hsion::OFF {} while !RCC.cr().read().hsi48on() {}
// XXX: support MCO ? // XXX: support MCO ?
@ -241,7 +238,7 @@ impl<'d> Rcc<'d> {
Some(hse) => { Some(hse) => {
// Ensure HSE is on and stable // Ensure HSE is on and stable
RCC.cr().modify(|w| { RCC.cr().modify(|w| {
w.set_hseon(Hsion::ON); w.set_hseon(true);
w.set_hsebyp(if self.config.bypass_hse { w.set_hsebyp(if self.config.bypass_hse {
Hsebyp::BYPASSED Hsebyp::BYPASSED
} else { } else {
@ -261,25 +258,27 @@ impl<'d> Rcc<'d> {
}; };
RCC.pllckselr().modify(|w| w.set_pllsrc(pllsrc)); RCC.pllckselr().modify(|w| w.set_pllsrc(pllsrc));
let enable_pll = |pll| {
RCC.cr().modify(|w| w.set_pllon(pll, true));
while !RCC.cr().read().pllrdy(pll) {}
};
if pll1_p_ck.is_some() { if pll1_p_ck.is_some() {
RCC.cr().modify(|w| w.set_pll1on(Hsion::ON)); enable_pll(0);
while !RCC.cr().read().pll1rdy() {}
} }
if pll2_p_ck.is_some() { if pll2_p_ck.is_some() {
RCC.cr().modify(|w| w.set_pll2on(Hsion::ON)); enable_pll(1);
while !RCC.cr().read().pll2rdy() {}
} }
if pll3_p_ck.is_some() { if pll3_p_ck.is_some() {
RCC.cr().modify(|w| w.set_pll3on(Hsion::ON)); enable_pll(2);
while !RCC.cr().read().pll3rdy() {}
} }
// Core Prescaler / AHB Prescaler / APB3 Prescaler // Core Prescaler / AHB Prescaler / APB3 Prescaler
RCC.d1cfgr().modify(|w| { RCC.d1cfgr().modify(|w| {
w.set_d1cpre(Hpre(d1cpre_bits)); w.set_d1cpre(Hpre(d1cpre_bits));
w.set_d1ppre(D1ppre(ppre3_bits)); w.set_d1ppre(Dppre(ppre3_bits));
w.set_hpre(hpre_bits) w.set_hpre(hpre_bits)
}); });
// Ensure core prescaler value is valid before future lower // Ensure core prescaler value is valid before future lower
@ -288,12 +287,12 @@ impl<'d> Rcc<'d> {
// APB1 / APB2 Prescaler // APB1 / APB2 Prescaler
RCC.d2cfgr().modify(|w| { RCC.d2cfgr().modify(|w| {
w.set_d2ppre1(D2ppre1(ppre1_bits)); w.set_d2ppre1(Dppre(ppre1_bits));
w.set_d2ppre2(D2ppre1(ppre2_bits)); w.set_d2ppre2(Dppre(ppre2_bits));
}); });
// APB4 Prescaler // APB4 Prescaler
RCC.d3cfgr().modify(|w| w.set_d3ppre(D3ppre(ppre4_bits))); RCC.d3cfgr().modify(|w| w.set_d3ppre(Dppre(ppre4_bits)));
// Peripheral Clock (per_ck) // Peripheral Clock (per_ck)
RCC.d1ccipr().modify(|w| w.set_ckpersel(ckpersel)); RCC.d1ccipr().modify(|w| w.set_ckpersel(ckpersel));
@ -312,8 +311,7 @@ impl<'d> Rcc<'d> {
// IO compensation cell - Requires CSI clock and SYSCFG // IO compensation cell - Requires CSI clock and SYSCFG
assert!(RCC.cr().read().csirdy()); assert!(RCC.cr().read().csirdy());
RCC.apb4enr() RCC.apb4enr().modify(|w| w.set_syscfgen(true));
.modify(|w| w.set_syscfgen(Apb4enrSyscfgen::ENABLED));
// Enable the compensation cell, using back-bias voltage code // Enable the compensation cell, using back-bias voltage code
// provide by the cell. // provide by the cell.
@ -364,13 +362,10 @@ impl<'d> Rcc<'d> {
/// Set `enable_dma1` to true if you do not have at least one bus master (other than the CPU) /// Set `enable_dma1` to true if you do not have at least one bus master (other than the CPU)
/// enable during WFI/WFE /// enable during WFI/WFE
pub fn enable_debug_wfe(&mut self, _dbg: &mut peripherals::DBGMCU, enable_dma1: bool) { pub fn enable_debug_wfe(&mut self, _dbg: &mut peripherals::DBGMCU, enable_dma1: bool) {
use crate::pac::rcc::vals::Ahb1enrDma1en;
// NOTE(unsafe) We have exclusive access to the RCC and DBGMCU // NOTE(unsafe) We have exclusive access to the RCC and DBGMCU
unsafe { unsafe {
if enable_dma1 { if enable_dma1 {
RCC.ahb1enr() RCC.ahb1enr().modify(|w| w.set_dma1en(true));
.modify(|w| w.set_dma1en(Ahb1enrDma1en::ENABLED));
} }
DBGMCU.cr().modify(|w| { DBGMCU.cr().modify(|w| {

View File

@ -46,7 +46,7 @@ fn vco_output_divider_setup(output: u32, plln: usize) -> (u32, u32) {
/// ///
/// Must have exclusive access to the RCC register block /// Must have exclusive access to the RCC register block
unsafe fn vco_setup(pll_src: u32, requested_output: u32, plln: usize) -> PllConfigResults { unsafe fn vco_setup(pll_src: u32, requested_output: u32, plln: usize) -> PllConfigResults {
use crate::pac::rcc::vals::{Pll1rge, Pll1vcosel}; use crate::pac::rcc::vals::{Pllrge, Pllvcosel};
let (vco_ck_target, pll_x_p) = vco_output_divider_setup(requested_output, plln); let (vco_ck_target, pll_x_p) = vco_output_divider_setup(requested_output, plln);
@ -60,8 +60,8 @@ unsafe fn vco_setup(pll_src: u32, requested_output: u32, plln: usize) -> PllConf
assert!((1_000_000..=2_000_000).contains(&ref_x_ck)); assert!((1_000_000..=2_000_000).contains(&ref_x_ck));
RCC.pllcfgr().modify(|w| { RCC.pllcfgr().modify(|w| {
w.set_pllvcosel(plln, Pll1vcosel::MEDIUMVCO); w.set_pllvcosel(plln, Pllvcosel::MEDIUMVCO);
w.set_pllrge(plln, Pll1rge::RANGE1); w.set_pllrge(plln, Pllrge::RANGE1);
}); });
PllConfigResults { PllConfigResults {
ref_x_ck, ref_x_ck,
@ -79,7 +79,7 @@ pub(super) unsafe fn pll_setup(
config: &PllConfig, config: &PllConfig,
plln: usize, plln: usize,
) -> (Option<u32>, Option<u32>, Option<u32>) { ) -> (Option<u32>, Option<u32>, Option<u32>) {
use crate::pac::rcc::vals::{Divp1, Divp1en, Pll1fracen}; use crate::pac::rcc::vals::Divp;
match config.p_ck { match config.p_ck {
Some(requested_output) => { Some(requested_output) => {
@ -101,22 +101,19 @@ pub(super) unsafe fn pll_setup(
.modify(|w| w.set_divn1((pll_x_n - 1) as u16)); .modify(|w| w.set_divn1((pll_x_n - 1) as u16));
// No FRACN // No FRACN
RCC.pllcfgr() RCC.pllcfgr().modify(|w| w.set_pllfracen(plln, false));
.modify(|w| w.set_pllfracen(plln, Pll1fracen::RESET));
let vco_ck = ref_x_ck * pll_x_n; let vco_ck = ref_x_ck * pll_x_n;
RCC.plldivr(plln) RCC.plldivr(plln)
.modify(|w| w.set_divp1(Divp1((pll_x_p - 1) as u8))); .modify(|w| w.set_divp1(Divp((pll_x_p - 1) as u8)));
RCC.pllcfgr() RCC.pllcfgr().modify(|w| w.set_divpen(plln, true));
.modify(|w| w.set_divpen(plln, Divp1en::ENABLED));
// Calulate additional output dividers // Calulate additional output dividers
let q_ck = match config.q_ck { let q_ck = match config.q_ck {
Some(Hertz(ck)) if ck > 0 => { Some(Hertz(ck)) if ck > 0 => {
let div = (vco_ck + ck - 1) / ck; let div = (vco_ck + ck - 1) / ck;
RCC.plldivr(plln).modify(|w| w.set_divq1((div - 1) as u8)); RCC.plldivr(plln).modify(|w| w.set_divq1((div - 1) as u8));
RCC.pllcfgr() RCC.pllcfgr().modify(|w| w.set_divqen(plln, true));
.modify(|w| w.set_divqen(plln, Divp1en::ENABLED));
Some(vco_ck / div) Some(vco_ck / div)
} }
_ => None, _ => None,
@ -125,8 +122,7 @@ pub(super) unsafe fn pll_setup(
Some(Hertz(ck)) if ck > 0 => { Some(Hertz(ck)) if ck > 0 => {
let div = (vco_ck + ck - 1) / ck; let div = (vco_ck + ck - 1) / ck;
RCC.plldivr(plln).modify(|w| w.set_divr1((div - 1) as u8)); RCC.plldivr(plln).modify(|w| w.set_divr1((div - 1) as u8));
RCC.pllcfgr() RCC.pllcfgr().modify(|w| w.set_divren(plln, true));
.modify(|w| w.set_divren(plln, Divp1en::ENABLED));
Some(vco_ck / div) Some(vco_ck / div)
} }
_ => None, _ => None,

View File

@ -7,9 +7,7 @@ use core::marker::PhantomData;
use embassy::util::Unborrow; use embassy::util::Unborrow;
use embassy_extras::unborrow; use embassy_extras::unborrow;
use pac::dbg::vals::{DbgSleep, DbgStandby, DbgStop}; use pac::dbg::vals::{DbgSleep, DbgStandby, DbgStop};
use pac::rcc::vals::{ use pac::rcc::vals::{Hpre, Msirange, Plldiv, Pllmul, Pllsrc, Ppre, Sw};
Crypen, Dbgen, Hpre, Iophen, Lptimen, Msirange, Plldiv, Pllmul, Pllon, Pllsrc, Ppre, Sw,
};
/// Most of clock setup is copied from stm32l0xx-hal, and adopted to the generated PAC, /// 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. /// and with the addition of the init function to configure a system clock.
@ -266,7 +264,7 @@ impl<'d> Rcc<'d> {
// NOTE(unsafe) We have exclusive access to the RCC and DBGMCU // NOTE(unsafe) We have exclusive access to the RCC and DBGMCU
unsafe { unsafe {
if enable_dma { if enable_dma {
pac::RCC.ahbenr().modify(|w| w.set_dmaen(Crypen::ENABLED)); pac::RCC.ahbenr().modify(|w| w.set_dmaen(true));
} }
pac::DBGMCU.cr().modify(|w| { pac::DBGMCU.cr().modify(|w| {
@ -285,14 +283,14 @@ impl<'d> Rcc<'d> {
rcc.apb2rstr().modify(|w| w.set_syscfgrst(false)); rcc.apb2rstr().modify(|w| w.set_syscfgrst(false));
// Enable SYSCFG peripheral // Enable SYSCFG peripheral
rcc.apb2enr().modify(|w| w.set_syscfgen(Dbgen::ENABLED)); rcc.apb2enr().modify(|w| w.set_syscfgen(true));
// Reset CRS peripheral // Reset CRS peripheral
rcc.apb1rstr().modify(|w| w.set_crsrst(true)); rcc.apb1rstr().modify(|w| w.set_crsrst(true));
rcc.apb1rstr().modify(|w| w.set_crsrst(false)); rcc.apb1rstr().modify(|w| w.set_crsrst(false));
// Enable CRS peripheral // Enable CRS peripheral
rcc.apb1enr().modify(|w| w.set_crsen(Lptimen::ENABLED)); rcc.apb1enr().modify(|w| w.set_crsen(true));
// Initialize CRS // Initialize CRS
let crs = pac::CRS; let crs = pac::CRS;
@ -369,7 +367,7 @@ impl RccExt for RCC {
// Enable MSI // Enable MSI
unsafe { unsafe {
rcc.cr().write(|w| w.set_msion(Pllon::ENABLED)); rcc.cr().write(|w| w.set_msion(true));
while !rcc.cr().read().msirdy() {} while !rcc.cr().read().msirdy() {}
} }
@ -379,7 +377,7 @@ impl RccExt for RCC {
ClockSrc::HSI16 => { ClockSrc::HSI16 => {
// Enable HSI16 // Enable HSI16
unsafe { unsafe {
rcc.cr().write(|w| w.set_hsi16on(Pllon::ENABLED)); rcc.cr().write(|w| w.set_hsi16on(true));
while !rcc.cr().read().hsi16rdyf() {} while !rcc.cr().read().hsi16rdyf() {}
} }
@ -388,7 +386,7 @@ impl RccExt for RCC {
ClockSrc::HSE(freq) => { ClockSrc::HSE(freq) => {
// Enable HSE // Enable HSE
unsafe { unsafe {
rcc.cr().write(|w| w.set_hseon(Pllon::ENABLED)); rcc.cr().write(|w| w.set_hseon(true));
while !rcc.cr().read().hserdy() {} while !rcc.cr().read().hserdy() {}
} }
@ -399,7 +397,7 @@ impl RccExt for RCC {
PLLSource::HSE(freq) => { PLLSource::HSE(freq) => {
// Enable HSE // Enable HSE
unsafe { unsafe {
rcc.cr().write(|w| w.set_hseon(Pllon::ENABLED)); rcc.cr().write(|w| w.set_hseon(true));
while !rcc.cr().read().hserdy() {} while !rcc.cr().read().hserdy() {}
} }
freq.0 freq.0
@ -407,7 +405,7 @@ impl RccExt for RCC {
PLLSource::HSI16 => { PLLSource::HSI16 => {
// Enable HSI // Enable HSI
unsafe { unsafe {
rcc.cr().write(|w| w.set_hsi16on(Pllon::ENABLED)); rcc.cr().write(|w| w.set_hsi16on(true));
while !rcc.cr().read().hsi16rdyf() {} while !rcc.cr().read().hsi16rdyf() {}
} }
HSI_FREQ HSI_FREQ
@ -416,7 +414,7 @@ impl RccExt for RCC {
// Disable PLL // Disable PLL
unsafe { unsafe {
rcc.cr().modify(|w| w.set_pllon(Pllon::DISABLED)); rcc.cr().modify(|w| w.set_pllon(false));
while rcc.cr().read().pllrdy() {} while rcc.cr().read().pllrdy() {}
} }
@ -447,7 +445,7 @@ impl RccExt for RCC {
}); });
// Enable PLL // Enable PLL
rcc.cr().modify(|w| w.set_pllon(Pllon::ENABLED)); rcc.cr().modify(|w| w.set_pllon(true));
while !rcc.cr().read().pllrdy() {} while !rcc.cr().read().pllrdy() {}
} }
@ -459,8 +457,8 @@ impl RccExt for RCC {
rcc.cfgr().modify(|w| { rcc.cfgr().modify(|w| {
w.set_sw(sw.into()); w.set_sw(sw.into());
w.set_hpre(cfgr.ahb_pre.into()); w.set_hpre(cfgr.ahb_pre.into());
w.set_ppre(0, cfgr.apb1_pre.into()); w.set_ppre1(cfgr.apb1_pre.into());
w.set_ppre(1, cfgr.apb2_pre.into()); w.set_ppre2(cfgr.apb2_pre.into());
}); });
} }
@ -526,14 +524,13 @@ pub struct LSE(());
pub unsafe fn init(config: Config) { pub unsafe fn init(config: Config) {
let rcc = pac::RCC; let rcc = pac::RCC;
let enabled = Iophen::ENABLED;
rcc.iopenr().write(|w| { rcc.iopenr().write(|w| {
w.set_iopaen(enabled); w.set_iopaen(true);
w.set_iopben(enabled); w.set_iopben(true);
w.set_iopcen(enabled); w.set_iopcen(true);
w.set_iopden(enabled); w.set_iopden(true);
w.set_iopeen(enabled); w.set_iopeen(true);
w.set_iophen(enabled); w.set_iophen(true);
}); });
let r = <peripherals::RCC as embassy::util::Steal>::steal(); let r = <peripherals::RCC as embassy::util::Steal>::steal();

@ -1 +1 @@
Subproject commit 33dfa674865b1b5f0bfb86f3217055a6a057a6fb Subproject commit ced687a382fa8b641af568dadc71af7abe62d5cd