diff options
| -rw-r--r-- | embassy-nrf/src/gpio.rs | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index 4269cbe16..14ac61822 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs | |||
| @@ -113,7 +113,12 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 113 | ) -> Self { | 113 | ) -> Self { |
| 114 | unborrow!(pin); | 114 | unborrow!(pin); |
| 115 | 115 | ||
| 116 | init_output(&pin, initial_output, drive); | 116 | match initial_output { |
| 117 | Level::High => pin.set_high(), | ||
| 118 | Level::Low => pin.set_low(), | ||
| 119 | } | ||
| 120 | |||
| 121 | init_output(&pin, drive); | ||
| 117 | 122 | ||
| 118 | Self { | 123 | Self { |
| 119 | pin, | 124 | pin, |
| @@ -168,7 +173,9 @@ impl<'d, T: Pin> StatefulOutputPin for Output<'d, T> { | |||
| 168 | 173 | ||
| 169 | /// GPIO flexible pin. | 174 | /// GPIO flexible pin. |
| 170 | /// | 175 | /// |
| 171 | /// This pin can either be a disconnected, input, or output pin. | 176 | /// This pin can either be a disconnected, input, or output pin. The level register bit will remain |
| 177 | /// set while not in output mode, so the pin's level will be 'remembered' when it is not in output | ||
| 178 | /// mode. | ||
| 172 | pub struct FlexPin<'d, T: Pin> { | 179 | pub struct FlexPin<'d, T: Pin> { |
| 173 | pub(crate) pin: T, | 180 | pub(crate) pin: T, |
| 174 | phantom: PhantomData<&'d mut T>, | 181 | phantom: PhantomData<&'d mut T>, |
| @@ -177,7 +184,8 @@ pub struct FlexPin<'d, T: Pin> { | |||
| 177 | impl<'d, T: Pin> FlexPin<'d, T> { | 184 | impl<'d, T: Pin> FlexPin<'d, T> { |
| 178 | /// Wrap the pin in a `FlexPin`. | 185 | /// Wrap the pin in a `FlexPin`. |
| 179 | /// | 186 | /// |
| 180 | /// The pin remains disconnected. | 187 | /// The pin remains disconnected. The initial output level is unspecified, but can be changed |
| 188 | /// before the pin is put into output mode. | ||
| 181 | pub fn new(pin: impl Unborrow<Target = T> + 'd) -> Self { | 189 | pub fn new(pin: impl Unborrow<Target = T> + 'd) -> Self { |
| 182 | unborrow!(pin); | 190 | unborrow!(pin); |
| 183 | // Pin will be in disconnected state. | 191 | // Pin will be in disconnected state. |
| @@ -187,17 +195,21 @@ impl<'d, T: Pin> FlexPin<'d, T> { | |||
| 187 | } | 195 | } |
| 188 | } | 196 | } |
| 189 | 197 | ||
| 190 | pub fn to_input(&self, pull: Pull) { | 198 | /// Put the pin into input mode. |
| 191 | self.pin.conf().reset(); // TODO is this necessary? | 199 | pub fn set_as_input(&mut self, pull: Pull) { |
| 192 | init_input(&self.pin, pull); | 200 | init_input(&self.pin, pull); |
| 193 | } | 201 | } |
| 194 | 202 | ||
| 195 | pub fn to_output(&self, initial_output: Level, drive: OutputDrive) { | 203 | /// Put the pin into output mode. |
| 196 | self.pin.conf().reset(); // TODO is this necessary? | 204 | /// |
| 197 | init_output(&self.pin, initial_output, drive); | 205 | /// The pin level will be whatever was set before (or low by default). If you want it to begin |
| 206 | /// at a specific level, call `set_high`/`set_low` on the pin first. | ||
| 207 | pub fn set_as_output(&mut self, drive: OutputDrive) { | ||
| 208 | init_output(&self.pin, drive); | ||
| 198 | } | 209 | } |
| 199 | 210 | ||
| 200 | pub fn disconnect(&self) { | 211 | /// Put the pin into disconnected mode. |
| 212 | pub fn set_as_disconnected(&mut self) { | ||
| 201 | self.pin.conf().reset(); | 213 | self.pin.conf().reset(); |
| 202 | } | 214 | } |
| 203 | } | 215 | } |
| @@ -397,12 +409,7 @@ fn init_input<T: Pin>(pin: &T, pull: Pull) { | |||
| 397 | 409 | ||
| 398 | /// Set up a pin for output | 410 | /// Set up a pin for output |
| 399 | #[inline] | 411 | #[inline] |
| 400 | fn init_output<T: Pin>(pin: &T, initial_output: Level, drive: OutputDrive) { | 412 | fn init_output<T: Pin>(pin: &T, drive: OutputDrive) { |
| 401 | match initial_output { | ||
| 402 | Level::High => pin.set_high(), | ||
| 403 | Level::Low => pin.set_low(), | ||
| 404 | } | ||
| 405 | |||
| 406 | let drive = match drive { | 413 | let drive = match drive { |
| 407 | OutputDrive::Standard => DRIVE_A::S0S1, | 414 | OutputDrive::Standard => DRIVE_A::S0S1, |
| 408 | OutputDrive::HighDrive0Standard1 => DRIVE_A::H0S1, | 415 | OutputDrive::HighDrive0Standard1 => DRIVE_A::H0S1, |
