stm32f1xx_hal/gpio/
hal_02.rs

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