Skip to content

Commit

Permalink
Use GenericPeripheralGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ committed Nov 20, 2024
1 parent 698c9e3 commit beddf0c
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 172 deletions.
13 changes: 4 additions & 9 deletions esp-hal/src/aes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use crate::{
peripheral::{Peripheral, PeripheralRef},
peripherals::AES,
reg_access::{AlignmentHelper, NativeEndianess},
system::GenericPeripheralGuard,
};

#[cfg_attr(esp32, path = "esp32.rs")]
Expand Down Expand Up @@ -136,20 +137,20 @@ pub enum Mode {
pub struct Aes<'d> {
aes: PeripheralRef<'d, AES>,
alignment_helper: AlignmentHelper<NativeEndianess>,
_guard: GenericPeripheralGuard<{ crate::system::Peripheral::Aes as u8 }>,
}

impl<'d> Aes<'d> {
/// Constructs a new `Aes` instance.
pub fn new(aes: impl Peripheral<P = AES> + '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();

Expand Down Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions esp-hal/src/analog/adc/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::efuse::Efuse;
use crate::{
peripheral::PeripheralRef,
peripherals::APB_SARADC,
system::{Peripheral, PeripheralClockControl},
system::{GenericPeripheralGuard, Peripheral},
};

mod calibration;
Expand Down Expand Up @@ -396,6 +396,7 @@ pub struct Adc<'d, ADCI> {
_adc: PeripheralRef<'d, ADCI>,
attenuations: [Option<Attenuation>; NUM_ATTENS],
active_channel: Option<u8>,
_guard: GenericPeripheralGuard<{ Peripheral::ApbSarAdc as u8 }>,
}

impl<'d, ADCI> Adc<'d, ADCI>
Expand All @@ -408,9 +409,7 @@ where
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> 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();
Expand All @@ -423,6 +422,7 @@ where
_adc: adc_instance.into_ref(),
attenuations: config.attenuations,
active_channel: None,
_guard: guard,
}
}

Expand Down Expand Up @@ -501,12 +501,6 @@ where
}
}

impl<ADCI> 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<u16> {
Expand Down
15 changes: 4 additions & 11 deletions esp-hal/src/analog/adc/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::efuse::Efuse;
use crate::{
peripheral::PeripheralRef,
peripherals::{APB_SARADC, SENS},
system::{Peripheral, PeripheralClockControl},
system::{GenericPeripheralGuard, Peripheral},
};

mod calibration;
Expand Down Expand Up @@ -389,6 +389,7 @@ pub struct Adc<'d, ADC> {
_adc: PeripheralRef<'d, ADC>,
active_channel: Option<u8>,
last_init_code: u16,
_guard: GenericPeripheralGuard<{ Peripheral::ApbSarAdc as u8 }>,
}

impl<'d, ADCI> Adc<'d, ADCI>
Expand All @@ -401,10 +402,7 @@ where
adc_instance: impl crate::peripheral::Peripheral<P = ADCI> + 'd,
config: AdcConfig<ADCI>,
) -> Self {
if PeripheralClockControl::enable(Peripheral::ApbSarAdc, true) {
PeripheralClockControl::reset(Peripheral::ApbSarAdc);
}

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

// Set attenuation for pins
Expand Down Expand Up @@ -473,6 +471,7 @@ where
_adc: adc_instance.into_ref(),
active_channel: None,
last_init_code: 0,
_guard: guard,
}
}

Expand Down Expand Up @@ -561,12 +560,6 @@ where
}
}

impl<ADCI> 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<u16> {
Expand Down
14 changes: 4 additions & 10 deletions esp-hal/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -41,6 +41,7 @@ pub struct Ecc<'d, DM: crate::Mode> {
ecc: PeripheralRef<'d, ECC>,
alignment_helper: AlignmentHelper<SocDependentEndianess>,
phantom: PhantomData<DM>,
_guard: GenericPeripheralGuard<{ system::Peripheral::Ecc as u8 }>,
}

/// ECC interface error
Expand Down Expand Up @@ -102,14 +103,13 @@ impl<'d> Ecc<'d, crate::Blocking> {
pub fn new(ecc: impl Peripheral<P = ECC> + '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,
}
}
}
Expand Down Expand Up @@ -965,9 +965,3 @@ impl<DM: crate::Mode> Ecc<'_, DM> {
}
}
}

impl<DM: crate::Mode> Drop for Ecc<'_, DM> {
fn drop(&mut self) {
PeripheralClockControl::enable(PeripheralEnable::Ecc, false);
}
}
12 changes: 5 additions & 7 deletions esp-hal/src/etm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
use crate::{
peripheral::{Peripheral, PeripheralRef},
system::PeripheralClockControl,
system::GenericPeripheralGuard,
};

/// Unconfigured EtmChannel.
Expand All @@ -75,11 +75,8 @@ impl<const C: u8> EtmChannel<C> {
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()
Expand All @@ -96,6 +93,7 @@ impl<const C: u8> EtmChannel<C> {
EtmConfiguredChannel {
_event: event,
_task: task,
_guard: guard,
}
}
}
Expand All @@ -121,6 +119,7 @@ where
{
_event: &'a E,
_task: &'a T,
_guard: GenericPeripheralGuard<{ crate::system::Peripheral::Etm as u8 }>,
}

impl<E, T, const C: u8> Drop for EtmConfiguredChannel<'_, E, T, C>
Expand All @@ -131,7 +130,6 @@ where
fn drop(&mut self) {
debug!("Drop ETM channel {}", C);
disable_channel(C);
PeripheralClockControl::enable(crate::system::Peripheral::Etm, false);
}
}

Expand All @@ -156,7 +154,7 @@ macro_rules! create_etm {

Self {
_peripheral: peripheral,
$([< channel $num >]: EtmChannel {},)+
$([< channel $num >]: EtmChannel { },)+
}
}
}
Expand Down
14 changes: 4 additions & 10 deletions esp-hal/src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -51,6 +51,7 @@ pub struct Hmac<'d> {
alignment_helper: AlignmentHelper<SocDependentEndianess>,
byte_written: usize,
next_command: NextCommand,
_guard: GenericPeripheralGuard<{ PeripheralEnable::Hmac as u8 }>,
}

/// HMAC interface error
Expand Down Expand Up @@ -107,15 +108,14 @@ impl<'d> Hmac<'d> {
pub fn new(hmac: impl Peripheral<P = HMAC> + '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,
}
}

Expand Down Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions esp-hal/src/lcd_cam/cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use crate::{
lcd_cam::{calculate_clkm, BitOrder, ByteOrder},
peripheral::{Peripheral, PeripheralRef},
peripherals::LCD_CAM,
system::PeripheralGuard,
system::{self, GenericPeripheralGuard},
Blocking,
};

Expand Down Expand Up @@ -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, <LCD_CAM as DmaEligible>::Dma>,
_guard: PeripheralGuard,
_guard: GenericPeripheralGuard<{ system::Peripheral::LcdCam as u8 }>,
}

impl<'d> Camera<'d> {
Expand Down
9 changes: 7 additions & 2 deletions esp-hal/src/lcd_cam/lcd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DM>,

pub(super) _guard: PeripheralGuard,
pub(super) _guard: GenericPeripheralGuard<{ system::Peripheral::LcdCam as u8 }>,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
Expand Down
6 changes: 3 additions & 3 deletions esp-hal/src/lcd_cam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
macros::handler,
peripheral::Peripheral,
peripherals::{Interrupt, LCD_CAM},
system::{self, PeripheralGuard},
system::GenericPeripheralGuard,
Async,
Blocking,
Cpu,
Expand All @@ -38,8 +38,8 @@ impl<'d> LcdCam<'d, Blocking> {
pub fn new(lcd_cam: impl Peripheral<P = LCD_CAM> + '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 {
Expand Down
Loading

0 comments on commit beddf0c

Please sign in to comment.