hw/misc: Add LED device
v2 cover hw/misc: Add LED device
Hello,
These patches are part of the GSoC unselected 'QEMU visualizer' project. As the AVR port is not merged, I switched to microbit to keep working on it.
This series presents a proof of concept of LED device that can be easily connected to a GPIO.
The LED emit QMP events, so an external visualizer can display the LED events.
Since v1: addressed Eric Blake review comments
- Added QMP rate limit
This is stable enough to be used for the GSoC UI.
Next steps planned:
- integrate Zephyr test
- have a centralized container for all the machine's LEDs, to track state changes in a single place and send less QMP events (grouping changes, restricted to what actually changed). [see to include keyboard LEDs].
- look at LED array/matrix such 7segments.
Regards,
Phil.
$ git backport-diff -u rfc-v1 Key: [----] : patches are identical [####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively
001/5:[0004] [FC] 'hw/misc: Add a LED device'
002/5:[0027] [FC] 'hw/misc/led: Add LED_STATUS_CHANGED QAPI event' 003/5:[----] [--] 'hw/misc/led: Add create_led_by_gpio_id() helper' 004/5:[----] [--] 'hw/arm/microbit: Add a fake LED to use as proof-of-concept with Zephyr' 005/5:[----] [--] 'hw/arm/tosa: Use LED device for the Bluetooth led'
Philippe Mathieu-Daudé (5):
hw/misc: Add a LED device
hw/misc/led: Add LED_STATUS_CHANGED QAPI event hw/misc/led: Add create_led_by_gpio_id() helper hw/arm/microbit: Add a fake LED to use as proof-of-concept with Zephyr hw/arm/tosa: Use LED device for the Bluetooth led
qapi/led.json | 47 ++++++++++++++++ qapi/qapi-schema.json | 1 + include/hw/misc/led.h | 45 +++++++++++++++ hw/arm/microbit.c | 3 + hw/arm/tosa.c | 7 +-- hw/misc/led.c | 126 ++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 7 +++ hw/arm/Kconfig | 2 + hw/misc/Kconfig | 3 + hw/misc/Makefile.objs | 1 + hw/misc/trace-events | 3 + qapi/Makefile.objs | 2 +- 12 files changed, 242 insertions(+), 5 deletions(-) create mode 100644 qapi/led.json create mode 100644 include/hw/misc/led.h create mode 100644 hw/misc/led.c
Signature · 1 line
2.21.3
RFC PATCH v2 1/5 hw/misc: Add a LED device
A LED device can be connected to a GPIO output.
--- include/hw/misc/led.h | 30 ++++++++++++++++ hw/misc/led.c | 84 +++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 6 ++++ hw/misc/Kconfig | 3 ++ hw/misc/Makefile.objs | 1 + hw/misc/trace-events | 3 ++ 6 files changed, 127 insertions(+) create mode 100644 include/hw/misc/led.h create mode 100644 hw/misc/led.c
include/hw/misc/led.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* QEMU single LED device +* +* Copyright (C) 2020 Philippe Mathieu-Daudé <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef HW_MISC_LED_H +#define HW_MISC_LED_H + +#include "hw/qdev-core.h" +#include "hw/sysbus.h" /* FIXME remove */ + +#define TYPE_LED "led" +#define LED (obj) OBJECT_CHECK(LEDState, (obj), TYPE_LED) + +typedef struct LEDState { +/* Private */ +SysBusDevice parent_obj;/* FIXME DeviceState */ +/* Public */ + +qemu_irq irq; +uint8_t current_state; + +/* Properties */ +char *name; +uint8_t reset_state;/* TODO [GPIO_ACTIVE_LOW, GPIO_ACTIVE_HIGH] */ +}LEDState ; + +#endif /* HW_MISC_LED_H */
hw/misc/led.c new file +84 -0
84 lines not on this page · raw message
MAINTAINERS +6 -0
@@ -1857,6 +1857,12 @@ F: docs/specs/vmgenid.txt F: tests/qtest/vmgenid-test.c F: stubs/vmgenid.c +LED +M: Philippe Mathieu-Daudé <[email protected]> +S: Maintained +F: include/hw/misc/led.h +F: hw/misc/led.c + Unimplemented device M: Peter Maydell <[email protected]> R: Philippe Mathieu-Daudé <[email protected]>
hw/misc/Kconfig +3 -0
@@ -126,6 +126,9 @@ config AUX config UNIMP bool +config LED + bool + config MAC_VIA bool select MOS6522
hw/misc/Makefile.objs +1 -0
@@ -91,3 +91,4 @@ common-obj-$(CONFIG_NRF51_SOC) += nrf51_rng.o obj-$(CONFIG_MAC_VIA) += mac_via.o common-obj-$(CONFIG_GRLIB) += grlib_ahb_apb_pnp.o +common-obj-$(CONFIG_LED) += led.o
hw/misc/trace-events +3 -0
@@ -206,3 +206,6 @@ via1_rtc_cmd_pram_sect_write(int sector, int offset, int addr, int value) "secto # grlib_ahb_apb_pnp.c grlib_ahb_pnp_read(uint64_t addr, uint32_t value) "AHB PnP read addr:0x%03"PRIx64" data:0x%08x" grlib_apb_pnp_read(uint64_t addr, uint32_t value) "APB PnP read addr:0x%03"PRIx64" data:0x%08x" + +# led.c +led_set(const char *name, uint8_t old_state, uint8_t new_state) "led name:'%s' state %d -> %d"
Signature · 1 line
2.21.3
Re: [RFC PATCH v2 1/5] hw/misc: Add a LED device
Am 12.06.20 um 19:54 schrieb Philippe Mathieu-Daudé:
Quoted 40 lines
A LED device can be connected to a GPIO output.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/hw/misc/led.h | 30 ++++++++++++++++ hw/misc/led.c | 84 +++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 6 ++++ hw/misc/Kconfig | 3 ++ hw/misc/Makefile.objs | 1 + hw/misc/trace-events | 3 ++ 6 files changed, 127 insertions(+) create mode 100644 include/hw/misc/led.h create mode 100644 hw/misc/led.cinclude/hw/misc/led.h new file +20
-0@@ -0,0 +1,30 @@ +/* +* QEMU single LED device +* +* Copyright (C) 2020 Philippe Mathieu-Daudé <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef HW_MISC_LED_H +#define HW_MISC_LED_H + +#include "hw/qdev-core.h" +#include "hw/sysbus.h" /* FIXME remove */ + +#define TYPE_LED "led" +#define LED(obj) OBJECT_CHECK(LEDState, (obj), TYPE_LED) + +typedef struct LEDState { + /* Private */ + SysBusDevice parent_obj; /* FIXME DeviceState */ + /* Public */
include/hw/misc/led.h line 30
Quoted 10 lines
include/hw/misc/led.h new file +10
-0+ +qemu_irq irq; +uint8_t current_state; + +/* Properties */ +char *name; + uint8_t reset_state; /* TODO [GPIO_ACTIVE_LOW, GPIO_ACTIVE_HIGH] */ +} LEDState; + +#endif /* HW_MISC_LED_H */
LEDSTate could be made smaller (less holes) by simply re-ordering the
elements: irq, name, current_state, reset_state
Kind regards
Stefan
Re: [RFC PATCH v2 1/5] hw/misc: Add a LED device
In reply to Philippe Mathieu-DaudéQuoted 32 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
A LED device can be connected to a GPIO output.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/hw/misc/led.h | 30 ++++++++++++++++ hw/misc/led.c | 84 +++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 6 ++++ hw/misc/Kconfig | 3 ++ hw/misc/Makefile.objs | 1 + hw/misc/trace-events | 3 ++ 6 files changed, 127 insertions(+) create mode 100644 include/hw/misc/led.h create mode 100644 hw/misc/led.cinclude/hw/misc/led.h new file +12
-0@@ -0,0 +1,30 @@ +/* +* QEMU single LED device +* +* Copyright (C) 2020 Philippe Mathieu-Daudé <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef HW_MISC_LED_H +#define HW_MISC_LED_H + +#include "hw/qdev-core.h" +#include "hw/sysbus.h" /* FIXME remove */
include/hw/misc/led.h line 22
Quoted 10 lines
include/hw/misc/led.h new file +10
-0+ +#define TYPE_LED "led" +#define LED (obj) OBJECT_CHECK(LEDState, (obj), TYPE_LED) + +typedef struct LEDState { + /* Private */ + SysBusDevice parent_obj; /* FIXME DeviceState */ + /* Public */ + + qemu_irq irq;
Why an irq?
include/hw/misc/led.h line 23
Quoted 1 line
include/hw/misc/led.h new file +1
-0+uint8_t current_state;
Is the state of this device boolean or is it a 0..255 0=off, 255=full on, analog thing?
Can an LED device be connected to a PWM device driving a GPIO - what happens?
Dave
Quoted 39 lines
include/hw/misc/led.h new file +6
-0+/* Properties */ +char *name; + uint8_t reset_state; /* TODO [GPIO_ACTIVE_LOW, GPIO_ACTIVE_HIGH] */ +} LEDState; + +#endif /* HW_MISC_LED_H */ hw/misc/led.c new file +27
-0@@ -0,0 +1,84 @@ +/* +* QEMU single LED device +* +* Copyright (C) 2020 Philippe Mathieu-Daudé <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "hw/qdev-properties.h" +#include "hw/misc/led.h" +#include "hw/irq.h" +#include "trace.h" + +static void led_set (void *opaque,int line,int new_state) +{ +LEDState *s =LED (opaque); + +trace_led_set (s->name, s->current_state, new_state); + + s->current_state = new_state; +} + +static void led_reset (DeviceState *dev) +{ +LEDState *s =LED (dev);
hw/misc/led.c line 37
Quoted 10 lines
hw/misc/led.c new file +10
-0+ +led_set (dev,0 , s->reset_state); +} + +static const VMStateDescription vmstate_led = { + .name =TYPE_LED , + .version_id =1 , + .minimum_version_id =1 , + .fields = (VMStateField []) { +VMSTATE_UINT8 (reset_state, LEDState),
I'm not sure you need to migrate this - this is a property that's set on the device, not a dynamic state of the device
Quoted 9 lines
hw/misc/led.c new file +9
-0+ VMSTATE_END_OF_LIST() + } +}; + +static void led_realize (DeviceState *dev,Error **errp) +{ +LEDState *s =LED (dev); + +if (s->name ==NULL ) {
hw/misc/led.c line 56
Quoted 10 lines
hw/misc/led.c new file +10
-0+error_setg (errp,"property 'name' not specified" ); +return ; + } + +qdev_init_gpio_in (DEVICE (s), led_set,1 ); +} + +static Property led_properties[] = { +DEFINE_PROP_STRING ("name" , LEDState, name), +DEFINE_PROP_UINT8 ("reset_state" , LEDState, reset_state,0 ),
I suggest you add a property for the notional colour; that way any UIs that are built can use that as a hint.
Dave
Quoted 82 lines
hw/misc/led.c new file +28
-0+ DEFINE_PROP_END_OF_LIST(), +}; + +static void led_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->desc ="LED" ; + dc->vmsd = &vmstate_led; + dc->reset = led_reset; + dc->realize = led_realize; +set_bit (DEVICE_CATEGORY_DISPLAY , dc->categories); +device_class_set_props (dc, led_properties); +} + +static const TypeInfo led_info = { + .name =TYPE_LED , + .parent =TYPE_SYS_BUS_DEVICE ,/* FIXME TYPE_DEVICE */ + .instance_size =sizeof (LEDState), + .class_init = led_class_init +}; + +static void led_register_types (void ) +{ +type_register_static (&led_info); +} + +type_init (led_register_types)MAINTAINERS +6
-0@@ -1857,6 +1857,12 @@ F: docs/specs/vmgenid.txt F: tests/qtest/vmgenid-test.c F: stubs/vmgenid.c +LED +M: Philippe Mathieu-Daudé <[email protected]> +S: Maintained +F: include/hw/misc/led.h +F: hw/misc/led.c + Unimplemented device M: Peter Maydell <[email protected]> R: Philippe Mathieu-Daudé <[email protected]>hw/misc/Kconfig +3
-0@@ -126,6 +126,9 @@ config AUX config UNIMP bool +config LED + bool + config MAC_VIA bool select MOS6522hw/misc/Makefile.objs +1
-0@@ -91,3 +91,4 @@ common-obj-$(CONFIG_NRF51_SOC) += nrf51_rng.o obj-$(CONFIG_MAC_VIA) += mac_via.o common-obj-$(CONFIG_GRLIB) += grlib_ahb_apb_pnp.o +common-obj-$(CONFIG_LED) += led.ohw/misc/trace-events +3
-0@@ -206,3 +206,6 @@ via1_rtc_cmd_pram_sect_write(int sector, int offset, int addr, int value) "secto # grlib_ahb_apb_pnp.c grlib_ahb_pnp_read(uint64_t addr, uint32_t value) "AHB PnP read addr:0x%03"PRIx64" data:0x%08x" grlib_apb_pnp_read(uint64_t addr, uint32_t value) "APB PnP read addr:0x%03"PRIx64" data:0x%08x" + +# led.c +led_set(const char *name, uint8_t old_state, uint8_t new_state) "led name:'%s' state %d -> %d"Signature · 1 line
2.21.3
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [RFC PATCH v2 1/5] hw/misc: Add a LED device
Quoted 37 lines · Dr. David Alan Gilbert
Quoted 36 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
A LED device can be connected to a GPIO output.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/hw/misc/led.h | 30 ++++++++++++++++ hw/misc/led.c | 84 +++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 6 ++++ hw/misc/Kconfig | 3 ++ hw/misc/Makefile.objs | 1 + hw/misc/trace-events | 3 ++ 6 files changed, 127 insertions(+) create mode 100644 include/hw/misc/led.h create mode 100644 hw/misc/led.cinclude/hw/misc/led.h new file +16
-0@@ -0,0 +1,30 @@ +/* +* QEMU single LED device +* +* Copyright (C) 2020 Philippe Mathieu-Daudé <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef HW_MISC_LED_H +#define HW_MISC_LED_H + +#include "hw/qdev-core.h" +#include "hw/sysbus.h" /* FIXME remove */ + +#define TYPE_LED "led" +#define LED (obj) OBJECT_CHECK(LEDState, (obj), TYPE_LED) +
Quoted 8 lines
include/hw/misc/led.h line 22
Quoted 6 lines
include/hw/misc/led.h new file +6
-0+typedef struct LEDState { + /* Private */ + SysBusDevice parent_obj; /* FIXME DeviceState */ + /* Public */ + + qemu_irq irq;Why an irq?
We model GPIO/IRQ the same way, this is simply a GPIO, right?
It makes modeling easier IMO. This is for visualization purpose.
Quoted 7 lines
include/hw/misc/led.h line 23
Quoted 2 lines
include/hw/misc/led.h new file +1
-0+uint8_t current_state;Is the state of this device boolean or is it a 0..255 0=off, 255=full on, analog thing?
Can an LED device be connected to a PWM device driving a GPIO - what happens?
Well I simply wanted to use a boolean, but I need to consider if we can model intensity here in case of PWM. This is interesting.
Quoted 48 lines
Dave
Quoted 44 lines
include/hw/misc/led.h new file +6
-0+/* Properties */ +char *name; + uint8_t reset_state; /* TODO [GPIO_ACTIVE_LOW, GPIO_ACTIVE_HIGH] */ +} LEDState; + +#endif /* HW_MISC_LED_H */ hw/misc/led.c new file +32
-0@@ -0,0 +1,84 @@ +/* +* QEMU single LED device +* +* Copyright (C) 2020 Philippe Mathieu-Daudé <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "hw/qdev-properties.h" +#include "hw/misc/led.h" +#include "hw/irq.h" +#include "trace.h" + +static void led_set (void *opaque,int line,int new_state) +{ +LEDState *s =LED (opaque); + +trace_led_set (s->name, s->current_state, new_state); + + s->current_state = new_state; +} + +static void led_reset (DeviceState *dev) +{ +LEDState *s =LED (dev); + +led_set (dev,0 , s->reset_state); +} + +static const VMStateDescription vmstate_led = {
Quoted 8 lines
hw/misc/led.c line 37
Quoted 5 lines
hw/misc/led.c new file +5
-0+ .name = TYPE_LED, + .version_id = 1, + .minimum_version_id = 1, + .fields = (VMStateField []) { +VMSTATE_UINT8 (reset_state, LEDState),I'm not sure you need to migrate this - this is a property that's set on the device, not a dynamic state of the device
Yes you are right.
Quoted 14 lines
Quoted 14 lines
hw/misc/led.c new file +14
-0+ VMSTATE_END_OF_LIST() + } +}; + +static void led_realize (DeviceState *dev,Error **errp) +{ +LEDState *s =LED (dev); + +if (s->name ==NULL ) { +error_setg (errp,"property 'name' not specified" ); +return ; + } + +qdev_init_gpio_in (DEVICE (s), led_set,1 );
Quoted 8 lines
hw/misc/led.c line 56
Quoted 5 lines
hw/misc/led.c new file +5
-0+} + +static Property led_properties[] = { +DEFINE_PROP_STRING ("name" , LEDState, name), +DEFINE_PROP_UINT8 ("reset_state" , LEDState, reset_state,0 ),I suggest you add a property for the notional colour; that way any UIs that are built can use that as a hint.
Great idea, thanks!
Quoted 88 lines
Dave
Quoted 82 lines
hw/misc/led.c new file +28
-0+ DEFINE_PROP_END_OF_LIST(), +}; + +static void led_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->desc ="LED" ; + dc->vmsd = &vmstate_led; + dc->reset = led_reset; + dc->realize = led_realize; +set_bit (DEVICE_CATEGORY_DISPLAY , dc->categories); +device_class_set_props (dc, led_properties); +} + +static const TypeInfo led_info = { + .name =TYPE_LED , + .parent =TYPE_SYS_BUS_DEVICE ,/* FIXME TYPE_DEVICE */ + .instance_size =sizeof (LEDState), + .class_init = led_class_init +}; + +static void led_register_types (void ) +{ +type_register_static (&led_info); +} + +type_init (led_register_types)MAINTAINERS +6
-0@@ -1857,6 +1857,12 @@ F: docs/specs/vmgenid.txt F: tests/qtest/vmgenid-test.c F: stubs/vmgenid.c +LED +M: Philippe Mathieu-Daudé <[email protected]> +S: Maintained +F: include/hw/misc/led.h +F: hw/misc/led.c + Unimplemented device M: Peter Maydell <[email protected]> R: Philippe Mathieu-Daudé <[email protected]>hw/misc/Kconfig +3
-0@@ -126,6 +126,9 @@ config AUX config UNIMP bool +config LED + bool + config MAC_VIA bool select MOS6522hw/misc/Makefile.objs +1
-0@@ -91,3 +91,4 @@ common-obj-$(CONFIG_NRF51_SOC) += nrf51_rng.o obj-$(CONFIG_MAC_VIA) += mac_via.o common-obj-$(CONFIG_GRLIB) += grlib_ahb_apb_pnp.o +common-obj-$(CONFIG_LED) += led.ohw/misc/trace-events +3
-0@@ -206,3 +206,6 @@ via1_rtc_cmd_pram_sect_write(int sector, int offset, int addr, int value) "secto # grlib_ahb_apb_pnp.c grlib_ahb_pnp_read(uint64_t addr, uint32_t value) "AHB PnP read addr:0x%03"PRIx64" data:0x%08x" grlib_apb_pnp_read(uint64_t addr, uint32_t value) "APB PnP read addr:0x%03"PRIx64" data:0x%08x" + +# led.c +led_set(const char *name, uint8_t old_state, uint8_t new_state) "led name:'%s' state %d -> %d"Signature · 1 line
2.21.3--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [RFC PATCH v2 1/5] hw/misc: Add a LED device
Quoted 54 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Quoted 37 lines · Dr. David Alan Gilbert
Quoted 36 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
A LED device can be connected to a GPIO output.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/hw/misc/led.h | 30 ++++++++++++++++ hw/misc/led.c | 84 +++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 6 ++++ hw/misc/Kconfig | 3 ++ hw/misc/Makefile.objs | 1 + hw/misc/trace-events | 3 ++ 6 files changed, 127 insertions(+) create mode 100644 include/hw/misc/led.h create mode 100644 hw/misc/led.cinclude/hw/misc/led.h new file +16
-0@@ -0,0 +1,30 @@ +/* +* QEMU single LED device +* +* Copyright (C) 2020 Philippe Mathieu-Daudé <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef HW_MISC_LED_H +#define HW_MISC_LED_H + +#include "hw/qdev-core.h" +#include "hw/sysbus.h" /* FIXME remove */ + +#define TYPE_LED "led" +#define LED (obj) OBJECT_CHECK(LEDState, (obj), TYPE_LED) +Quoted 8 lines
include/hw/misc/led.h line 22
Quoted 6 lines
include/hw/misc/led.h new file +6
-0+typedef struct LEDState { + /* Private */ + SysBusDevice parent_obj; /* FIXME DeviceState */ + /* Public */ + + qemu_irq irq;Why an irq?
We model GPIO/IRQ the same way, this is simply a GPIO, right?
It makes modeling easier IMO. This is for visualization purpose.
Quoted 3 lines
Quoted 2 lines
include/hw/misc/led.h new file +1
-0+uint8_t current_state;
Quoted 7 lines
Quoted 4 lines
Is the state of this device boolean or is it a 0..255 0=off, 255=full on, analog thing?
Can an LED device be connected to a PWM device driving a GPIO - what happens?Well I simply wanted to use a boolean, but I need to consider if we can model intensity here in case of PWM. This is interesting.
Which then leads to an interesting question about your events in the next mail; if I was to connect an LED to a PWM driven GPIO I'd get zillions of events which may not be what I want.
Dave
Quoted 82 lines
Quoted 48 lines
Dave
Quoted 44 lines
include/hw/misc/led.h new file +6
-0+/* Properties */ +char *name; + uint8_t reset_state; /* TODO [GPIO_ACTIVE_LOW, GPIO_ACTIVE_HIGH] */ +} LEDState; + +#endif /* HW_MISC_LED_H */ hw/misc/led.c new file +32
-0@@ -0,0 +1,84 @@ +/* +* QEMU single LED device +* +* Copyright (C) 2020 Philippe Mathieu-Daudé <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "hw/qdev-properties.h" +#include "hw/misc/led.h" +#include "hw/irq.h" +#include "trace.h" + +static void led_set (void *opaque,int line,int new_state) +{ +LEDState *s =LED (opaque); + +trace_led_set (s->name, s->current_state, new_state); + + s->current_state = new_state; +} + +static void led_reset (DeviceState *dev) +{ +LEDState *s =LED (dev); + +led_set (dev,0 , s->reset_state); +} + +static const VMStateDescription vmstate_led = {Quoted 8 lines
hw/misc/led.c line 37
Quoted 5 lines
hw/misc/led.c new file +5
-0+ .name = TYPE_LED, + .version_id = 1, + .minimum_version_id = 1, + .fields = (VMStateField []) { +VMSTATE_UINT8 (reset_state, LEDState),I'm not sure you need to migrate this - this is a property that's set on the device, not a dynamic state of the device
Yes you are right.
Quoted 14 lines
Quoted 14 lines
hw/misc/led.c new file +14
-0+ VMSTATE_END_OF_LIST() + } +}; + +static void led_realize (DeviceState *dev,Error **errp) +{ +LEDState *s =LED (dev); + +if (s->name ==NULL ) { +error_setg (errp,"property 'name' not specified" ); +return ; + } + +qdev_init_gpio_in (DEVICE (s), led_set,1 );Quoted 8 lines
hw/misc/led.c line 56
Quoted 5 lines
hw/misc/led.c new file +5
-0+} + +static Property led_properties[] = { +DEFINE_PROP_STRING ("name" , LEDState, name), +DEFINE_PROP_UINT8 ("reset_state" , LEDState, reset_state,0 ),I suggest you add a property for the notional colour; that way any UIs that are built can use that as a hint.
Quoted 91 lines
Great idea, thanks!
Quoted 88 lines
Dave
Quoted 82 lines
hw/misc/led.c new file +28
-0+ DEFINE_PROP_END_OF_LIST(), +}; + +static void led_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->desc ="LED" ; + dc->vmsd = &vmstate_led; + dc->reset = led_reset; + dc->realize = led_realize; +set_bit (DEVICE_CATEGORY_DISPLAY , dc->categories); +device_class_set_props (dc, led_properties); +} + +static const TypeInfo led_info = { + .name =TYPE_LED , + .parent =TYPE_SYS_BUS_DEVICE ,/* FIXME TYPE_DEVICE */ + .instance_size =sizeof (LEDState), + .class_init = led_class_init +}; + +static void led_register_types (void ) +{ +type_register_static (&led_info); +} + +type_init (led_register_types)MAINTAINERS +6
-0@@ -1857,6 +1857,12 @@ F: docs/specs/vmgenid.txt F: tests/qtest/vmgenid-test.c F: stubs/vmgenid.c +LED +M: Philippe Mathieu-Daudé <[email protected]> +S: Maintained +F: include/hw/misc/led.h +F: hw/misc/led.c + Unimplemented device M: Peter Maydell <[email protected]> R: Philippe Mathieu-Daudé <[email protected]>hw/misc/Kconfig +3
-0@@ -126,6 +126,9 @@ config AUX config UNIMP bool +config LED + bool + config MAC_VIA bool select MOS6522hw/misc/Makefile.objs +1
-0@@ -91,3 +91,4 @@ common-obj-$(CONFIG_NRF51_SOC) += nrf51_rng.o obj-$(CONFIG_MAC_VIA) += mac_via.o common-obj-$(CONFIG_GRLIB) += grlib_ahb_apb_pnp.o +common-obj-$(CONFIG_LED) += led.ohw/misc/trace-events +3
-0@@ -206,3 +206,6 @@ via1_rtc_cmd_pram_sect_write(int sector, int offset, int addr, int value) "secto # grlib_ahb_apb_pnp.c grlib_ahb_pnp_read(uint64_t addr, uint32_t value) "AHB PnP read addr:0x%03"PRIx64" data:0x%08x" grlib_apb_pnp_read(uint64_t addr, uint32_t value) "APB PnP read addr:0x%03"PRIx64" data:0x%08x" + +# led.c +led_set(const char *name, uint8_t old_state, uint8_t new_state) "led name:'%s' state %d -> %d"Signature · 1 line
2.21.3--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
RFC PATCH v2 2/5 hw/misc/led: Add LED_STATUS_CHANGED QAPI event
In reply to Philippe Mathieu-DaudéAllow LED devices to emit STATUS_CHANGED events on a QMP chardev.
QMP event examples:
{
"timestamp": {
"seconds": 1591704274,
"microseconds": 520850
},
"event": "LED_STATUS_CHANGED",
"data": {
"name": "Green LED #0",
"status": "on"
}
}
{
"timestamp": {
"seconds": 1591704275,
"microseconds": 530912
},
"event": "LED_STATUS_CHANGED",
"data": {
"name": "Green LED #0",
"status": "off"
}
}---
Since v1: rate limit 4/sec (eblake)
--- qapi/led.json | 47 +++++++++++++++++++++++++++++++++++++++++++ qapi/qapi-schema.json | 1 + include/hw/misc/led.h | 1 + hw/misc/led.c | 24 +++++++++++++++++++++- MAINTAINERS | 1 + qapi/Makefile.objs | 2 +- 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 qapi/led.json
qapi/led.json new file +47 -0
@@ -0,0 +1,47 @@ +# -*- Mode: Python -*- +# + +## +# = LED device +## + +## +# @LedState: +# +# Status of a LED +# +# @on: device is emitting +# +# @off: device is off +# +# Since: 5.1 +## +{ 'enum': 'LedState', 'data': [ 'on', 'off' ] } + +## +# @LED_STATUS_CHANGED: +# +# Emitted when LED status changed +# +# @name: LED description +# +# @status: New status +# +# Since: 5.1 +# +# Example: +# +# <- {"timestamp": {"seconds": 1541579657, "microseconds": 986760}, +# "event": "LED_STATUS_CHANGED", +# "data": +# {"name": "Blue LED #3", +# "status": "on" +# } +# } +# +## +{ 'event': 'LED_STATUS_CHANGED', + 'data': { 'name' : 'str', + 'status' : 'LedState' + } +}
qapi/qapi-schema.json +1 -0
@@ -84,3 +84,4 @@ { 'include': 'misc.json' } { 'include': 'misc-target.json' } { 'include': 'audio.json' } +{ 'include': 'led.json' }
include/hw/misc/led.h +1 -0
@@ -21,6 +21,7 @@ typedef struct LEDState {qemu_irq irq;uint8_t current_state; +int64_t last_event_ms;/* Properties */ char *name;
hw/misc/led.c +23 -1
@@ -7,18 +7,40 @@*/ #include "qemu/osdep.h" #include "qapi/error.h" +#include "qapi/qapi-events-led.h" +#include "qemu/timer.h" #include "migration/vmstate.h" #include "hw/qdev-properties.h" #include "hw/misc/led.h" #include "hw/irq.h" #include "trace.h" +#define MAX_QMP_LED_EVENTS_PER_SEC 4/* TODO shared between LED children? */ + +static void emit_led_status_changed_event (LEDState *s,int state) +{ +static const int64_t delay_min_ms =NANOSECONDS_PER_SECOND /SCALE_MS + /MAX_QMP_LED_EVENTS_PER_SEC ; +int64_t now =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); + +if (now - s->last_event_ms > delay_min_ms) { +qapi_event_send_led_status_changed (s->name, state + ?LED_STATE_ON + :LED_STATE_OFF ); + }else { +/* TODO count skipped events? */ + } + s->last_event_ms = now; +} +static void led_set (void *opaque,int line,int new_state) {LEDState *s =LED (opaque);trace_led_set (s->name, s->current_state, new_state); - +if (new_state != s->current_state) { +emit_led_status_changed_event (s, new_state); + } s->current_state = new_state; }
MAINTAINERS +1 -0
@@ -1860,6 +1860,7 @@ F: stubs/vmgenid.c LED M: Philippe Mathieu-Daudé <[email protected]> S: Maintained +F: qapi/led.json F: include/hw/misc/led.h F: hw/misc/led.c
qapi/Makefile.objs +1 -1
@@ -6,7 +6,7 @@ util-obj-y += qmp-event.o util-obj-y += qapi-util.o QAPI_COMMON_MODULES = audio authz block-core block char common control crypto -QAPI_COMMON_MODULES += dump error introspect job machine migration misc +QAPI_COMMON_MODULES += dump error introspect job led machine migration misc QAPI_COMMON_MODULES += net pragma qdev qom rdma rocker run-state sockets tpm QAPI_COMMON_MODULES += trace transaction ui QAPI_TARGET_MODULES = machine-target misc-target
Signature · 1 line
2.21.3
Re: [RFC PATCH v2 2/5] hw/misc/led: Add LED_STATUS_CHANGED QAPI event
Quoted 19 lines · Philippe Mathieu-Daudé
Allow LED devices to emit
STATUS_CHANGEDevents on a QMP chardev.QMP event examples:
{ "timestamp": { "seconds": 1591704274, "microseconds": 520850 }, "event": "LED_STATUS_CHANGED", "data": { "name": "Green LED #0", "status": "on" } } { "timestamp": { "seconds": 1591704275,
Quoted 8 lines
"microseconds": 530912
},
"event": "LED_STATUS_CHANGED",
"data": {
"name": "Green LED #0",
"status": "off"
}
}Is there a proof-of-concept of what might be on the other end of this QMP link consuming these events and doing something useful with them?
thanks
-- PMM
RFC PATCH v2 3/5 hw/misc/led: Add create_led_by_gpio_id() helper
In reply to Philippe Mathieu-DaudéAdd create_led_by_gpio_id() to easily connect a LED to a GPIO output.
--- include/hw/misc/led.h | 14 ++++++++++++++ hw/misc/led.c | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+)
include/hw/misc/led.h +14 -0
@@ -28,4 +28,18 @@ typedef struct LEDState { uint8_t reset_state; /* TODO [GPIO_ACTIVE_LOW, GPIO_ACTIVE_HIGH] */ } LEDState; +/** +* create_led_by_gpio_id: create and LED device +* @parent: the parent object +* @gpio_dev: device exporting GPIOs +* @gpio_id: GPIO ID of this LED +* @name: name of the LED +* +* This utility function creates a LED and connects it to a +* GPIO exported by another device. +*/ +DeviceState *create_led_by_gpio_id (Object *parentobj, +DeviceState *gpio_dev,unsigned gpio_id, +const char *led_name); +#endif /* HW_MISC_LED_H */
hw/misc/led.c +20 -0
@@ -104,3 +104,23 @@ static void led_register_types(void) }type_init (led_register_types) + +DeviceState *create_led_by_gpio_id(Object *parentobj, +DeviceState *gpio_dev,unsigned gpio_id, +const char *led_name) +{ +DeviceState *dev; +char *name; + + dev =qdev_create (NULL ,TYPE_LED ); +/* TODO set "reset_state" */ +qdev_prop_set_string (dev,"name" , led_name); + name =g_ascii_strdown (led_name,-1 ); + name =g_strdelimit (name," #" ,'-' ); +object_property_add_child (parentobj, name,OBJECT (dev)); +g_free (name); +qdev_init_nofail (dev); +qdev_connect_gpio_out (gpio_dev, gpio_id,qdev_get_gpio_in (dev,0 )); + +return dev; +}
Signature · 1 line
2.21.3
RFC PATCH v2 4/5 hw/arm/microbit: Add a fake LED to use as proof-of-concept with Zephyr
In reply to Philippe Mathieu-DaudéWe were using an AVR based Arduino to use this device, but since the port is not merged, the microbit is the easiest board to use with Zephyr.
Note the microbit doesn't have a such LED, this is simply a proof of concept.
How to test:
- Apply this patch on zephyr-v2.3.0
diff --git a/boards/arm/qemu_cortex_m0/qemu_cortex_m0.dts b/boards/arm/qemu_cortex_m0/qemu_cortex_m0.dts index a1b3044275..61b39506b1 100644
--- a/boards/arm/qemu_cortex_m0/qemu_cortex_m0.dts
+++ b/boards/arm/qemu_cortex_m0/qemu_cortex_m0.dts
@@ -21,6 +21,18 @@
zephyr,flash = &flash0;
zephyr,code-partition = &slot0_partition;
}; +
+ leds {
+ compatible = "gpio-leds";
+ led0: led_0 {
+ gpios = <&gpio0 21 GPIO_ACTIVE_LOW>;
+ label = "Green LED 0";
+ };
+ };
+
+ aliases {
+ led0 = &led0;
+ };
};
&gpiote {- Build Zephyr blinky:
$ west build -b qemu_cortex_m0 samples/basic/blinky
- Run QEMU
$ qemu-system-arm -M microbit -trace led\* \
-kernel ~/zephyrproject/zephyr/build/zephyr/zephyr.elf -trace led\*
[email protected]:led_set led name:'Green LED #0' state 0 -> 0
[email protected]:led_set led name:'Green LED #0' state 0 -> 1
[email protected]:led_set led name:'Green LED #0' state 1 -> 0--- hw/arm/microbit.c | 3 +++ hw/arm/Kconfig | 1 + 2 files changed, 4 insertions(+)
hw/arm/microbit.c +3 -0
@@ -18,6 +18,7 @@#include "hw/arm/nrf51_soc.h" #include "hw/i2c/microbit_i2c.h" #include "hw/qdev-properties.h" +#include "hw/misc/led.h" typedef struct { MachineState parent;
@@ -58,6 +59,8 @@ static void microbit_init(MachineState *machine)memory_region_add_subregion_overlap (&s->nrf51.container,NRF51_TWI_BASE , mr,-1 ); +create_led_by_gpio_id (OBJECT (machine),DEVICE (soc),21 ,"Green LED #0" ); +armv7m_load_kernel (ARM_CPU (first_cpu), machine->kernel_filename,NRF51_SOC (soc)->flash_size); }
hw/arm/Kconfig +1 -0
@@ -436,6 +436,7 @@ config FSL_IMX6UL config MICROBIT bool select NRF51_SOC + select LED config NRF51_SOC bool
Signature · 1 line
2.21.3
Re: [RFC PATCH v2 4/5] hw/arm/microbit: Add a fake LED to use as proof-of-concept with Zephyr
Quoted 6 lines · Philippe Mathieu-Daudé
We were using an AVR based Arduino to use this device, but since the port is not merged, the microbit is the easiest board to use with Zephyr.
Note the microbit doesn't have a such LED, this is simply a proof of concept.
I know this is an RFC patchset, but just for the record, I don't think we should add things to the board that the real hardware doesn't have. (The microbit does have a 5x5 LED "display" but I dunno if this would be the best way to implement that...)
thanks
-- PMM
Re: [RFC PATCH v2 4/5] hw/arm/microbit: Add a fake LED to use as proof-of-concept with Zephyr
Quoted 8 lines · Peter Maydell
Quoted 6 lines · Philippe Mathieu-Daudé
We were using an AVR based Arduino to use this device, but since the port is not merged, the microbit is the easiest board to use with Zephyr.
Note the microbit doesn't have a such LED, this is simply a proof of concept.
Quoted 5 lines
I know this is an RFC patchset, but just for the record, I don't think we should add things to the board that the real hardware doesn't have. (The microbit does have a 5x5 LED "display" but I dunno if this would be the best way to implement that...)
Yes, I plan to implement a generic LED array matrix and use it in the microbit (and few other boards).
Without the AVR port merged, the microbit is the quickest way for me to test with Zephyr.
Quoted 4 lines
thanks
-- PMM
RFC PATCH v2 5/5 hw/arm/tosa: Use LED device for the Bluetooth led
In reply to Philippe Mathieu-Daudé--- hw/arm/tosa.c | 7 +++---- hw/arm/Kconfig | 1 + 2 files changed, 4 insertions(+), 4 deletions(-)
hw/arm/tosa.c +3 -4
@@ -24,6 +24,7 @@#include "hw/irq.h" #include "hw/ssi/ssi.h" #include "hw/sysbus.h" +#include "hw/misc/led.h" #include "exec/address-spaces.h" #define TOSA_RAM 0x04000000
@@ -68,9 +69,6 @@ static void tosa_microdrive_attach(PXA2xxState *cpu)static void tosa_out_switch (void *opaque,int line,int level) {switch (line) { -case 0 : -fprintf (stderr,"blue LED %s.\n" , level ?"on" :"off" ); -break ;case 1 :fprintf (stderr,"green LED %s.\n" , level ?"on" :"off" );break ;
@@ -119,7 +117,6 @@ static void tosa_gpio_setup(PXA2xxState *cpu,qdev_get_gpio_in (cpu->gpio,TOSA_GPIO_JC_CF_IRQ ), NULL); -qdev_connect_gpio_out (scp1,TOSA_GPIO_BT_LED , outsignals[0 ]);qdev_connect_gpio_out (scp1,TOSA_GPIO_NOTE_LED , outsignals[1 ]);qdev_connect_gpio_out (scp1,TOSA_GPIO_CHRG_ERR_LED , outsignals[2 ]);qdev_connect_gpio_out (scp1,TOSA_GPIO_WLAN_LED , outsignals[3 ]);
@@ -234,6 +231,8 @@ static void tosa_init(MachineState *machine) scp0 =sysbus_create_simple ("scoop" ,0x08800000 ,NULL ); scp1 =sysbus_create_simple ("scoop" ,0x14800040 ,NULL ); +create_led_by_gpio_id (OBJECT (machine),DEVICE (scp1), +TOSA_GPIO_BT_LED ,"blue LED" );tosa_gpio_setup (mpu, scp0, scp1, tmio);
hw/arm/Kconfig +1 -0
@@ -150,6 +150,7 @@ config TOSA select ZAURUS # scoop select MICRODRIVE select PXA2XX + select LED config SPITZ bool
Signature · 1 line
2.21.3
Re: [RFC PATCH v2 5/5] hw/arm/tosa: Use LED device for the Bluetooth led
Quoted 20 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/arm/tosa.c | 7 +++---- hw/arm/Kconfig | 1 + 2 files changed, 4 insertions(+), 4 deletions(-)hw/arm/tosa.c +1
-0@@ -24,6 +24,7 @@#include "hw/irq.h" #include "hw/ssi/ssi.h" #include "hw/sysbus.h" +#include "hw/misc/led.h" #include "exec/address-spaces.h" #define TOSA_RAM 0x04000000
hw/arm/tosa.c static void tosa_microdrive_attach(PXA2xxState *cpu) · line 74
Quoted 9 lines
hw/arm/tosa.c +0
-3@@ -68,9 +69,6 @@ static void tosa_microdrive_attach(PXA2xxState *cpu)static void tosa_out_switch (void *opaque,int line,int level) {switch (line) { -case 0 : -fprintf (stderr,"blue LED %s.\n" , level ?"on" :"off" ); -break ;case 1 :fprintf (stderr,"green LED %s.\n" , level ?"on" :"off" );break ;
Why convert the blue LED and not the green, amber or wlan LEDs ?
thanks
-- PMM
Re: [RFC PATCH v2 5/5] hw/arm/tosa: Use LED device for the Bluetooth led
Quoted 19 lines · Peter Maydell
Quoted 18 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/arm/tosa.c | 7 +++---- hw/arm/Kconfig | 1 + 2 files changed, 4 insertions(+), 4 deletions(-)hw/arm/tosa.c +1
-0@@ -24,6 +24,7 @@#include "hw/irq.h" #include "hw/ssi/ssi.h" #include "hw/sysbus.h" +#include "hw/misc/led.h" #include "exec/address-spaces.h"
Quoted 13 lines
hw/arm/tosa.c static void tosa_microdrive_attach(PXA2xxState *cpu) · line 74
Quoted 11 lines
hw/arm/tosa.c +0
-3#define TOSA_RAM 0x04000000@@ -68,9 +69,6 @@ static void tosa_microdrive_attach(PXA2xxState *cpu)static void tosa_out_switch (void *opaque,int line,int level) {switch (line) { -case 0 : -fprintf (stderr,"blue LED %s.\n" , level ?"on" :"off" ); -break ;case 1 :fprintf (stderr,"green LED %s.\n" , level ?"on" :"off" );break ;Why convert the blue LED and not the green, amber or wlan LEDs ?
I don't have the schematics to verify the mapping is correct:
#define TOSA_GPIO_BT_LED (TOSA_SCOOP_JC_GPIO_BASE + 0)
#define TOSA_GPIO_NOTE_LED (TOSA_SCOOP_JC_GPIO_BASE + 1)
#define TOSA_GPIO_CHRG_ERR_LED (TOSA_SCOOP_JC_GPIO_BASE + 2)
#define TOSA_GPIO_TC6393XB_L3V_ON (TOSA_SCOOP_JC_GPIO_BASE + 5)
#define TOSA_GPIO_WLAN_LED (TOSA_SCOOP_JC_GPIO_BASE + 7)
static void tosa_out_switch(void *opaque, int line, int level)
{
switch (line) {
case 0:
fprintf(stderr, "blue LED %s.\n", level ? "on" : "off");
break;
case 1:
fprintf(stderr, "green LED %s.\n", level ? "on" : "off");
break;
case 2:
fprintf(stderr, "amber LED %s.\n", level ? "on" : "off");
break;
case 3:
fprintf(stderr, "wlan LED %s.\n", level ? "on" : "off");
break;
default:
fprintf(stderr, "Uhandled out event: %d = %d\n", line, level);
break;
}
} qdev_connect_gpio_out(scp1, TOSA_GPIO_BT_LED, outsignals[0]);
qdev_connect_gpio_out(scp1, TOSA_GPIO_NOTE_LED, outsignals[1]);
qdev_connect_gpio_out(scp1, TOSA_GPIO_CHRG_ERR_LED, outsignals[2]);
qdev_connect_gpio_out(scp1, TOSA_GPIO_WLAN_LED, outsignals[3]);I wanted a simple proof-of-concept I could test with a board, to show how easy this API can be used.
Using the binaries from here:
https://www.omegamoon.com/blog/static.php?page=ZaurusAndroid
the blue LED never blinks, probably because we remove the bluetooth stack. Or maybe I haven't figured the correct command line options.
For the next RFC I plan to rework to have a single LED container per board, to have a unique singleton object that generates QMP events.
Quoted 4 lines
thanks
-- PMM