Reset peripherals on driver construction (where missing) (#1893)

* Reset peripherals on driver contruction (where missing)

* Don't enable and reset SHA in HMAC ctor

* changelog

* Don't reset the TIMG0
This commit is contained in:
Juraj Sadel 2024-08-13 14:09:04 +02:00 committed by GitHub
parent 23f76b0bea
commit 64a7d49a29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
22 changed files with 110 additions and 3 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added touch pad support for esp32 (#1873) - Added touch pad support for esp32 (#1873)
- Allow configuration of period updating method for MCPWM timers (#1898) - Allow configuration of period updating method for MCPWM timers (#1898)
- Add self-testing mode for TWAI peripheral. (#1929) - Add self-testing mode for TWAI peripheral. (#1929)
- Added a `PeripheralClockControl::reset` to the driver constructors where missing (#1893)
### Changed ### Changed
@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- DMA: don't require `Sealed` to implement `ReadBuffer` and `WriteBuffer` (#1921) - DMA: don't require `Sealed` to implement `ReadBuffer` and `WriteBuffer` (#1921)
- Allow DMA to/from psram for esp32s3 (#1827) - Allow DMA to/from psram for esp32s3 (#1827)
- DMA buffers now don't require a static lifetime. Make sure to never `mem::forget` an in-progress DMA transfer (consider using `#[deny(clippy::mem_forget)]`) (#1837) - DMA buffers now don't require a static lifetime. Make sure to never `mem::forget` an in-progress DMA transfer (consider using `#[deny(clippy::mem_forget)]`) (#1837)
- Peripherals (where possible) are now explicitly reset and enabled in their constructors (#1893)
### Fixed ### Fixed

View File

@ -135,6 +135,10 @@ impl<'d> Aes<'d> {
/// Constructs a new `Aes` instance. /// Constructs a new `Aes` instance.
pub fn new(aes: impl Peripheral<P = AES> + 'd) -> Self { pub fn new(aes: impl Peripheral<P = AES> + 'd) -> Self {
crate::into_ref!(aes); crate::into_ref!(aes);
crate::system::PeripheralClockControl::reset(crate::system::Peripheral::Aes);
crate::system::PeripheralClockControl::enable(crate::system::Peripheral::Aes);
let mut ret = Self { let mut ret = Self {
aes, aes,
alignment_helper: AlignmentHelper::native_endianess(), alignment_helper: AlignmentHelper::native_endianess(),

View File

@ -408,6 +408,7 @@ where
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd, adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>, config: AdcConfig<ADCI>,
) -> Self { ) -> Self {
PeripheralClockControl::reset(Peripheral::ApbSarAdc);
PeripheralClockControl::enable(Peripheral::ApbSarAdc); PeripheralClockControl::enable(Peripheral::ApbSarAdc);
unsafe { &*APB_SARADC::PTR }.ctrl().modify(|_, w| unsafe { unsafe { &*APB_SARADC::PTR }.ctrl().modify(|_, w| unsafe {

View File

@ -6,6 +6,7 @@ use crate::efuse::Efuse;
use crate::{ use crate::{
peripheral::PeripheralRef, peripheral::PeripheralRef,
peripherals::{APB_SARADC, SENS}, peripherals::{APB_SARADC, SENS},
system::{Peripheral, PeripheralClockControl},
}; };
mod calibration; mod calibration;
@ -400,6 +401,9 @@ where
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd, adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>, config: AdcConfig<ADCI>,
) -> Self { ) -> Self {
PeripheralClockControl::reset(Peripheral::ApbSarAdc);
PeripheralClockControl::enable(Peripheral::ApbSarAdc);
let sensors = unsafe { &*SENS::ptr() }; let sensors = unsafe { &*SENS::ptr() };
// Set attenuation for pins // Set attenuation for pins

View File

@ -85,6 +85,7 @@ impl<'d> Ecc<'d, crate::Blocking> {
pub fn new(ecc: impl Peripheral<P = ECC> + 'd) -> Self { pub fn new(ecc: impl Peripheral<P = ECC> + 'd) -> Self {
crate::into_ref!(ecc); crate::into_ref!(ecc);
PeripheralClockControl::reset(PeripheralEnable::Ecc);
PeripheralClockControl::enable(PeripheralEnable::Ecc); PeripheralClockControl::enable(PeripheralEnable::Ecc);
Self { Self {

View File

@ -149,6 +149,7 @@ macro_rules! create_etm {
pub fn new(peripheral: impl Peripheral<P = crate::peripherals::SOC_ETM> + 'd) -> Self { pub fn new(peripheral: impl Peripheral<P = crate::peripherals::SOC_ETM> + 'd) -> Self {
crate::into_ref!(peripheral); crate::into_ref!(peripheral);
PeripheralClockControl::reset(crate::system::Peripheral::Etm);
PeripheralClockControl::enable(crate::system::Peripheral::Etm); PeripheralClockControl::enable(crate::system::Peripheral::Etm);
Self { Self {

View File

@ -96,7 +96,7 @@ impl<'d> Hmac<'d> {
pub fn new(hmac: impl Peripheral<P = HMAC> + 'd) -> Self { pub fn new(hmac: impl Peripheral<P = HMAC> + 'd) -> Self {
crate::into_ref!(hmac); crate::into_ref!(hmac);
PeripheralClockControl::enable(PeripheralEnable::Sha); PeripheralClockControl::reset(PeripheralEnable::Hmac);
PeripheralClockControl::enable(PeripheralEnable::Hmac); PeripheralClockControl::enable(PeripheralEnable::Hmac);
Self { Self {

View File

@ -330,6 +330,7 @@ where
// the targets the same and force same configuration for both, TX and RX // the targets the same and force same configuration for both, TX and RX
channel.tx.init_channel(); channel.tx.init_channel();
PeripheralClockControl::reset(I::get_peripheral());
PeripheralClockControl::enable(I::get_peripheral()); PeripheralClockControl::enable(I::get_peripheral());
I::set_clock(calculate_clock( I::set_clock(calculate_clock(
sample_rate, sample_rate,

View File

@ -28,6 +28,7 @@ impl<'d> LcdCam<'d, crate::Blocking> {
pub fn new(lcd_cam: impl Peripheral<P = LCD_CAM> + 'd) -> Self { pub fn new(lcd_cam: impl Peripheral<P = LCD_CAM> + 'd) -> Self {
crate::into_ref!(lcd_cam); crate::into_ref!(lcd_cam);
PeripheralClockControl::reset(system::Peripheral::LcdCam);
PeripheralClockControl::enable(system::Peripheral::LcdCam); PeripheralClockControl::enable(system::Peripheral::LcdCam);
Self { Self {

View File

@ -114,6 +114,8 @@ impl<'d> Ledc<'d> {
clock_control_config: &'d Clocks<'d>, clock_control_config: &'d Clocks<'d>,
) -> Self { ) -> Self {
crate::into_ref!(_instance); crate::into_ref!(_instance);
PeripheralClockControl::reset(PeripheralEnable::Ledc);
PeripheralClockControl::enable(PeripheralEnable::Ledc); PeripheralClockControl::enable(PeripheralEnable::Ledc);
let ledc = unsafe { &*crate::peripherals::LEDC::ptr() }; let ledc = unsafe { &*crate::peripherals::LEDC::ptr() };

View File

@ -132,6 +132,7 @@ impl<'d, PWM: PwmPeripheral> McPwm<'d, PWM> {
) -> Self { ) -> Self {
crate::into_ref!(peripheral); crate::into_ref!(peripheral);
PWM::reset();
PWM::enable(); PWM::enable();
#[cfg(not(esp32c6))] #[cfg(not(esp32c6))]
@ -312,6 +313,8 @@ pub struct FrequencyError;
pub trait PwmPeripheral: Deref<Target = RegisterBlock> + crate::private::Sealed { pub trait PwmPeripheral: Deref<Target = RegisterBlock> + crate::private::Sealed {
/// Enable peripheral /// Enable peripheral
fn enable(); fn enable();
/// Reset peripheral
fn reset();
/// Get a pointer to the peripheral RegisterBlock /// Get a pointer to the peripheral RegisterBlock
fn block() -> *const RegisterBlock; fn block() -> *const RegisterBlock;
/// Get operator GPIO mux output signal /// Get operator GPIO mux output signal
@ -324,6 +327,10 @@ impl PwmPeripheral for crate::peripherals::MCPWM0 {
PeripheralClockControl::enable(PeripheralEnable::Mcpwm0) PeripheralClockControl::enable(PeripheralEnable::Mcpwm0)
} }
fn reset() {
PeripheralClockControl::reset(PeripheralEnable::Mcpwm0)
}
fn block() -> *const RegisterBlock { fn block() -> *const RegisterBlock {
Self::PTR Self::PTR
} }
@ -347,6 +354,10 @@ impl PwmPeripheral for crate::peripherals::MCPWM1 {
PeripheralClockControl::enable(PeripheralEnable::Mcpwm1) PeripheralClockControl::enable(PeripheralEnable::Mcpwm1)
} }
fn reset() {
PeripheralClockControl::reset(PeripheralEnable::Mcpwm1)
}
fn block() -> *const RegisterBlock { fn block() -> *const RegisterBlock {
Self::PTR Self::PTR
} }

View File

@ -66,6 +66,7 @@ impl<'d> Usb<'d> {
P: UsbDp + Send + Sync, P: UsbDp + Send + Sync,
M: UsbDm + Send + Sync, M: UsbDm + Send + Sync,
{ {
PeripheralClockControl::reset(PeripheralEnable::Usb);
PeripheralClockControl::enable(PeripheralEnable::Usb); PeripheralClockControl::enable(PeripheralEnable::Usb);
Self { Self {

View File

@ -1381,6 +1381,7 @@ where
return Err(Error::UnreachableClockRate); return Err(Error::UnreachableClockRate);
} }
PeripheralClockControl::reset(crate::system::Peripheral::ParlIo);
PeripheralClockControl::enable(crate::system::Peripheral::ParlIo); PeripheralClockControl::enable(crate::system::Peripheral::ParlIo);
let pcr = unsafe { &*crate::peripherals::PCR::PTR }; let pcr = unsafe { &*crate::peripherals::PCR::PTR };

View File

@ -61,6 +61,7 @@ impl<'d> Pcnt<'d> {
/// Return a new PCNT /// Return a new PCNT
pub fn new(_instance: impl Peripheral<P = peripherals::PCNT> + 'd) -> Self { pub fn new(_instance: impl Peripheral<P = peripherals::PCNT> + 'd) -> Self {
crate::into_ref!(_instance); crate::into_ref!(_instance);
// Enable the PCNT peripherals clock in the system peripheral // Enable the PCNT peripherals clock in the system peripheral
PeripheralClockControl::reset(crate::system::Peripheral::Pcnt); PeripheralClockControl::reset(crate::system::Peripheral::Pcnt);
PeripheralClockControl::enable(crate::system::Peripheral::Pcnt); PeripheralClockControl::enable(crate::system::Peripheral::Pcnt);

View File

@ -227,6 +227,7 @@ where
return Err(Error::UnreachableTargetFrequency); return Err(Error::UnreachableTargetFrequency);
} }
PeripheralClockControl::reset(crate::system::Peripheral::Rmt);
PeripheralClockControl::enable(crate::system::Peripheral::Rmt); PeripheralClockControl::enable(crate::system::Peripheral::Rmt);
#[cfg(not(any(esp32, esp32s2)))] #[cfg(not(any(esp32, esp32s2)))]

View File

@ -101,6 +101,7 @@ impl<'d, DM: crate::Mode> Rsa<'d, DM> {
fn new_internal(rsa: impl Peripheral<P = RSA> + 'd) -> Self { fn new_internal(rsa: impl Peripheral<P = RSA> + 'd) -> Self {
crate::into_ref!(rsa); crate::into_ref!(rsa);
PeripheralClockControl::reset(PeripheralEnable::Rsa);
PeripheralClockControl::enable(PeripheralEnable::Rsa); PeripheralClockControl::enable(PeripheralEnable::Rsa);
Self { Self {

View File

@ -136,6 +136,7 @@ impl<'d> Sha<'d, crate::Blocking> {
pub fn new(sha: impl Peripheral<P = SHA> + 'd, mode: ShaMode) -> Self { pub fn new(sha: impl Peripheral<P = SHA> + 'd, mode: ShaMode) -> Self {
crate::into_ref!(sha); crate::into_ref!(sha);
PeripheralClockControl::reset(crate::system::Peripheral::Sha);
PeripheralClockControl::enable(crate::system::Peripheral::Sha); PeripheralClockControl::enable(crate::system::Peripheral::Sha);
// Setup SHA Mode // Setup SHA Mode

View File

@ -544,6 +544,7 @@ where
mode: SpiMode, mode: SpiMode,
clocks: &Clocks<'d>, clocks: &Clocks<'d>,
) -> Spi<'d, T, FullDuplexMode> { ) -> Spi<'d, T, FullDuplexMode> {
spi.reset_peripheral();
spi.enable_peripheral(); spi.enable_peripheral();
let mut spi = Spi { let mut spi = Spi {
@ -721,6 +722,7 @@ where
mode: SpiMode, mode: SpiMode,
clocks: &Clocks<'d>, clocks: &Clocks<'d>,
) -> Spi<'d, T, HalfDuplexMode> { ) -> Spi<'d, T, HalfDuplexMode> {
spi.reset_peripheral();
spi.enable_peripheral(); spi.enable_peripheral();
let mut spi = Spi { let mut spi = Spi {
@ -2295,6 +2297,8 @@ pub trait Instance: private::Sealed {
fn enable_peripheral(&self); fn enable_peripheral(&self);
fn reset_peripheral(&self);
fn spi_num(&self) -> u8; fn spi_num(&self) -> u8;
/// Initialize for full-duplex 1 bit mode /// Initialize for full-duplex 1 bit mode
@ -3255,6 +3259,11 @@ impl Instance for crate::peripherals::SPI2 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi2); PeripheralClockControl::enable(crate::system::Peripheral::Spi2);
} }
#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi2);
}
#[inline(always)] #[inline(always)]
fn spi_num(&self) -> u8 { fn spi_num(&self) -> u8 {
2 2
@ -3332,6 +3341,11 @@ impl Instance for crate::peripherals::SPI2 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi2); PeripheralClockControl::enable(crate::system::Peripheral::Spi2);
} }
#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi2);
}
#[inline(always)] #[inline(always)]
fn spi_num(&self) -> u8 { fn spi_num(&self) -> u8 {
2 2
@ -3403,6 +3417,11 @@ impl Instance for crate::peripherals::SPI3 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi3) PeripheralClockControl::enable(crate::system::Peripheral::Spi3)
} }
#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi3)
}
#[inline(always)] #[inline(always)]
fn spi_num(&self) -> u8 { fn spi_num(&self) -> u8 {
3 3
@ -3447,6 +3466,11 @@ impl Instance for crate::peripherals::SPI2 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi2) PeripheralClockControl::enable(crate::system::Peripheral::Spi2)
} }
#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi2)
}
#[inline(always)] #[inline(always)]
fn spi_num(&self) -> u8 { fn spi_num(&self) -> u8 {
2 2
@ -3524,6 +3548,11 @@ impl Instance for crate::peripherals::SPI3 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi3) PeripheralClockControl::enable(crate::system::Peripheral::Spi3)
} }
#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi3)
}
#[inline(always)] #[inline(always)]
fn spi_num(&self) -> u8 { fn spi_num(&self) -> u8 {
3 3

View File

@ -106,6 +106,8 @@ pub trait TimerGroupInstance {
fn id() -> u8; fn id() -> u8;
fn register_block() -> *const RegisterBlock; fn register_block() -> *const RegisterBlock;
fn configure_src_clk(); fn configure_src_clk();
fn enable_peripheral();
fn reset_peripheral();
fn configure_wdt_src_clk(); fn configure_wdt_src_clk();
} }
@ -144,6 +146,14 @@ impl TimerGroupInstance for TIMG0 {
// ESP32 has only APB clock source, do nothing // ESP32 has only APB clock source, do nothing
} }
fn enable_peripheral() {
crate::system::PeripheralClockControl::enable(crate::system::Peripheral::Timg0)
}
fn reset_peripheral() {
// for TIMG0 do nothing for now because the reset breaks `current_time`
}
#[inline(always)] #[inline(always)]
#[cfg(any(esp32c2, esp32c3))] #[cfg(any(esp32c2, esp32c3))]
fn configure_wdt_src_clk() { fn configure_wdt_src_clk() {
@ -206,6 +216,16 @@ impl TimerGroupInstance for TIMG1 {
// ESP32-C2 and ESP32-C3 don't have t1config only t0config, do nothing // ESP32-C2 and ESP32-C3 don't have t1config only t0config, do nothing
} }
#[inline(always)]
fn enable_peripheral() {
crate::system::PeripheralClockControl::enable(crate::system::Peripheral::Timg1)
}
#[inline(always)]
fn reset_peripheral() {
crate::system::PeripheralClockControl::reset(crate::system::Peripheral::Timg1)
}
#[inline(always)] #[inline(always)]
#[cfg(any(esp32c6, esp32h2))] #[cfg(any(esp32c6, esp32h2))]
fn configure_wdt_src_clk() { fn configure_wdt_src_clk() {
@ -230,6 +250,9 @@ where
pub fn new(_timer_group: impl Peripheral<P = T> + 'd, clocks: &Clocks<'d>) -> Self { pub fn new(_timer_group: impl Peripheral<P = T> + 'd, clocks: &Clocks<'d>) -> Self {
crate::into_ref!(_timer_group); crate::into_ref!(_timer_group);
T::reset_peripheral();
T::enable_peripheral();
T::configure_src_clk(); T::configure_src_clk();
// ESP32-H2 is using PLL_48M_CLK source instead of APB_CLK // ESP32-H2 is using PLL_48M_CLK source instead of APB_CLK
@ -269,6 +292,9 @@ where
pub fn new_async(_timer_group: impl Peripheral<P = T> + 'd, clocks: &Clocks<'d>) -> Self { pub fn new_async(_timer_group: impl Peripheral<P = T> + 'd, clocks: &Clocks<'d>) -> Self {
crate::into_ref!(_timer_group); crate::into_ref!(_timer_group);
T::reset_peripheral();
T::enable_peripheral();
T::configure_src_clk(); T::configure_src_clk();
// ESP32-H2 is using PLL_48M_CLK source instead of APB_CLK // ESP32-H2 is using PLL_48M_CLK source instead of APB_CLK

View File

@ -66,6 +66,7 @@ where
pub fn new(peripheral: impl Peripheral<P = T> + 'd) -> Self { pub fn new(peripheral: impl Peripheral<P = T> + 'd) -> Self {
crate::into_ref!(peripheral); crate::into_ref!(peripheral);
PeripheralClockControl::reset(crate::system::Peripheral::Trace0);
PeripheralClockControl::enable(crate::system::Peripheral::Trace0); PeripheralClockControl::enable(crate::system::Peripheral::Trace0);
Self { Self {

View File

@ -735,7 +735,11 @@ where
no_transceiver: bool, no_transceiver: bool,
mode: TwaiMode, mode: TwaiMode,
) -> Self { ) -> Self {
// Set up the GPIO pins.
crate::into_ref!(tx_pin, rx_pin);
// Enable the peripheral clock for the TWAI peripheral. // Enable the peripheral clock for the TWAI peripheral.
T::reset_peripheral();
T::enable_peripheral(); T::enable_peripheral();
// Set RESET bit to 1 // Set RESET bit to 1
@ -743,8 +747,6 @@ where
.mode() .mode()
.write(|w| w.reset_mode().set_bit()); .write(|w| w.reset_mode().set_bit());
// Set up the GPIO pins.
crate::into_ref!(tx_pin, rx_pin);
if no_transceiver { if no_transceiver {
tx_pin.set_to_open_drain_output(crate::private::Internal); tx_pin.set_to_open_drain_output(crate::private::Internal);
} }
@ -1285,6 +1287,8 @@ pub trait Instance: crate::private::Sealed {
fn enable_peripheral(); fn enable_peripheral();
fn reset_peripheral();
fn enable_interrupts(); fn enable_interrupts();
} }
@ -1475,6 +1479,10 @@ impl Instance for crate::peripherals::TWAI0 {
unsafe { &*crate::peripherals::TWAI0::PTR } unsafe { &*crate::peripherals::TWAI0::PTR }
} }
fn reset_peripheral() {
PeripheralClockControl::reset(crate::system::Peripheral::Twai0);
}
fn enable_peripheral() { fn enable_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai0); PeripheralClockControl::enable(crate::system::Peripheral::Twai0);
} }
@ -1519,6 +1527,10 @@ impl Instance for crate::peripherals::TWAI0 {
unsafe { &*crate::peripherals::TWAI0::PTR } unsafe { &*crate::peripherals::TWAI0::PTR }
} }
fn reset_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai0);
}
fn enable_peripheral() { fn enable_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai0); PeripheralClockControl::enable(crate::system::Peripheral::Twai0);
} }
@ -1563,6 +1575,10 @@ impl Instance for crate::peripherals::TWAI1 {
unsafe { &*crate::peripherals::TWAI1::PTR } unsafe { &*crate::peripherals::TWAI1::PTR }
} }
fn reset_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai1);
}
fn enable_peripheral() { fn enable_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai1); PeripheralClockControl::enable(crate::system::Peripheral::Twai1);
} }

View File

@ -273,6 +273,7 @@ where
M: Mode, M: Mode,
{ {
fn new_inner(_usb_device: impl Peripheral<P = USB_DEVICE> + 'd) -> Self { fn new_inner(_usb_device: impl Peripheral<P = USB_DEVICE> + 'd) -> Self {
PeripheralClockControl::reset(crate::system::Peripheral::UsbDevice);
PeripheralClockControl::enable(crate::system::Peripheral::UsbDevice); PeripheralClockControl::enable(crate::system::Peripheral::UsbDevice);
USB_DEVICE::disable_tx_interrupts(); USB_DEVICE::disable_tx_interrupts();