Skip to content

Commit eb68754

Browse files
committed
erasedpin -> anypin
1 parent 9d2b5b9 commit eb68754

File tree

6 files changed

+58
-47
lines changed

6 files changed

+58
-47
lines changed

src/gpio.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
//! ```rust
77
//! // Acquire the GPIOC peripheral
88
//! // NOTE: `dp` is the device peripherals from the `PAC` crate
9-
//! let mut gpioa = dp.GPIOA.split();
9+
//! let mut gpioa = dp.GPIOA.split(&mut rcc);
1010
//! ```
1111
//!
1212
//! This gives you a struct containing all the pins `px0..px15`.
13-
//! By default pins are in floating input mode. You can change their modes.
13+
//! By default pins are in `Analog` mode. You can change their modes.
1414
//! For example, to set `pa5` high, you would call
1515
//!
1616
//! ```rust
@@ -62,8 +62,8 @@ mod convert;
6262
pub use convert::PinMode;
6363
mod partially_erased;
6464
pub use partially_erased::{PEPin, PartiallyErasedPin};
65-
mod erased;
66-
pub use erased::{AnyPin, ErasedPin};
65+
mod anypin;
66+
pub use anypin::AnyPin;
6767
mod exti;
6868
pub use exti::ExtiPin;
6969
mod dynamic;
@@ -408,8 +408,8 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
408408
///
409409
/// This is useful when you want to collect the pins into an array where you
410410
/// need all the elements to have the same type
411-
pub fn erase(self) -> ErasedPin<MODE> {
412-
ErasedPin::new(P as u8 - b'A', N)
411+
pub fn erase(self) -> AnyPin<MODE> {
412+
AnyPin::new(P as u8 - b'A', N)
413413
}
414414
}
415415

@@ -422,7 +422,7 @@ impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>> for PartiallyErased
422422
}
423423
}
424424

425-
impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>> for ErasedPin<MODE> {
425+
impl<const P: char, const N: u8, MODE> From<Pin<P, N, MODE>> for AnyPin<MODE> {
426426
/// Pin-to-erased pin conversion using the [`From`] trait.
427427
///
428428
/// Note that [`From`] is the reciprocal of [`Into`].

src/gpio/erased.rs renamed to src/gpio/anypin.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use super::*;
22

3-
pub use ErasedPin as AnyPin;
4-
53
/// Fully erased pin
64
///
75
/// `MODE` is one of the pin modes (see [Modes](crate::gpio#modes) section).
8-
pub struct ErasedPin<MODE> {
6+
pub struct AnyPin<MODE> {
97
// Bits 0-3: Pin, Bits 4-7: Port
108
pin_port: u8,
119
_mode: PhantomData<MODE>,
1210
}
1311

14-
impl<MODE> fmt::Debug for ErasedPin<MODE> {
12+
impl<MODE> fmt::Debug for AnyPin<MODE> {
1513
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1614
formatter.write_fmt(format_args!(
1715
"P({}{})<{}>",
@@ -23,7 +21,7 @@ impl<MODE> fmt::Debug for ErasedPin<MODE> {
2321
}
2422

2523
#[cfg(feature = "defmt")]
26-
impl<MODE> defmt::Format for ErasedPin<MODE> {
24+
impl<MODE> defmt::Format for AnyPin<MODE> {
2725
fn format(&self, f: defmt::Formatter) {
2826
defmt::write!(
2927
f,
@@ -35,7 +33,7 @@ impl<MODE> defmt::Format for ErasedPin<MODE> {
3533
}
3634
}
3735

38-
impl<MODE> PinExt for ErasedPin<MODE> {
36+
impl<MODE> PinExt for AnyPin<MODE> {
3937
type Mode = MODE;
4038

4139
#[inline(always)]
@@ -48,7 +46,7 @@ impl<MODE> PinExt for ErasedPin<MODE> {
4846
}
4947
}
5048

51-
impl<MODE> ErasedPin<MODE> {
49+
impl<MODE> AnyPin<MODE> {
5250
pub(crate) fn from_pin_port(pin_port: u8) -> Self {
5351
Self {
5452
pin_port,
@@ -73,7 +71,7 @@ impl<MODE> ErasedPin<MODE> {
7371
}
7472

7573
#[inline]
76-
pub(crate) fn block(&self) -> &crate::pac::gpioa::RegisterBlock {
74+
pub(crate) unsafe fn block(&self) -> *const crate::pac::gpioa::RegisterBlock {
7775
// This function uses pointer arithmetic instead of branching to be more efficient
7876

7977
// The logic relies on the following assumptions:
@@ -86,29 +84,30 @@ impl<MODE> ErasedPin<MODE> {
8684
const GPIO_REGISTER_OFFSET: usize = 0x0400;
8785

8886
let offset = GPIO_REGISTER_OFFSET * self.port_id() as usize;
89-
let block_ptr =
90-
(crate::pac::GPIOA::ptr() as usize + offset) as *const crate::pac::gpioa::RegisterBlock;
91-
92-
unsafe { &*block_ptr }
87+
(crate::pac::GPIOA::ptr() as usize + offset) as *const crate::pac::gpioa::RegisterBlock
9388
}
9489
}
9590

96-
impl<MODE> ErasedPin<Output<MODE>> {
91+
impl<MODE> AnyPin<Output<MODE>> {
9792
/// Drives the pin high
9893
#[inline(always)]
9994
pub fn set_high(&mut self) {
10095
// NOTE(unsafe) atomic write to a stateless register
101-
unsafe { self.block().bsrr().write(|w| w.bits(1 << self.pin_id())) };
96+
unsafe {
97+
(*self.block())
98+
.bsrr()
99+
.write(|w| w.bs(self.pin_id()).set_bit())
100+
};
102101
}
103102

104103
/// Drives the pin low
105104
#[inline(always)]
106105
pub fn set_low(&mut self) {
107106
// NOTE(unsafe) atomic write to a stateless register
108107
unsafe {
109-
self.block()
108+
(*self.block())
110109
.bsrr()
111-
.write(|w| w.bits(1 << (self.pin_id() + 16)))
110+
.write(|w| w.br(self.pin_id()).set_bit())
112111
};
113112
}
114113

@@ -140,7 +139,13 @@ impl<MODE> ErasedPin<Output<MODE>> {
140139
/// Is the pin in drive low mode?
141140
#[inline(always)]
142141
pub fn is_set_low(&self) -> bool {
143-
self.block().odr().read().bits() & (1 << self.pin_id()) == 0
142+
unsafe {
143+
(*self.block())
144+
.odr()
145+
.read()
146+
.odr(self.pin_id())
147+
.bit_is_clear()
148+
}
144149
}
145150

146151
/// Toggle pin output
@@ -154,7 +159,7 @@ impl<MODE> ErasedPin<Output<MODE>> {
154159
}
155160
}
156161

157-
impl<MODE> ErasedPin<MODE>
162+
impl<MODE> AnyPin<MODE>
158163
where
159164
MODE: marker::Readable,
160165
{
@@ -167,6 +172,12 @@ where
167172
/// Is the input pin low?
168173
#[inline(always)]
169174
pub fn is_low(&self) -> bool {
170-
self.block().idr().read().bits() & (1 << self.pin_id()) == 0
175+
unsafe {
176+
(*self.block())
177+
.idr()
178+
.read()
179+
.idr(self.pin_id())
180+
.bit_is_clear()
181+
}
171182
}
172183
}

src/gpio/convert.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,19 @@ macro_rules! change_mode {
166166
}
167167
use change_mode;
168168

169-
use super::ErasedPin;
170-
impl<MODE: PinMode> ErasedPin<MODE> {
169+
use super::AnyPin;
170+
impl<MODE: PinMode> AnyPin<MODE> {
171171
#[inline(always)]
172172
pub(super) fn mode<M: PinMode>(&mut self) {
173173
let n = self.pin_id();
174-
change_mode!(self.block(), n);
174+
change_mode!((*self.block()), n);
175175
}
176176

177177
#[inline(always)]
178178
/// Converts pin into specified mode
179-
pub fn into_mode<M: PinMode>(mut self) -> ErasedPin<M> {
179+
pub fn into_mode<M: PinMode>(mut self) -> AnyPin<M> {
180180
self.mode::<M>();
181-
ErasedPin::from_pin_port(self.into_pin_port())
181+
AnyPin::from_pin_port(self.into_pin_port())
182182
}
183183
}
184184

src/gpio/hal_02.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::convert::Infallible;
22

33
use super::{
4-
dynamic::PinModeError, marker, DynamicPin, ErasedPin, Input, OpenDrain, Output,
4+
dynamic::PinModeError, marker, DynamicPin, AnyPin, Input, OpenDrain, Output,
55
PartiallyErasedPin, Pin, PinMode, PinState,
66
};
77

@@ -107,7 +107,7 @@ where
107107

108108
// Implementations for `ErasedPin`
109109

110-
impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
110+
impl<MODE> OutputPin for AnyPin<Output<MODE>> {
111111
type Error = core::convert::Infallible;
112112

113113
#[inline(always)]
@@ -123,7 +123,7 @@ impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
123123
}
124124
}
125125

126-
impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
126+
impl<MODE> StatefulOutputPin for AnyPin<Output<MODE>> {
127127
#[inline(always)]
128128
fn is_set_high(&self) -> Result<bool, Self::Error> {
129129
Ok(self.is_set_high())
@@ -135,7 +135,7 @@ impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
135135
}
136136
}
137137

138-
impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
138+
impl<MODE> ToggleableOutputPin for AnyPin<Output<MODE>> {
139139
type Error = Infallible;
140140

141141
#[inline(always)]
@@ -145,7 +145,7 @@ impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
145145
}
146146
}
147147

148-
impl<MODE> InputPin for ErasedPin<MODE>
148+
impl<MODE> InputPin for AnyPin<MODE>
149149
where
150150
MODE: marker::Readable,
151151
{

src/gpio/hal_1.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::convert::Infallible;
22

33
use super::{
4-
dynamic::PinModeError, marker, DynamicPin, ErasedPin, Output, PartiallyErasedPin, Pin,
4+
dynamic::PinModeError, marker, DynamicPin, AnyPin, Output, PartiallyErasedPin, Pin,
55
};
66

77
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};
@@ -53,11 +53,11 @@ where
5353
}
5454

5555
// Implementations for `ErasedPin`
56-
impl<MODE> ErrorType for ErasedPin<MODE> {
56+
impl<MODE> ErrorType for AnyPin<MODE> {
5757
type Error = core::convert::Infallible;
5858
}
5959

60-
impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
60+
impl<MODE> OutputPin for AnyPin<Output<MODE>> {
6161
#[inline(always)]
6262
fn set_high(&mut self) -> Result<(), Self::Error> {
6363
self.set_high();
@@ -71,7 +71,7 @@ impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
7171
}
7272
}
7373

74-
impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
74+
impl<MODE> StatefulOutputPin for AnyPin<Output<MODE>> {
7575
#[inline(always)]
7676
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
7777
Ok(Self::is_set_high(self))
@@ -83,7 +83,7 @@ impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
8383
}
8484
}
8585

86-
impl<MODE> InputPin for ErasedPin<MODE>
86+
impl<MODE> InputPin for AnyPin<MODE>
8787
where
8888
MODE: marker::Readable,
8989
{

src/gpio/partially_erased.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<const P: char, MODE> PartiallyErasedPin<P, Output<MODE>> {
6969
pub fn set_high(&mut self) {
7070
// NOTE(unsafe) atomic write to a stateless register
7171
unsafe {
72-
(*gpiox::<P>()).bsrr().write(|w| w.bits(1 << self.i));
72+
(*gpiox::<P>()).bsrr().write(|w| w.bs(self.i).set_bit());
7373
}
7474
}
7575

@@ -78,7 +78,7 @@ impl<const P: char, MODE> PartiallyErasedPin<P, Output<MODE>> {
7878
pub fn set_low(&mut self) {
7979
// NOTE(unsafe) atomic write to a stateless register
8080
unsafe {
81-
(*gpiox::<P>()).bsrr().write(|w| w.bits(1 << (self.i + 16)));
81+
(*gpiox::<P>()).bsrr().write(|w| w.br(self.i).set_bit());
8282
}
8383
}
8484

@@ -111,7 +111,7 @@ impl<const P: char, MODE> PartiallyErasedPin<P, Output<MODE>> {
111111
#[inline(always)]
112112
pub fn is_set_low(&self) -> bool {
113113
// NOTE(unsafe) atomic read with no side effects
114-
unsafe { (*gpiox::<P>()).odr().read().bits() & (1 << self.i) == 0 }
114+
unsafe { (*gpiox::<P>()).odr().read().odr(self.i).bit_is_clear() }
115115
}
116116

117117
/// Toggle pin output
@@ -139,15 +139,15 @@ where
139139
#[inline(always)]
140140
pub fn is_low(&self) -> bool {
141141
// NOTE(unsafe) atomic read with no side effects
142-
unsafe { (*gpiox::<P>()).idr().read().bits() & (1 << self.i) == 0 }
142+
unsafe { (*gpiox::<P>()).idr().read().idr(self.i).bit_is_clear() }
143143
}
144144
}
145145

146-
impl<const P: char, MODE> From<PartiallyErasedPin<P, MODE>> for ErasedPin<MODE> {
146+
impl<const P: char, MODE> From<PartiallyErasedPin<P, MODE>> for AnyPin<MODE> {
147147
/// Partially erased pin-to-erased pin conversion using the [`From`] trait.
148148
///
149149
/// Note that [`From`] is the reciprocal of [`Into`].
150150
fn from(p: PartiallyErasedPin<P, MODE>) -> Self {
151-
ErasedPin::new(P as u8 - b'A', p.i)
151+
AnyPin::new(P as u8 - b'A', p.i)
152152
}
153153
}

0 commit comments

Comments
 (0)