Skip to content

Commit 8e15939

Browse files
committed
InputPin cleanups & DynamicPin ehal1
1 parent 4ecf7df commit 8e15939

File tree

6 files changed

+62
-119
lines changed

6 files changed

+62
-119
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: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
@@ -156,21 +145,10 @@ impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
156145
}
157146
}
158147

159-
impl InputPin for ErasedPin<Output<OpenDrain>> {
160-
type Error = core::convert::Infallible;
161-
162-
#[inline(always)]
163-
fn is_high(&self) -> Result<bool, Self::Error> {
164-
Ok(self.is_high())
165-
}
166-
167-
#[inline(always)]
168-
fn is_low(&self) -> Result<bool, Self::Error> {
169-
Ok(self.is_low())
170-
}
171-
}
172-
173-
impl InputPin for ErasedPin<Input> {
148+
impl<MODE> InputPin for ErasedPin<MODE>
149+
where
150+
MODE: super::sealed::Readable,
151+
{
174152
type Error = core::convert::Infallible;
175153

176154
#[inline(always)]
@@ -224,21 +202,10 @@ impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<M
224202
}
225203
}
226204

227-
impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> {
228-
type Error = Infallible;
229-
230-
#[inline(always)]
231-
fn is_high(&self) -> Result<bool, Self::Error> {
232-
Ok(self.is_high())
233-
}
234-
235-
#[inline(always)]
236-
fn is_low(&self) -> Result<bool, Self::Error> {
237-
Ok(self.is_low())
238-
}
239-
}
240-
241-
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+
{
242209
type Error = Infallible;
243210

244211
#[inline(always)]

src/gpio/hal_1.rs

Lines changed: 33 additions & 34 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, PinMode};
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())
@@ -156,19 +150,10 @@ impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
156150
}
157151
}
158152

159-
impl InputPin for ErasedPin<Output<OpenDrain>> {
160-
#[inline(always)]
161-
fn is_high(&self) -> Result<bool, Self::Error> {
162-
Ok(self.is_high())
163-
}
164-
165-
#[inline(always)]
166-
fn is_low(&self) -> Result<bool, Self::Error> {
167-
Ok(self.is_low())
168-
}
169-
}
170-
171-
impl InputPin for ErasedPin<Input> {
153+
impl<MODE> InputPin for ErasedPin<MODE>
154+
where
155+
MODE: super::sealed::Readable,
156+
{
172157
#[inline(always)]
173158
fn is_high(&self) -> Result<bool, Self::Error> {
174159
Ok(self.is_high())
@@ -219,7 +204,10 @@ impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<M
219204
}
220205
}
221206

222-
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+
{
223211
#[inline(always)]
224212
fn is_high(&self) -> Result<bool, Self::Error> {
225213
Ok(self.is_high())
@@ -231,14 +219,25 @@ impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> {
231219
}
232220
}
233221

234-
impl<const P: char> InputPin for PartiallyErasedPin<P, Input> {
235-
#[inline(always)]
236-
fn is_high(&self) -> Result<bool, Self::Error> {
237-
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()
230+
}
231+
fn set_low(&mut self) -> Result<(), Self::Error> {
232+
self.set_low()
238233
}
234+
}
239235

240-
#[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+
}
241240
fn is_low(&self) -> Result<bool, Self::Error> {
242-
Ok(self.is_low())
241+
self.is_low()
243242
}
244243
}

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)