Skip to content

Commit 5e1c5a3

Browse files
committed
gpio & serial
1 parent 1892463 commit 5e1c5a3

File tree

6 files changed

+367
-18
lines changed

6 files changed

+367
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
fix errata.
1515
- `PwmHz::get_period`: fix computation of return value, prevent division by zero
1616
- return `i2c::Error::Timeout` instead of `nb::WouldBlock` when time is out
17+
- support `embedded-hal-1.0-alpha`
18+
- `gpio`: port and pin generics first, then mode, `PinMode` for modes instead of pins, other cleanups
1719

1820
### Breaking changes
1921

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ fugit-timer = "0.1.3"
3131
rtic-monotonic = { version = "1.0", optional = true }
3232
bitflags = "1.3.2"
3333

34+
[dependencies.embedded-hal-one]
35+
version = "=1.0.0-alpha.8"
36+
package = "embedded-hal"
37+
3438
[dependencies.stm32-usbd]
3539
version = "0.6.0"
3640
optional = true

src/gpio.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ mod erased;
8585
pub use erased::{EPin, ErasedPin};
8686

8787
mod hal_02;
88+
mod hal_1;
8889

8990
/// Slew rates available for Output and relevant AlternateMode Pins
9091
///
@@ -171,11 +172,7 @@ pub struct Alternate<MODE = PushPull> {
171172
impl<MODE> Active for Alternate<MODE> {}
172173

173174
/// Digital output pin state
174-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
175-
pub enum PinState {
176-
High,
177-
Low,
178-
}
175+
pub use embedded_hal::digital::v2::PinState;
179176

180177
// Using SCREAMING_SNAKE_CASE to be consistent with other HALs
181178
// see 59b2740 and #125 for motivation

src/gpio/hal_1.rs

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
use core::convert::Infallible;
2+
3+
use super::{Dynamic, ErasedPin, Input, OpenDrain, Output, PartiallyErasedPin, Pin, PinModeError};
4+
5+
pub use embedded_hal_one::digital::PinState;
6+
use embedded_hal_one::digital::{
7+
blocking::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin},
8+
ErrorType,
9+
};
10+
11+
fn into_state(state: PinState) -> super::PinState {
12+
match state {
13+
PinState::Low => super::PinState::Low,
14+
PinState::High => super::PinState::High,
15+
}
16+
}
17+
18+
// Implementations for `Pin`
19+
impl<const P: char, const N: u8, MODE> ErrorType for Pin<P, N, Output<MODE>> {
20+
type Error = Infallible;
21+
}
22+
impl<const P: char, const N: u8, MODE> ErrorType for Pin<P, N, Input<MODE>> {
23+
type Error = Infallible;
24+
}
25+
26+
impl<const P: char, const N: u8> ErrorType for Pin<P, N, Dynamic> {
27+
type Error = PinModeError;
28+
}
29+
30+
impl<const P: char, const N: u8> OutputPin for Pin<P, N, Dynamic> {
31+
fn set_high(&mut self) -> Result<(), Self::Error> {
32+
if self.mode.is_output() {
33+
self._set_state(into_state(PinState::High));
34+
Ok(())
35+
} else {
36+
Err(PinModeError::IncorrectMode)
37+
}
38+
}
39+
fn set_low(&mut self) -> Result<(), Self::Error> {
40+
if self.mode.is_output() {
41+
self._set_state(into_state(PinState::Low));
42+
Ok(())
43+
} else {
44+
Err(PinModeError::IncorrectMode)
45+
}
46+
}
47+
}
48+
49+
impl<const P: char, const N: u8> InputPin for Pin<P, N, Dynamic> {
50+
fn is_high(&self) -> Result<bool, Self::Error> {
51+
self.is_low().map(|b| !b)
52+
}
53+
fn is_low(&self) -> Result<bool, Self::Error> {
54+
if self.mode.is_input() {
55+
Ok(self._is_low())
56+
} else {
57+
Err(PinModeError::IncorrectMode)
58+
}
59+
}
60+
}
61+
62+
impl<const P: char, const N: u8, MODE> OutputPin for Pin<P, N, Output<MODE>> {
63+
#[inline]
64+
fn set_high(&mut self) -> Result<(), Self::Error> {
65+
self.set_high();
66+
Ok(())
67+
}
68+
#[inline]
69+
fn set_low(&mut self) -> Result<(), Self::Error> {
70+
self.set_low();
71+
Ok(())
72+
}
73+
}
74+
75+
impl<const P: char, const N: u8, MODE> StatefulOutputPin for Pin<P, N, Output<MODE>> {
76+
#[inline]
77+
fn is_set_high(&self) -> Result<bool, Self::Error> {
78+
Ok(self.is_set_high())
79+
}
80+
#[inline]
81+
fn is_set_low(&self) -> Result<bool, Self::Error> {
82+
Ok(self.is_set_low())
83+
}
84+
}
85+
86+
impl<const P: char, const N: u8, MODE> ToggleableOutputPin for Pin<P, N, Output<MODE>> {
87+
#[inline(always)]
88+
fn toggle(&mut self) -> Result<(), Self::Error> {
89+
self.toggle();
90+
Ok(())
91+
}
92+
}
93+
94+
impl<const P: char, const N: u8, MODE> InputPin for Pin<P, N, Input<MODE>> {
95+
#[inline]
96+
fn is_high(&self) -> Result<bool, Self::Error> {
97+
Ok(self.is_high())
98+
}
99+
100+
#[inline]
101+
fn is_low(&self) -> Result<bool, Self::Error> {
102+
Ok(self.is_low())
103+
}
104+
}
105+
106+
impl<const P: char, const N: u8> InputPin for Pin<P, N, Output<OpenDrain>> {
107+
#[inline]
108+
fn is_high(&self) -> Result<bool, Self::Error> {
109+
Ok(self.is_high())
110+
}
111+
112+
#[inline]
113+
fn is_low(&self) -> Result<bool, Self::Error> {
114+
Ok(self.is_low())
115+
}
116+
}
117+
118+
// PartiallyErasedPin
119+
120+
impl<const P: char, MODE> ErrorType for PartiallyErasedPin<P, MODE> {
121+
type Error = Infallible;
122+
}
123+
124+
impl<const P: char, MODE> OutputPin for PartiallyErasedPin<P, Output<MODE>> {
125+
#[inline(always)]
126+
fn set_high(&mut self) -> Result<(), Self::Error> {
127+
self.set_high();
128+
Ok(())
129+
}
130+
131+
#[inline(always)]
132+
fn set_low(&mut self) -> Result<(), Self::Error> {
133+
self.set_low();
134+
Ok(())
135+
}
136+
}
137+
138+
impl<const P: char, MODE> StatefulOutputPin for PartiallyErasedPin<P, Output<MODE>> {
139+
#[inline(always)]
140+
fn is_set_high(&self) -> Result<bool, Self::Error> {
141+
Ok(self.is_set_high())
142+
}
143+
144+
#[inline(always)]
145+
fn is_set_low(&self) -> Result<bool, Self::Error> {
146+
Ok(self.is_set_low())
147+
}
148+
}
149+
150+
impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<MODE>> {
151+
#[inline(always)]
152+
fn toggle(&mut self) -> Result<(), Self::Error> {
153+
self.toggle();
154+
Ok(())
155+
}
156+
}
157+
158+
impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> {
159+
#[inline(always)]
160+
fn is_high(&self) -> Result<bool, Self::Error> {
161+
Ok(self.is_high())
162+
}
163+
164+
#[inline(always)]
165+
fn is_low(&self) -> Result<bool, Self::Error> {
166+
Ok(self.is_low())
167+
}
168+
}
169+
170+
impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, Input<MODE>> {
171+
#[inline(always)]
172+
fn is_high(&self) -> Result<bool, Self::Error> {
173+
Ok(self.is_high())
174+
}
175+
176+
#[inline(always)]
177+
fn is_low(&self) -> Result<bool, Self::Error> {
178+
Ok(self.is_low())
179+
}
180+
}
181+
182+
// ErasedPin
183+
184+
impl<MODE> ErrorType for ErasedPin<MODE> {
185+
type Error = core::convert::Infallible;
186+
}
187+
188+
impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
189+
fn set_high(&mut self) -> Result<(), Infallible> {
190+
self.set_high();
191+
Ok(())
192+
}
193+
194+
fn set_low(&mut self) -> Result<(), Infallible> {
195+
self.set_low();
196+
Ok(())
197+
}
198+
}
199+
200+
impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
201+
fn is_set_high(&self) -> Result<bool, Self::Error> {
202+
Ok(self.is_set_high())
203+
}
204+
205+
fn is_set_low(&self) -> Result<bool, Self::Error> {
206+
Ok(self.is_set_low())
207+
}
208+
}
209+
210+
impl<MODE> InputPin for ErasedPin<Input<MODE>> {
211+
fn is_high(&self) -> Result<bool, Infallible> {
212+
Ok(self.is_high())
213+
}
214+
215+
fn is_low(&self) -> Result<bool, Infallible> {
216+
Ok(self.is_low())
217+
}
218+
}
219+
220+
impl InputPin for ErasedPin<Output<OpenDrain>> {
221+
fn is_high(&self) -> Result<bool, Infallible> {
222+
Ok(self.is_high())
223+
}
224+
225+
fn is_low(&self) -> Result<bool, Infallible> {
226+
Ok(self.is_low())
227+
}
228+
}

src/serial.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ use crate::rcc::{BusClock, Clocks, Enable, Reset};
7575
use crate::time::{Bps, U32Ext};
7676

7777
mod hal_02;
78+
mod hal_1;
7879

7980
// USART REMAPPING, see: https://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf
8081
// Section 9.3.8
@@ -134,18 +135,7 @@ inst! {
134135
}
135136

136137
/// Serial error
137-
#[derive(Debug)]
138-
#[non_exhaustive]
139-
pub enum Error {
140-
/// Framing error
141-
Framing,
142-
/// Noise error
143-
Noise,
144-
/// RX buffer overrun
145-
Overrun,
146-
/// Parity check error
147-
Parity,
148-
}
138+
pub use embedded_hal_one::serial::ErrorKind as Error;
149139

150140
pub enum WordLength {
151141
/// When parity is enabled, a word has 7 data bits + 1 parity bit,
@@ -514,7 +504,7 @@ impl<USART: Instance> Rx<USART> {
514504
let err = if sr.pe().bit_is_set() {
515505
Some(Error::Parity)
516506
} else if sr.fe().bit_is_set() {
517-
Some(Error::Framing)
507+
Some(Error::FrameFormat)
518508
} else if sr.ne().bit_is_set() {
519509
Some(Error::Noise)
520510
} else if sr.ore().bit_is_set() {

0 commit comments

Comments
 (0)