stm32f4xx_hal/gpio/
hal_02.rs

1use core::convert::Infallible;
2
3use super::{
4    dynamic::PinModeError, marker, AnyPin, DynamicPin, Input, OpenDrain, Output,
5    PartiallyErasedPin, Pin, PinMode, PinState,
6};
7
8use embedded_hal_02::digital::v2::{
9    InputPin, IoPin, OutputPin, StatefulOutputPin, ToggleableOutputPin,
10};
11
12// Implementations for `Pin`
13
14impl<const P: char, const N: u8, MODE> OutputPin for Pin<P, N, Output<MODE>> {
15    type Error = Infallible;
16
17    #[inline(always)]
18    fn set_high(&mut self) -> Result<(), Self::Error> {
19        self.set_high();
20        Ok(())
21    }
22
23    #[inline(always)]
24    fn set_low(&mut self) -> Result<(), Self::Error> {
25        self.set_low();
26        Ok(())
27    }
28}
29
30impl<const P: char, const N: u8, MODE> StatefulOutputPin for Pin<P, N, Output<MODE>> {
31    #[inline(always)]
32    fn is_set_high(&self) -> Result<bool, Self::Error> {
33        Ok(self.is_set_high())
34    }
35
36    #[inline(always)]
37    fn is_set_low(&self) -> Result<bool, Self::Error> {
38        Ok(self.is_set_low())
39    }
40}
41
42impl<const P: char, const N: u8, MODE> ToggleableOutputPin for Pin<P, N, Output<MODE>> {
43    type Error = Infallible;
44
45    #[inline(always)]
46    fn toggle(&mut self) -> Result<(), Self::Error> {
47        self.toggle();
48        Ok(())
49    }
50}
51
52impl<const P: char, const N: u8, MODE> InputPin for Pin<P, N, MODE>
53where
54    MODE: marker::Readable,
55{
56    type Error = Infallible;
57
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
69impl<const P: char, const N: u8> IoPin<Self, Self> for Pin<P, N, Output<OpenDrain>> {
70    type Error = Infallible;
71    fn into_input_pin(self) -> Result<Self, Self::Error> {
72        Ok(self)
73    }
74    fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
75        self.set_state(state);
76        Ok(self)
77    }
78}
79
80impl<const P: char, const N: u8, Otype> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Output<Otype>>
81where
82    Output<Otype>: PinMode,
83{
84    type Error = Infallible;
85    fn into_input_pin(self) -> Result<Pin<P, N, Input>, Self::Error> {
86        Ok(self.into_input())
87    }
88    fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
89        self.set_state(state);
90        Ok(self)
91    }
92}
93
94impl<const P: char, const N: u8, Otype> IoPin<Self, Pin<P, N, Output<Otype>>> for Pin<P, N, Input>
95where
96    Output<Otype>: PinMode,
97{
98    type Error = Infallible;
99    fn into_input_pin(self) -> Result<Self, Self::Error> {
100        Ok(self)
101    }
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())
105    }
106}
107
108// Implementations for `ErasedPin`
109
110impl<MODE> OutputPin for AnyPin<Output<MODE>> {
111    type Error = core::convert::Infallible;
112
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
126impl<MODE> StatefulOutputPin for AnyPin<Output<MODE>> {
127    #[inline(always)]
128    fn is_set_high(&self) -> Result<bool, Self::Error> {
129        Ok(self.is_set_high())
130    }
131
132    #[inline(always)]
133    fn is_set_low(&self) -> Result<bool, Self::Error> {
134        Ok(self.is_set_low())
135    }
136}
137
138impl<MODE> ToggleableOutputPin for AnyPin<Output<MODE>> {
139    type Error = Infallible;
140
141    #[inline(always)]
142    fn toggle(&mut self) -> Result<(), Self::Error> {
143        self.toggle();
144        Ok(())
145    }
146}
147
148impl<MODE> InputPin for AnyPin<MODE>
149where
150    MODE: marker::Readable,
151{
152    type Error = core::convert::Infallible;
153
154    #[inline(always)]
155    fn is_high(&self) -> Result<bool, Self::Error> {
156        Ok(self.is_high())
157    }
158
159    #[inline(always)]
160    fn is_low(&self) -> Result<bool, Self::Error> {
161        Ok(self.is_low())
162    }
163}
164
165// Implementations for `PartiallyErasedPin`
166
167impl<const P: char, MODE> OutputPin for PartiallyErasedPin<P, Output<MODE>> {
168    type Error = Infallible;
169
170    #[inline(always)]
171    fn set_high(&mut self) -> Result<(), Self::Error> {
172        self.set_high();
173        Ok(())
174    }
175
176    #[inline(always)]
177    fn set_low(&mut self) -> Result<(), Self::Error> {
178        self.set_low();
179        Ok(())
180    }
181}
182
183impl<const P: char, MODE> StatefulOutputPin for PartiallyErasedPin<P, Output<MODE>> {
184    #[inline(always)]
185    fn is_set_high(&self) -> Result<bool, Self::Error> {
186        Ok(self.is_set_high())
187    }
188
189    #[inline(always)]
190    fn is_set_low(&self) -> Result<bool, Self::Error> {
191        Ok(self.is_set_low())
192    }
193}
194
195impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<MODE>> {
196    type Error = Infallible;
197
198    #[inline(always)]
199    fn toggle(&mut self) -> Result<(), Self::Error> {
200        self.toggle();
201        Ok(())
202    }
203}
204
205impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, MODE>
206where
207    MODE: marker::Readable,
208{
209    type Error = Infallible;
210
211    #[inline(always)]
212    fn is_high(&self) -> Result<bool, Self::Error> {
213        Ok(self.is_high())
214    }
215
216    #[inline(always)]
217    fn is_low(&self) -> Result<bool, Self::Error> {
218        Ok(self.is_low())
219    }
220}
221
222// Implementations for `DynamicPin`
223
224impl<const P: char, const N: u8> OutputPin for DynamicPin<P, N> {
225    type Error = PinModeError;
226    fn set_high(&mut self) -> Result<(), Self::Error> {
227        self.set_high()
228    }
229    fn set_low(&mut self) -> Result<(), Self::Error> {
230        self.set_low()
231    }
232}
233
234impl<const P: char, const N: u8> InputPin for DynamicPin<P, N> {
235    type Error = PinModeError;
236    fn is_high(&self) -> Result<bool, Self::Error> {
237        self.is_high()
238    }
239    fn is_low(&self) -> Result<bool, Self::Error> {
240        self.is_low()
241    }
242}