Skip to content

Commit 3db88de

Browse files
committed
improve Ptr trait
1 parent 5156884 commit 3db88de

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/i2c/dma.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ pub struct Rx<I2C> {
934934
unsafe impl<I2C: Instance> PeriAddress for Rx<I2C> {
935935
#[inline(always)]
936936
fn address(&self) -> u32 {
937-
unsafe { (*I2C::ptr()).dr().as_ptr() as u32 }
937+
unsafe { (*I2C::PTR).dr().as_ptr() as u32 }
938938
}
939939

940940
type MemSize = u8;
@@ -943,7 +943,7 @@ unsafe impl<I2C: Instance> PeriAddress for Rx<I2C> {
943943
unsafe impl<I2C: Instance> PeriAddress for Tx<I2C> {
944944
#[inline(always)]
945945
fn address(&self) -> u32 {
946-
unsafe { (*I2C::ptr()).dr().as_ptr() as u32 }
946+
unsafe { (*I2C::PTR).dr().as_ptr() as u32 }
947947
}
948948

949949
type MemSize = u8;

src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,18 @@ impl<RB, const A: usize> Sealed for Periph<RB, A> {}
210210
pub trait Ptr: Sealed {
211211
/// RegisterBlock structure
212212
type RB;
213+
/// Pointer to the register block
214+
const PTR: *const Self::RB;
213215
/// Return the pointer to the register block
214-
fn ptr() -> *const Self::RB;
216+
#[inline(always)]
217+
fn ptr() -> *const Self::RB {
218+
Self::PTR
219+
}
215220
}
216221

217222
impl<RB, const A: usize> Ptr for Periph<RB, A> {
218223
type RB = RB;
219-
fn ptr() -> *const Self::RB {
220-
Self::ptr()
221-
}
224+
const PTR: *const Self::RB = Self::PTR;
222225
}
223226

224227
pub trait Steal: Sealed {
@@ -239,6 +242,7 @@ pub trait Steal: Sealed {
239242
}
240243

241244
impl<RB, const A: usize> Steal for Periph<RB, A> {
245+
#[inline(always)]
242246
unsafe fn steal() -> Self {
243247
Self::steal()
244248
}

src/serial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub trait Instance:
138138
#[doc(hidden)]
139139
#[inline(always)]
140140
fn peri_address() -> u32 {
141-
unsafe { &*Self::ptr() }.peri_address()
141+
unsafe { &*Self::PTR }.peri_address()
142142
}
143143
}
144144

src/spi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ impl<SPI: Instance> DmaBuilder<SPI> {
957957
unsafe impl<SPI: Instance> PeriAddress for Rx<SPI> {
958958
#[inline(always)]
959959
fn address(&self) -> u32 {
960-
unsafe { (*SPI::ptr()).dr().as_ptr() as u32 }
960+
unsafe { (*SPI::PTR).dr().as_ptr() as u32 }
961961
}
962962

963963
type MemSize = u8;
@@ -971,7 +971,7 @@ unsafe impl<SPI, STREAM, const CHANNEL: u8> DMASet<STREAM, CHANNEL, PeripheralTo
971971
unsafe impl<SPI: Instance> PeriAddress for Tx<SPI> {
972972
#[inline(always)]
973973
fn address(&self) -> u32 {
974-
unsafe { (*SPI::ptr()).dr().as_ptr() as u32 }
974+
unsafe { (*SPI::PTR).dr().as_ptr() as u32 }
975975
}
976976

977977
type MemSize = u8;

0 commit comments

Comments
 (0)