863: Add more convenience GPIO functions r=Dirbaio a=chemicstry

This reduces boilerplate code from:
```rs
if config.pin_high {
    pin.set_high()
} else {
    pin.set_low()
}
```
to
```rs
pin.set_level(config.pin_high.into());
```

Co-authored-by: chemicstry <chemicstry@gmail.com>
This commit is contained in:
bors[bot] 2022-07-12 23:56:45 +00:00 committed by GitHub
commit 7c8dffeb12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 224 additions and 1 deletions

View file

@ -38,6 +38,7 @@ pub struct Input<'d, T: Pin> {
} }
impl<'d, T: Pin> Input<'d, T> { impl<'d, T: Pin> Input<'d, T> {
#[inline]
pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self { pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self {
let mut pin = Flex::new(pin); let mut pin = Flex::new(pin);
pin.set_as_input(pull); pin.set_as_input(pull);
@ -45,13 +46,21 @@ impl<'d, T: Pin> Input<'d, T> {
Self { pin } Self { pin }
} }
#[inline]
pub fn is_high(&self) -> bool { pub fn is_high(&self) -> bool {
self.pin.is_high() self.pin.is_high()
} }
#[inline]
pub fn is_low(&self) -> bool { pub fn is_low(&self) -> bool {
self.pin.is_low() self.pin.is_low()
} }
/// Returns current pin level
#[inline]
pub fn get_level(&self) -> Level {
self.pin.get_level()
}
} }
/// Digital input or output level. /// Digital input or output level.
@ -62,6 +71,24 @@ pub enum Level {
High, High,
} }
impl From<bool> for Level {
fn from(val: bool) -> Self {
match val {
true => Self::High,
false => Self::Low,
}
}
}
impl Into<bool> 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. // These numbers match DRIVE_A exactly so hopefully the compiler will unify them.
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[cfg_attr(feature = "defmt", derive(defmt::Format))]
@ -91,6 +118,7 @@ pub struct Output<'d, T: Pin> {
} }
impl<'d, T: Pin> Output<'d, T> { impl<'d, T: Pin> Output<'d, T> {
#[inline]
pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level, drive: OutputDrive) -> Self { pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level, drive: OutputDrive) -> Self {
let mut pin = Flex::new(pin); let mut pin = Flex::new(pin);
match initial_output { match initial_output {
@ -103,24 +131,40 @@ impl<'d, T: Pin> Output<'d, T> {
} }
/// Set the output as high. /// Set the output as high.
#[inline]
pub fn set_high(&mut self) { pub fn set_high(&mut self) {
self.pin.set_high() self.pin.set_high()
} }
/// Set the output as low. /// Set the output as low.
#[inline]
pub fn set_low(&mut self) { pub fn set_low(&mut self) {
self.pin.set_low() self.pin.set_low()
} }
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
self.pin.set_level(level)
}
/// Is the output pin set as high? /// Is the output pin set as high?
#[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
self.pin.is_set_high() self.pin.is_set_high()
} }
/// Is the output pin set as low? /// Is the output pin set as low?
#[inline]
pub fn is_set_low(&self) -> bool { pub fn is_set_low(&self) -> bool {
self.pin.is_set_low() self.pin.is_set_low()
} }
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
self.pin.get_output_level()
}
} }
fn convert_drive(drive: OutputDrive) -> DRIVE_A { fn convert_drive(drive: OutputDrive) -> DRIVE_A {
@ -159,6 +203,7 @@ impl<'d, T: Pin> Flex<'d, T> {
/// ///
/// The pin remains disconnected. The initial output level is unspecified, but can be changed /// The pin remains disconnected. The initial output level is unspecified, but can be changed
/// before the pin is put into output mode. /// before the pin is put into output mode.
#[inline]
pub fn new(pin: impl Unborrow<Target = T> + 'd) -> Self { pub fn new(pin: impl Unborrow<Target = T> + 'd) -> Self {
unborrow!(pin); unborrow!(pin);
// Pin will be in disconnected state. // Pin will be in disconnected state.
@ -169,6 +214,7 @@ impl<'d, T: Pin> Flex<'d, T> {
} }
/// Put the pin into input mode. /// Put the pin into input mode.
#[inline]
pub fn set_as_input(&mut self, pull: Pull) { pub fn set_as_input(&mut self, pull: Pull) {
self.pin.conf().write(|w| { self.pin.conf().write(|w| {
w.dir().input(); w.dir().input();
@ -184,6 +230,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 /// 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. /// at a specific level, call `set_high`/`set_low` on the pin first.
#[inline]
pub fn set_as_output(&mut self, drive: OutputDrive) { pub fn set_as_output(&mut self, drive: OutputDrive) {
self.pin.conf().write(|w| { self.pin.conf().write(|w| {
w.dir().output(); w.dir().output();
@ -204,6 +251,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 /// 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. /// 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) { pub fn set_as_input_output(&mut self, pull: Pull, drive: OutputDrive) {
self.pin.conf().write(|w| { self.pin.conf().write(|w| {
w.dir().output(); w.dir().output();
@ -216,37 +264,65 @@ impl<'d, T: Pin> Flex<'d, T> {
} }
/// Put the pin into disconnected mode. /// Put the pin into disconnected mode.
#[inline]
pub fn set_as_disconnected(&mut self) { pub fn set_as_disconnected(&mut self) {
self.pin.conf().reset(); self.pin.conf().reset();
} }
#[inline]
pub fn is_high(&self) -> bool { pub fn is_high(&self) -> bool {
!self.is_low() !self.is_low()
} }
#[inline]
pub fn is_low(&self) -> bool { pub fn is_low(&self) -> bool {
self.pin.block().in_.read().bits() & (1 << self.pin.pin()) == 0 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. /// Set the output as high.
#[inline]
pub fn set_high(&mut self) { pub fn set_high(&mut self) {
self.pin.set_high() self.pin.set_high()
} }
/// Set the output as low. /// Set the output as low.
#[inline]
pub fn set_low(&mut self) { pub fn set_low(&mut self) {
self.pin.set_low() 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? /// Is the output pin set as high?
#[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
!self.is_set_low() !self.is_set_low()
} }
/// Is the output pin set as low? /// Is the output pin set as low?
#[inline]
pub fn is_set_low(&self) -> bool { pub fn is_set_low(&self) -> bool {
self.pin.block().out.read().bits() & (1 << self.pin.pin()) == 0 self.pin.block().out.read().bits() & (1 << self.pin.pin()) == 0
} }
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
self.is_set_high().into()
}
} }
impl<'d, T: Pin> Drop for Flex<'d, T> { impl<'d, T: Pin> Drop for Flex<'d, T> {

View file

@ -14,6 +14,24 @@ pub enum Level {
High, High,
} }
impl From<bool> for Level {
fn from(val: bool) -> Self {
match val {
true => Self::High,
false => Self::Low,
}
}
}
impl Into<bool> for Level {
fn into(self) -> bool {
match self {
Level::Low => false,
Level::High => true,
}
}
}
/// Represents a pull setting for an input. /// Represents a pull setting for an input.
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum Pull { pub enum Pull {
@ -34,6 +52,7 @@ pub struct Input<'d, T: Pin> {
} }
impl<'d, T: Pin> Input<'d, T> { impl<'d, T: Pin> Input<'d, T> {
#[inline]
pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self { pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self {
let mut pin = Flex::new(pin); let mut pin = Flex::new(pin);
pin.set_as_input(); pin.set_as_input();
@ -41,13 +60,21 @@ impl<'d, T: Pin> Input<'d, T> {
Self { pin } Self { pin }
} }
#[inline]
pub fn is_high(&self) -> bool { pub fn is_high(&self) -> bool {
self.pin.is_high() self.pin.is_high()
} }
#[inline]
pub fn is_low(&self) -> bool { pub fn is_low(&self) -> bool {
self.pin.is_low() self.pin.is_low()
} }
/// Returns current pin level
#[inline]
pub fn get_level(&self) -> Level {
self.pin.get_level()
}
} }
pub struct Output<'d, T: Pin> { pub struct Output<'d, T: Pin> {
@ -79,6 +106,12 @@ impl<'d, T: Pin> Output<'d, T> {
self.pin.set_low() self.pin.set_low()
} }
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
self.pin.set_level(level)
}
/// Is the output pin set as high? /// Is the output pin set as high?
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
@ -91,6 +124,12 @@ impl<'d, T: Pin> Output<'d, T> {
self.pin.is_set_low() self.pin.is_set_low()
} }
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
self.pin.get_output_level()
}
/// Toggle pin output /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {
@ -129,6 +168,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
self.pin.set_as_output() 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? /// Is the output level high?
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
@ -141,6 +189,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
self.pin.is_set_as_output() self.pin.is_set_as_output()
} }
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
self.is_set_high().into()
}
/// Toggle pin output /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {
@ -236,6 +290,12 @@ impl<'d, T: Pin> Flex<'d, T> {
unsafe { self.pin.sio_in().read() & self.bit() == 0 } 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. /// Set the output as high.
#[inline] #[inline]
pub fn set_high(&mut self) { pub fn set_high(&mut self) {
@ -248,6 +308,15 @@ impl<'d, T: Pin> Flex<'d, T> {
unsafe { self.pin.sio_out().value_clr().write_value(self.bit()) } 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? /// Is the output level high?
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
@ -260,6 +329,12 @@ impl<'d, T: Pin> Flex<'d, T> {
!self.is_set_high() !self.is_set_high()
} }
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
self.is_set_high().into()
}
/// Toggle pin output /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {

View file

@ -141,6 +141,11 @@ impl<'d, T: Pin> Flex<'d, T> {
state == vals::Idr::LOW state == vals::Idr::LOW
} }
#[inline]
pub fn get_level(&self) -> Level {
self.is_high().into()
}
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
!self.is_set_low() !self.is_set_low()
@ -153,6 +158,12 @@ impl<'d, T: Pin> Flex<'d, T> {
state == vals::Odr::LOW state == vals::Odr::LOW
} }
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
self.is_set_high().into()
}
#[inline] #[inline]
pub fn set_high(&mut self) { pub fn set_high(&mut self) {
self.pin.set_high(); self.pin.set_high();
@ -164,6 +175,14 @@ impl<'d, T: Pin> Flex<'d, T> {
self.pin.set_low(); 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 /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {
@ -281,6 +300,11 @@ impl<'d, T: Pin> Input<'d, T> {
pub fn is_low(&self) -> bool { pub fn is_low(&self) -> bool {
self.pin.is_low() self.pin.is_low()
} }
#[inline]
pub fn get_level(&self) -> Level {
self.pin.get_level()
}
} }
/// Digital input or output level. /// Digital input or output level.
@ -291,6 +315,24 @@ pub enum Level {
High, High,
} }
impl From<bool> for Level {
fn from(val: bool) -> Self {
match val {
true => Self::High,
false => Self::Low,
}
}
}
impl Into<bool> for Level {
fn into(self) -> bool {
match self {
Level::Low => false,
Level::High => true,
}
}
}
/// GPIO output driver. /// GPIO output driver.
pub struct Output<'d, T: Pin> { pub struct Output<'d, T: Pin> {
pub(crate) pin: Flex<'d, T>, pub(crate) pin: Flex<'d, T>,
@ -320,6 +362,12 @@ impl<'d, T: Pin> Output<'d, T> {
self.pin.set_low(); self.pin.set_low();
} }
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
self.pin.set_level(level)
}
/// Is the output pin set as high? /// Is the output pin set as high?
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
@ -332,6 +380,12 @@ impl<'d, T: Pin> Output<'d, T> {
self.pin.is_set_low() self.pin.is_set_low()
} }
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
self.pin.get_output_level()
}
/// Toggle pin output /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {
@ -368,6 +422,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
self.pin.is_low() self.pin.is_low()
} }
/// Returns current pin level
#[inline]
pub fn get_level(&self) -> Level {
self.pin.get_level()
}
/// Set the output as high. /// Set the output as high.
#[inline] #[inline]
pub fn set_high(&mut self) { pub fn set_high(&mut self) {
@ -380,10 +440,16 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
self.pin.set_low(); self.pin.set_low();
} }
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
self.pin.set_level(level);
}
/// Is the output pin set as high? /// Is the output pin set as high?
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
!self.is_set_low() self.pin.is_set_high()
} }
/// Is the output pin set as low? /// Is the output pin set as low?
@ -392,6 +458,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
self.pin.is_set_low() self.pin.is_set_low()
} }
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
self.pin.get_output_level()
}
/// Toggle pin output /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {