qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [RFCv2] generic-gpio-led


From: Liviu Ionescu
Subject: Re: [Qemu-devel] [RFCv2] generic-gpio-led
Date: Wed, 17 Jun 2015 15:55:21 +0300

This second version of the GPIO LED is more general and can be used with
any MCU that has a GPIO port which implements outgoing gpio-irq's.

Object creation is done at .instance_init and configuration is 
entirely via qdev properties.

# Usage

The STM32-H103 use case is:


    DeviceState *led = qdev_create(NULL, TYPE_GPIO_LED);
    {
        /* STM32-H103 Green LED, GPIOC[12], active low */
        qdev_prop_set_bool(led, "active-low", true);
        qdev_prop_set_string(led, "on-message", "[Green LED On]\n");
        qdev_prop_set_string(led, "off-message", "[Green LED Off]\n");

        gpio_led_connect(led, "/machine/stm32/gpio[c]", 12);
    }
    qdev_realize(led);


Regards,

Liviu



diff --git a/hw/display/gpio-led.c b/hw/display/gpio-led.c
new file mode 100644
index 0000000..eccb6f7
--- /dev/null
+++ b/hw/display/gpio-led.c
@@ -0,0 +1,129 @@
+/*
+ * GPIO connected LED device emulation.
+ *
+ * Copyright (c) 2015 Liviu Ionescu
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "hw/display/gpio-led.h"
+
+/**
+ * This class implements a LED connected to a GPIO device.
+ */
+
+/* ----- Public ------------------------------------------------------------ */
+
+/**
+ * Connect this LED to the named GPIO port pin.
+ */
+void gpio_led_connect(DeviceState *dev, const char *port_name, int port_bit)
+{
+    GPIOLEDState *state = GPIO_LED_STATE(dev);
+
+    qdev_connect_gpio_out(DEVICE(object_resolve_path(port_name, NULL)),
+            port_bit, state->irq);
+}
+
+/* ----- Private ----------------------------------------------------------- */
+
+/**
+ * Callback used to notify the LED status change.
+ */
+static void gpio_led_irq_handler(void *opaque, int n, int level)
+{
+    GPIOLEDState *state = GPIO_LED_STATE(opaque);
+
+    /* There should be only one IRQ for the LED */
+    assert(n == 0);
+
+    FILE *file = stderr;
+    /*
+     * Assume that the IRQ is only triggered if the LED has changed state.
+     * If this is not correct, we may get multiple LED Offs or Ons in a row.
+     */
+    switch (level) {
+    case 0:
+        fprintf(file, "%s",
+                state->active_low ? state->on_message : state->off_message);
+        break;
+
+    case 1:
+        fprintf(file, "%s",
+                state->active_low ? state->off_message : state->on_message);
+        break;
+    }
+}
+
+static void gpio_led_instance_init_callback(Object *obj)
+{
+    qemu_log_function_name();
+
+    GPIOLEDState *state = GPIO_LED_STATE(obj);
+
+    state->active_low = false;
+
+    /*
+     * String properties must be dynamically allocated, otherwise setting
+     * them as properties fails. (this is a bit odd).
+     */
+    state->on_message = g_strdup("[LED on]\n");
+    state->off_message = g_strdup("[LED off]\n");
+
+    /*
+     * Allocate 1 single incoming irq, and fill it with
+     * [handler, this device, n=0]. (n==0 is checked in the
+     * handler by an assert).
+     */
+    state->irq = qemu_allocate_irq(gpio_led_irq_handler, obj, 0);
+    // qdev_init_gpio_in(DEVICE(obj), gpio_led_irq_handler, 1);
+
+    /* The connection will be done by the machine */
+}
+
+#define DEFINE_PROP_DEVICE_STATE_PTR(_n, _s, _f) \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, DeviceState*)
+
+#define DEFINE_PROP_CONST_STRING(_n, _s, _f)             \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_string, const char*)
+
+static Property gpio_led_properties[] = {
+        DEFINE_PROP_BOOL("active-low", GPIOLEDState, active_low, false),
+        DEFINE_PROP_CONST_STRING("on-message", GPIOLEDState, on_message),
+        DEFINE_PROP_CONST_STRING("off-message", GPIOLEDState, off_message),
+    DEFINE_PROP_END_OF_LIST() };
+
+static void gpio_led_class_init_callback(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->props = gpio_led_properties;
+}
+
+static const TypeInfo gpio_led_type_info = {
+    .name = TYPE_GPIO_LED,
+    .parent = TYPE_GPIO_LED_PARENT,
+    .instance_size = sizeof(GPIOLEDState),
+    .instance_init = gpio_led_instance_init_callback,
+    .class_init = gpio_led_class_init_callback,
+    .class_size = sizeof(GPIOLEDClass) };
+
+static void gpio_led_type_init()
+{
+    type_register_static(&gpio_led_type_info);
+}
+
+type_init(gpio_led_type_init);
+
+/* ------------------------------------------------------------------------- */
diff --git a/include/hw/display/gpio-led.h b/include/hw/display/gpio-led.h
new file mode 100644
index 0000000..865d487
--- /dev/null
+++ b/include/hw/display/gpio-led.h
@@ -0,0 +1,79 @@
+/*
+ * GPIO connected LED device emulation.
+ *
+ * Copyright (c) 2015 Liviu Ionescu
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GPIO_LED_H_
+#define GPIO_LED_H_
+
+#include "hw/qdev.h"
+#include "qemu/typedefs.h"
+#include "hw/sysbus.h"
+
+/* ------------------------------------------------------------------------- */
+
+#define DEFINE_PROP_GPIO_LED_PTR(_n, _s, _f) \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, GPIOLEDInfo*)
+
+/* ------------------------------------------------------------------------- */
+
+#define TYPE_GPIO_LED "gpio-led"
+
+#define GPIO_LED_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(GPIOLEDClass, (obj), TYPE_GPIO_LED)
+#define GPIO_LED_CLASS(klass) \
+    OBJECT_CLASS_CHECK(GPIOLEDClass, (klass), TYPE_GPIO_LED)
+
+// TODO: Change this to TYPE_DEVICE
+#define TYPE_GPIO_LED_PARENT TYPE_SYS_BUS_DEVICE
+typedef SysBusDeviceClass GPIOLEDParentClass;
+typedef SysBusDevice GPIOLEDParentState;
+
+typedef struct {
+    /*< private >*/
+    GPIOLEDParentClass parent_class;
+    /*< public >*/
+
+} GPIOLEDClass;
+
+/* ------------------------------------------------------------------------- */
+
+#define GPIO_LED_STATE(obj) \
+    OBJECT_CHECK(GPIOLEDState, (obj), TYPE_GPIO_LED)
+
+typedef struct {
+    /*< private >*/
+    GPIOLEDParentState parent_obj;
+    /*< public >*/
+
+    bool active_low;
+    const char *on_message;
+    const char *off_message;
+
+    /**
+     * The actual irq used to blink the LED. It works connected to
+     * a GPIO device outgoing irq.
+     */
+    qemu_irq irq;
+
+} GPIOLEDState;
+
+void gpio_led_connect(DeviceState *dev, const char *port_name, int port_bit);
+
+/* ------------------------------------------------------------------------- */
+
+#endif /* GPIO_LED_H_ */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]