Skip to content

Commit f848188

Browse files
authored
Merge pull request #719 from stm32-rs/gpioptr
shorten gpio ptr access
2 parents fd2855a + c8d30fa commit f848188

File tree

5 files changed

+40
-62
lines changed

5 files changed

+40
-62
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- shorten gpio ptr access
1213
- bump embedded-hal to `1.0.0-rc.3`
1314

1415
## [v0.19.0] - 2023-12-11

src/gpio.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ where
321321
let offset = 2 * { N };
322322

323323
unsafe {
324-
(*Gpio::<P>::ptr())
324+
(*gpiox::<P>())
325325
.ospeedr
326326
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | ((speed as u32) << offset)));
327327
}
@@ -353,7 +353,7 @@ where
353353
let offset = 2 * { N };
354354
let value = resistor as u32;
355355
unsafe {
356-
(*Gpio::<P>::ptr())
356+
(*gpiox::<P>())
357357
.pupdr
358358
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (value << offset)));
359359
}
@@ -435,22 +435,22 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
435435
#[inline(always)]
436436
fn _set_high(&mut self) {
437437
// NOTE(unsafe) atomic write to a stateless register
438-
unsafe { (*Gpio::<P>::ptr()).bsrr.write(|w| w.bits(1 << N)) }
438+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(1 << N)) }
439439
}
440440
#[inline(always)]
441441
fn _set_low(&mut self) {
442442
// NOTE(unsafe) atomic write to a stateless register
443-
unsafe { (*Gpio::<P>::ptr()).bsrr.write(|w| w.bits(1 << (16 + N))) }
443+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(1 << (16 + N))) }
444444
}
445445
#[inline(always)]
446446
fn _is_set_low(&self) -> bool {
447447
// NOTE(unsafe) atomic read with no side effects
448-
unsafe { (*Gpio::<P>::ptr()).odr.read().bits() & (1 << N) == 0 }
448+
unsafe { (*gpiox::<P>()).odr.read().bits() & (1 << N) == 0 }
449449
}
450450
#[inline(always)]
451451
fn _is_low(&self) -> bool {
452452
// NOTE(unsafe) atomic read with no side effects
453-
unsafe { (*Gpio::<P>::ptr()).idr.read().bits() & (1 << N) == 0 }
453+
unsafe { (*gpiox::<P>()).idr.read().bits() & (1 << N) == 0 }
454454
}
455455
}
456456

@@ -603,29 +603,26 @@ use gpio;
603603
mod f4;
604604
pub use f4::*;
605605

606-
struct Gpio<const P: char>;
607-
impl<const P: char> Gpio<P> {
608-
const fn ptr() -> *const crate::pac::gpioa::RegisterBlock {
609-
match P {
610-
'A' => crate::pac::GPIOA::ptr(),
611-
'B' => crate::pac::GPIOB::ptr() as _,
612-
'C' => crate::pac::GPIOC::ptr() as _,
613-
#[cfg(feature = "gpiod")]
614-
'D' => crate::pac::GPIOD::ptr() as _,
615-
#[cfg(feature = "gpioe")]
616-
'E' => crate::pac::GPIOE::ptr() as _,
617-
#[cfg(feature = "gpiof")]
618-
'F' => crate::pac::GPIOF::ptr() as _,
619-
#[cfg(feature = "gpiog")]
620-
'G' => crate::pac::GPIOG::ptr() as _,
621-
'H' => crate::pac::GPIOH::ptr() as _,
622-
#[cfg(feature = "gpioi")]
623-
'I' => crate::pac::GPIOI::ptr() as _,
624-
#[cfg(feature = "gpioj")]
625-
'J' => crate::pac::GPIOJ::ptr() as _,
626-
#[cfg(feature = "gpiok")]
627-
'K' => crate::pac::GPIOK::ptr() as _,
628-
_ => panic!("Unknown GPIO port"),
629-
}
606+
const fn gpiox<const P: char>() -> *const crate::pac::gpioa::RegisterBlock {
607+
match P {
608+
'A' => crate::pac::GPIOA::ptr(),
609+
'B' => crate::pac::GPIOB::ptr() as _,
610+
'C' => crate::pac::GPIOC::ptr() as _,
611+
#[cfg(feature = "gpiod")]
612+
'D' => crate::pac::GPIOD::ptr() as _,
613+
#[cfg(feature = "gpioe")]
614+
'E' => crate::pac::GPIOE::ptr() as _,
615+
#[cfg(feature = "gpiof")]
616+
'F' => crate::pac::GPIOF::ptr() as _,
617+
#[cfg(feature = "gpiog")]
618+
'G' => crate::pac::GPIOG::ptr() as _,
619+
'H' => crate::pac::GPIOH::ptr() as _,
620+
#[cfg(feature = "gpioi")]
621+
'I' => crate::pac::GPIOI::ptr() as _,
622+
#[cfg(feature = "gpioj")]
623+
'J' => crate::pac::GPIOJ::ptr() as _,
624+
#[cfg(feature = "gpiok")]
625+
'K' => crate::pac::GPIOK::ptr() as _,
626+
_ => panic!("Unknown GPIO port"),
630627
}
631628
}

src/gpio/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<const P: char, const N: u8, MODE: PinMode> Pin<P, N, MODE> {
9797
/// ensure they use this properly.
9898
#[inline(always)]
9999
pub(super) fn mode<M: PinMode>(&mut self) {
100-
change_mode!((*Gpio::<P>::ptr()), N);
100+
change_mode!((*gpiox::<P>()), N);
101101
}
102102

103103
#[inline(always)]
@@ -167,7 +167,7 @@ impl<const P: char, MODE: PinMode> PartiallyErasedPin<P, MODE> {
167167
#[inline(always)]
168168
pub(super) fn mode<M: PinMode>(&mut self) {
169169
let n = self.pin_id();
170-
change_mode!((*Gpio::<P>::ptr()), n);
170+
change_mode!((*gpiox::<P>()), n);
171171
}
172172

173173
#[inline(always)]

src/gpio/outport.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,17 @@ macro_rules! out_port {
3131
#[doc=concat!("Set/reset pins according to `", $n, "` lower bits")]
3232
#[inline(never)]
3333
pub fn write(&mut self, word: u32) {
34-
unsafe {
35-
(*Gpio::<P>::ptr())
36-
.bsrr
37-
.write(|w| w.bits(Self::value_for_write_bsrr(word)))
38-
}
34+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(Self::value_for_write_bsrr(word))) }
3935
}
4036

4137
/// Set all pins to `PinState::High`
4238
pub fn all_high(&mut self) {
43-
unsafe {
44-
(*Gpio::<P>::ptr())
45-
.bsrr
46-
.write(|w| w.bits(Self::mask()))
47-
}
39+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(Self::mask())) }
4840
}
4941

5042
/// Reset all pins to `PinState::Low`
5143
pub fn all_low(&mut self) {
52-
unsafe {
53-
(*Gpio::<P>::ptr())
54-
.bsrr
55-
.write(|w| w.bits(Self::mask() << 16))
56-
}
44+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(Self::mask() << 16)) }
5745
}
5846
}
5947
}
@@ -98,23 +86,19 @@ impl<const P: char, const SIZE: usize> OutPortArray<P, SIZE> {
9886
#[inline(never)]
9987
pub fn write(&mut self, word: u32) {
10088
unsafe {
101-
(*Gpio::<P>::ptr())
89+
(*gpiox::<P>())
10290
.bsrr
10391
.write(|w| w.bits(self.value_for_write_bsrr(word)))
10492
}
10593
}
10694

10795
/// Set all pins to `PinState::High`
10896
pub fn all_high(&mut self) {
109-
unsafe { (*Gpio::<P>::ptr()).bsrr.write(|w| w.bits(self.mask())) }
97+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(self.mask())) }
11098
}
11199

112100
/// Reset all pins to `PinState::Low`
113101
pub fn all_low(&mut self) {
114-
unsafe {
115-
(*Gpio::<P>::ptr())
116-
.bsrr
117-
.write(|w| w.bits(self.mask() << 16))
118-
}
102+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(self.mask() << 16)) }
119103
}
120104
}

src/gpio/partially_erased.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,14 @@ impl<const P: char, MODE> PartiallyErasedPin<P, Output<MODE>> {
6868
#[inline(always)]
6969
pub fn set_high(&mut self) {
7070
// NOTE(unsafe) atomic write to a stateless register
71-
unsafe { (*Gpio::<P>::ptr()).bsrr.write(|w| w.bits(1 << self.i)) }
71+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(1 << self.i)) }
7272
}
7373

7474
/// Drives the pin low
7575
#[inline(always)]
7676
pub fn set_low(&mut self) {
7777
// NOTE(unsafe) atomic write to a stateless register
78-
unsafe {
79-
(*Gpio::<P>::ptr())
80-
.bsrr
81-
.write(|w| w.bits(1 << (self.i + 16)))
82-
}
78+
unsafe { (*gpiox::<P>()).bsrr.write(|w| w.bits(1 << (self.i + 16))) }
8379
}
8480

8581
/// Is the pin in drive high or low mode?
@@ -111,7 +107,7 @@ impl<const P: char, MODE> PartiallyErasedPin<P, Output<MODE>> {
111107
#[inline(always)]
112108
pub fn is_set_low(&self) -> bool {
113109
// NOTE(unsafe) atomic read with no side effects
114-
unsafe { (*Gpio::<P>::ptr()).odr.read().bits() & (1 << self.i) == 0 }
110+
unsafe { (*gpiox::<P>()).odr.read().bits() & (1 << self.i) == 0 }
115111
}
116112

117113
/// Toggle pin output
@@ -139,7 +135,7 @@ where
139135
#[inline(always)]
140136
pub fn is_low(&self) -> bool {
141137
// NOTE(unsafe) atomic read with no side effects
142-
unsafe { (*Gpio::<P>::ptr()).idr.read().bits() & (1 << self.i) == 0 }
138+
unsafe { (*gpiox::<P>()).idr.read().bits() & (1 << self.i) == 0 }
143139
}
144140
}
145141

0 commit comments

Comments
 (0)