Skip to content

Commit ae82c7b

Browse files
authored
Merge pull request #773 from stm32-rs/ptr-trait
Ptr & Steal traits
2 parents 47de723 + 8388c52 commit ae82c7b

File tree

12 files changed

+110
-65
lines changed

12 files changed

+110
-65
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2323
- RTIC2 monotonics fix: CC1 instead of CC3
2424
- Allow different lengths of buffers in hal_1 SpiBus impl [#566]
2525
- Clean SPI write impls
26+
- move `ptr()` to `Ptr` trait [#773]
2627
- `steal` UART peripheral on `Rx::new`
2728

2829
[#248]: https://github.com/stm32-rs/stm32f4xx-hal/pull/248
2930
[#566]: https://github.com/stm32-rs/stm32f4xx-hal/pull/566
3031
[#706]: https://github.com/stm32-rs/stm32f4xx-hal/pull/706
32+
[#725]: https://github.com/stm32-rs/stm32f4xx-hal/pull/725
3133
[#731]: https://github.com/stm32-rs/stm32f4xx-hal/pull/731
3234
[#758]: https://github.com/stm32-rs/stm32f4xx-hal/pull/758
33-
[#725]: https://github.com/stm32-rs/stm32f4xx-hal/pull/725
35+
[#773]: https://github.com/stm32-rs/stm32f4xx-hal/pull/773
3436

3537
## [v0.21.0] - 2024-05-30
3638

src/dma/traits.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,27 @@ use address;
302302
pub type DMARegisterBlock = pac::dma1::RegisterBlock;
303303

304304
/// Trait that represents an instance of a DMA peripheral.
305-
pub trait Instance: Deref<Target = DMARegisterBlock> + crate::Sealed {
306-
/// Gives a pointer to the RegisterBlock.
307-
fn ptr() -> *const DMARegisterBlock;
305+
pub trait Instance:
306+
crate::Sealed + crate::Ptr<RB = DMARegisterBlock> + Deref<Target = Self::RB>
307+
{
308308
}
309309

310-
impl Instance for DMA1 {
310+
impl Instance for DMA1 {}
311+
impl Instance for DMA2 {}
312+
313+
impl crate::Ptr for DMA1 {
314+
type RB = DMARegisterBlock;
311315
#[inline(always)]
312-
fn ptr() -> *const DMARegisterBlock {
313-
DMA1::ptr()
316+
fn ptr() -> *const Self::RB {
317+
Self::ptr()
314318
}
315319
}
316320

317-
impl Instance for DMA2 {
321+
impl crate::Ptr for DMA2 {
322+
type RB = DMARegisterBlock;
318323
#[inline(always)]
319-
fn ptr() -> *const DMARegisterBlock {
320-
DMA2::ptr()
324+
fn ptr() -> *const Self::RB {
325+
Self::ptr()
321326
}
322327
}
323328

src/fmpi2c.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,30 @@ mod hal_02;
1010
mod hal_1;
1111

1212
pub trait Instance:
13-
crate::Sealed + Deref<Target = fmpi2c1::RegisterBlock> + Enable + Reset + gpio::alt::I2cCommon
13+
crate::Sealed
14+
+ crate::Ptr<RB = fmpi2c1::RegisterBlock>
15+
+ Deref<Target = Self::RB>
16+
+ Enable
17+
+ Reset
18+
+ gpio::alt::I2cCommon
1419
{
15-
#[doc(hidden)]
16-
fn ptr() -> *const fmpi2c1::RegisterBlock;
1720
fn clock_hsi(rcc: &crate::pac::rcc::RegisterBlock);
1821
}
1922

2023
impl Instance for FMPI2C1 {
21-
fn ptr() -> *const fmpi2c1::RegisterBlock {
22-
FMPI2C1::ptr() as *const _
23-
}
2424
fn clock_hsi(rcc: &crate::pac::rcc::RegisterBlock) {
2525
rcc.dckcfgr2().modify(|_, w| w.fmpi2c1sel().hsi());
2626
}
2727
}
2828

29+
impl crate::Ptr for FMPI2C1 {
30+
type RB = fmpi2c1::RegisterBlock;
31+
#[inline(always)]
32+
fn ptr() -> *const Self::RB {
33+
Self::ptr()
34+
}
35+
}
36+
2937
/// I2C FastMode+ abstraction
3038
pub struct FMPI2c<I2C: Instance> {
3139
i2c: I2C,

src/i2c.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,26 @@ impl Error {
104104
}
105105

106106
pub trait Instance:
107-
crate::Sealed + Deref<Target = i2c1::RegisterBlock> + Enable + Reset + gpio::alt::I2cCommon
107+
crate::Sealed
108+
+ crate::Ptr<RB = i2c1::RegisterBlock>
109+
+ Deref<Target = Self::RB>
110+
+ Enable
111+
+ Reset
112+
+ gpio::alt::I2cCommon
108113
{
109-
#[doc(hidden)]
110-
fn ptr() -> *const i2c1::RegisterBlock;
111114
}
112115

113116
// Implemented by all I2C instances
114117
macro_rules! i2c {
115118
($I2C:ty: $I2c:ident) => {
116119
pub type $I2c = I2c<$I2C>;
117120

118-
impl Instance for $I2C {
119-
fn ptr() -> *const i2c1::RegisterBlock {
120-
<$I2C>::ptr() as *const _
121+
impl Instance for $I2C {}
122+
123+
impl crate::Ptr for $I2C {
124+
type RB = i2c1::RegisterBlock;
125+
fn ptr() -> *const Self::RB {
126+
Self::ptr()
121127
}
122128
}
123129
};

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,12 @@ pub trait Listen {
198198
self.unlisten(BitFlags::ALL)
199199
}
200200
}
201+
202+
pub trait Ptr {
203+
type RB;
204+
fn ptr() -> *const Self::RB;
205+
}
206+
207+
pub trait Steal {
208+
unsafe fn steal() -> Self;
209+
}

src/sai.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,14 @@ pub struct Receive;
211211
pub struct Transmit;
212212

213213
pub trait Instance:
214-
crate::Sealed + Deref<Target = sai::RegisterBlock> + rcc::Enable + rcc::Reset + rcc::BusClock
214+
crate::Sealed
215+
+ crate::Ptr<RB = sai::RegisterBlock>
216+
+ crate::Steal
217+
+ Deref<Target = Self::RB>
218+
+ rcc::Enable
219+
+ rcc::Reset
220+
+ rcc::BusClock
215221
{
216-
#[doc(hidden)]
217-
fn ptr() -> *const sai::RegisterBlock;
218-
#[doc(hidden)]
219-
unsafe fn steal() -> Self;
220222
}
221223

222224
impl<SAI: Instance, const C: bool> SAICH<SAI, C> {
@@ -234,12 +236,19 @@ macro_rules! sai_impl {
234236
($SAI:ty, $sai:ident, $SAIA:ident, $SAIB:ident) => {
235237
pub type $SAIA = SAIA<$SAI>;
236238
pub type $SAIB = SAIB<$SAI>;
237-
impl Instance for $SAI {
238-
fn ptr() -> *const sai::RegisterBlock {
239-
<$SAI>::ptr()
239+
240+
impl Instance for $SAI {}
241+
242+
impl crate::Ptr for $SAI {
243+
type RB = sai::RegisterBlock;
244+
fn ptr() -> *const Self::RB {
245+
Self::ptr()
240246
}
247+
}
248+
249+
impl crate::Steal for $SAI {
241250
unsafe fn steal() -> Self {
242-
<$SAI>::steal()
251+
Self::steal()
243252
}
244253
}
245254
};

src/serial.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ impl<USART: Instance, WORD> Serial<USART, WORD> {
233233
clocks: &Clocks,
234234
) -> Result<Self, config::InvalidConfig>
235235
where
236-
<USART as Instance>::RegisterBlock: uart_impls::RegisterBlockImpl,
236+
<USART as crate::Ptr>::RB: uart_impls::RegisterBlockImpl,
237237
{
238-
<USART as Instance>::RegisterBlock::new(usart, pins, config, clocks)
238+
<USART as crate::Ptr>::RB::new(usart, pins, config, clocks)
239239
}
240240
}
241241

@@ -257,12 +257,6 @@ macro_rules! halUsart {
257257
pub type $Rx<WORD = u8> = Rx<$USART, WORD>;
258258

259259
impl Instance for $USART {
260-
type RegisterBlock = crate::serial::uart_impls::RegisterBlockUsart;
261-
262-
fn ptr() -> *const crate::serial::uart_impls::RegisterBlockUsart {
263-
<$USART>::ptr() as *const _
264-
}
265-
266260
fn set_stopbits(&self, bits: config::StopBits) {
267261
use crate::pac::usart1::cr2::STOP;
268262
use config::StopBits;
@@ -276,7 +270,17 @@ macro_rules! halUsart {
276270
})
277271
});
278272
}
273+
}
274+
275+
impl crate::Ptr for $USART {
276+
type RB = crate::serial::uart_impls::RegisterBlockUsart;
277+
278+
fn ptr() -> *const Self::RB {
279+
Self::ptr()
280+
}
281+
}
279282

283+
impl crate::Steal for $USART {
280284
unsafe fn steal() -> Self {
281285
Self::steal()
282286
}

src/serial/hal_02.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ mod blocking {
114114

115115
impl<USART: Instance> Write<u16> for Tx<USART, u16>
116116
where
117-
USART: Deref<Target = <USART as Instance>::RegisterBlock>,
117+
USART: Deref<Target = <USART as crate::Ptr>::RB>,
118118
{
119119
type Error = Error;
120120

src/serial/hal_1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ mod io {
115115

116116
impl<USART: Instance> Write for Tx<USART, u8>
117117
where
118-
<USART as Instance>::RegisterBlock: RegisterBlockImpl,
119-
USART: Deref<Target = <USART as Instance>::RegisterBlock>,
118+
<USART as crate::Ptr>::RB: RegisterBlockImpl,
119+
USART: Deref<Target = <USART as crate::Ptr>::RB>,
120120
{
121121
fn write(&mut self, bytes: &[u8]) -> Result<usize, Self::Error> {
122122
let mut i = 0;

src/serial/uart_impls.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,26 @@ impl crate::Sealed for RegisterBlockUsart {}
2424
// Implemented by all USART/UART instances
2525
pub trait Instance:
2626
crate::Sealed
27+
+ crate::Ptr<RB: RegisterBlockImpl>
28+
+ crate::Steal
29+
+ core::ops::Deref<Target = Self::RB>
2730
+ rcc::Enable
2831
+ rcc::Reset
2932
+ rcc::BusClock
3033
+ CommonPins
31-
+ core::ops::Deref<Target = Self::RegisterBlock>
3234
{
33-
type RegisterBlock: RegisterBlockImpl;
34-
35-
#[doc(hidden)]
36-
fn ptr() -> *const Self::RegisterBlock;
3735
#[doc(hidden)]
3836
fn set_stopbits(&self, bits: config::StopBits);
3937
#[doc(hidden)]
4038
#[inline(always)]
4139
fn peri_address() -> u32 {
4240
unsafe { &*Self::ptr() }.peri_address()
4341
}
44-
#[doc(hidden)]
45-
unsafe fn steal() -> Self;
4642
}
4743

4844
pub trait RegisterBlockImpl: crate::Sealed {
4945
#[allow(clippy::new_ret_no_self)]
50-
fn new<UART: Instance<RegisterBlock = Self>, WORD>(
46+
fn new<UART: Instance + crate::Ptr<RB = Self>, WORD>(
5147
uart: UART,
5248
pins: (impl Into<UART::Tx<PushPull>>, impl Into<UART::Rx<PushPull>>),
5349
config: impl Into<config::Config>,
@@ -267,7 +263,7 @@ macro_rules! uartCommon {
267263
}
268264

269265
impl RegisterBlockImpl for RegisterBlockUsart {
270-
fn new<UART: Instance<RegisterBlock = Self>, WORD>(
266+
fn new<UART: Instance + crate::Ptr<RB = Self>, WORD>(
271267
uart: UART,
272268
pins: (impl Into<UART::Tx<PushPull>>, impl Into<UART::Rx<PushPull>>),
273269
config: impl Into<config::Config>,
@@ -408,7 +404,7 @@ where {
408404

409405
#[cfg(feature = "uart4")]
410406
impl RegisterBlockImpl for RegisterBlockUart {
411-
fn new<UART: Instance<RegisterBlock = Self>, WORD>(
407+
fn new<UART: Instance + crate::Ptr<RB = Self>, WORD>(
412408
uart: UART,
413409
pins: (impl Into<UART::Tx<PushPull>>, impl Into<UART::Rx<PushPull>>),
414410
config: impl Into<config::Config>,

0 commit comments

Comments
 (0)