From 98dcce81ca239a0bb04b91a6f109aee6c0b18ada Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 13 Jul 2022 01:43:22 +0300 Subject: [PATCH 1/6] Add more convenience GPIO functions --- embassy-stm32/src/gpio.rs | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 806b5eb6..b97d5772 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -141,6 +141,14 @@ impl<'d, T: Pin> Flex<'d, T> { state == vals::Idr::LOW } + #[inline] + pub fn get_level(&self) -> Level { + match self.is_high() { + true => Level::High, + false => Level::Low, + } + } + #[inline] pub fn is_set_high(&self) -> bool { !self.is_set_low() @@ -153,6 +161,15 @@ impl<'d, T: Pin> Flex<'d, T> { state == vals::Odr::LOW } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + match self.is_set_high() { + true => Level::High, + false => Level::Low, + } + } + #[inline] pub fn set_high(&mut self) { self.pin.set_high(); @@ -164,6 +181,14 @@ impl<'d, T: Pin> Flex<'d, T> { self.pin.set_low(); } + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.pin.set_low(), + Level::High => self.pin.set_high(), + } + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { @@ -281,6 +306,14 @@ impl<'d, T: Pin> Input<'d, T> { pub fn is_low(&self) -> bool { self.pin.is_low() } + + #[inline] + pub fn get_level(&self) -> Level { + match self.pin.is_high() { + true => Level::High, + false => Level::Low, + } + } } /// Digital input or output level. @@ -291,6 +324,24 @@ pub enum Level { High, } +impl From for Level { + fn from(val: bool) -> Self { + match val { + true => Self::High, + false => Self::Low, + } + } +} + +impl Into for Level { + fn into(self) -> bool { + match self { + Level::Low => false, + Level::High => true, + } + } +} + /// GPIO output driver. pub struct Output<'d, T: Pin> { pub(crate) pin: Flex<'d, T>, @@ -320,6 +371,15 @@ impl<'d, T: Pin> Output<'d, T> { self.pin.set_low(); } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.pin.set_low(), + Level::High => self.pin.set_high(), + } + } + /// Is the output pin set as high? #[inline] pub fn is_set_high(&self) -> bool { @@ -332,6 +392,15 @@ impl<'d, T: Pin> Output<'d, T> { self.pin.is_set_low() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + match self.pin.is_set_high() { + true => Level::High, + false => Level::Low, + } + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { @@ -368,6 +437,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.is_low() } + /// Returns current pin level + #[inline] + pub fn get_level(&self) -> Level { + match self.pin.is_high() { + true => Level::High, + false => Level::Low, + } + } + /// Set the output as high. #[inline] pub fn set_high(&mut self) { @@ -380,6 +458,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.set_low(); } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.pin.set_low(), + Level::High => self.pin.set_high(), + } + } + /// Is the output pin set as high? #[inline] pub fn is_set_high(&self) -> bool { @@ -392,6 +479,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.is_set_low() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + match self.pin.is_set_high() { + true => Level::High, + false => Level::Low, + } + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { From 329955f718ac6a99f98856386e8a3708f285de43 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 13 Jul 2022 02:08:31 +0300 Subject: [PATCH 2/6] Use Into conversions instead of matches --- embassy-stm32/src/gpio.rs | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index b97d5772..5f0d94b1 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -143,10 +143,7 @@ impl<'d, T: Pin> Flex<'d, T> { #[inline] pub fn get_level(&self) -> Level { - match self.is_high() { - true => Level::High, - false => Level::Low, - } + self.is_high().into() } #[inline] @@ -164,10 +161,7 @@ impl<'d, T: Pin> Flex<'d, T> { /// What level output is set to #[inline] pub fn get_set_level(&self) -> Level { - match self.is_set_high() { - true => Level::High, - false => Level::Low, - } + self.is_set_high().into() } #[inline] @@ -309,10 +303,7 @@ impl<'d, T: Pin> Input<'d, T> { #[inline] pub fn get_level(&self) -> Level { - match self.pin.is_high() { - true => Level::High, - false => Level::Low, - } + self.pin.is_high().into() } } @@ -395,10 +386,7 @@ impl<'d, T: Pin> Output<'d, T> { /// What level output is set to #[inline] pub fn get_set_level(&self) -> Level { - match self.pin.is_set_high() { - true => Level::High, - false => Level::Low, - } + self.pin.is_set_high().into() } /// Toggle pin output @@ -440,10 +428,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// Returns current pin level #[inline] pub fn get_level(&self) -> Level { - match self.pin.is_high() { - true => Level::High, - false => Level::Low, - } + self.pin.is_high().into() } /// Set the output as high. @@ -482,10 +467,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// What level output is set to #[inline] pub fn get_set_level(&self) -> Level { - match self.pin.is_set_high() { - true => Level::High, - false => Level::Low, - } + self.pin.is_set_high().into() } /// Toggle pin output From 8cebbde1013bb98901e6fb18a4a0b095bf28f58f Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 13 Jul 2022 02:21:42 +0300 Subject: [PATCH 3/6] Add convenience GPIO functions to NRF --- embassy-nrf/src/gpio.rs | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index 0ba20b0b..d8295fc0 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs @@ -38,6 +38,7 @@ pub struct Input<'d, T: Pin> { } impl<'d, T: Pin> Input<'d, T> { + #[inline] pub fn new(pin: impl Unborrow + 'd, pull: Pull) -> Self { let mut pin = Flex::new(pin); pin.set_as_input(pull); @@ -45,13 +46,21 @@ impl<'d, T: Pin> Input<'d, T> { Self { pin } } + #[inline] pub fn is_high(&self) -> bool { self.pin.is_high() } + #[inline] pub fn is_low(&self) -> bool { self.pin.is_low() } + + /// Returns current pin level + #[inline] + pub fn get_level(&self) -> Level { + self.pin.is_high().into() + } } /// Digital input or output level. @@ -62,6 +71,24 @@ pub enum Level { High, } +impl From for Level { + fn from(val: bool) -> Self { + match val { + true => Self::High, + false => Self::Low, + } + } +} + +impl Into for Level { + fn into(self) -> bool { + match self { + Level::Low => false, + Level::High => true, + } + } +} + // These numbers match DRIVE_A exactly so hopefully the compiler will unify them. #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -91,6 +118,7 @@ pub struct Output<'d, T: Pin> { } impl<'d, T: Pin> Output<'d, T> { + #[inline] pub fn new(pin: impl Unborrow + 'd, initial_output: Level, drive: OutputDrive) -> Self { let mut pin = Flex::new(pin); match initial_output { @@ -103,24 +131,43 @@ impl<'d, T: Pin> Output<'d, T> { } /// Set the output as high. + #[inline] pub fn set_high(&mut self) { self.pin.set_high() } /// Set the output as low. + #[inline] pub fn set_low(&mut self) { self.pin.set_low() } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.pin.set_low(), + Level::High => self.pin.set_high(), + } + } + /// Is the output pin set as high? + #[inline] pub fn is_set_high(&self) -> bool { self.pin.is_set_high() } /// Is the output pin set as low? + #[inline] pub fn is_set_low(&self) -> bool { self.pin.is_set_low() } + + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + self.pin.is_set_high().into() + } } fn convert_drive(drive: OutputDrive) -> DRIVE_A { @@ -159,6 +206,7 @@ impl<'d, T: Pin> Flex<'d, T> { /// /// The pin remains disconnected. The initial output level is unspecified, but can be changed /// before the pin is put into output mode. + #[inline] pub fn new(pin: impl Unborrow + 'd) -> Self { unborrow!(pin); // Pin will be in disconnected state. @@ -169,6 +217,7 @@ impl<'d, T: Pin> Flex<'d, T> { } /// Put the pin into input mode. + #[inline] pub fn set_as_input(&mut self, pull: Pull) { self.pin.conf().write(|w| { w.dir().input(); @@ -184,6 +233,7 @@ impl<'d, T: Pin> Flex<'d, T> { /// /// The pin level will be whatever was set before (or low by default). If you want it to begin /// at a specific level, call `set_high`/`set_low` on the pin first. + #[inline] pub fn set_as_output(&mut self, drive: OutputDrive) { self.pin.conf().write(|w| { w.dir().output(); @@ -204,6 +254,7 @@ impl<'d, T: Pin> Flex<'d, T> { /// /// The pin level will be whatever was set before (or low by default). If you want it to begin /// at a specific level, call `set_high`/`set_low` on the pin first. + #[inline] pub fn set_as_input_output(&mut self, pull: Pull, drive: OutputDrive) { self.pin.conf().write(|w| { w.dir().output(); @@ -216,37 +267,65 @@ impl<'d, T: Pin> Flex<'d, T> { } /// Put the pin into disconnected mode. + #[inline] pub fn set_as_disconnected(&mut self) { self.pin.conf().reset(); } + #[inline] pub fn is_high(&self) -> bool { !self.is_low() } + #[inline] pub fn is_low(&self) -> bool { self.pin.block().in_.read().bits() & (1 << self.pin.pin()) == 0 } + /// Returns current pin level + #[inline] + pub fn get_level(&self) -> Level { + self.is_high().into() + } + /// Set the output as high. + #[inline] pub fn set_high(&mut self) { self.pin.set_high() } /// Set the output as low. + #[inline] pub fn set_low(&mut self) { self.pin.set_low() } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.pin.set_low(), + Level::High => self.pin.set_high(), + } + } + /// Is the output pin set as high? + #[inline] pub fn is_set_high(&self) -> bool { !self.is_set_low() } /// Is the output pin set as low? + #[inline] pub fn is_set_low(&self) -> bool { self.pin.block().out.read().bits() & (1 << self.pin.pin()) == 0 } + + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + self.is_set_high().into() + } } impl<'d, T: Pin> Drop for Flex<'d, T> { From 57002875d6bf9ac4378955a4e33f77154fd32edd Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 13 Jul 2022 02:22:46 +0300 Subject: [PATCH 4/6] Add convenience GPIO functions to RP --- embassy-rp/src/gpio.rs | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index 131330e7..5e8126b4 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs @@ -14,6 +14,24 @@ pub enum Level { High, } +impl From for Level { + fn from(val: bool) -> Self { + match val { + true => Self::High, + false => Self::Low, + } + } +} + +impl Into for Level { + fn into(self) -> bool { + match self { + Level::Low => false, + Level::High => true, + } + } +} + /// Represents a pull setting for an input. #[derive(Debug, Eq, PartialEq)] pub enum Pull { @@ -34,6 +52,7 @@ pub struct Input<'d, T: Pin> { } impl<'d, T: Pin> Input<'d, T> { + #[inline] pub fn new(pin: impl Unborrow + 'd, pull: Pull) -> Self { let mut pin = Flex::new(pin); pin.set_as_input(); @@ -41,13 +60,21 @@ impl<'d, T: Pin> Input<'d, T> { Self { pin } } + #[inline] pub fn is_high(&self) -> bool { self.pin.is_high() } + #[inline] pub fn is_low(&self) -> bool { self.pin.is_low() } + + /// Returns current pin level + #[inline] + pub fn get_level(&self) -> Level { + self.pin.is_high().into() + } } pub struct Output<'d, T: Pin> { @@ -79,6 +106,15 @@ impl<'d, T: Pin> Output<'d, T> { self.pin.set_low() } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.pin.set_low(), + Level::High => self.pin.set_high(), + } + } + /// Is the output pin set as high? #[inline] pub fn is_set_high(&self) -> bool { @@ -91,6 +127,12 @@ impl<'d, T: Pin> Output<'d, T> { self.pin.is_set_low() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + self.pin.is_set_high().into() + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { @@ -129,6 +171,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.set_as_output() } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.set_low(), + Level::High => self.set_high(), + } + } + /// Is the output level high? #[inline] pub fn is_set_high(&self) -> bool { @@ -141,6 +192,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.is_set_as_output() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + self.is_set_high().into() + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { @@ -236,6 +293,12 @@ impl<'d, T: Pin> Flex<'d, T> { unsafe { self.pin.sio_in().read() & self.bit() == 0 } } + /// Returns current pin level + #[inline] + pub fn get_level(&self) -> Level { + self.is_high().into() + } + /// Set the output as high. #[inline] pub fn set_high(&mut self) { @@ -248,6 +311,15 @@ impl<'d, T: Pin> Flex<'d, T> { unsafe { self.pin.sio_out().value_clr().write_value(self.bit()) } } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.set_low(), + Level::High => self.set_high(), + } + } + /// Is the output level high? #[inline] pub fn is_set_high(&self) -> bool { @@ -260,6 +332,12 @@ impl<'d, T: Pin> Flex<'d, T> { !self.is_set_high() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + self.is_set_high().into() + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { From a335589f345c32cd74c00af63366fc197ca410ab Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 13 Jul 2022 02:25:09 +0300 Subject: [PATCH 5/6] Change get_set_level to get_output_level --- embassy-nrf/src/gpio.rs | 4 ++-- embassy-rp/src/gpio.rs | 6 +++--- embassy-stm32/src/gpio.rs | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index d8295fc0..3e3e14b5 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs @@ -165,7 +165,7 @@ impl<'d, T: Pin> Output<'d, T> { /// What level output is set to #[inline] - pub fn get_set_level(&self) -> Level { + pub fn get_output_level(&self) -> Level { self.pin.is_set_high().into() } } @@ -323,7 +323,7 @@ impl<'d, T: Pin> Flex<'d, T> { /// What level output is set to #[inline] - pub fn get_set_level(&self) -> Level { + pub fn get_output_level(&self) -> Level { self.is_set_high().into() } } diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index 5e8126b4..aa1508e9 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs @@ -129,7 +129,7 @@ impl<'d, T: Pin> Output<'d, T> { /// What level output is set to #[inline] - pub fn get_set_level(&self) -> Level { + pub fn get_output_level(&self) -> Level { self.pin.is_set_high().into() } @@ -194,7 +194,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// What level output is set to #[inline] - pub fn get_set_level(&self) -> Level { + pub fn get_output_level(&self) -> Level { self.is_set_high().into() } @@ -334,7 +334,7 @@ impl<'d, T: Pin> Flex<'d, T> { /// What level output is set to #[inline] - pub fn get_set_level(&self) -> Level { + pub fn get_output_level(&self) -> Level { self.is_set_high().into() } diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 5f0d94b1..1ba481e6 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -160,7 +160,7 @@ impl<'d, T: Pin> Flex<'d, T> { /// What level output is set to #[inline] - pub fn get_set_level(&self) -> Level { + pub fn get_output_level(&self) -> Level { self.is_set_high().into() } @@ -385,7 +385,7 @@ impl<'d, T: Pin> Output<'d, T> { /// What level output is set to #[inline] - pub fn get_set_level(&self) -> Level { + pub fn get_output_level(&self) -> Level { self.pin.is_set_high().into() } @@ -466,7 +466,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// What level output is set to #[inline] - pub fn get_set_level(&self) -> Level { + pub fn get_output_level(&self) -> Level { self.pin.is_set_high().into() } From 53e40860c17b2525ad4a8d25d6d0aa7fe4da789b Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 13 Jul 2022 02:45:37 +0300 Subject: [PATCH 6/6] Move all gpio logic to Flex --- embassy-nrf/src/gpio.rs | 9 +++------ embassy-rp/src/gpio.rs | 9 +++------ embassy-stm32/src/gpio.rs | 20 +++++++------------- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index 3e3e14b5..fd4ae2ec 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs @@ -59,7 +59,7 @@ impl<'d, T: Pin> Input<'d, T> { /// Returns current pin level #[inline] pub fn get_level(&self) -> Level { - self.pin.is_high().into() + self.pin.get_level() } } @@ -145,10 +145,7 @@ impl<'d, T: Pin> Output<'d, T> { /// Set the output level. #[inline] pub fn set_level(&mut self, level: Level) { - match level { - Level::Low => self.pin.set_low(), - Level::High => self.pin.set_high(), - } + self.pin.set_level(level) } /// Is the output pin set as high? @@ -166,7 +163,7 @@ impl<'d, T: Pin> Output<'d, T> { /// What level output is set to #[inline] pub fn get_output_level(&self) -> Level { - self.pin.is_set_high().into() + self.pin.get_output_level() } } diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index aa1508e9..c6dbb3d4 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs @@ -73,7 +73,7 @@ impl<'d, T: Pin> Input<'d, T> { /// Returns current pin level #[inline] pub fn get_level(&self) -> Level { - self.pin.is_high().into() + self.pin.get_level() } } @@ -109,10 +109,7 @@ impl<'d, T: Pin> Output<'d, T> { /// Set the output level. #[inline] pub fn set_level(&mut self, level: Level) { - match level { - Level::Low => self.pin.set_low(), - Level::High => self.pin.set_high(), - } + self.pin.set_level(level) } /// Is the output pin set as high? @@ -130,7 +127,7 @@ impl<'d, T: Pin> Output<'d, T> { /// What level output is set to #[inline] pub fn get_output_level(&self) -> Level { - self.pin.is_set_high().into() + self.pin.get_output_level() } /// Toggle pin output diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 1ba481e6..1059ebf8 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -303,7 +303,7 @@ impl<'d, T: Pin> Input<'d, T> { #[inline] pub fn get_level(&self) -> Level { - self.pin.is_high().into() + self.pin.get_level() } } @@ -365,10 +365,7 @@ impl<'d, T: Pin> Output<'d, T> { /// Set the output level. #[inline] pub fn set_level(&mut self, level: Level) { - match level { - Level::Low => self.pin.set_low(), - Level::High => self.pin.set_high(), - } + self.pin.set_level(level) } /// Is the output pin set as high? @@ -386,7 +383,7 @@ impl<'d, T: Pin> Output<'d, T> { /// What level output is set to #[inline] pub fn get_output_level(&self) -> Level { - self.pin.is_set_high().into() + self.pin.get_output_level() } /// Toggle pin output @@ -428,7 +425,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// Returns current pin level #[inline] pub fn get_level(&self) -> Level { - self.pin.is_high().into() + self.pin.get_level() } /// Set the output as high. @@ -446,16 +443,13 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// Set the output level. #[inline] pub fn set_level(&mut self, level: Level) { - match level { - Level::Low => self.pin.set_low(), - Level::High => self.pin.set_high(), - } + self.pin.set_level(level); } /// Is the output pin set as high? #[inline] pub fn is_set_high(&self) -> bool { - !self.is_set_low() + self.pin.is_set_high() } /// Is the output pin set as low? @@ -467,7 +461,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// What level output is set to #[inline] pub fn get_output_level(&self) -> Level { - self.pin.is_set_high().into() + self.pin.get_output_level() } /// Toggle pin output