Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Reset peripherals on driver contruction (where missing)
  • Loading branch information
JurajSadel committed Aug 13, 2024
commit 588451a42a5cc1a6fba8c83d499c26c5b74be76f
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: 2 additions & 0 deletions esp-hal/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ impl<'d> Hmac<'d> {
pub fn new(hmac: impl Peripheral<P = HMAC> + 'd) -> Self {
crate::into_ref!(hmac);

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

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() {
crate::system::PeripheralClockControl::reset(crate::system::Peripheral::Timg0)
}

#[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
20 changes: 18 additions & 2 deletions esp-hal/src/twai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,16 +735,18 @@ where
no_transceiver: bool,
mode: TwaiMode,
) -> Self {
// Set up the GPIO pins.
crate::into_ref!(tx_pin, rx_pin);

// Enable the peripheral clock for the TWAI peripheral.
T::reset_peripheral();
T::enable_peripheral();

// Set RESET bit to 1
T::register_block()
.mode()
.write(|w| w.reset_mode().set_bit());

// Set up the GPIO pins.
crate::into_ref!(tx_pin, rx_pin);
if no_transceiver {
tx_pin.set_to_open_drain_output(crate::private::Internal);
}
Expand Down Expand Up @@ -1285,6 +1287,8 @@ pub trait Instance: crate::private::Sealed {

fn enable_peripheral();

fn reset_peripheral();

fn enable_interrupts();
}

Expand Down Expand Up @@ -1475,6 +1479,10 @@ impl Instance for crate::peripherals::TWAI0 {
unsafe { &*crate::peripherals::TWAI0::PTR }
}

fn reset_peripheral() {
PeripheralClockControl::reset(crate::system::Peripheral::Twai0);
}

fn enable_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai0);
}
Expand Down Expand Up @@ -1519,6 +1527,10 @@ impl Instance for crate::peripherals::TWAI0 {
unsafe { &*crate::peripherals::TWAI0::PTR }
}

fn reset_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai0);
}

fn enable_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai0);
}
Expand Down Expand Up @@ -1563,6 +1575,10 @@ impl Instance for crate::peripherals::TWAI1 {
unsafe { &*crate::peripherals::TWAI1::PTR }
}

fn reset_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai1);
}

fn enable_peripheral() {
PeripheralClockControl::enable(crate::system::Peripheral::Twai1);
}
Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/usb_serial_jtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ where
M: Mode,
{
fn new_inner(_usb_device: impl Peripheral<P = USB_DEVICE> + 'd) -> Self {
PeripheralClockControl::reset(crate::system::Peripheral::UsbDevice);
PeripheralClockControl::enable(crate::system::Peripheral::UsbDevice);

USB_DEVICE::disable_tx_interrupts();
Expand Down