Skip to contentSkip to conversation

misc/led: LED state is set opposite of what is expected

PATCH Superseded Glenn Miles 2 participants 3 messages Oct 24, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc/led: LED state is set opposite of what is expected

Testing of the LED state showed that when the LED polarity was set to GPIO_POLARITY_ACTIVE_LOW and a low logic value was set on the input GPIO of the LED, the LED was being turned off when it was expected to be turned on.

---
 hw/misc/led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/led.c +1 -1
index f6d6d68bce..96cad7578e 100644
@@ -63,7 +63,7 @@ static void led_set_state_gpio_handler(void *opaque, int line, int new_state)
     LEDState *s = LED(opaque);
 
     assert(line == 0);
-    led_set_state(s, !!new_state != s->gpio_active_high);
+    led_set_state(s, !new_state != s->gpio_active_high);
 }
 
 static void led_reset(DeviceState *dev)
Signature · 1 line
2.31.1
#1 lore raw
Quoted 5 lines · Glenn Miles

Testing of the LED state showed that when the LED polarity was set to GPIO_POLARITY_ACTIVE_LOW and a low logic value was set on the input GPIO of the LED, the LED was being turned off when it was expected to be turned on.

It looks to me from reading the code like the bug is there for active-high GPIO as well ?

Quoted 10 lines
---
 hw/misc/led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/led.c +0 -0
index f6d6d68bce..96cad7578e 100644

hw/misc/led.c static void led_set_state_gpio_handler(void *opaque, int line, int new_state) · line 67

Quoted 6 lines
hw/misc/led.c +1 -1
@@ -63,7 +63,7 @@ static void led_set_state_gpio_handler(void *opaque, int line, int new_state)
     LEDState *s = LED(opaque);
 
     assert(line == 0);
-    led_set_state(s, !!new_state != s->gpio_active_high);
+    led_set_state(s, !new_state != s->gpio_active_high);
 }

Maybe "!!new_state == s->gpio_active_high" would be clearer? Then you can see that we are (1) converting the int new_state to a bool with the !! idiom and (2) we enable the LED for a high input and active-high GPIO.

thanks
-- PMM

#2 lore raw
Quoted 9 lines · Peter Maydell
Quoted 4 lines · Glenn Miles

Testing of the LED state showed that when the LED polarity was set to GPIO_POLARITY_ACTIVE_LOW and a low logic value was set on the input GPIO of the LED, the LED was being turned off when it was expected to be turned on.

It looks to me from reading the code like the bug is there for active-high GPIO as well ?

Yes, this should fix the issue for both situations.

Quoted 24 lines
Quoted 11 lines
---
 hw/misc/led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/led.c +0 -0
index f6d6d68bce..96cad7578e 100644

hw/misc/led.c static void led_set_state_gpio_handler(void · line 68

Quoted 7 lines
hw/misc/led.c +1 -1
@@ -63,7 +63,7 @@ static void led_set_state_gpio_handler(void
 *opaque, int line, int new_state)
     LEDState *s = LED(opaque);
 
     assert(line == 0);
-    led_set_state(s, !!new_state != s->gpio_active_high);
+    led_set_state(s, !new_state != s->gpio_active_high);
 }

Maybe "!!new_state == s->gpio_active_high" would be clearer? Then you can see that we are (1) converting the int new_state to a bool with the !! idiom and (2) we enable the LED for a high input and active-high GPIO.

Quoted 2 lines

thanks
-- PMM

Yes, I agree, that is easier to read. Thanks!

Glenn