stm32f1xx_hal/gpio/
hal_1.rs

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