diff options
| author | chemicstry <[email protected]> | 2022-07-13 02:22:46 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-07-13 02:22:46 +0300 |
| commit | 57002875d6bf9ac4378955a4e33f77154fd32edd (patch) | |
| tree | 78b85f644e4e620e6602e5f4f5fad44717f09fd0 | |
| parent | 8cebbde1013bb98901e6fb18a4a0b095bf28f58f (diff) | |
Add convenience GPIO functions to RP
| -rw-r--r-- | embassy-rp/src/gpio.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index 131330e79..5e8126b4d 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs | |||
| @@ -14,6 +14,24 @@ pub enum Level { | |||
| 14 | High, | 14 | High, |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | impl From<bool> for Level { | ||
| 18 | fn from(val: bool) -> Self { | ||
| 19 | match val { | ||
| 20 | true => Self::High, | ||
| 21 | false => Self::Low, | ||
| 22 | } | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | impl Into<bool> for Level { | ||
| 27 | fn into(self) -> bool { | ||
| 28 | match self { | ||
| 29 | Level::Low => false, | ||
| 30 | Level::High => true, | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 17 | /// Represents a pull setting for an input. | 35 | /// Represents a pull setting for an input. |
| 18 | #[derive(Debug, Eq, PartialEq)] | 36 | #[derive(Debug, Eq, PartialEq)] |
| 19 | pub enum Pull { | 37 | pub enum Pull { |
| @@ -34,6 +52,7 @@ pub struct Input<'d, T: Pin> { | |||
| 34 | } | 52 | } |
| 35 | 53 | ||
| 36 | impl<'d, T: Pin> Input<'d, T> { | 54 | impl<'d, T: Pin> Input<'d, T> { |
| 55 | #[inline] | ||
| 37 | pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self { | 56 | pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self { |
| 38 | let mut pin = Flex::new(pin); | 57 | let mut pin = Flex::new(pin); |
| 39 | pin.set_as_input(); | 58 | pin.set_as_input(); |
| @@ -41,13 +60,21 @@ impl<'d, T: Pin> Input<'d, T> { | |||
| 41 | Self { pin } | 60 | Self { pin } |
| 42 | } | 61 | } |
| 43 | 62 | ||
| 63 | #[inline] | ||
| 44 | pub fn is_high(&self) -> bool { | 64 | pub fn is_high(&self) -> bool { |
| 45 | self.pin.is_high() | 65 | self.pin.is_high() |
| 46 | } | 66 | } |
| 47 | 67 | ||
| 68 | #[inline] | ||
| 48 | pub fn is_low(&self) -> bool { | 69 | pub fn is_low(&self) -> bool { |
| 49 | self.pin.is_low() | 70 | self.pin.is_low() |
| 50 | } | 71 | } |
| 72 | |||
| 73 | /// Returns current pin level | ||
| 74 | #[inline] | ||
| 75 | pub fn get_level(&self) -> Level { | ||
| 76 | self.pin.is_high().into() | ||
| 77 | } | ||
| 51 | } | 78 | } |
| 52 | 79 | ||
| 53 | pub struct Output<'d, T: Pin> { | 80 | pub struct Output<'d, T: Pin> { |
| @@ -79,6 +106,15 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 79 | self.pin.set_low() | 106 | self.pin.set_low() |
| 80 | } | 107 | } |
| 81 | 108 | ||
| 109 | /// Set the output level. | ||
| 110 | #[inline] | ||
| 111 | pub fn set_level(&mut self, level: Level) { | ||
| 112 | match level { | ||
| 113 | Level::Low => self.pin.set_low(), | ||
| 114 | Level::High => self.pin.set_high(), | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 82 | /// Is the output pin set as high? | 118 | /// Is the output pin set as high? |
| 83 | #[inline] | 119 | #[inline] |
| 84 | pub fn is_set_high(&self) -> bool { | 120 | pub fn is_set_high(&self) -> bool { |
| @@ -91,6 +127,12 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 91 | self.pin.is_set_low() | 127 | self.pin.is_set_low() |
| 92 | } | 128 | } |
| 93 | 129 | ||
| 130 | /// What level output is set to | ||
| 131 | #[inline] | ||
| 132 | pub fn get_set_level(&self) -> Level { | ||
| 133 | self.pin.is_set_high().into() | ||
| 134 | } | ||
| 135 | |||
| 94 | /// Toggle pin output | 136 | /// Toggle pin output |
| 95 | #[inline] | 137 | #[inline] |
| 96 | pub fn toggle(&mut self) { | 138 | pub fn toggle(&mut self) { |
| @@ -129,6 +171,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 129 | self.pin.set_as_output() | 171 | self.pin.set_as_output() |
| 130 | } | 172 | } |
| 131 | 173 | ||
| 174 | /// Set the output level. | ||
| 175 | #[inline] | ||
| 176 | pub fn set_level(&mut self, level: Level) { | ||
| 177 | match level { | ||
| 178 | Level::Low => self.set_low(), | ||
| 179 | Level::High => self.set_high(), | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 132 | /// Is the output level high? | 183 | /// Is the output level high? |
| 133 | #[inline] | 184 | #[inline] |
| 134 | pub fn is_set_high(&self) -> bool { | 185 | pub fn is_set_high(&self) -> bool { |
| @@ -141,6 +192,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 141 | self.pin.is_set_as_output() | 192 | self.pin.is_set_as_output() |
| 142 | } | 193 | } |
| 143 | 194 | ||
| 195 | /// What level output is set to | ||
| 196 | #[inline] | ||
| 197 | pub fn get_set_level(&self) -> Level { | ||
| 198 | self.is_set_high().into() | ||
| 199 | } | ||
| 200 | |||
| 144 | /// Toggle pin output | 201 | /// Toggle pin output |
| 145 | #[inline] | 202 | #[inline] |
| 146 | pub fn toggle(&mut self) { | 203 | pub fn toggle(&mut self) { |
| @@ -236,6 +293,12 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 236 | unsafe { self.pin.sio_in().read() & self.bit() == 0 } | 293 | unsafe { self.pin.sio_in().read() & self.bit() == 0 } |
| 237 | } | 294 | } |
| 238 | 295 | ||
| 296 | /// Returns current pin level | ||
| 297 | #[inline] | ||
| 298 | pub fn get_level(&self) -> Level { | ||
| 299 | self.is_high().into() | ||
| 300 | } | ||
| 301 | |||
| 239 | /// Set the output as high. | 302 | /// Set the output as high. |
| 240 | #[inline] | 303 | #[inline] |
| 241 | pub fn set_high(&mut self) { | 304 | pub fn set_high(&mut self) { |
| @@ -248,6 +311,15 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 248 | unsafe { self.pin.sio_out().value_clr().write_value(self.bit()) } | 311 | unsafe { self.pin.sio_out().value_clr().write_value(self.bit()) } |
| 249 | } | 312 | } |
| 250 | 313 | ||
| 314 | /// Set the output level. | ||
| 315 | #[inline] | ||
| 316 | pub fn set_level(&mut self, level: Level) { | ||
| 317 | match level { | ||
| 318 | Level::Low => self.set_low(), | ||
| 319 | Level::High => self.set_high(), | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 251 | /// Is the output level high? | 323 | /// Is the output level high? |
| 252 | #[inline] | 324 | #[inline] |
| 253 | pub fn is_set_high(&self) -> bool { | 325 | pub fn is_set_high(&self) -> bool { |
| @@ -260,6 +332,12 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 260 | !self.is_set_high() | 332 | !self.is_set_high() |
| 261 | } | 333 | } |
| 262 | 334 | ||
| 335 | /// What level output is set to | ||
| 336 | #[inline] | ||
| 337 | pub fn get_set_level(&self) -> Level { | ||
| 338 | self.is_set_high().into() | ||
| 339 | } | ||
| 340 | |||
| 263 | /// Toggle pin output | 341 | /// Toggle pin output |
| 264 | #[inline] | 342 | #[inline] |
| 265 | pub fn toggle(&mut self) { | 343 | pub fn toggle(&mut self) { |
