From 91c1d17f166541c5dafc765e952486f21841e120 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 11 Jul 2023 12:38:53 +0200 Subject: [PATCH] rp/gpio: fix is_set_high/is_set_low, expand tests. --- embassy-rp/src/gpio.rs | 4 ++-- tests/rp/src/bin/gpio.rs | 36 ++++++++++++++++++++++++++++++++++-- tests/stm32/src/bin/gpio.rs | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index d18fb909..a3d330cd 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs @@ -566,13 +566,13 @@ impl<'d, T: Pin> Flex<'d, T> { /// Is the output level high? #[inline] pub fn is_set_high(&self) -> bool { - (self.pin.sio_out().value().read() & self.bit()) == 0 + !self.is_set_low() } /// Is the output level low? #[inline] pub fn is_set_low(&self) -> bool { - !self.is_set_high() + (self.pin.sio_out().value().read() & self.bit()) == 0 } /// What level output is set to diff --git a/tests/rp/src/bin/gpio.rs b/tests/rp/src/bin/gpio.rs index 51112d31..946b7dc8 100644 --- a/tests/rp/src/bin/gpio.rs +++ b/tests/rp/src/bin/gpio.rs @@ -21,14 +21,46 @@ async fn main(_spawner: Spawner) { let b = Input::new(&mut b, Pull::None); { - let _a = Output::new(&mut a, Level::Low); + let a = Output::new(&mut a, Level::Low); delay(); assert!(b.is_low()); + assert!(!b.is_high()); + assert!(a.is_set_low()); + assert!(!a.is_set_high()); } { - let _a = Output::new(&mut a, Level::High); + let mut a = Output::new(&mut a, Level::High); + delay(); + assert!(!b.is_low()); + assert!(b.is_high()); + assert!(!a.is_set_low()); + assert!(a.is_set_high()); + + // Test is_set_low / is_set_high + a.set_low(); + delay(); + assert!(b.is_low()); + assert!(a.is_set_low()); + assert!(!a.is_set_high()); + + a.set_high(); delay(); assert!(b.is_high()); + assert!(!a.is_set_low()); + assert!(a.is_set_high()); + + // Test toggle + a.toggle(); + delay(); + assert!(b.is_low()); + assert!(a.is_set_low()); + assert!(!a.is_set_high()); + + a.toggle(); + delay(); + assert!(b.is_high()); + assert!(!a.is_set_low()); + assert!(a.is_set_high()); } } diff --git a/tests/stm32/src/bin/gpio.rs b/tests/stm32/src/bin/gpio.rs index 67f44317..aad17443 100644 --- a/tests/stm32/src/bin/gpio.rs +++ b/tests/stm32/src/bin/gpio.rs @@ -40,14 +40,46 @@ async fn main(_spawner: Spawner) { let b = Input::new(&mut b, Pull::None); { - let _a = Output::new(&mut a, Level::Low, Speed::Low); + let a = Output::new(&mut a, Level::Low, Speed::Low); delay(); assert!(b.is_low()); + assert!(!b.is_high()); + assert!(a.is_set_low()); + assert!(!a.is_set_high()); } { - let _a = Output::new(&mut a, Level::High, Speed::Low); + let mut a = Output::new(&mut a, Level::High, Speed::Low); + delay(); + assert!(!b.is_low()); + assert!(b.is_high()); + assert!(!a.is_set_low()); + assert!(a.is_set_high()); + + // Test is_set_low / is_set_high + a.set_low(); + delay(); + assert!(b.is_low()); + assert!(a.is_set_low()); + assert!(!a.is_set_high()); + + a.set_high(); delay(); assert!(b.is_high()); + assert!(!a.is_set_low()); + assert!(a.is_set_high()); + + // Test toggle + a.toggle(); + delay(); + assert!(b.is_low()); + assert!(a.is_set_low()); + assert!(!a.is_set_high()); + + a.toggle(); + delay(); + assert!(b.is_high()); + assert!(!a.is_set_low()); + assert!(a.is_set_high()); } }