Skip to contentSkip to conversation

AVR GPIO Emulation and Arduino D13 LED

PATCH In review G S Niteesh Babu 3 participants 7 messages Mar 11, 2021 – Mar 13, 2021 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover AVR GPIO Emulation and Arduino D13 LED

Hello,

The following series of the patches add a basic AVR GPIO emulation to QEMU. The AVR GPIO emulation patch was originally written by Heecheol Yang and was posted on the mailing list around 5 months ago. I am re-publishing the patch with some bug fixes along with my own patch, Adding the D13 onboard LED.

G S Niteesh Babu (2):
hw/gpio/avr_gpio.c: add tracing for read and writes avr/arduino: Add D13 LED

Heecheol Yang (1):
hw/avr: Add limited support for avr gpio registers

 hw/avr/Kconfig             |   2 +
 hw/avr/arduino.c           |  15 ++++
 hw/avr/atmega.c            |   7 +-
 hw/avr/atmega.h            |   2 +
 hw/gpio/Kconfig            |   3 +
 hw/gpio/avr_gpio.c         | 148 +++++++++++++++++++++++++++++++++++++
 hw/gpio/meson.build        |   1 +
 hw/gpio/trace-events       |   6 ++
 include/hw/gpio/avr_gpio.h |  53 +++++++++++++
 9 files changed, 235 insertions(+), 2 deletions(-)
 create mode 100644 hw/gpio/avr_gpio.c
 create mode 100644 include/hw/gpio/avr_gpio.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/3 hw/avr: Add limited support for avr gpio registers

From: Heecheol Yang <[email protected]>

Add some of these features for AVR GPIO:

- GPIO I/O : PORTx registers
- Data Direction : DDRx registers
- DDRx toggling : PINx registers

Following things are not supported yet:
- MCUR registers

Message-id: DM6PR16MB2473D96C4D975DE569A330F2E60C0@DM6PR16MB2473.namprd16.prod.outlook.com

---
 hw/avr/Kconfig             |   1 +
 hw/avr/atmega.c            |   7 +-
 hw/avr/atmega.h            |   2 +
 hw/gpio/Kconfig            |   3 +
 hw/gpio/avr_gpio.c         | 140 +++++++++++++++++++++++++++++++++++++
 hw/gpio/meson.build        |   1 +
 include/hw/gpio/avr_gpio.h |  53 ++++++++++++++
 7 files changed, 205 insertions(+), 2 deletions(-)
 create mode 100644 hw/gpio/avr_gpio.c
 create mode 100644 include/hw/gpio/avr_gpio.h
hw/avr/Kconfig +1 -0
index d31298c3cc..16a57ced11 100644
@@ -3,6 +3,7 @@ config AVR_ATMEGA_MCU
     select AVR_TIMER16
     select AVR_USART
     select AVR_POWER
+    select AVR_GPIO
 
 config ARDUINO
     select AVR_ATMEGA_MCU
hw/avr/atmega.c +5 -2
index 44c6afebbb..ad942028fd 100644
@@ -283,8 +283,11 @@ static void atmega_realize(DeviceState *dev, Error **errp)
             continue;
         }
         devname = g_strdup_printf("atmega-gpio-%c", 'a' + (char)i);
-        create_unimplemented_device(devname,
-                                    OFFSET_DATA + mc->dev[idx].addr, 3);
+        object_initialize_child(OBJECT(dev), devname, &s->gpio[i],
+                                TYPE_AVR_GPIO);
+        sysbus_realize(SYS_BUS_DEVICE(&s->gpio[i]), &error_abort);
+        sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0,
+            OFFSET_DATA + mc->dev[idx].addr);
         g_free(devname);
     }
 
hw/avr/atmega.h +2 -0
index a99ee15c7e..e2289d5744 100644
@@ -13,6 +13,7 @@
 
 #include "hw/char/avr_usart.h"
 #include "hw/timer/avr_timer16.h"
+#include "hw/gpio/avr_gpio.h"
 #include "hw/misc/avr_power.h"
 #include "target/avr/cpu.h"
 #include "qom/object.h"
@@ -44,6 +45,7 @@ struct AtmegaMcuState {
     DeviceState *io;
     AVRMaskState pwr[POWER_MAX];
     AVRUsartState usart[USART_MAX];
+    AVRGPIOState gpio[GPIO_MAX];
     AVRTimer16State timer[TIMER_MAX];
     uint64_t xtal_freq_hz;
 };
hw/gpio/Kconfig +3 -0
index f0e7405f6e..fde7019b2b 100644
@@ -13,3 +13,6 @@ config GPIO_PWR
 
 config SIFIVE_GPIO
     bool
+
+config AVR_GPIO
+    bool
hw/gpio/avr_gpio.c new file +140 -0

140 lines not on this page · raw message

hw/gpio/meson.build +1 -0
index 79568f00ce..366aca52ca 100644
@@ -13,3 +13,4 @@ softmmu_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_gpio.c'))
 softmmu_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_gpio.c'))
 softmmu_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_gpio.c'))
 softmmu_ss.add(when: 'CONFIG_SIFIVE_GPIO', if_true: files('sifive_gpio.c'))
+softmmu_ss.add(when: 'CONFIG_AVR_GPIO', if_true: files('avr_gpio.c'))
include/hw/gpio/avr_gpio.h new file +53 -0

53 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/3 hw/gpio/avr_gpio.c: add tracing for read and writes

In reply to G S Niteesh Babu

Added tracing for gpio read, write, and update output irq.

1) trace_avr_gpio_update_ouput_irq
2) trace_avr_gpio_read
3) trace_avr_gpio_write

---
 hw/gpio/avr_gpio.c   | 16 ++++++++++++----
 hw/gpio/trace-events |  6 ++++++
 2 files changed, 18 insertions(+), 4 deletions(-)
hw/gpio/avr_gpio.c +12 -4
index 8fc192dbcb..8498a99dd2 100644
@@ -26,6 +26,7 @@
 #include "hw/irq.h"
 #include "hw/gpio/avr_gpio.h"
 #include "hw/qdev-properties.h"
+#include "trace.h"
 
 static void avr_gpio_reset(DeviceState *dev)
 {
@@ -49,6 +50,7 @@ static void avr_gpio_write_port(AVRGPIOState *s, uint64_t value)
 
         if (cur_ddr_pin_val && (cur_port_pin_val != new_port_pin_val)) {
             qemu_set_irq(s->out[pin], new_port_pin_val);
+            trace_avr_gpio_update_output_irq(pin, new_port_pin_val);
         }
         cur_port_val >>= 1u;
         cur_ddr_val >>= 1u;
@@ -58,19 +60,25 @@ static void avr_gpio_write_port(AVRGPIOState *s, uint64_t value)
 }
 static uint64_t avr_gpio_read(void *opaque, hwaddr offset, unsigned int size)
 {
+    uint8_t val = 0;
     AVRGPIOState *s = (AVRGPIOState *)opaque;
     switch (offset) {
     case GPIO_PIN:
-        return s->reg.pin;
+        val = s->reg.pin;
+        break;
     case GPIO_DDR:
-        return s->reg.ddr;
+        val = s->reg.ddr;
+        break;
     case GPIO_PORT:
-        return s->reg.port;
+        val = s->reg.port;
+        break;
     default:
         g_assert_not_reached();
         break;
     }
-    return 0;
+
+    trace_avr_gpio_read(offset, val);
+    return val;
 }
 
 static void avr_gpio_write(void *opaque, hwaddr offset, uint64_t value,
hw/gpio/trace-events +6 -0
index 46ab9323bd..a054def07c 100644
@@ -18,3 +18,9 @@ sifive_gpio_read(uint64_t offset, uint64_t r) "offset 0x%" PRIx64 " value 0x%" P
 sifive_gpio_write(uint64_t offset, uint64_t value) "offset 0x%" PRIx64 " value 0x%" PRIx64
 sifive_gpio_set(int64_t line, int64_t value) "line %" PRIi64 " value %" PRIi64
 sifive_gpio_update_output_irq(int64_t line, int64_t value) "line %" PRIi64 " value %" PRIi64
+
+# avr_gpio.c
+avr_gpio_read(uint64_t offset, uint64_t r) "offset 0x%" PRIx64 " value 0x%" PRIx64
+avr_gpio_write(uint64_t offset, uint64_t value) "offset 0x%" PRIx64 " value 0x%" PRIx64
+avr_gpio_set(int64_t line, int64_t value) "line %" PRIi64 " value %" PRIi64
+avr_gpio_update_output_irq(int64_t line, int64_t value) "pin %" PRIi64 " value %" PRIi64
Signature · 1 line
2.17.1
#3 lore raw

Re: [PATCH 2/3] hw/gpio/avr_gpio.c: add tracing for read and writes

Quoted 12 lines · G S Niteesh Babu

Added tracing for gpio read, write, and update output irq.

1) trace_avr_gpio_update_ouput_irq
2) trace_avr_gpio_read
3) trace_avr_gpio_write

---
 hw/gpio/avr_gpio.c   | 16 ++++++++++++----
 hw/gpio/trace-events |  6 ++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

hw/gpio/trace-events sifive_gpio_read(uint64_t offset, uint64_t r) "offset 0x%" PRIx64 " value 0x%" P · line 25

Quoted 5 lines
hw/gpio/trace-events +5 -0
+
+# avr_gpio.c
+avr_gpio_read(uint64_t offset, uint64_t r) "offset 0x%" PRIx64 " value 0x%" PRIx64
+avr_gpio_write(uint64_t offset, uint64_t value) "offset 0x%" PRIx64 " value 0x%" PRIx64
+avr_gpio_set(int64_t line, int64_t value) "line %" PRIi64 " value %" PRIi64

^ unused?

Quoted 2 lines

+avr_gpio_update_output_irq(int64_t line, int64_t value) "pin %" PRIi64 " value %" PRIi64

#4 lore raw

PATCH 3/3 avr/arduino: Add D13 LED

In reply to G S Niteesh Babu
---
 hw/avr/Kconfig   |  1 +
 hw/avr/arduino.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+)
hw/avr/Kconfig +1 -0
index 16a57ced11..e0d4fc5537 100644
@@ -8,3 +8,4 @@ config AVR_ATMEGA_MCU
 config ARDUINO
     select AVR_ATMEGA_MCU
     select UNIMP
+    select LED
hw/avr/arduino.c +15 -0
index 3c8388490d..5cdba3201c 100644
@@ -13,6 +13,7 @@
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "hw/boards.h"
+#include "hw/misc/led.h"
 #include "atmega.h"
 #include "boot.h"
 #include "qom/object.h"
@@ -22,6 +23,8 @@ struct ArduinoMachineState {
     MachineState parent_obj;
     /*< public >*/
     AtmegaMcuState mcu;
+
+    LEDState *onboard_led;
 };
 typedef struct ArduinoMachineState ArduinoMachineState;
 
@@ -49,6 +52,18 @@ static void arduino_machine_init(MachineState *machine)
                              amc->xtal_hz, &error_abort);
     sysbus_realize(SYS_BUS_DEVICE(&ams->mcu), &error_abort);
 
+    ams->onboard_led = led_create_simple(OBJECT(ams),
+                                         GPIO_POLARITY_ACTIVE_HIGH,
+                                         LED_COLOR_BLUE,
+                                         "D13 LED");
+
+    /* TODO: Add macro or function to map pins to ports */
+    /* The onboard led is connected to PIN 13 in all boards currently supported
+     * in QEMU. And PIN 13 is mapped to PORT B BIT 5.
+     */
+    qdev_connect_gpio_out(DEVICE(&ams->mcu.gpio[1]), 5,
+                          qdev_get_gpio_in(DEVICE(ams->onboard_led), 0));
+
     if (machine->firmware) {
         if (!avr_load_firmware(&ams->mcu.cpu, machine,
                                &ams->mcu.flash, machine->firmware)) {
Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH 3/3] avr/arduino: Add D13 LED

Quoted 37 lines · G S Niteesh Babu
---
 hw/avr/Kconfig   |  1 +
 hw/avr/arduino.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+)
hw/avr/Kconfig +1 -0
index 16a57ced11..e0d4fc5537 100644
@@ -8,3 +8,4 @@ config AVR_ATMEGA_MCU
 config ARDUINO
     select AVR_ATMEGA_MCU
     select UNIMP
+    select LED
hw/avr/arduino.c +3 -0
index 3c8388490d..5cdba3201c 100644
@@ -13,6 +13,7 @@
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "hw/boards.h"
+#include "hw/misc/led.h"
 #include "atmega.h"
 #include "boot.h"
 #include "qom/object.h"
@@ -22,6 +23,8 @@ struct ArduinoMachineState {
     MachineState parent_obj;
     /*< public >*/
     AtmegaMcuState mcu;
+
+    LEDState *onboard_led;
 };
 typedef struct ArduinoMachineState ArduinoMachineState;
 

hw/avr/arduino.c static void arduino_machine_init(MachineState *machine) · line 61

Quoted 10 lines
hw/avr/arduino.c +7 -0
@@ -49,6 +52,18 @@ static void arduino_machine_init(MachineState *machine)
                              amc->xtal_hz, &error_abort);
     sysbus_realize(SYS_BUS_DEVICE(&ams->mcu), &error_abort);
 
+    ams->onboard_led = led_create_simple(OBJECT(ams),
+                                         GPIO_POLARITY_ACTIVE_HIGH,
+                                         LED_COLOR_BLUE,
+                                         "D13 LED");
+
+    /* TODO: Add macro or function to map pins to ports */
+    /* The onboard led is connected to PIN 13 in all boards currently supported

Yes,

hw/avr/arduino.c static void arduino_machine_init(MachineState *machine) · line 62

Quoted 1 line
hw/avr/arduino.c +1 -0
+     * in QEMU. And PIN 13 is mapped to PORT B BIT 5.

No. Only on the Duemilanove / UNO. On both Mega it is bit 7.

Quoted 8 lines
hw/avr/arduino.c +4 -0
+     */
+    qdev_connect_gpio_out(DEVICE(&ams->mcu.gpio[1]), 5,
+                          qdev_get_gpio_in(DEVICE(ams->onboard_led), 0));
+
     if (machine->firmware) {
         if (!avr_load_firmware(&ams->mcu.cpu, machine,
                                &ams->mcu.flash, machine->firmware)) {
Reviewed-by on the v1 series
#6 lore raw
In reply to G S Niteesh Babu
Quoted 32 lines · G S Niteesh Babu

Hello,

The following series of the patches add a basic AVR GPIO emulation to QEMU. The AVR GPIO emulation patch was originally written by Heecheol Yang and was posted on the mailing list around 5 months ago. I am re-publishing the patch with some bug fixes along with my own patch, Adding the D13 onboard LED.

G S Niteesh Babu (2):
hw/gpio/avr_gpio.c: add tracing for read and writes avr/arduino: Add D13 LED

Heecheol Yang (1):
hw/avr: Add limited support for avr gpio registers

 hw/avr/Kconfig             |   2 +
 hw/avr/arduino.c           |  15 ++++
 hw/avr/atmega.c            |   7 +-
 hw/avr/atmega.h            |   2 +
 hw/gpio/Kconfig            |   3 +
 hw/gpio/avr_gpio.c         | 148 +++++++++++++++++++++++++++++++++++++
 hw/gpio/meson.build        |   1 +
 hw/gpio/trace-events       |   6 ++
 include/hw/gpio/avr_gpio.h |  53 +++++++++++++
 9 files changed, 235 insertions(+), 2 deletions(-)
 create mode 100644 hw/gpio/avr_gpio.c
 create mode 100644 include/hw/gpio/avr_gpio.h

--
2.17.1

Signature · 2 lines
Best Regards,
Michael Rolnik