From c28d288118000138b4ca08676db832b648d9f1f6 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Thu, 5 May 2022 18:54:39 +0000 Subject: [PATCH] Fix StatefulOutputPin According to https://docs.rs/embedded-hal/latest/embedded_hal/digital/v2/trait.StatefulOutputPin.html, the functions in StatefulOutputPin should not read the actual electrical value of the pin, but the current state of the output driver. The fe310 manual states: "Reading the output_val register returns the written value. Reading the input_val register returns the actual value of the pin gated by input_en." So the right thing to do here is to read the corresponding bit in the output register. --- src/gpio.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/gpio.rs b/src/gpio.rs index 63239f0..bb2a437 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -95,6 +95,11 @@ trait PeripheralAccess { atomic_set_bit(r, index, bit); } + fn output_value(index: usize) -> bool { + let p = Self::peripheral(); + (p.output_val.read().bits() >> (index & 31) & 1) != 0 + } + fn toggle_pin(index: usize) { let p = Self::peripheral(); let r: &AtomicU32 = unsafe { core::mem::transmute(&p.output_val) }; @@ -290,7 +295,7 @@ macro_rules! gpio { impl StatefulOutputPin for $PXi> { fn is_set_high(&self) -> Result { - Ok($GPIOX::input_value(Self::INDEX)) + Ok($GPIOX::output_value(Self::INDEX)) } fn is_set_low(&self) -> Result {