diff options
| author | chemicstry <[email protected]> | 2022-07-13 01:43:22 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-07-13 01:43:22 +0300 |
| commit | 98dcce81ca239a0bb04b91a6f109aee6c0b18ada (patch) | |
| tree | 6018085dd2e560412fa31bf503557be7a9bf18ae | |
| parent | e4cacc3bb812c8870584c513020f0747146c860a (diff) | |
Add more convenience GPIO functions
| -rw-r--r-- | embassy-stm32/src/gpio.rs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 806b5eb68..b97d5772e 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs | |||
| @@ -142,6 +142,14 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 142 | } | 142 | } |
| 143 | 143 | ||
| 144 | #[inline] | 144 | #[inline] |
| 145 | pub fn get_level(&self) -> Level { | ||
| 146 | match self.is_high() { | ||
| 147 | true => Level::High, | ||
| 148 | false => Level::Low, | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | #[inline] | ||
| 145 | pub fn is_set_high(&self) -> bool { | 153 | pub fn is_set_high(&self) -> bool { |
| 146 | !self.is_set_low() | 154 | !self.is_set_low() |
| 147 | } | 155 | } |
| @@ -153,6 +161,15 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 153 | state == vals::Odr::LOW | 161 | state == vals::Odr::LOW |
| 154 | } | 162 | } |
| 155 | 163 | ||
| 164 | /// What level output is set to | ||
| 165 | #[inline] | ||
| 166 | pub fn get_set_level(&self) -> Level { | ||
| 167 | match self.is_set_high() { | ||
| 168 | true => Level::High, | ||
| 169 | false => Level::Low, | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 156 | #[inline] | 173 | #[inline] |
| 157 | pub fn set_high(&mut self) { | 174 | pub fn set_high(&mut self) { |
| 158 | self.pin.set_high(); | 175 | self.pin.set_high(); |
| @@ -164,6 +181,14 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 164 | self.pin.set_low(); | 181 | self.pin.set_low(); |
| 165 | } | 182 | } |
| 166 | 183 | ||
| 184 | #[inline] | ||
| 185 | pub fn set_level(&mut self, level: Level) { | ||
| 186 | match level { | ||
| 187 | Level::Low => self.pin.set_low(), | ||
| 188 | Level::High => self.pin.set_high(), | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 167 | /// Toggle pin output | 192 | /// Toggle pin output |
| 168 | #[inline] | 193 | #[inline] |
| 169 | pub fn toggle(&mut self) { | 194 | pub fn toggle(&mut self) { |
| @@ -281,6 +306,14 @@ impl<'d, T: Pin> Input<'d, T> { | |||
| 281 | pub fn is_low(&self) -> bool { | 306 | pub fn is_low(&self) -> bool { |
| 282 | self.pin.is_low() | 307 | self.pin.is_low() |
| 283 | } | 308 | } |
| 309 | |||
| 310 | #[inline] | ||
| 311 | pub fn get_level(&self) -> Level { | ||
| 312 | match self.pin.is_high() { | ||
| 313 | true => Level::High, | ||
| 314 | false => Level::Low, | ||
| 315 | } | ||
| 316 | } | ||
| 284 | } | 317 | } |
| 285 | 318 | ||
| 286 | /// Digital input or output level. | 319 | /// Digital input or output level. |
| @@ -291,6 +324,24 @@ pub enum Level { | |||
| 291 | High, | 324 | High, |
| 292 | } | 325 | } |
| 293 | 326 | ||
| 327 | impl From<bool> for Level { | ||
| 328 | fn from(val: bool) -> Self { | ||
| 329 | match val { | ||
| 330 | true => Self::High, | ||
| 331 | false => Self::Low, | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | impl Into<bool> for Level { | ||
| 337 | fn into(self) -> bool { | ||
| 338 | match self { | ||
| 339 | Level::Low => false, | ||
| 340 | Level::High => true, | ||
| 341 | } | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 294 | /// GPIO output driver. | 345 | /// GPIO output driver. |
| 295 | pub struct Output<'d, T: Pin> { | 346 | pub struct Output<'d, T: Pin> { |
| 296 | pub(crate) pin: Flex<'d, T>, | 347 | pub(crate) pin: Flex<'d, T>, |
| @@ -320,6 +371,15 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 320 | self.pin.set_low(); | 371 | self.pin.set_low(); |
| 321 | } | 372 | } |
| 322 | 373 | ||
| 374 | /// Set the output level. | ||
| 375 | #[inline] | ||
| 376 | pub fn set_level(&mut self, level: Level) { | ||
| 377 | match level { | ||
| 378 | Level::Low => self.pin.set_low(), | ||
| 379 | Level::High => self.pin.set_high(), | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 323 | /// Is the output pin set as high? | 383 | /// Is the output pin set as high? |
| 324 | #[inline] | 384 | #[inline] |
| 325 | pub fn is_set_high(&self) -> bool { | 385 | pub fn is_set_high(&self) -> bool { |
| @@ -332,6 +392,15 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 332 | self.pin.is_set_low() | 392 | self.pin.is_set_low() |
| 333 | } | 393 | } |
| 334 | 394 | ||
| 395 | /// What level output is set to | ||
| 396 | #[inline] | ||
| 397 | pub fn get_set_level(&self) -> Level { | ||
| 398 | match self.pin.is_set_high() { | ||
| 399 | true => Level::High, | ||
| 400 | false => Level::Low, | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 335 | /// Toggle pin output | 404 | /// Toggle pin output |
| 336 | #[inline] | 405 | #[inline] |
| 337 | pub fn toggle(&mut self) { | 406 | pub fn toggle(&mut self) { |
| @@ -368,6 +437,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 368 | self.pin.is_low() | 437 | self.pin.is_low() |
| 369 | } | 438 | } |
| 370 | 439 | ||
| 440 | /// Returns current pin level | ||
| 441 | #[inline] | ||
| 442 | pub fn get_level(&self) -> Level { | ||
| 443 | match self.pin.is_high() { | ||
| 444 | true => Level::High, | ||
| 445 | false => Level::Low, | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 371 | /// Set the output as high. | 449 | /// Set the output as high. |
| 372 | #[inline] | 450 | #[inline] |
| 373 | pub fn set_high(&mut self) { | 451 | pub fn set_high(&mut self) { |
| @@ -380,6 +458,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 380 | self.pin.set_low(); | 458 | self.pin.set_low(); |
| 381 | } | 459 | } |
| 382 | 460 | ||
| 461 | /// Set the output level. | ||
| 462 | #[inline] | ||
| 463 | pub fn set_level(&mut self, level: Level) { | ||
| 464 | match level { | ||
| 465 | Level::Low => self.pin.set_low(), | ||
| 466 | Level::High => self.pin.set_high(), | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 383 | /// Is the output pin set as high? | 470 | /// Is the output pin set as high? |
| 384 | #[inline] | 471 | #[inline] |
| 385 | pub fn is_set_high(&self) -> bool { | 472 | pub fn is_set_high(&self) -> bool { |
| @@ -392,6 +479,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 392 | self.pin.is_set_low() | 479 | self.pin.is_set_low() |
| 393 | } | 480 | } |
| 394 | 481 | ||
| 482 | /// What level output is set to | ||
| 483 | #[inline] | ||
| 484 | pub fn get_set_level(&self) -> Level { | ||
| 485 | match self.pin.is_set_high() { | ||
| 486 | true => Level::High, | ||
| 487 | false => Level::Low, | ||
| 488 | } | ||
| 489 | } | ||
| 490 | |||
| 395 | /// Toggle pin output | 491 | /// Toggle pin output |
| 396 | #[inline] | 492 | #[inline] |
| 397 | pub fn toggle(&mut self) { | 493 | pub fn toggle(&mut self) { |
