From beddf0ca1254dd2096ea4ceac8063fed06e51352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Quentin?= Date: Mon, 18 Nov 2024 10:50:46 +0100 Subject: [PATCH] Use `GenericPeripheralGuard` --- esp-hal/src/aes/mod.rs | 13 ++---- esp-hal/src/analog/adc/riscv.rs | 14 ++---- esp-hal/src/analog/adc/xtensa.rs | 15 ++----- esp-hal/src/ecc.rs | 14 ++---- esp-hal/src/etm.rs | 12 +++-- esp-hal/src/hmac.rs | 14 ++---- esp-hal/src/lcd_cam/cam.rs | 6 +-- esp-hal/src/lcd_cam/lcd/mod.rs | 9 +++- esp-hal/src/lcd_cam/mod.rs | 6 +-- esp-hal/src/otg_fs.rs | 14 ++---- esp-hal/src/parl_io.rs | 36 +++++++-------- esp-hal/src/pcnt/channel.rs | 6 +-- esp-hal/src/pcnt/mod.rs | 6 +-- esp-hal/src/pcnt/unit.rs | 6 +-- esp-hal/src/rmt.rs | 76 ++++++++++++++++---------------- esp-hal/src/rsa/mod.rs | 6 +-- esp-hal/src/sha.rs | 16 ++----- esp-hal/src/system.rs | 33 ++++++++++++++ esp-hal/src/trace.rs | 18 ++------ 19 files changed, 148 insertions(+), 172 deletions(-) diff --git a/esp-hal/src/aes/mod.rs b/esp-hal/src/aes/mod.rs index f64b4acb769..5112ebbd2c1 100644 --- a/esp-hal/src/aes/mod.rs +++ b/esp-hal/src/aes/mod.rs @@ -59,6 +59,7 @@ use crate::{ peripheral::{Peripheral, PeripheralRef}, peripherals::AES, reg_access::{AlignmentHelper, NativeEndianess}, + system::GenericPeripheralGuard, }; #[cfg_attr(esp32, path = "esp32.rs")] @@ -136,6 +137,7 @@ pub enum Mode { pub struct Aes<'d> { aes: PeripheralRef<'d, AES>, alignment_helper: AlignmentHelper, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Aes as u8 }>, } impl<'d> Aes<'d> { @@ -143,13 +145,12 @@ impl<'d> Aes<'d> { pub fn new(aes: impl Peripheral

+ 'd) -> Self { crate::into_ref!(aes); - if crate::system::PeripheralClockControl::enable(crate::system::Peripheral::Aes, true) { - crate::system::PeripheralClockControl::reset(crate::system::Peripheral::Aes); - } + let guard = GenericPeripheralGuard::new(); let mut ret = Self { aes, alignment_helper: AlignmentHelper::native_endianess(), + _guard: guard, }; ret.init(); @@ -191,12 +192,6 @@ impl<'d> Aes<'d> { } } -impl Drop for Aes<'_> { - fn drop(&mut self) { - crate::system::PeripheralClockControl::enable(crate::system::Peripheral::Aes, false); - } -} - /// Specifications for AES flavours pub trait AesFlavour: crate::private::Sealed { /// Type of the AES key, a fixed-size array of bytes diff --git a/esp-hal/src/analog/adc/riscv.rs b/esp-hal/src/analog/adc/riscv.rs index 63b592857e6..c228502e4e2 100644 --- a/esp-hal/src/analog/adc/riscv.rs +++ b/esp-hal/src/analog/adc/riscv.rs @@ -8,7 +8,7 @@ use crate::efuse::Efuse; use crate::{ peripheral::PeripheralRef, peripherals::APB_SARADC, - system::{Peripheral, PeripheralClockControl}, + system::{GenericPeripheralGuard, Peripheral}, }; mod calibration; @@ -396,6 +396,7 @@ pub struct Adc<'d, ADCI> { _adc: PeripheralRef<'d, ADCI>, attenuations: [Option; NUM_ATTENS], active_channel: Option, + _guard: GenericPeripheralGuard<{ Peripheral::ApbSarAdc as u8 }>, } impl<'d, ADCI> Adc<'d, ADCI> @@ -408,9 +409,7 @@ where adc_instance: impl crate::peripheral::Peripheral

+ 'd, config: AdcConfig, ) -> Self { - if PeripheralClockControl::enable(Peripheral::ApbSarAdc, true) { - PeripheralClockControl::reset(Peripheral::ApbSarAdc); - } + let guard = GenericPeripheralGuard::new(); unsafe { &*APB_SARADC::PTR }.ctrl().modify(|_, w| unsafe { w.start_force().set_bit(); @@ -423,6 +422,7 @@ where _adc: adc_instance.into_ref(), attenuations: config.attenuations, active_channel: None, + _guard: guard, } } @@ -501,12 +501,6 @@ where } } -impl Drop for Adc<'_, ADCI> { - fn drop(&mut self) { - PeripheralClockControl::enable(Peripheral::ApbSarAdc, false); - } -} - #[cfg(any(esp32c2, esp32c3, esp32c6))] impl super::AdcCalEfuse for crate::peripherals::ADC1 { fn init_code(atten: Attenuation) -> Option { diff --git a/esp-hal/src/analog/adc/xtensa.rs b/esp-hal/src/analog/adc/xtensa.rs index b60a76c0f6d..c8f547dce5a 100644 --- a/esp-hal/src/analog/adc/xtensa.rs +++ b/esp-hal/src/analog/adc/xtensa.rs @@ -6,7 +6,7 @@ use crate::efuse::Efuse; use crate::{ peripheral::PeripheralRef, peripherals::{APB_SARADC, SENS}, - system::{Peripheral, PeripheralClockControl}, + system::{GenericPeripheralGuard, Peripheral}, }; mod calibration; @@ -389,6 +389,7 @@ pub struct Adc<'d, ADC> { _adc: PeripheralRef<'d, ADC>, active_channel: Option, last_init_code: u16, + _guard: GenericPeripheralGuard<{ Peripheral::ApbSarAdc as u8 }>, } impl<'d, ADCI> Adc<'d, ADCI> @@ -401,10 +402,7 @@ where adc_instance: impl crate::peripheral::Peripheral

+ 'd, config: AdcConfig, ) -> Self { - if PeripheralClockControl::enable(Peripheral::ApbSarAdc, true) { - PeripheralClockControl::reset(Peripheral::ApbSarAdc); - } - + let guard = GenericPeripheralGuard::new(); let sensors = unsafe { &*SENS::ptr() }; // Set attenuation for pins @@ -473,6 +471,7 @@ where _adc: adc_instance.into_ref(), active_channel: None, last_init_code: 0, + _guard: guard, } } @@ -561,12 +560,6 @@ where } } -impl Drop for Adc<'_, ADCI> { - fn drop(&mut self) { - PeripheralClockControl::enable(Peripheral::ApbSarAdc, false); - } -} - #[cfg(esp32s3)] impl super::AdcCalEfuse for crate::peripherals::ADC1 { fn init_code(atten: Attenuation) -> Option { diff --git a/esp-hal/src/ecc.rs b/esp-hal/src/ecc.rs index 74a31ff671c..6130d986891 100644 --- a/esp-hal/src/ecc.rs +++ b/esp-hal/src/ecc.rs @@ -32,7 +32,7 @@ use crate::{ peripheral::{Peripheral, PeripheralRef}, peripherals::{Interrupt, ECC}, reg_access::{AlignmentHelper, SocDependentEndianess}, - system::{Peripheral as PeripheralEnable, PeripheralClockControl}, + system::{self, GenericPeripheralGuard}, InterruptConfigurable, }; @@ -41,6 +41,7 @@ pub struct Ecc<'d, DM: crate::Mode> { ecc: PeripheralRef<'d, ECC>, alignment_helper: AlignmentHelper, phantom: PhantomData, + _guard: GenericPeripheralGuard<{ system::Peripheral::Ecc as u8 }>, } /// ECC interface error @@ -102,14 +103,13 @@ impl<'d> Ecc<'d, crate::Blocking> { pub fn new(ecc: impl Peripheral

+ 'd) -> Self { crate::into_ref!(ecc); - if PeripheralClockControl::enable(PeripheralEnable::Ecc, true) { - PeripheralClockControl::reset(PeripheralEnable::Ecc); - } + let guard = GenericPeripheralGuard::new(); Self { ecc, alignment_helper: AlignmentHelper::default(), phantom: PhantomData, + _guard: guard, } } } @@ -965,9 +965,3 @@ impl Ecc<'_, DM> { } } } - -impl Drop for Ecc<'_, DM> { - fn drop(&mut self) { - PeripheralClockControl::enable(PeripheralEnable::Ecc, false); - } -} diff --git a/esp-hal/src/etm.rs b/esp-hal/src/etm.rs index a2590cdeab7..b39d865730a 100644 --- a/esp-hal/src/etm.rs +++ b/esp-hal/src/etm.rs @@ -59,7 +59,7 @@ use crate::{ peripheral::{Peripheral, PeripheralRef}, - system::PeripheralClockControl, + system::GenericPeripheralGuard, }; /// Unconfigured EtmChannel. @@ -75,11 +75,8 @@ impl EtmChannel { E: EtmEvent, T: EtmTask, { - if PeripheralClockControl::enable(crate::system::Peripheral::Etm, true) { - PeripheralClockControl::reset(crate::system::Peripheral::Etm); - } - let etm = unsafe { crate::peripherals::SOC_ETM::steal() }; + let guard = GenericPeripheralGuard::new(); etm.ch(C as usize) .evt_id() @@ -96,6 +93,7 @@ impl EtmChannel { EtmConfiguredChannel { _event: event, _task: task, + _guard: guard, } } } @@ -121,6 +119,7 @@ where { _event: &'a E, _task: &'a T, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Etm as u8 }>, } impl Drop for EtmConfiguredChannel<'_, E, T, C> @@ -131,7 +130,6 @@ where fn drop(&mut self) { debug!("Drop ETM channel {}", C); disable_channel(C); - PeripheralClockControl::enable(crate::system::Peripheral::Etm, false); } } @@ -156,7 +154,7 @@ macro_rules! create_etm { Self { _peripheral: peripheral, - $([< channel $num >]: EtmChannel {},)+ + $([< channel $num >]: EtmChannel { },)+ } } } diff --git a/esp-hal/src/hmac.rs b/esp-hal/src/hmac.rs index 5946220a904..67b5e262074 100644 --- a/esp-hal/src/hmac.rs +++ b/esp-hal/src/hmac.rs @@ -40,7 +40,7 @@ use crate::{ peripheral::{Peripheral, PeripheralRef}, peripherals::HMAC, reg_access::{AlignmentHelper, SocDependentEndianess}, - system::{Peripheral as PeripheralEnable, PeripheralClockControl}, + system::{GenericPeripheralGuard, Peripheral as PeripheralEnable}, }; /// Provides an interface for interacting with the HMAC hardware peripheral. @@ -51,6 +51,7 @@ pub struct Hmac<'d> { alignment_helper: AlignmentHelper, byte_written: usize, next_command: NextCommand, + _guard: GenericPeripheralGuard<{ PeripheralEnable::Hmac as u8 }>, } /// HMAC interface error @@ -107,15 +108,14 @@ impl<'d> Hmac<'d> { pub fn new(hmac: impl Peripheral

+ 'd) -> Self { crate::into_ref!(hmac); - if PeripheralClockControl::enable(PeripheralEnable::Hmac, true) { - PeripheralClockControl::reset(PeripheralEnable::Hmac); - } + let guard = GenericPeripheralGuard::new(); Self { hmac, alignment_helper: AlignmentHelper::default(), byte_written: 64, next_command: NextCommand::None, + _guard: guard, } } @@ -346,9 +346,3 @@ impl<'d> Hmac<'d> { while self.is_busy() {} } } - -impl Drop for Hmac<'_> { - fn drop(&mut self) { - PeripheralClockControl::enable(PeripheralEnable::Hmac, false); - } -} diff --git a/esp-hal/src/lcd_cam/cam.rs b/esp-hal/src/lcd_cam/cam.rs index a91dcdc95f8..e22ab286864 100644 --- a/esp-hal/src/lcd_cam/cam.rs +++ b/esp-hal/src/lcd_cam/cam.rs @@ -82,7 +82,7 @@ use crate::{ lcd_cam::{calculate_clkm, BitOrder, ByteOrder}, peripheral::{Peripheral, PeripheralRef}, peripherals::LCD_CAM, - system::PeripheralGuard, + system::{self, GenericPeripheralGuard}, Blocking, }; @@ -122,14 +122,14 @@ pub enum VsyncFilterThreshold { pub struct Cam<'d> { /// The LCD_CAM peripheral reference for managing the camera functionality. pub(crate) lcd_cam: PeripheralRef<'d, LCD_CAM>, - pub(super) _guard: PeripheralGuard, + pub(super) _guard: GenericPeripheralGuard<{ system::Peripheral::LcdCam as u8 }>, } /// Represents the camera interface with DMA support. pub struct Camera<'d> { lcd_cam: PeripheralRef<'d, LCD_CAM>, rx_channel: ChannelRx<'d, Blocking, ::Dma>, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ system::Peripheral::LcdCam as u8 }>, } impl<'d> Camera<'d> { diff --git a/esp-hal/src/lcd_cam/lcd/mod.rs b/esp-hal/src/lcd_cam/lcd/mod.rs index f780f855385..66bd82a65c8 100644 --- a/esp-hal/src/lcd_cam/lcd/mod.rs +++ b/esp-hal/src/lcd_cam/lcd/mod.rs @@ -10,7 +10,12 @@ //! ## Implementation State //! - RGB is not supported yet -use crate::{peripheral::PeripheralRef, peripherals::LCD_CAM, system::PeripheralGuard}; +use super::GenericPeripheralGuard; +use crate::{ + peripheral::PeripheralRef, + peripherals::LCD_CAM, + system::{self}, +}; pub mod dpi; pub mod i8080; @@ -23,7 +28,7 @@ pub struct Lcd<'d, DM: crate::Mode> { /// A marker for the mode of operation (blocking or asynchronous). pub(crate) _mode: core::marker::PhantomData, - pub(super) _guard: PeripheralGuard, + pub(super) _guard: GenericPeripheralGuard<{ system::Peripheral::LcdCam as u8 }>, } #[derive(Debug, Clone, Copy, PartialEq, Default)] diff --git a/esp-hal/src/lcd_cam/mod.rs b/esp-hal/src/lcd_cam/mod.rs index 6593783c42b..7e22c22992f 100644 --- a/esp-hal/src/lcd_cam/mod.rs +++ b/esp-hal/src/lcd_cam/mod.rs @@ -18,7 +18,7 @@ use crate::{ macros::handler, peripheral::Peripheral, peripherals::{Interrupt, LCD_CAM}, - system::{self, PeripheralGuard}, + system::GenericPeripheralGuard, Async, Blocking, Cpu, @@ -38,8 +38,8 @@ impl<'d> LcdCam<'d, Blocking> { pub fn new(lcd_cam: impl Peripheral

+ 'd) -> Self { crate::into_ref!(lcd_cam); - let lcd_guard = PeripheralGuard::new(system::Peripheral::LcdCam); - let cam_guard = PeripheralGuard::new(system::Peripheral::LcdCam); + let lcd_guard = GenericPeripheralGuard::new(); + let cam_guard = GenericPeripheralGuard::new(); Self { lcd: Lcd { diff --git a/esp-hal/src/otg_fs.rs b/esp-hal/src/otg_fs.rs index dc8a91b0e27..6b680b50208 100644 --- a/esp-hal/src/otg_fs.rs +++ b/esp-hal/src/otg_fs.rs @@ -43,7 +43,7 @@ use crate::{ gpio::InputSignal, peripheral::{Peripheral, PeripheralRef}, peripherals, - system::{Peripheral as PeripheralEnable, PeripheralClockControl}, + system::{GenericPeripheralGuard, Peripheral as PeripheralEnable}, }; #[doc(hidden)] @@ -57,6 +57,7 @@ pub trait UsbDm: crate::private::Sealed {} /// USB peripheral. pub struct Usb<'d> { _usb0: PeripheralRef<'d, peripherals::USB0>, + _guard: GenericPeripheralGuard<{ PeripheralEnable::Usb as u8 }>, } impl<'d> Usb<'d> { @@ -70,12 +71,11 @@ impl<'d> Usb<'d> { P: UsbDp + Send + Sync, M: UsbDm + Send + Sync, { - if PeripheralClockControl::enable(PeripheralEnable::Usb, true) { - PeripheralClockControl::reset(PeripheralEnable::Usb); - } + let guard = GenericPeripheralGuard::new(); Self { _usb0: usb0.into_ref(), + _guard: guard, } } @@ -111,12 +111,6 @@ impl<'d> Usb<'d> { } } -impl Drop for Usb<'_> { - fn drop(&mut self) { - PeripheralClockControl::enable(PeripheralEnable::Usb, false); - } -} - unsafe impl<'d> Sync for Usb<'d> {} unsafe impl<'d> UsbPeripheral for Usb<'d> { diff --git a/esp-hal/src/parl_io.rs b/esp-hal/src/parl_io.rs index a67e6ba19f5..e2badab6131 100644 --- a/esp-hal/src/parl_io.rs +++ b/esp-hal/src/parl_io.rs @@ -54,7 +54,7 @@ use crate::{ interrupt::InterruptHandler, peripheral::{self, Peripheral}, peripherals::{self, Interrupt, PARL_IO}, - system::PeripheralGuard, + system::{self, GenericPeripheralGuard}, Async, Blocking, InterruptConfigurable, @@ -757,8 +757,6 @@ where P: FullDuplex + TxPins + ConfigurePins, CP: TxClkPin, { - let guard = PeripheralGuard::new(crate::system::Peripheral::ParlIo); - tx_pins.configure()?; clk_pin.configure(); @@ -769,7 +767,7 @@ where Ok(ParlIoTx { tx_channel: self.tx_channel, tx_chain: DescriptorChain::new(self.descriptors), - _guard: guard, + _guard: self._guard, }) } } @@ -791,8 +789,6 @@ where P: TxPins + ConfigurePins, CP: TxClkPin, { - let guard = PeripheralGuard::new(crate::system::Peripheral::ParlIo); - tx_pins.configure()?; clk_pin.configure(); @@ -803,7 +799,7 @@ where Ok(ParlIoTx { tx_channel: self.tx_channel, tx_chain: DescriptorChain::new(self.descriptors), - _guard: guard, + _guard: self._guard, }) } } @@ -815,7 +811,7 @@ where { tx_channel: ChannelTx<'d, DM, ::Dma>, tx_chain: DescriptorChain, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::ParlIo as u8 }>, } impl core::fmt::Debug for ParlIoTx<'_, DM> @@ -843,7 +839,7 @@ where P: FullDuplex + RxPins + ConfigurePins, CP: RxClkPin, { - let guard = PeripheralGuard::new(crate::system::Peripheral::ParlIo); + let guard = GenericPeripheralGuard::new(); rx_pins.configure()?; clk_pin.configure(); @@ -875,8 +871,6 @@ where P: RxPins + ConfigurePins, CP: RxClkPin, { - let guard = PeripheralGuard::new(crate::system::Peripheral::ParlIo); - rx_pins.configure()?; clk_pin.configure(); @@ -886,7 +880,7 @@ where Ok(ParlIoRx { rx_channel: self.rx_channel, rx_chain: DescriptorChain::new(self.descriptors), - _guard: guard, + _guard: self._guard, }) } } @@ -898,7 +892,7 @@ where { rx_channel: ChannelRx<'d, DM, ::Dma>, rx_chain: DescriptorChain, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::ParlIo as u8 }>, } impl core::fmt::Debug for ParlIoRx<'_, DM> @@ -1021,8 +1015,8 @@ impl<'d> ParlIoFullDuplex<'d, Blocking> { CH: DmaChannelConvert<::Dma>, Channel<'d, Blocking, CH>: From>, { - let tx_guard = PeripheralGuard::new(crate::system::Peripheral::ParlIo); - let rx_guard = PeripheralGuard::new(crate::system::Peripheral::ParlIo); + let tx_guard = GenericPeripheralGuard::new(); + let rx_guard = GenericPeripheralGuard::new(); let dma_channel = Channel::::from(dma_channel); internal_init(frequency)?; @@ -1136,7 +1130,7 @@ where where CH: DmaChannelConvert<::Dma>, { - let guard = PeripheralGuard::new(crate::system::Peripheral::ParlIo); + let guard = GenericPeripheralGuard::new(); internal_init(frequency)?; Ok(Self { @@ -1212,7 +1206,7 @@ where where CH: DmaChannelConvert<::Dma>, { - let guard = PeripheralGuard::new(crate::system::Peripheral::ParlIo); + let guard = GenericPeripheralGuard::new(); internal_init(frequency)?; Ok(Self { @@ -1481,7 +1475,7 @@ where { tx_channel: ChannelTx<'d, DM, ::Dma>, descriptors: &'static mut [DmaDescriptor], - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ system::Peripheral::ParlIo as u8 }>, } /// Creates a RX channel @@ -1491,7 +1485,7 @@ where { rx_channel: ChannelRx<'d, DM, ::Dma>, descriptors: &'static mut [DmaDescriptor], - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ system::Peripheral::ParlIo as u8 }>, } /// Creates a TX channel @@ -1501,7 +1495,7 @@ where { tx_channel: ChannelTx<'d, DM, ::Dma>, descriptors: &'static mut [DmaDescriptor], - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ system::Peripheral::ParlIo as u8 }>, } /// Creates a RX channel @@ -1511,7 +1505,7 @@ where { rx_channel: ChannelRx<'d, DM, ::Dma>, descriptors: &'static mut [DmaDescriptor], - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ system::Peripheral::ParlIo as u8 }>, } #[doc(hidden)] diff --git a/esp-hal/src/pcnt/channel.rs b/esp-hal/src/pcnt/channel.rs index 9bc7bf1c442..677c792f6d0 100644 --- a/esp-hal/src/pcnt/channel.rs +++ b/esp-hal/src/pcnt/channel.rs @@ -13,7 +13,7 @@ pub use crate::peripherals::pcnt::unit::conf0::{CTRL_MODE as CtrlMode, EDGE_MODE use crate::{ gpio::{interconnect::PeripheralInput, InputSignal}, peripheral::Peripheral, - system::PeripheralGuard, + system::GenericPeripheralGuard, }; /// Represents a channel within a pulse counter unit. @@ -21,13 +21,13 @@ pub struct Channel<'d, const UNIT: usize, const NUM: usize> { _phantom: PhantomData<&'d ()>, // Individual channels are not Send, since they share registers. _not_send: PhantomData<*const ()>, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Pcnt as u8 }>, } impl Channel<'_, UNIT, NUM> { /// return a new Channel pub(super) fn new() -> Self { - let guard = PeripheralGuard::new(crate::system::Peripheral::Pcnt); + let guard = GenericPeripheralGuard::new(); Self { _phantom: PhantomData, diff --git a/esp-hal/src/pcnt/mod.rs b/esp-hal/src/pcnt/mod.rs index c5aad52e3e5..e0072619a6d 100644 --- a/esp-hal/src/pcnt/mod.rs +++ b/esp-hal/src/pcnt/mod.rs @@ -25,7 +25,7 @@ use crate::{ interrupt::{self, InterruptHandler}, peripheral::{Peripheral, PeripheralRef}, peripherals::{self, Interrupt}, - system::PeripheralGuard, + system::GenericPeripheralGuard, InterruptConfigurable, }; @@ -57,7 +57,7 @@ pub struct Pcnt<'d> { /// Unit 7 pub unit7: Unit<'d, 7>, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Pcnt as u8 }>, } impl<'d> Pcnt<'d> { @@ -65,7 +65,7 @@ impl<'d> Pcnt<'d> { pub fn new(_instance: impl Peripheral

+ 'd) -> Self { crate::into_ref!(_instance); - let guard = PeripheralGuard::new(crate::system::Peripheral::Pcnt); + let guard = GenericPeripheralGuard::new(); let pcnt = unsafe { &*crate::peripherals::PCNT::ptr() }; // disable filter, all events, and channel settings diff --git a/esp-hal/src/pcnt/unit.rs b/esp-hal/src/pcnt/unit.rs index 9a2aad15bef..ee3d3584a51 100644 --- a/esp-hal/src/pcnt/unit.rs +++ b/esp-hal/src/pcnt/unit.rs @@ -13,7 +13,7 @@ use core::marker::PhantomData; use critical_section::CriticalSection; -use crate::{pcnt::channel::Channel, system::PeripheralGuard}; +use crate::{pcnt::channel::Channel, system::GenericPeripheralGuard}; /// Invalid filter threshold value #[derive(Debug, Clone, Copy, PartialEq)] @@ -83,13 +83,13 @@ pub struct Unit<'d, const NUM: usize> { /// The second channel in PCNT unit. pub channel1: Channel<'d, NUM, 1>, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Pcnt as u8 }>, } impl Unit<'_, NUM> { /// return a new Unit pub(super) fn new() -> Self { - let guard = PeripheralGuard::new(crate::system::Peripheral::Pcnt); + let guard = GenericPeripheralGuard::new(); Self { counter: Counter::new(), diff --git a/esp-hal/src/rmt.rs b/esp-hal/src/rmt.rs index 6df8756a038..1cb83b368f8 100644 --- a/esp-hal/src/rmt.rs +++ b/esp-hal/src/rmt.rs @@ -94,7 +94,7 @@ use crate::{ peripheral::Peripheral, peripherals::Interrupt, soc::constants, - system::PeripheralGuard, + system::{self, GenericPeripheralGuard}, Async, Blocking, InterruptConfigurable, @@ -602,7 +602,7 @@ mod impl_for_chip { use crate::{ peripheral::{Peripheral, PeripheralRef}, - system::PeripheralGuard, + system::GenericPeripheralGuard, }; /// RMT Instance @@ -635,19 +635,19 @@ mod impl_for_chip { peripheral, channel0: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel1: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel2: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel3: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, phantom: PhantomData, } @@ -660,7 +660,7 @@ mod impl_for_chip { M: crate::Mode, { phantom: PhantomData, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Rmt as u8 }>, } impl_tx_channel_creator!(0); @@ -682,7 +682,7 @@ mod impl_for_chip { use crate::{ peripheral::{Peripheral, PeripheralRef}, - system::PeripheralGuard, + system::GenericPeripheralGuard, }; /// RMT Instance @@ -722,35 +722,35 @@ mod impl_for_chip { peripheral, channel0: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel1: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel2: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel3: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel4: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel5: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel6: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel7: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, phantom: PhantomData, } @@ -763,7 +763,7 @@ mod impl_for_chip { M: crate::Mode, { phantom: PhantomData, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Rmt as u8 }>, } impl_tx_channel_creator!(0); @@ -809,7 +809,7 @@ mod impl_for_chip { use crate::{ peripheral::{Peripheral, PeripheralRef}, - system::PeripheralGuard, + system::GenericPeripheralGuard, }; /// RMT Instance @@ -842,19 +842,19 @@ mod impl_for_chip { peripheral, channel0: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel1: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel2: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel3: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, phantom: PhantomData, } @@ -867,7 +867,7 @@ mod impl_for_chip { M: crate::Mode, { phantom: PhantomData, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Rmt as u8 }>, } impl_tx_channel_creator!(0); @@ -897,7 +897,7 @@ mod impl_for_chip { use crate::{ peripheral::{Peripheral, PeripheralRef}, - system::PeripheralGuard, + system::GenericPeripheralGuard, }; /// RMT Instance @@ -938,35 +938,35 @@ mod impl_for_chip { peripheral, channel0: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel1: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel2: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel3: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel4: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel5: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel6: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, channel7: ChannelCreator { phantom: PhantomData, - _guard: PeripheralGuard::new(crate::system::Peripheral::Rmt), + _guard: GenericPeripheralGuard::new(), }, phantom: PhantomData, } @@ -979,7 +979,7 @@ mod impl_for_chip { M: crate::Mode, { phantom: PhantomData, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Rmt as u8 }>, } impl_tx_channel_creator!(0); @@ -1011,7 +1011,7 @@ where M: crate::Mode, { phantom: PhantomData, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ system::Peripheral::Rmt as u8 }>, } /// Channel in TX mode @@ -1631,7 +1631,7 @@ mod chip_specific { const CHANNEL: u8 = $ch_num; fn new() -> Self { - let guard = PeripheralGuard::new(crate::system::Peripheral::Rmt); + let guard = GenericPeripheralGuard::new(); Self { phantom: core::marker::PhantomData, _guard: guard, @@ -1794,7 +1794,7 @@ mod chip_specific { const CHANNEL: u8 = $ch_num; fn new() -> Self { - let guard = PeripheralGuard::new(crate::system::Peripheral::Rmt); + let guard = GenericPeripheralGuard::new(); Self { phantom: core::marker::PhantomData, _guard: guard, @@ -1985,7 +1985,7 @@ mod chip_specific { const CHANNEL: u8 = $ch_num; fn new() -> Self { - let guard = PeripheralGuard::new(crate::system::Peripheral::Rmt); + let guard = GenericPeripheralGuard::new(); Self { phantom: core::marker::PhantomData, _guard: guard, @@ -2138,7 +2138,7 @@ mod chip_specific { const CHANNEL: u8 = $ch_num; fn new() -> Self { - let guard = PeripheralGuard::new(crate::system::Peripheral::Rmt); + let guard = GenericPeripheralGuard::new(); Self { phantom: core::marker::PhantomData, _guard: guard, diff --git a/esp-hal/src/rsa/mod.rs b/esp-hal/src/rsa/mod.rs index 77211cedfd8..6734589451b 100644 --- a/esp-hal/src/rsa/mod.rs +++ b/esp-hal/src/rsa/mod.rs @@ -27,7 +27,7 @@ use crate::{ interrupt::InterruptHandler, peripheral::{Peripheral, PeripheralRef}, peripherals::{Interrupt, RSA}, - system::{Peripheral as PeripheralEnable, PeripheralGuard}, + system::{GenericPeripheralGuard, Peripheral as PeripheralEnable}, Async, Blocking, Cpu, @@ -48,7 +48,7 @@ pub use rsa_spec_impl::operand_sizes; pub struct Rsa<'d, DM: crate::Mode> { rsa: PeripheralRef<'d, RSA>, phantom: PhantomData, - _guard: PeripheralGuard, + _guard: GenericPeripheralGuard<{ PeripheralEnable::Rsa as u8 }>, } impl<'d> Rsa<'d, Blocking> { @@ -98,7 +98,7 @@ impl<'d, DM: crate::Mode> Rsa<'d, DM> { fn new_internal(rsa: impl Peripheral

+ 'd) -> Self { crate::into_ref!(rsa); - let guard = PeripheralGuard::new(PeripheralEnable::Rsa); + let guard = GenericPeripheralGuard::new(); Self { rsa, diff --git a/esp-hal/src/sha.rs b/esp-hal/src/sha.rs index e5b333070f9..91526b09c4e 100644 --- a/esp-hal/src/sha.rs +++ b/esp-hal/src/sha.rs @@ -68,24 +68,22 @@ use crate::{ peripheral::{Peripheral, PeripheralRef}, peripherals::SHA, reg_access::{AlignmentHelper, SocDependentEndianess}, - system::PeripheralClockControl, + system::GenericPeripheralGuard, }; /// The SHA Accelerator driver instance pub struct Sha<'d> { sha: PeripheralRef<'d, SHA>, + _guard: GenericPeripheralGuard<{ crate::system::Peripheral::Sha as u8 }>, } impl<'d> Sha<'d> { /// Create a new instance of the SHA Accelerator driver. pub fn new(sha: impl Peripheral

+ 'd) -> Self { crate::into_ref!(sha); + let guard = GenericPeripheralGuard::new(); - if PeripheralClockControl::enable(crate::system::Peripheral::Sha, true) { - PeripheralClockControl::reset(crate::system::Peripheral::Sha); - } - - Self { sha } + Self { sha, _guard: guard } } /// Start a new digest. @@ -103,12 +101,6 @@ impl<'d> Sha<'d> { impl crate::private::Sealed for Sha<'_> {} -impl Drop for Sha<'_> { - fn drop(&mut self) { - PeripheralClockControl::enable(crate::system::Peripheral::Sha, false); - } -} - #[cfg(not(esp32))] impl crate::InterruptConfigurable for Sha<'_> { fn set_interrupt_handler(&mut self, handler: crate::interrupt::InterruptHandler) { diff --git a/esp-hal/src/system.rs b/esp-hal/src/system.rs index b3019ed50aa..1c677df4dcf 100755 --- a/esp-hal/src/system.rs +++ b/esp-hal/src/system.rs @@ -135,6 +135,16 @@ pub enum Peripheral { Systimer, } +impl Peripheral { + pub fn try_from(value: u8) -> Option { + if value >= Peripheral::COUNT as u8 { + return None; + } + + Some(unsafe { core::mem::transmute::(value) }) + } +} + static PERIPHERAL_REF_COUNT: [AtomicUsize; Peripheral::COUNT] = [const { AtomicUsize::new(0) }; Peripheral::COUNT]; @@ -173,6 +183,29 @@ impl Drop for PeripheralGuard { } } +#[derive(Debug)] +pub(crate) struct GenericPeripheralGuard {} + +impl GenericPeripheralGuard

{ + pub(crate) fn new() -> Self { + let peripheral = unwrap!(Peripheral::try_from(P)); + if !KEEP_ENABLED.contains(&peripheral) && PeripheralClockControl::enable(peripheral, true) { + PeripheralClockControl::reset(peripheral); + } + + Self {} + } +} + +impl Drop for GenericPeripheralGuard

{ + fn drop(&mut self) { + let peripheral = unwrap!(Peripheral::try_from(P)); + if !KEEP_ENABLED.contains(&peripheral) { + PeripheralClockControl::enable(peripheral, false); + } + } +} + /// Controls the enablement of peripheral clocks. pub(crate) struct PeripheralClockControl; diff --git a/esp-hal/src/trace.rs b/esp-hal/src/trace.rs index f093b525e89..09777515764 100644 --- a/esp-hal/src/trace.rs +++ b/esp-hal/src/trace.rs @@ -36,7 +36,7 @@ use crate::{ peripheral::{Peripheral, PeripheralRef}, peripherals::trace::RegisterBlock, - system::PeripheralClockControl, + system::PeripheralGuard, }; /// Errors returned from [Trace::stop_trace] @@ -62,6 +62,7 @@ where { peripheral: PeripheralRef<'d, T>, buffer: Option<&'d mut [u8]>, + _guard: PeripheralGuard, } impl<'d, T> Trace<'d, T> @@ -71,14 +72,12 @@ where /// Construct a new instance pub fn new(peripheral: impl Peripheral

+ 'd) -> Self { crate::into_ref!(peripheral); - - if PeripheralClockControl::enable(peripheral.peripheral(), true) { - PeripheralClockControl::reset(peripheral.peripheral()); - } + let guard = PeripheralGuard::new(peripheral.peripheral()); Self { peripheral, buffer: None, + _guard: guard, } } @@ -207,15 +206,6 @@ where } } -impl Drop for Trace<'_, T> -where - T: Instance, -{ - fn drop(&mut self) { - PeripheralClockControl::enable(self.peripheral.peripheral(), false); - } -} - /// Trace peripheral instance pub trait Instance: crate::private::Sealed { /// Get a reference to the peripheral's underlying register block