Skip to content

Commit 8c0f667

Browse files
committed
Add unsafe peripheral function
to mutate underlying peripheral
1 parent 7fab5c5 commit 8c0f667

File tree

9 files changed

+107
-0
lines changed

9 files changed

+107
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6464
- Add `use_pll` to `CFGR` - the clock configuration - to force to use the PLL
6565
source for the systemclock. Also `Clocks::pllclk()` was introduced to be able
6666
to check, whether PLL is used.
67+
- Add unsafe peripheral function to access underlying peripheral ([#277])
6768

6869
[`enumset`]: https://crates.io/crates/enumset
6970

@@ -143,6 +144,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
143144
- A generic `Instance` trait now represents all Spi peripherals.
144145
- `Spi::spi1` and so on are renamed to `Spi::new`.
145146
- Add SPI configuration type to be passed into `Spi::new`
147+
- Remove public fields from `Adc` and `Rtc` ([#277])
146148

147149
## [v0.7.0] - 2021-06-18
148150

@@ -468,6 +470,7 @@ let clocks = rcc
468470
[defmt]: https://github.com/knurling-rs/defmt
469471
[filter]: https://defmt.ferrous-systems.com/filtering.html
470472

473+
[#277]: https://github.com/stm32-rs/stm32f3xx-hal/pull/277
471474
[#273]: https://github.com/stm32-rs/stm32f3xx-hal/pull/273
472475
[#271]: https://github.com/stm32-rs/stm32f3xx-hal/pull/271
473476
[#270]: https://github.com/stm32-rs/stm32f3xx-hal/pull/270

src/adc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,19 @@ macro_rules! adc_hal {
510510
};
511511
}
512512

513+
/// Get access to the underlying register block.
514+
///
515+
/// # Safety
516+
///
517+
/// This function is not _memory_ unsafe per se, but does not guarantee
518+
/// anything about assumptions of invariants made in this implementation.
519+
///
520+
/// Changing specific options can lead to un-expected behavior and nothing
521+
/// is guaranteed.
522+
pub unsafe fn peripheral(&mut self) -> &mut $ADC {
523+
&mut self.adc
524+
}
525+
513526
}
514527

515528
impl<Word, Pin> OneShot<$ADC, Word, Pin> for Adc<$ADC>

src/delay.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ impl Delay {
5555
Delay { clocks, syst }
5656
}
5757

58+
/// Get access to the underlying register block.
59+
///
60+
/// # Safety
61+
///
62+
/// This function is not _memory_ unsafe per se, but does not guarantee
63+
/// anything about assumptions of invariants made in this implementation.
64+
///
65+
/// Changing specific options can lead to un-expected behavior and nothing
66+
/// is guaranteed.
67+
pub unsafe fn peripheral(&mut self) -> &mut SYST {
68+
&mut self.syst
69+
}
70+
5871
/// Releases the system timer (SysTick) resource
5972
pub fn free(self) -> SYST {
6073
self.syst

src/i2c.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)> {
195195
Self { i2c, pins }
196196
}
197197

198+
/// Get access to the underlying register block.
199+
///
200+
/// # Safety
201+
///
202+
/// This function is not _memory_ unsafe per se, but does not guarantee
203+
/// anything about assumptions of invariants made in this implementation.
204+
///
205+
/// Changing specific options can lead to un-expected behavior and nothing
206+
/// is guaranteed.
207+
pub unsafe fn peripheral(&mut self) -> &mut I2C {
208+
&mut self.i2c
209+
}
210+
198211
/// Releases the I2C peripheral and associated pins
199212
pub fn free(self) -> (I2C, (SCL, SDA)) {
200213
(self.i2c, self.pins)

src/rtc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ impl Rtc {
8484
self.rtc.cr.read().fmt().bit()
8585
}
8686

87+
/// Get access to the underlying register block.
88+
///
89+
/// # Safety
90+
///
91+
/// This function is not _memory_ unsafe per se, but does not guarantee
92+
/// anything about assumptions of invariants made in this implementation.
93+
///
94+
/// Changing specific options can lead to un-expected behavior and nothing
95+
/// is guaranteed.
96+
pub unsafe fn peripheral(&mut self) -> &mut RTC {
97+
&mut self.rtc
98+
}
99+
87100
/// Release the RTC peripheral
88101
pub fn free(self) -> RTC {
89102
// TODO(Sh3Rm4n): Disable peripheral before releasing it.

src/serial.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,19 @@ where
511511
Self { usart, pins }
512512
}
513513

514+
/// Get access to the underlying register block.
515+
///
516+
/// # Safety
517+
///
518+
/// This function is not _memory_ unsafe per se, but does not guarantee
519+
/// anything about assumptions of invariants made in this implementation.
520+
///
521+
/// Changing specific options can lead to un-expected behavior and nothing
522+
/// is guaranteed.
523+
pub unsafe fn peripheral(&mut self) -> &mut Usart {
524+
&mut self.usart
525+
}
526+
514527
/// Releases the USART peripheral and associated pins
515528
pub fn free(self) -> (Usart, (Tx, Rx)) {
516529
self.usart

src/spi.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ impl<SPI, Sck, Miso, Mosi, WORD> Spi<SPI, (Sck, Miso, Mosi), WORD> {
270270
}
271271
}
272272

273+
/// Get access to the underlying register block.
274+
///
275+
/// # Safety
276+
///
277+
/// This function is not _memory_ unsafe per se, but does not guarantee
278+
/// anything about assumptions of invariants made in this implementation.
279+
///
280+
/// Changing specific options can lead to un-expected behavior and nothing
281+
/// is guaranteed.
282+
pub unsafe fn peripheral(&mut self) -> &mut SPI {
283+
&mut self.spi
284+
}
285+
273286
/// Releases the SPI peripheral and associated pins
274287
pub fn free(self) -> (SPI, (Sck, Miso, Mosi)) {
275288
(self.spi, self.pins)

src/timer.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ where
212212
self.tim.clear_sr();
213213
}
214214

215+
/// Get access to the underlying register block.
216+
///
217+
/// # Safety
218+
///
219+
/// This function is not _memory_ unsafe per se, but does not guarantee
220+
/// anything about assumptions of invariants made in this implementation.
221+
///
222+
/// Changing specific options can lead to un-expected behavior and nothing
223+
/// is guaranteed.
224+
pub unsafe fn peripheral(&mut self) -> &mut TIM {
225+
&mut self.tim
226+
}
227+
215228
/// Releases the TIM peripheral
216229
#[inline]
217230
pub fn free(mut self) -> TIM {

src/watchdog.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ impl IndependentWatchDog {
106106
// pub fn free(self) -> IWDG {}
107107
}
108108

109+
/// Get access to the underlying register block.
110+
///
111+
/// # Safety
112+
///
113+
/// This function is not _memory_ unsafe per se, but does not guarantee
114+
/// anything about assumptions of invariants made in this implementation.
115+
///
116+
/// Changing specific options can lead to un-expected behavior and nothing
117+
/// is guaranteed.
118+
pub unsafe fn peripheral(&mut self) -> &mut IWDG {
119+
&mut self.iwdg
120+
}
121+
109122
/// Returns the currently set interval
110123
pub fn interval(&self) -> Milliseconds {
111124
// If the prescaler was changed wait until the change procedure is finished.

0 commit comments

Comments
 (0)