Skip to content

Commit aa1959a

Browse files
authored
gpio: Add embedded-hal implementation (#33)
This adds the implementation of the embedded-hal traits for the various GPIO struct definitions.
1 parent 1bfebd8 commit aa1959a

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

src/gpio.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ mod dynamic;
5959
mod erased;
6060
mod exti;
6161
mod gpio_def;
62+
mod hal;
6263
mod partially_erased;
6364

6465
use core::{fmt, marker::PhantomData};

src/gpio/hal.rs

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
use core::convert::Infallible;
2+
3+
use super::{
4+
dynamic::PinModeError, marker, DynamicPin, ErasedPin, Output,
5+
PartiallyErasedPin, Pin,
6+
};
7+
8+
use embedded_hal::digital::{
9+
Error, ErrorKind, ErrorType, InputPin, OutputPin, StatefulOutputPin,
10+
};
11+
12+
// Implementations for `Pin`
13+
14+
impl<const P: char, const N: u8, MODE> ErrorType for Pin<P, N, MODE> {
15+
type Error = Infallible;
16+
}
17+
18+
impl<const P: char, const N: u8, MODE> OutputPin for Pin<P, N, Output<MODE>> {
19+
#[inline(always)]
20+
fn set_high(&mut self) -> Result<(), Self::Error> {
21+
self.set_high();
22+
Ok(())
23+
}
24+
25+
#[inline(always)]
26+
fn set_low(&mut self) -> Result<(), Self::Error> {
27+
self.set_low();
28+
Ok(())
29+
}
30+
}
31+
32+
impl<const P: char, const N: u8, MODE> StatefulOutputPin
33+
for Pin<P, N, Output<MODE>>
34+
{
35+
#[inline(always)]
36+
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
37+
Ok(self.is_set_high())
38+
}
39+
40+
#[inline(always)]
41+
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
42+
Ok(self.is_set_low())
43+
}
44+
}
45+
46+
impl<const P: char, const N: u8, MODE> InputPin for Pin<P, N, MODE>
47+
where
48+
MODE: marker::Readable,
49+
{
50+
#[inline(always)]
51+
fn is_high(&mut self) -> Result<bool, Self::Error> {
52+
Ok(self.is_high())
53+
}
54+
55+
#[inline(always)]
56+
fn is_low(&mut self) -> Result<bool, Self::Error> {
57+
Ok(self.is_low())
58+
}
59+
}
60+
61+
// Implementations for `ErasedPin`
62+
impl<MODE> ErrorType for ErasedPin<MODE> {
63+
type Error = Infallible;
64+
}
65+
66+
impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
67+
#[inline(always)]
68+
fn set_high(&mut self) -> Result<(), Self::Error> {
69+
self.set_high();
70+
Ok(())
71+
}
72+
73+
#[inline(always)]
74+
fn set_low(&mut self) -> Result<(), Self::Error> {
75+
self.set_low();
76+
Ok(())
77+
}
78+
}
79+
80+
impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
81+
#[inline(always)]
82+
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
83+
Ok(self.is_set_high())
84+
}
85+
86+
#[inline(always)]
87+
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
88+
Ok(self.is_set_low())
89+
}
90+
}
91+
92+
impl<MODE> InputPin for ErasedPin<MODE>
93+
where
94+
MODE: marker::Readable,
95+
{
96+
#[inline(always)]
97+
fn is_high(&mut self) -> Result<bool, Self::Error> {
98+
Ok(self.is_high())
99+
}
100+
101+
#[inline(always)]
102+
fn is_low(&mut self) -> Result<bool, Self::Error> {
103+
Ok(self.is_low())
104+
}
105+
}
106+
107+
// Implementations for `PartiallyErasedPin`
108+
impl<const P: char, MODE> ErrorType for PartiallyErasedPin<P, MODE> {
109+
type Error = Infallible;
110+
}
111+
112+
impl<const P: char, MODE> OutputPin for PartiallyErasedPin<P, Output<MODE>> {
113+
#[inline(always)]
114+
fn set_high(&mut self) -> Result<(), Self::Error> {
115+
self.set_high();
116+
Ok(())
117+
}
118+
119+
#[inline(always)]
120+
fn set_low(&mut self) -> Result<(), Self::Error> {
121+
self.set_low();
122+
Ok(())
123+
}
124+
}
125+
126+
impl<const P: char, MODE> StatefulOutputPin
127+
for PartiallyErasedPin<P, Output<MODE>>
128+
{
129+
#[inline(always)]
130+
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
131+
Ok(self.is_set_high())
132+
}
133+
134+
#[inline(always)]
135+
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
136+
Ok(self.is_set_low())
137+
}
138+
}
139+
140+
impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, MODE>
141+
where
142+
MODE: marker::Readable,
143+
{
144+
#[inline(always)]
145+
fn is_high(&mut self) -> Result<bool, Self::Error> {
146+
Ok(PartiallyErasedPin::is_high(self))
147+
}
148+
149+
#[inline(always)]
150+
fn is_low(&mut self) -> Result<bool, Self::Error> {
151+
Ok(PartiallyErasedPin::is_low(self))
152+
}
153+
}
154+
155+
impl Error for PinModeError {
156+
fn kind(&self) -> ErrorKind {
157+
ErrorKind::Other
158+
}
159+
}
160+
161+
// Implementations for `DynamicPin`
162+
impl<const P: char, const N: u8> ErrorType for DynamicPin<P, N> {
163+
type Error = PinModeError;
164+
}
165+
166+
impl<const P: char, const N: u8> OutputPin for DynamicPin<P, N> {
167+
fn set_high(&mut self) -> Result<(), Self::Error> {
168+
self.set_high()
169+
}
170+
fn set_low(&mut self) -> Result<(), Self::Error> {
171+
self.set_low()
172+
}
173+
}
174+
175+
impl<const P: char, const N: u8> InputPin for DynamicPin<P, N> {
176+
fn is_high(&mut self) -> Result<bool, Self::Error> {
177+
DynamicPin::is_high(self)
178+
}
179+
fn is_low(&mut self) -> Result<bool, Self::Error> {
180+
DynamicPin::is_low(self)
181+
}
182+
}

0 commit comments

Comments
 (0)