Skip to content

Commit

Permalink
Merge pull request #2035 from pbert519/stm_reset_and_enable
Browse files Browse the repository at this point in the history
STM32: combine RccPeripherals reset() and enable() to enable_and_reset()
  • Loading branch information
Dirbaio authored Oct 12, 2023
2 parents 01eb1a7 + 65f81a1 commit 66e399b
Show file tree
Hide file tree
Showing 39 changed files with 114 additions and 178 deletions.
12 changes: 4 additions & 8 deletions embassy-stm32/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,8 @@ fn main() {
let rst_reg = format_ident!("{}", rst.register.to_ascii_lowercase());
let set_rst_field = format_ident!("set_{}", rst.field.to_ascii_lowercase());
quote! {
critical_section::with(|_| {
crate::pac::RCC.#rst_reg().modify(|w| w.#set_rst_field(true));
crate::pac::RCC.#rst_reg().modify(|w| w.#set_rst_field(false));
});
crate::pac::RCC.#rst_reg().modify(|w| w.#set_rst_field(true));
crate::pac::RCC.#rst_reg().modify(|w| w.#set_rst_field(false));
}
}
None => TokenStream::new(),
Expand Down Expand Up @@ -556,13 +554,14 @@ fn main() {
fn frequency() -> crate::time::Hertz {
#clock_frequency
}
fn enable() {
fn enable_and_reset() {
critical_section::with(|_cs| {
#before_enable
#[cfg(feature = "low-power")]
crate::rcc::clock_refcount_add(_cs);
crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true));
#after_enable
#rst
})
}
fn disable() {
Expand All @@ -573,9 +572,6 @@ fn main() {
crate::rcc::clock_refcount_sub(_cs);
})
}
fn reset() {
#rst
}
}

impl crate::rcc::RccPeripheral for peripherals::#pname {}
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/adc/f1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ impl<T: Instance> super::sealed::AdcPin<T> for Temperature {
impl<'d, T: Instance> Adc<'d, T> {
pub fn new(adc: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u32>) -> Self {
into_ref!(adc);
T::enable();
T::reset();
T::enable_and_reset();
T::regs().cr2().modify(|reg| reg.set_adon(true));

// 11.4: Before starting a calibration, the ADC must have been in power-on state (ADON bit = ‘1’)
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/adc/f3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ impl<'d, T: Instance> Adc<'d, T> {

into_ref!(adc);

T::enable();
T::reset();
T::enable_and_reset();

// Enable the adc regulator
T::regs().cr().modify(|w| w.set_advregen(vals::Advregen::INTERMEDIATE));
Expand Down
4 changes: 2 additions & 2 deletions embassy-stm32/src/adc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ pub(crate) mod sealed {
}
}

#[cfg(not(any(adc_f1, adc_v1, adc_v2, adc_v4, adc_f3)))]
#[cfg(not(any(adc_f1, adc_v1, adc_v2, adc_v3, adc_v4, adc_f3, adc_g0)))]
pub trait Instance: sealed::Instance + crate::Peripheral<P = Self> {}
#[cfg(any(adc_f1, adc_v1, adc_v2, adc_v4, adc_f3))]
#[cfg(any(adc_f1, adc_v1, adc_v2, adc_v3, adc_v4, adc_f3, adc_g0))]
pub trait Instance: sealed::Instance + crate::Peripheral<P = Self> + crate::rcc::RccPeripheral {}

pub trait AdcPin<T: Instance>: sealed::AdcPin<T> {}
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/adc/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ impl<'d, T: Instance> Adc<'d, T> {
delay: &mut impl DelayUs<u32>,
) -> Self {
into_ref!(adc);
T::enable();
T::reset();
T::enable_and_reset();

// Delay 1μs when using HSI14 as the ADC clock.
//
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/adc/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ where
{
pub fn new(adc: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u32>) -> Self {
into_ref!(adc);
T::enable();
T::reset();
T::enable_and_reset();

let presc = Prescaler::from_pclk2(T::frequency());
T::common_regs().ccr().modify(|w| w.set_adcpre(presc.adcpre()));
Expand Down
15 changes: 1 addition & 14 deletions embassy-stm32/src/adc/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ pub const VREF_DEFAULT_MV: u32 = 3300;
/// VREF voltage used for factory calibration of VREFINTCAL register.
pub const VREF_CALIB_MV: u32 = 3000;

/// Sadly we cannot use `RccPeripheral::enable` since devices are quite inconsistent ADC clock
/// configuration.
fn enable() {
critical_section::with(|_| {
#[cfg(any(stm32h7, stm32wl))]
crate::pac::RCC.apb2enr().modify(|w| w.set_adcen(true));
#[cfg(stm32g0)]
crate::pac::RCC.apbenr2().modify(|w| w.set_adcen(true));
#[cfg(any(stm32l4, stm32l5, stm32wb))]
crate::pac::RCC.ahb2enr().modify(|w| w.set_adcen(true));
});
}

pub struct VrefInt;
impl<T: Instance> AdcPin<T> for VrefInt {}
impl<T: Instance> super::sealed::AdcPin<T> for VrefInt {
Expand Down Expand Up @@ -61,7 +48,7 @@ impl<T: Instance> super::sealed::AdcPin<T> for Vbat {
impl<'d, T: Instance> Adc<'d, T> {
pub fn new(adc: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u32>) -> Self {
into_ref!(adc);
enable();
T::enable_and_reset();
T::regs().cr().modify(|reg| {
#[cfg(not(adc_g0))]
reg.set_deeppwd(false);
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/adc/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ impl Prescaler {
impl<'d, T: Instance> Adc<'d, T> {
pub fn new(adc: impl Peripheral<P = T> + 'd, delay: &mut impl DelayUs<u16>) -> Self {
embassy_hal_internal::into_ref!(adc);
T::enable();
T::reset();
T::enable_and_reset();

let prescaler = Prescaler::from_ker_ck(T::frequency());

Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/can/bxcan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ impl<'d, T: Instance> Can<'d, T> {
rx.set_as_af(rx.af_num(), AFType::Input);
tx.set_as_af(tx.af_num(), AFType::OutputPushPull);

T::enable();
T::reset();
T::enable_and_reset();

{
use crate::pac::can::vals::{Errie, Fmpie, Tmeie};
Expand Down
4 changes: 1 addition & 3 deletions embassy-stm32/src/crc/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ impl<'d> Crc<'d> {

// Note: enable and reset come from RccPeripheral.
// enable CRC clock in RCC.
CRC::enable();
// Reset CRC to default values.
CRC::reset();
CRC::enable_and_reset();
// Peripheral the peripheral
let mut instance = Self { _peri: peripheral };
instance.reset();
Expand Down
7 changes: 2 additions & 5 deletions embassy-stm32/src/crc/v2v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,13 @@ impl<'d> Crc<'d> {
/// Instantiates the CRC32 peripheral and initializes it to default values.
pub fn new(peripheral: impl Peripheral<P = CRC> + 'd, config: Config) -> Self {
// Note: enable and reset come from RccPeripheral.
// enable CRC clock in RCC.
CRC::enable();
// Reset CRC to default values.
CRC::reset();
// reset to default values and enable CRC clock in RCC.
CRC::enable_and_reset();
into_ref!(peripheral);
let mut instance = Self {
_peripheral: peripheral,
_config: config,
};
CRC::reset();
instance.reconfigure();
instance.reset();
instance
Expand Down
62 changes: 27 additions & 35 deletions embassy-stm32/src/dac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ impl<'d, T: Instance, Tx> DacCh1<'d, T, Tx> {
) -> Self {
pin.set_as_analog();
into_ref!(peri, dma);
T::enable();
T::reset();
T::enable_and_reset();

let mut dac = Self { _peri: peri, dma };

Expand Down Expand Up @@ -366,8 +365,7 @@ impl<'d, T: Instance, Tx> DacCh2<'d, T, Tx> {
) -> Self {
pin.set_as_analog();
into_ref!(_peri, dma);
T::enable();
T::reset();
T::enable_and_reset();

let mut dac = Self {
phantom: PhantomData,
Expand Down Expand Up @@ -483,8 +481,7 @@ impl<'d, T: Instance, TxCh1, TxCh2> Dac<'d, T, TxCh1, TxCh2> {
pin_ch1.set_as_analog();
pin_ch2.set_as_analog();
into_ref!(peri, dma_ch1, dma_ch2);
T::enable();
T::reset();
T::enable_and_reset();

let mut dac_ch1 = DacCh1 {
_peri: peri,
Expand Down Expand Up @@ -563,35 +560,30 @@ pub trait DacPin<T: Instance, const C: u8>: crate::gpio::Pin + 'static {}

foreach_peripheral!(
(dac, $inst:ident) => {
// H7 uses single bit for both DAC1 and DAC2, this is a hack until a proper fix is implemented
#[cfg(any(rcc_h7, rcc_h7rm0433))]
impl crate::rcc::sealed::RccPeripheral for peripherals::$inst {
fn frequency() -> crate::time::Hertz {
critical_section::with(|_| unsafe { crate::rcc::get_freqs().apb1 })
}

fn reset() {
critical_section::with(|_| {
crate::pac::RCC.apb1lrstr().modify(|w| w.set_dac12rst(true));
crate::pac::RCC.apb1lrstr().modify(|w| w.set_dac12rst(false));
})
}

fn enable() {
critical_section::with(|_| {
crate::pac::RCC.apb1lenr().modify(|w| w.set_dac12en(true));
})
}

fn disable() {
critical_section::with(|_| {
crate::pac::RCC.apb1lenr().modify(|w| w.set_dac12en(false))
})
}
}

#[cfg(any(rcc_h7, rcc_h7rm0433))]
impl crate::rcc::RccPeripheral for peripherals::$inst {}
// H7 uses single bit for both DAC1 and DAC2, this is a hack until a proper fix is implemented
#[cfg(any(rcc_h7, rcc_h7rm0433))]
impl crate::rcc::sealed::RccPeripheral for peripherals::$inst {
fn frequency() -> crate::time::Hertz {
critical_section::with(|_| unsafe { crate::rcc::get_freqs().apb1 })
}

fn enable_and_reset() {
critical_section::with(|_| {
crate::pac::RCC.apb1lrstr().modify(|w| w.set_dac12rst(true));
crate::pac::RCC.apb1lrstr().modify(|w| w.set_dac12rst(false));
crate::pac::RCC.apb1lenr().modify(|w| w.set_dac12en(true));
})
}

fn disable() {
critical_section::with(|_| {
crate::pac::RCC.apb1lenr().modify(|w| w.set_dac12en(false))
})
}
}

#[cfg(any(rcc_h7, rcc_h7rm0433))]
impl crate::rcc::RccPeripheral for peripherals::$inst {}

impl crate::dac::sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::dac::Dac {
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/dcmi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,7 @@ where
use_embedded_synchronization: bool,
edm: u8,
) -> Self {
T::reset();
T::enable();
T::enable_and_reset();

peri.regs().cr().modify(|r| {
r.set_cm(true); // disable continuous mode (snapshot mode)
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/fmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ where
const REGISTERS: *const () = T::REGS.as_ptr() as *const _;

fn enable(&mut self) {
<T as crate::rcc::sealed::RccPeripheral>::enable();
<T as crate::rcc::sealed::RccPeripheral>::reset();
T::enable_and_reset();
}

fn memory_controller_enable(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion embassy-stm32/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ foreach_pin!(

pub(crate) unsafe fn init() {
#[cfg(afio)]
<crate::peripherals::AFIO as crate::rcc::sealed::RccPeripheral>::enable();
<crate::peripherals::AFIO as crate::rcc::sealed::RccPeripheral>::enable_and_reset();

crate::_generated::init_gpio();
}
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/hrtim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ impl<'d, T: Instance> AdvancedPwm<'d, T> {
fn new_inner(tim: impl Peripheral<P = T> + 'd) -> Self {
into_ref!(tim);

T::enable();
<T as crate::rcc::sealed::RccPeripheral>::reset();
T::enable_and_reset();

#[cfg(stm32f334)]
if unsafe { get_freqs() }.hrtim.is_some() {
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/i2c/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
) -> Self {
into_ref!(scl, sda, tx_dma, rx_dma);

T::enable();
T::reset();
T::enable_and_reset();

scl.set_as_af_pull(
scl.af_num(),
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/i2c/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
) -> Self {
into_ref!(peri, scl, sda, tx_dma, rx_dma);

T::enable();
T::reset();
T::enable_and_reset();

scl.set_as_af_pull(
scl.af_num(),
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/ipcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ pub struct Ipcc;

impl Ipcc {
pub fn enable(_config: Config) {
IPCC::enable();
IPCC::reset();
IPCC::enable_and_reset();
IPCC::set_cpu2(true);

_configure_pwr();
Expand Down
6 changes: 3 additions & 3 deletions embassy-stm32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ pub fn init(config: Config) -> Peripherals {
}

#[cfg(not(any(stm32f1, stm32wb, stm32wl)))]
peripherals::SYSCFG::enable();
peripherals::SYSCFG::enable_and_reset();
#[cfg(not(any(stm32h5, stm32h7, stm32wb, stm32wl)))]
peripherals::PWR::enable();
peripherals::PWR::enable_and_reset();
#[cfg(not(any(stm32f2, stm32f4, stm32f7, stm32l0, stm32h5, stm32h7)))]
peripherals::FLASH::enable();
peripherals::FLASH::enable_and_reset();

unsafe {
#[cfg(feature = "_split-pins-enabled")]
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/qspi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ impl<'d, T: Instance, Dma> Qspi<'d, T, Dma> {
) -> Self {
into_ref!(peri, dma);

T::enable();
T::reset();
T::enable_and_reset();

while T::REGS.sr().read().busy() {}

Expand Down
2 changes: 1 addition & 1 deletion embassy-stm32/src/rcc/g4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub(crate) unsafe fn init(config: Config) {

// Enable and setup CRS if needed
if let Some(crs_config) = crs_config {
crate::peripherals::CRS::enable();
crate::peripherals::CRS::enable_and_reset();

let sync_src = match crs_config.sync_src {
CrsSyncSource::Gpio => crate::pac::crs::vals::Syncsrc::GPIO,
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/rcc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ pub mod low_level {
pub(crate) mod sealed {
pub trait RccPeripheral {
fn frequency() -> crate::time::Hertz;
fn reset();
fn enable();
fn enable_and_reset();
fn disable();
}
}
Expand Down
3 changes: 1 addition & 2 deletions embassy-stm32/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ impl<'d, T: Instance> Rng<'d, T> {
inner: impl Peripheral<P = T> + 'd,
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
) -> Self {
T::enable();
T::reset();
T::enable_and_reset();
into_ref!(inner);
let mut random = Self { _inner: inner };
random.reset();
Expand Down
2 changes: 1 addition & 1 deletion embassy-stm32/src/rtc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl Default for RtcCalibrationCyclePeriod {
impl Rtc {
pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self {
#[cfg(not(any(stm32l0, stm32f3, stm32l1, stm32f0, stm32f2)))]
<RTC as crate::rcc::sealed::RccPeripheral>::enable();
<RTC as crate::rcc::sealed::RccPeripheral>::enable_and_reset();

let mut this = Self {
#[cfg(feature = "low-power")]
Expand Down
Loading

0 comments on commit 66e399b

Please sign in to comment.