Skip to content

Commit

Permalink
Add inner and inner_mut functions to peripherals where it's easy to d…
Browse files Browse the repository at this point in the history
…o so

Where the registers are owned by a single struct and not shared amongst many channels, etc.
  • Loading branch information
mattico committed Jan 4, 2022
1 parent 4ebb5c1 commit 6b26f84
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,16 @@ macro_rules! adc_hal {
panic!("Cannot read linear calibration value when the ADC is disabled");
}
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &$ADC {
&self.rb
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut $ADC {
&mut self.rb
}
}

impl<WORD, PIN> OneShot<$ADC, WORD, PIN> for Adc<$ADC, Enabled>
Expand Down
10 changes: 10 additions & 0 deletions src/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ impl Crc {
pub fn get_idr(&self) -> u32 {
self.reg.idr.read().idr().bits()
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &CRC {
&self.reg
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut CRC {
&mut self.reg
}
}

#[macro_use]
Expand Down
12 changes: 12 additions & 0 deletions src/fmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ impl FmcExt for stm32::FMC {
}
}

impl FMC {
/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &stm32::FMC {
&self.fmc
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut stm32::FMC {
&mut self.fmc
}
}

unsafe impl FmcPeripheral for FMC {
const REGISTERS: *const () = stm32::FMC::ptr() as *const ();

Expand Down
5 changes: 5 additions & 0 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ macro_rules! i2c {
&self.i2c
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut $I2CX {
&mut self.i2c
}

/// Enable or disable the DMA mode for reception
pub fn rx_dma(&mut self, enable: bool) {
self.i2c.cr1.modify(|_,w| w.rxdmaen().bit(enable));
Expand Down
10 changes: 10 additions & 0 deletions src/ltdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ impl Ltdc {
// unsafe: clear write-one interrupt flag
unsafe { (*LTDC::ptr()).icr.write(|w| w.crrif().set_bit()) };
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &LTDC {
&self.ltdc
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut LTDC {
&mut self.ltdc
}
}

impl DisplayController for Ltdc {
Expand Down
10 changes: 10 additions & 0 deletions src/pwr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ impl Pwr {
});
while d3cr!(self.rb).read().vosrdy().bit_is_clear() {}
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &PWR {
&self.rb
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut PWR {
&mut self.rb
}
}

/// Builder methods
Expand Down
10 changes: 10 additions & 0 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ impl Rng {
pub fn release(self) -> RNG {
self.rb
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &RNG {
&self.rb
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut RNG {
&mut self.rb
}
}

impl core::iter::Iterator for Rng {
Expand Down
10 changes: 10 additions & 0 deletions src/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,4 +785,14 @@ impl Rtc {
// We're allowed to change this once after the LSE fails
self.prec.kernel_clk_mux(backup::RtcClkSel::LSI);
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &RTC {
&self.reg
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut RTC {
&mut self.reg
}
}
10 changes: 10 additions & 0 deletions src/sdmmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ impl<S> Sdmmc<S> {
_ => Err(Error::BadClock),
}
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &S {
&self.sdmmc
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut S {
&mut self.sdmmc
}
}

macro_rules! sdmmc {
Expand Down
10 changes: 10 additions & 0 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ macro_rules! usart {

self.usart
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &$USARTX {
&self.usart
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut $USARTX {
&mut self.usart
}
}

impl SerialExt<$USARTX> for $USARTX {
Expand Down
10 changes: 10 additions & 0 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,16 @@ macro_rules! hal {

(self.tim, rec::$Rec { _marker: PhantomData })
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &$TIMX {
&self.tim
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut $TIMX {
&mut self.tim
}
}
)+
}
Expand Down
10 changes: 10 additions & 0 deletions src/watchdog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ impl SystemWindowWatchdog {
}
}
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &WWDG {
&self.wwdg
}

/// Returns a mutable reference to the inner peripheral
pub fn inner_mut(&mut self) -> &mut WWDG {
&mut self.wwdg
}
}

impl Watchdog for SystemWindowWatchdog {
Expand Down

0 comments on commit 6b26f84

Please sign in to comment.