diff options
| author | Ulf Lilleengen <[email protected]> | 2022-08-22 10:36:33 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2022-08-22 16:37:35 +0200 |
| commit | 3e155d2ec366379584bf7ba4a447109555aa0d77 (patch) | |
| tree | e1a6a8340354ea67316697bf737bd08ec0ab7d00 /embassy-nrf/src/gpio.rs | |
| parent | 5fddff849eea74fb240147432a1739ae1759cb6c (diff) | |
nRF documentation warning fixes
Diffstat (limited to 'embassy-nrf/src/gpio.rs')
| -rw-r--r-- | embassy-nrf/src/gpio.rs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index a61ff6aa5..924629908 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | //! General purpose input/output for nRF. | ||
| 1 | #![macro_use] | 2 | #![macro_use] |
| 2 | 3 | ||
| 3 | use core::convert::Infallible; | 4 | use core::convert::Infallible; |
| @@ -26,8 +27,11 @@ pub enum Port { | |||
| 26 | #[derive(Debug, Eq, PartialEq)] | 27 | #[derive(Debug, Eq, PartialEq)] |
| 27 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 28 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 28 | pub enum Pull { | 29 | pub enum Pull { |
| 30 | /// No pull. | ||
| 29 | None, | 31 | None, |
| 32 | /// Internal pull-up resistor. | ||
| 30 | Up, | 33 | Up, |
| 34 | /// Internal pull-down resistor. | ||
| 31 | Down, | 35 | Down, |
| 32 | } | 36 | } |
| 33 | 37 | ||
| @@ -37,6 +41,7 @@ pub struct Input<'d, T: Pin> { | |||
| 37 | } | 41 | } |
| 38 | 42 | ||
| 39 | impl<'d, T: Pin> Input<'d, T> { | 43 | impl<'d, T: Pin> Input<'d, T> { |
| 44 | /// Create GPIO input driver for a [Pin] with the provided [Pull] configuration. | ||
| 40 | #[inline] | 45 | #[inline] |
| 41 | pub fn new(pin: impl Peripheral<P = T> + 'd, pull: Pull) -> Self { | 46 | pub fn new(pin: impl Peripheral<P = T> + 'd, pull: Pull) -> Self { |
| 42 | let mut pin = Flex::new(pin); | 47 | let mut pin = Flex::new(pin); |
| @@ -45,11 +50,13 @@ impl<'d, T: Pin> Input<'d, T> { | |||
| 45 | Self { pin } | 50 | Self { pin } |
| 46 | } | 51 | } |
| 47 | 52 | ||
| 53 | /// Test if current pin level is high. | ||
| 48 | #[inline] | 54 | #[inline] |
| 49 | pub fn is_high(&self) -> bool { | 55 | pub fn is_high(&self) -> bool { |
| 50 | self.pin.is_high() | 56 | self.pin.is_high() |
| 51 | } | 57 | } |
| 52 | 58 | ||
| 59 | /// Test if current pin level is low. | ||
| 53 | #[inline] | 60 | #[inline] |
| 54 | pub fn is_low(&self) -> bool { | 61 | pub fn is_low(&self) -> bool { |
| 55 | self.pin.is_low() | 62 | self.pin.is_low() |
| @@ -66,7 +73,9 @@ impl<'d, T: Pin> Input<'d, T> { | |||
| 66 | #[derive(Debug, Eq, PartialEq)] | 73 | #[derive(Debug, Eq, PartialEq)] |
| 67 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 74 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 68 | pub enum Level { | 75 | pub enum Level { |
| 76 | /// Logical low. | ||
| 69 | Low, | 77 | Low, |
| 78 | /// Logical high. | ||
| 70 | High, | 79 | High, |
| 71 | } | 80 | } |
| 72 | 81 | ||
| @@ -88,6 +97,7 @@ impl Into<bool> for Level { | |||
| 88 | } | 97 | } |
| 89 | } | 98 | } |
| 90 | 99 | ||
| 100 | /// Drive strength settings for an output pin. | ||
| 91 | // These numbers match DRIVE_A exactly so hopefully the compiler will unify them. | 101 | // These numbers match DRIVE_A exactly so hopefully the compiler will unify them. |
| 92 | #[derive(Clone, Copy, Debug, PartialEq)] | 102 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 93 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 103 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -117,6 +127,7 @@ pub struct Output<'d, T: Pin> { | |||
| 117 | } | 127 | } |
| 118 | 128 | ||
| 119 | impl<'d, T: Pin> Output<'d, T> { | 129 | impl<'d, T: Pin> Output<'d, T> { |
| 130 | /// Create GPIO output driver for a [Pin] with the provided [Level] and [OutputDriver] configuration. | ||
| 120 | #[inline] | 131 | #[inline] |
| 121 | pub fn new(pin: impl Peripheral<P = T> + 'd, initial_output: Level, drive: OutputDrive) -> Self { | 132 | pub fn new(pin: impl Peripheral<P = T> + 'd, initial_output: Level, drive: OutputDrive) -> Self { |
| 122 | let mut pin = Flex::new(pin); | 133 | let mut pin = Flex::new(pin); |
| @@ -264,11 +275,13 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 264 | self.pin.conf().reset(); | 275 | self.pin.conf().reset(); |
| 265 | } | 276 | } |
| 266 | 277 | ||
| 278 | /// Test if current pin level is high. | ||
| 267 | #[inline] | 279 | #[inline] |
| 268 | pub fn is_high(&self) -> bool { | 280 | pub fn is_high(&self) -> bool { |
| 269 | !self.is_low() | 281 | !self.is_low() |
| 270 | } | 282 | } |
| 271 | 283 | ||
| 284 | /// Test if current pin level is low. | ||
| 272 | #[inline] | 285 | #[inline] |
| 273 | pub fn is_low(&self) -> bool { | 286 | pub fn is_low(&self) -> bool { |
| 274 | self.pin.block().in_.read().bits() & (1 << self.pin.pin()) == 0 | 287 | self.pin.block().in_.read().bits() & (1 << self.pin.pin()) == 0 |
| @@ -374,6 +387,7 @@ pub(crate) mod sealed { | |||
| 374 | } | 387 | } |
| 375 | } | 388 | } |
| 376 | 389 | ||
| 390 | /// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an [AnyPin]. | ||
| 377 | pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + sealed::Pin + Sized + 'static { | 391 | pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + sealed::Pin + Sized + 'static { |
| 378 | /// Number of the pin within the port (0..31) | 392 | /// Number of the pin within the port (0..31) |
| 379 | #[inline] | 393 | #[inline] |
| @@ -392,6 +406,7 @@ pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + sealed::Pin + Sized + 'stat | |||
| 392 | } | 406 | } |
| 393 | } | 407 | } |
| 394 | 408 | ||
| 409 | /// Peripheral port register value | ||
| 395 | #[inline] | 410 | #[inline] |
| 396 | fn psel_bits(&self) -> u32 { | 411 | fn psel_bits(&self) -> u32 { |
| 397 | self.pin_port() as u32 | 412 | self.pin_port() as u32 |
| @@ -406,12 +421,16 @@ pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + sealed::Pin + Sized + 'stat | |||
| 406 | } | 421 | } |
| 407 | } | 422 | } |
| 408 | 423 | ||
| 409 | // Type-erased GPIO pin | 424 | /// Type-erased GPIO pin |
| 410 | pub struct AnyPin { | 425 | pub struct AnyPin { |
| 411 | pin_port: u8, | 426 | pin_port: u8, |
| 412 | } | 427 | } |
| 413 | 428 | ||
| 414 | impl AnyPin { | 429 | impl AnyPin { |
| 430 | /// Create an [AnyPin] for a specific pin. | ||
| 431 | /// | ||
| 432 | /// # Safety | ||
| 433 | /// - `pin_port` should not in use by another driver. | ||
| 415 | #[inline] | 434 | #[inline] |
| 416 | pub unsafe fn steal(pin_port: u8) -> Self { | 435 | pub unsafe fn steal(pin_port: u8) -> Self { |
| 417 | Self { pin_port } | 436 | Self { pin_port } |
