Skip to content

Commit

Permalink
Reset peripherals on driver construction (where missing) (#1893)
Browse files Browse the repository at this point in the history
* Reset peripherals on driver contruction (where missing)

* Don't enable and reset SHA in HMAC ctor

* changelog

* Don't reset the TIMG0
  • Loading branch information
JurajSadel authored Aug 13, 2024
1 parent a3a3ef1 commit e8a0811
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 3 deletions.
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Allow configuration of period updating method for MCPWM timers (#1898)
- Add self-testing mode for TWAI peripheral. (#1929)
- Added a `PeripheralClockControl::reset` to the driver constructors where missing (#1893)

### Changed

Expand All @@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- 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)
- Peripherals (where possible) are now explicitly reset and enabled in their constructors (#1893)

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions esp-hal/src/aes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ impl<'d> Aes<'d> {
/// Constructs a new `Aes` instance.
pub fn new(aes: impl Peripheral<P = AES> + 'd) -> Self {
crate::into_ref!(aes);

crate::system::PeripheralClockControl::reset(crate::system::Peripheral::Aes);
crate::system::PeripheralClockControl::enable(crate::system::Peripheral::Aes);

let mut ret = Self {
aes,
alignment_helper: AlignmentHelper::native_endianess(),
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/analog/adc/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ where
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Self {
PeripheralClockControl::reset(Peripheral::ApbSarAdc);
PeripheralClockControl::enable(Peripheral::ApbSarAdc);

unsafe { &*APB_SARADC::PTR }.ctrl().modify(|_, w| unsafe {
Expand Down
4 changes: 4 additions & 0 deletions esp-hal/src/analog/adc/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::efuse::Efuse;
use crate::{
peripheral::PeripheralRef,
peripherals::{APB_SARADC, SENS},
system::{Peripheral, PeripheralClockControl},
};

mod calibration;
Expand Down Expand Up @@ -400,6 +401,9 @@ where
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Self {
PeripheralClockControl::reset(Peripheral::ApbSarAdc);
PeripheralClockControl::enable(Peripheral::ApbSarAdc);

let sensors = unsafe { &*SENS::ptr() };

// Set attenuation for pins
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl<'d> Ecc<'d, crate::Blocking> {
pub fn new(ecc: impl Peripheral<P = ECC> + 'd) -> Self {
crate::into_ref!(ecc);

PeripheralClockControl::reset(PeripheralEnable::Ecc);
PeripheralClockControl::enable(PeripheralEnable::Ecc);

Self {
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/etm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ macro_rules! create_etm {
pub fn new(peripheral: impl Peripheral<P = crate::peripherals::SOC_ETM> + 'd) -> Self {
crate::into_ref!(peripheral);

PeripheralClockControl::reset(crate::system::Peripheral::Etm);
PeripheralClockControl::enable(crate::system::Peripheral::Etm);

Self {
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'d> Hmac<'d> {
pub fn new(hmac: impl Peripheral<P = HMAC> + 'd) -> Self {
crate::into_ref!(hmac);

PeripheralClockControl::enable(PeripheralEnable::Sha);
PeripheralClockControl::reset(PeripheralEnable::Hmac);
PeripheralClockControl::enable(PeripheralEnable::Hmac);

Self {
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/i2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ where
// the targets the same and force same configuration for both, TX and RX

channel.tx.init_channel();
PeripheralClockControl::reset(I::get_peripheral());
PeripheralClockControl::enable(I::get_peripheral());
I::set_clock(calculate_clock(
sample_rate,
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/lcd_cam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl<'d> LcdCam<'d, crate::Blocking> {
pub fn new(lcd_cam: impl Peripheral<P = LCD_CAM> + 'd) -> Self {
crate::into_ref!(lcd_cam);

PeripheralClockControl::reset(system::Peripheral::LcdCam);
PeripheralClockControl::enable(system::Peripheral::LcdCam);

Self {
Expand Down
2 changes: 2 additions & 0 deletions esp-hal/src/ledc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ impl<'d> Ledc<'d> {
clock_control_config: &'d Clocks<'d>,
) -> Self {
crate::into_ref!(_instance);

PeripheralClockControl::reset(PeripheralEnable::Ledc);
PeripheralClockControl::enable(PeripheralEnable::Ledc);

let ledc = unsafe { &*crate::peripherals::LEDC::ptr() };
Expand Down
11 changes: 11 additions & 0 deletions esp-hal/src/mcpwm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl<'d, PWM: PwmPeripheral> McPwm<'d, PWM> {
) -> Self {
crate::into_ref!(peripheral);

PWM::reset();
PWM::enable();

#[cfg(not(esp32c6))]
Expand Down Expand Up @@ -312,6 +313,8 @@ pub struct FrequencyError;
pub trait PwmPeripheral: Deref<Target = RegisterBlock> + crate::private::Sealed {
/// Enable peripheral
fn enable();
/// Reset peripheral
fn reset();
/// Get a pointer to the peripheral RegisterBlock
fn block() -> *const RegisterBlock;
/// Get operator GPIO mux output signal
Expand All @@ -324,6 +327,10 @@ impl PwmPeripheral for crate::peripherals::MCPWM0 {
PeripheralClockControl::enable(PeripheralEnable::Mcpwm0)
}

fn reset() {
PeripheralClockControl::reset(PeripheralEnable::Mcpwm0)
}

fn block() -> *const RegisterBlock {
Self::PTR
}
Expand All @@ -347,6 +354,10 @@ impl PwmPeripheral for crate::peripherals::MCPWM1 {
PeripheralClockControl::enable(PeripheralEnable::Mcpwm1)
}

fn reset() {
PeripheralClockControl::reset(PeripheralEnable::Mcpwm1)
}

fn block() -> *const RegisterBlock {
Self::PTR
}
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/otg_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl<'d> Usb<'d> {
P: UsbDp + Send + Sync,
M: UsbDm + Send + Sync,
{
PeripheralClockControl::reset(PeripheralEnable::Usb);
PeripheralClockControl::enable(PeripheralEnable::Usb);

Self {
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/parl_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,7 @@ where
return Err(Error::UnreachableClockRate);
}

PeripheralClockControl::reset(crate::system::Peripheral::ParlIo);
PeripheralClockControl::enable(crate::system::Peripheral::ParlIo);

let pcr = unsafe { &*crate::peripherals::PCR::PTR };
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/pcnt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl<'d> Pcnt<'d> {
/// Return a new PCNT
pub fn new(_instance: impl Peripheral<P = peripherals::PCNT> + 'd) -> Self {
crate::into_ref!(_instance);

// Enable the PCNT peripherals clock in the system peripheral
PeripheralClockControl::reset(crate::system::Peripheral::Pcnt);
PeripheralClockControl::enable(crate::system::Peripheral::Pcnt);
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ where
return Err(Error::UnreachableTargetFrequency);
}

PeripheralClockControl::reset(crate::system::Peripheral::Rmt);
PeripheralClockControl::enable(crate::system::Peripheral::Rmt);

#[cfg(not(any(esp32, esp32s2)))]
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/rsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl<'d, DM: crate::Mode> Rsa<'d, DM> {
fn new_internal(rsa: impl Peripheral<P = RSA> + 'd) -> Self {
crate::into_ref!(rsa);

PeripheralClockControl::reset(PeripheralEnable::Rsa);
PeripheralClockControl::enable(PeripheralEnable::Rsa);

Self {
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl<'d> Sha<'d, crate::Blocking> {
pub fn new(sha: impl Peripheral<P = SHA> + 'd, mode: ShaMode) -> Self {
crate::into_ref!(sha);

PeripheralClockControl::reset(crate::system::Peripheral::Sha);
PeripheralClockControl::enable(crate::system::Peripheral::Sha);

// Setup SHA Mode
Expand Down
29 changes: 29 additions & 0 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ where
mode: SpiMode,
clocks: &Clocks<'d>,
) -> Spi<'d, T, FullDuplexMode> {
spi.reset_peripheral();
spi.enable_peripheral();

let mut spi = Spi {
Expand Down Expand Up @@ -721,6 +722,7 @@ where
mode: SpiMode,
clocks: &Clocks<'d>,
) -> Spi<'d, T, HalfDuplexMode> {
spi.reset_peripheral();
spi.enable_peripheral();

let mut spi = Spi {
Expand Down Expand Up @@ -2295,6 +2297,8 @@ pub trait Instance: private::Sealed {

fn enable_peripheral(&self);

fn reset_peripheral(&self);

fn spi_num(&self) -> u8;

/// Initialize for full-duplex 1 bit mode
Expand Down Expand Up @@ -3255,6 +3259,11 @@ impl Instance for crate::peripherals::SPI2 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi2);
}

#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi2);
}

#[inline(always)]
fn spi_num(&self) -> u8 {
2
Expand Down Expand Up @@ -3332,6 +3341,11 @@ impl Instance for crate::peripherals::SPI2 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi2);
}

#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi2);
}

#[inline(always)]
fn spi_num(&self) -> u8 {
2
Expand Down Expand Up @@ -3403,6 +3417,11 @@ impl Instance for crate::peripherals::SPI3 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi3)
}

#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi3)
}

#[inline(always)]
fn spi_num(&self) -> u8 {
3
Expand Down Expand Up @@ -3447,6 +3466,11 @@ impl Instance for crate::peripherals::SPI2 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi2)
}

#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi2)
}

#[inline(always)]
fn spi_num(&self) -> u8 {
2
Expand Down Expand Up @@ -3524,6 +3548,11 @@ impl Instance for crate::peripherals::SPI3 {
PeripheralClockControl::enable(crate::system::Peripheral::Spi3)
}

#[inline(always)]
fn reset_peripheral(&self) {
PeripheralClockControl::reset(crate::system::Peripheral::Spi3)
}

#[inline(always)]
fn spi_num(&self) -> u8 {
3
Expand Down
26 changes: 26 additions & 0 deletions esp-hal/src/timer/timg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub trait TimerGroupInstance {
fn id() -> u8;
fn register_block() -> *const RegisterBlock;
fn configure_src_clk();
fn enable_peripheral();
fn reset_peripheral();
fn configure_wdt_src_clk();
}

Expand Down Expand Up @@ -144,6 +146,14 @@ impl TimerGroupInstance for TIMG0 {
// 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)]
#[cfg(any(esp32c2, esp32c3))]
fn configure_wdt_src_clk() {
Expand Down Expand Up @@ -206,6 +216,16 @@ impl TimerGroupInstance for TIMG1 {
// 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)]
#[cfg(any(esp32c6, esp32h2))]
fn configure_wdt_src_clk() {
Expand All @@ -230,6 +250,9 @@ where
pub fn new(_timer_group: impl Peripheral<P = T> + 'd, clocks: &Clocks<'d>) -> Self {
crate::into_ref!(_timer_group);

T::reset_peripheral();
T::enable_peripheral();

T::configure_src_clk();

// ESP32-H2 is using PLL_48M_CLK source instead of APB_CLK
Expand Down Expand Up @@ -269,6 +292,9 @@ where
pub fn new_async(_timer_group: impl Peripheral<P = T> + 'd, clocks: &Clocks<'d>) -> Self {
crate::into_ref!(_timer_group);

T::reset_peripheral();
T::enable_peripheral();

T::configure_src_clk();

// ESP32-H2 is using PLL_48M_CLK source instead of APB_CLK
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ where
pub fn new(peripheral: impl Peripheral<P = T> + 'd) -> Self {
crate::into_ref!(peripheral);

PeripheralClockControl::reset(crate::system::Peripheral::Trace0);
PeripheralClockControl::enable(crate::system::Peripheral::Trace0);

Self {
Expand Down
Loading

0 comments on commit e8a0811

Please sign in to comment.