Skip to content

Commit ce4994c

Browse files
bors[bot]burrbull
andauthored
Merge #470
470: more gpio cleanups r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents cf1eace + 8e15939 commit ce4994c

File tree

6 files changed

+85
-170
lines changed

6 files changed

+85
-170
lines changed

CHANGELOG.md

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

1212
- CI updates + cache
13+
- Add missing `embedded-hal 1.0` for `DynamicPin`
1314
- Remove pull resistor from `Input` mode, use `Pull` enum instead, add universal `into_mode` pin converter
1415
- Move pin mode at the end of generics, add defaults for modes,
1516
bump MSRV to 1.59 [#418]

src/gpio.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ pub type Debugger = Alternate<0, PushPull>;
135135
mod sealed {
136136
/// Marker trait that show if `ExtiPin` can be implemented
137137
pub trait Interruptable {}
138+
/// Marker trait for readable pin modes
139+
pub trait Readable {}
138140
/// Marker trait for slew rate configurable pin modes
139141
pub trait OutputSpeed {}
140142
/// Marker trait for active pin modes
@@ -143,6 +145,8 @@ mod sealed {
143145
pub trait NotAlt {}
144146
}
145147

148+
impl sealed::Readable for Input {}
149+
impl sealed::Readable for Output<OpenDrain> {}
146150
impl sealed::Active for Input {}
147151
impl<Otype> sealed::OutputSpeed for Output<Otype> {}
148152
impl<const A: u8, Otype> sealed::OutputSpeed for Alternate<A, Otype> {}
@@ -479,19 +483,10 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, Output<MODE>> {
479483
}
480484
}
481485

482-
impl<const P: char, const N: u8> Pin<P, N, Output<OpenDrain>> {
483-
#[inline(always)]
484-
pub fn is_high(&self) -> bool {
485-
!self.is_low()
486-
}
487-
488-
#[inline(always)]
489-
pub fn is_low(&self) -> bool {
490-
self._is_low()
491-
}
492-
}
493-
494-
impl<const P: char, const N: u8> Pin<P, N, Input> {
486+
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
487+
where
488+
MODE: sealed::Readable,
489+
{
495490
#[inline(always)]
496491
pub fn is_high(&self) -> bool {
497492
!self.is_low()

src/gpio/erased.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,10 @@ impl<MODE> ErasedPin<Output<MODE>> {
131131
}
132132
}
133133

134-
impl ErasedPin<Output<OpenDrain>> {
135-
#[inline(always)]
136-
pub fn is_high(&self) -> bool {
137-
!self.is_low()
138-
}
139-
140-
#[inline(always)]
141-
pub fn is_low(&self) -> bool {
142-
self.block().idr.read().bits() & (1 << self.pin_id()) == 0
143-
}
144-
}
145-
146-
impl ErasedPin<Input> {
134+
impl<MODE> ErasedPin<MODE>
135+
where
136+
MODE: super::sealed::Readable,
137+
{
147138
#[inline(always)]
148139
pub fn is_high(&self) -> bool {
149140
!self.is_low()

src/gpio/hal_02.rs

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::convert::Infallible;
22

33
use super::{
44
dynamic::PinModeError, DynamicPin, ErasedPin, Input, OpenDrain, Output, PartiallyErasedPin,
5-
Pin, PinState, PushPull,
5+
Pin, PinMode, PinState,
66
};
77

88
use embedded_hal::digital::v2::{
@@ -49,21 +49,10 @@ impl<const P: char, const N: u8, MODE> ToggleableOutputPin for Pin<P, N, Output<
4949
}
5050
}
5151

52-
impl<const P: char, const N: u8> InputPin for Pin<P, N, Output<OpenDrain>> {
53-
type Error = Infallible;
54-
55-
#[inline(always)]
56-
fn is_high(&self) -> Result<bool, Self::Error> {
57-
Ok(self.is_high())
58-
}
59-
60-
#[inline(always)]
61-
fn is_low(&self) -> Result<bool, Self::Error> {
62-
Ok(self.is_low())
63-
}
64-
}
65-
66-
impl<const P: char, const N: u8> InputPin for Pin<P, N, Input> {
52+
impl<const P: char, const N: u8, MODE> InputPin for Pin<P, N, MODE>
53+
where
54+
MODE: super::sealed::Readable,
55+
{
6756
type Error = Infallible;
6857

6958
#[inline(always)]
@@ -88,28 +77,10 @@ impl<const P: char, const N: u8> IoPin<Self, Self> for Pin<P, N, Output<OpenDrai
8877
}
8978
}
9079

91-
impl<const P: char, const N: u8> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Output<OpenDrain>> {
92-
type Error = Infallible;
93-
fn into_input_pin(self) -> Result<Pin<P, N, Input>, Self::Error> {
94-
Ok(self.into_input())
95-
}
96-
fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
97-
self.set_state(state);
98-
Ok(self)
99-
}
100-
}
101-
102-
impl<const P: char, const N: u8> IoPin<Self, Pin<P, N, Output<OpenDrain>>> for Pin<P, N, Input> {
103-
type Error = Infallible;
104-
fn into_input_pin(self) -> Result<Self, Self::Error> {
105-
Ok(self)
106-
}
107-
fn into_output_pin(self, state: PinState) -> Result<Pin<P, N, Output<OpenDrain>>, Self::Error> {
108-
Ok(self.into_open_drain_output_in_state(state))
109-
}
110-
}
111-
112-
impl<const P: char, const N: u8> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Output<PushPull>> {
80+
impl<const P: char, const N: u8, Otype> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Output<Otype>>
81+
where
82+
Output<Otype>: PinMode,
83+
{
11384
type Error = Infallible;
11485
fn into_input_pin(self) -> Result<Pin<P, N, Input>, Self::Error> {
11586
Ok(self.into_input())
@@ -120,13 +91,17 @@ impl<const P: char, const N: u8> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Out
12091
}
12192
}
12293

123-
impl<const P: char, const N: u8> IoPin<Self, Pin<P, N, Output<PushPull>>> for Pin<P, N, Input> {
94+
impl<const P: char, const N: u8, Otype> IoPin<Self, Pin<P, N, Output<Otype>>> for Pin<P, N, Input>
95+
where
96+
Output<Otype>: PinMode,
97+
{
12498
type Error = Infallible;
12599
fn into_input_pin(self) -> Result<Self, Self::Error> {
126100
Ok(self)
127101
}
128-
fn into_output_pin(self, state: PinState) -> Result<Pin<P, N, Output<PushPull>>, Self::Error> {
129-
Ok(self.into_push_pull_output_in_state(state))
102+
fn into_output_pin(mut self, state: PinState) -> Result<Pin<P, N, Output<Otype>>, Self::Error> {
103+
self._set_state(state);
104+
Ok(self.into_mode())
130105
}
131106
}
132107

@@ -170,21 +145,10 @@ impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
170145
}
171146
}
172147

173-
impl InputPin for ErasedPin<Output<OpenDrain>> {
174-
type Error = core::convert::Infallible;
175-
176-
#[inline(always)]
177-
fn is_high(&self) -> Result<bool, Self::Error> {
178-
Ok(self.is_high())
179-
}
180-
181-
#[inline(always)]
182-
fn is_low(&self) -> Result<bool, Self::Error> {
183-
Ok(self.is_low())
184-
}
185-
}
186-
187-
impl InputPin for ErasedPin<Input> {
148+
impl<MODE> InputPin for ErasedPin<MODE>
149+
where
150+
MODE: super::sealed::Readable,
151+
{
188152
type Error = core::convert::Infallible;
189153

190154
#[inline(always)]
@@ -238,21 +202,10 @@ impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<M
238202
}
239203
}
240204

241-
impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> {
242-
type Error = Infallible;
243-
244-
#[inline(always)]
245-
fn is_high(&self) -> Result<bool, Self::Error> {
246-
Ok(self.is_high())
247-
}
248-
249-
#[inline(always)]
250-
fn is_low(&self) -> Result<bool, Self::Error> {
251-
Ok(self.is_low())
252-
}
253-
}
254-
255-
impl<const P: char> InputPin for PartiallyErasedPin<P, Input> {
205+
impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, MODE>
206+
where
207+
MODE: super::sealed::Readable,
208+
{
256209
type Error = Infallible;
257210

258211
#[inline(always)]

src/gpio/hal_1.rs

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use core::convert::Infallible;
22

3-
use super::{ErasedPin, Input, OpenDrain, Output, PartiallyErasedPin, Pin, PushPull};
3+
use super::{
4+
dynamic::PinModeError, DynamicPin, ErasedPin, Input, OpenDrain, Output, PartiallyErasedPin,
5+
Pin, PinMode,
6+
};
47

58
pub use embedded_hal_one::digital::PinState;
69
use embedded_hal_one::digital::{
@@ -54,19 +57,10 @@ impl<const P: char, const N: u8, MODE> ToggleableOutputPin for Pin<P, N, Output<
5457
}
5558
}
5659

57-
impl<const P: char, const N: u8> InputPin for Pin<P, N, Output<OpenDrain>> {
58-
#[inline(always)]
59-
fn is_high(&self) -> Result<bool, Self::Error> {
60-
Ok(self.is_high())
61-
}
62-
63-
#[inline(always)]
64-
fn is_low(&self) -> Result<bool, Self::Error> {
65-
Ok(self.is_low())
66-
}
67-
}
68-
69-
impl<const P: char, const N: u8> InputPin for Pin<P, N, Input> {
60+
impl<const P: char, const N: u8, MODE> InputPin for Pin<P, N, MODE>
61+
where
62+
MODE: super::sealed::Readable,
63+
{
7064
#[inline(always)]
7165
fn is_high(&self) -> Result<bool, Self::Error> {
7266
Ok(self.is_high())
@@ -89,7 +83,10 @@ impl<const P: char, const N: u8> IoPin<Self, Self> for Pin<P, N, Output<OpenDrai
8983
}
9084
}
9185

92-
impl<const P: char, const N: u8> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Output<OpenDrain>> {
86+
impl<const P: char, const N: u8, Otype> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Output<Otype>>
87+
where
88+
Output<Otype>: PinMode,
89+
{
9390
type Error = Infallible;
9491
fn into_input_pin(self) -> Result<Pin<P, N, Input>, Self::Error> {
9592
Ok(self.into_input())
@@ -100,34 +97,17 @@ impl<const P: char, const N: u8> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Out
10097
}
10198
}
10299

103-
impl<const P: char, const N: u8> IoPin<Self, Pin<P, N, Output<OpenDrain>>> for Pin<P, N, Input> {
100+
impl<const P: char, const N: u8, Otype> IoPin<Self, Pin<P, N, Output<Otype>>> for Pin<P, N, Input>
101+
where
102+
Output<Otype>: PinMode,
103+
{
104104
type Error = Infallible;
105105
fn into_input_pin(self) -> Result<Self, Self::Error> {
106106
Ok(self)
107107
}
108-
fn into_output_pin(self, state: PinState) -> Result<Pin<P, N, Output<OpenDrain>>, Self::Error> {
109-
Ok(self.into_open_drain_output_in_state(into_state(state)))
110-
}
111-
}
112-
113-
impl<const P: char, const N: u8> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Output<PushPull>> {
114-
type Error = Infallible;
115-
fn into_input_pin(self) -> Result<Pin<P, N, Input>, Self::Error> {
116-
Ok(self.into_input())
117-
}
118-
fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
119-
self.set_state(into_state(state));
120-
Ok(self)
121-
}
122-
}
123-
124-
impl<const P: char, const N: u8> IoPin<Self, Pin<P, N, Output<PushPull>>> for Pin<P, N, Input> {
125-
type Error = Infallible;
126-
fn into_input_pin(self) -> Result<Self, Self::Error> {
127-
Ok(self)
128-
}
129-
fn into_output_pin(self, state: PinState) -> Result<Pin<P, N, Output<PushPull>>, Self::Error> {
130-
Ok(self.into_push_pull_output_in_state(into_state(state)))
108+
fn into_output_pin(mut self, state: PinState) -> Result<Pin<P, N, Output<Otype>>, Self::Error> {
109+
self._set_state(into_state(state));
110+
Ok(self.into_mode())
131111
}
132112
}
133113

@@ -170,19 +150,10 @@ impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
170150
}
171151
}
172152

173-
impl InputPin for ErasedPin<Output<OpenDrain>> {
174-
#[inline(always)]
175-
fn is_high(&self) -> Result<bool, Self::Error> {
176-
Ok(self.is_high())
177-
}
178-
179-
#[inline(always)]
180-
fn is_low(&self) -> Result<bool, Self::Error> {
181-
Ok(self.is_low())
182-
}
183-
}
184-
185-
impl InputPin for ErasedPin<Input> {
153+
impl<MODE> InputPin for ErasedPin<MODE>
154+
where
155+
MODE: super::sealed::Readable,
156+
{
186157
#[inline(always)]
187158
fn is_high(&self) -> Result<bool, Self::Error> {
188159
Ok(self.is_high())
@@ -233,7 +204,10 @@ impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<M
233204
}
234205
}
235206

236-
impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> {
207+
impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, MODE>
208+
where
209+
MODE: super::sealed::Readable,
210+
{
237211
#[inline(always)]
238212
fn is_high(&self) -> Result<bool, Self::Error> {
239213
Ok(self.is_high())
@@ -245,14 +219,25 @@ impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> {
245219
}
246220
}
247221

248-
impl<const P: char> InputPin for PartiallyErasedPin<P, Input> {
249-
#[inline(always)]
250-
fn is_high(&self) -> Result<bool, Self::Error> {
251-
Ok(self.is_high())
222+
// Implementations for `DynamicPin
223+
impl<const P: char, const N: u8> ErrorType for DynamicPin<P, N> {
224+
type Error = PinModeError;
225+
}
226+
227+
impl<const P: char, const N: u8> OutputPin for DynamicPin<P, N> {
228+
fn set_high(&mut self) -> Result<(), Self::Error> {
229+
self.set_high()
252230
}
231+
fn set_low(&mut self) -> Result<(), Self::Error> {
232+
self.set_low()
233+
}
234+
}
253235

254-
#[inline(always)]
236+
impl<const P: char, const N: u8> InputPin for DynamicPin<P, N> {
237+
fn is_high(&self) -> Result<bool, Self::Error> {
238+
self.is_high()
239+
}
255240
fn is_low(&self) -> Result<bool, Self::Error> {
256-
Ok(self.is_low())
241+
self.is_low()
257242
}
258243
}

src/gpio/partially_erased.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,10 @@ impl<const P: char, MODE> PartiallyErasedPin<P, Output<MODE>> {
112112
}
113113
}
114114

115-
impl<const P: char> PartiallyErasedPin<P, Output<OpenDrain>> {
116-
#[inline(always)]
117-
pub fn is_high(&self) -> bool {
118-
!self.is_low()
119-
}
120-
121-
#[inline(always)]
122-
pub fn is_low(&self) -> bool {
123-
// NOTE(unsafe) atomic read with no side effects
124-
unsafe { (*Gpio::<P>::ptr()).idr.read().bits() & (1 << self.i) == 0 }
125-
}
126-
}
127-
128-
impl<const P: char> PartiallyErasedPin<P, Input> {
115+
impl<const P: char, MODE> PartiallyErasedPin<P, MODE>
116+
where
117+
MODE: super::sealed::Readable,
118+
{
129119
#[inline(always)]
130120
pub fn is_high(&self) -> bool {
131121
!self.is_low()

0 commit comments

Comments
 (0)