Skip to contentSkip to conversation

arm: Add nRF51 SoC UART support

PATCH Superseded Julia Suvorova 5 participants 16 messages Aug 8, 2018 – Aug 16, 2018

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover arm: Add nRF51 SoC UART support

This series adds support for the nRF51 SoC UART, that used in BBC Micro:bit board, and QTest for it.

v2:
* Suspend/Enable functionality added
* Connection to SoC moved to a separate patch
* Added QTest for checking reception functionality
* Mini-kernel test changed to fit current implementation
* Addressed review comments on R_*, uart_can_receive, VMState, uart_transmit

Julia Suvorova (4):
hw/char: Implement nRF51 SoC UART hw/arm/nrf51_soc: Connect UART to nRF51 SoC tests/boot-serial-test: Add microbit board testcase tests/microbit-test: Check nRF51 UART functionality

 hw/arm/nrf51_soc.c           |  20 +++
 hw/char/Makefile.objs        |   1 +
 hw/char/nrf51_uart.c         | 329 +++++++++++++++++++++++++++++++++++
 hw/char/trace-events         |   4 +
 include/hw/arm/nrf51_soc.h   |   3 +
 include/hw/char/nrf51_uart.h |  78 +++++++++
 tests/boot-serial-test.c     |  19 ++
 tests/microbit-test.c        | 106 ++++++++++-
 8 files changed, 557 insertions(+), 3 deletions(-)
 create mode 100644 hw/char/nrf51_uart.c
 create mode 100644 include/hw/char/nrf51_uart.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/4 hw/char: Implement nRF51 SoC UART

Not implemented: CTS/NCTS, PSEL*.

---
 hw/char/Makefile.objs        |   1 +
 hw/char/nrf51_uart.c         | 329 +++++++++++++++++++++++++++++++++++
 hw/char/trace-events         |   4 +
 include/hw/char/nrf51_uart.h |  78 +++++++++
 4 files changed, 412 insertions(+)
 create mode 100644 hw/char/nrf51_uart.c
 create mode 100644 include/hw/char/nrf51_uart.h
hw/char/Makefile.objs +1 -0
index b570531291..c4947d7ae7 100644
@@ -1,5 +1,6 @@
 common-obj-$(CONFIG_IPACK) += ipoctal232.o
 common-obj-$(CONFIG_ESCC) += escc.o
+common-obj-$(CONFIG_NRF51_SOC) += nrf51_uart.o
 common-obj-$(CONFIG_PARALLEL) += parallel.o
 common-obj-$(CONFIG_PARALLEL) += parallel-isa.o
 common-obj-$(CONFIG_PL011) += pl011.o
hw/char/nrf51_uart.c new file +329 -0

329 lines not on this page · raw message

hw/char/trace-events +4 -0
index b64213d4dd..de34a74399 100644
@@ -73,3 +73,7 @@ cmsdk_apb_uart_receive(uint8_t c) "CMSDK APB UART: got character 0x%x from backe
 cmsdk_apb_uart_tx_pending(void) "CMSDK APB UART: character send to backend pending"
 cmsdk_apb_uart_tx(uint8_t c) "CMSDK APB UART: character 0x%x sent to backend"
 cmsdk_apb_uart_set_params(int speed) "CMSDK APB UART: params set to %d 8N1"
+
+# hw/char/nrf51_uart.c
+nrf51_uart_read(uint64_t addr, uint64_t r, unsigned int size) "addr 0x%" PRIx64 " value 0x%" PRIx64 " size %u"
+nrf51_uart_write(uint64_t addr, uint64_t value, unsigned int size) "addr 0x%" PRIx64 " value 0x%" PRIx64 " size %u"
include/hw/char/nrf51_uart.h new file +78 -0

78 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] hw/char: Implement nRF51 SoC UART

Quoted 4 lines · Julia Suvorova
hw/char/nrf51_uart.c new file +4 -0
+static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)
+{
+    NRF51UARTState *s = NRF51_UART(opaque);
+    uint64_t r;

hw/char/nrf51_uart.c line 49

Quoted 10 lines
hw/char/nrf51_uart.c new file +10 -0
+
+    if (!s->enabled) {
+        return 0;
+    }
+
+    switch (addr) {
+    case A_UART_RXD:
+        r = s->rx_fifo[s->rx_fifo_pos];
+        if (s->rx_started && s->rx_fifo_len) {
+            qemu_chr_fe_accept_input(&s->chr);

Should this be called after popping a byte from the rx fifo? That way .can_receive() will return true again.

hw/char/nrf51_uart.c line 260

Quoted 7 lines
hw/char/nrf51_uart.c new file +7 -0
+static void nrf51_uart_realize(DeviceState *dev, Error **errp)
+{
+    NRF51UARTState *s = NRF51_UART(dev);
+
+    qemu_chr_fe_set_handlers(&s->chr, uart_can_receive, uart_receive,
+                             uart_event, NULL, s, NULL, true);
+}

unrealize() should set the handlers to NULL. That way the device can be removed without leaving callbacks registered.

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] hw/char: Implement nRF51 SoC UART

Quoted 10 lines · Stefan Hajnoczi
Quoted 9 lines · Julia Suvorova
hw/char/nrf51_uart.c new file +9 -0
+static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)
+{
+    NRF51UARTState *s = NRF51_UART(opaque);
+    uint64_t r;
+
+    if (!s->enabled) {
+        return 0;
+    }
+
Quoted 8 lines

hw/char/nrf51_uart.c line 49

Quoted 5 lines
hw/char/nrf51_uart.c new file +5 -0
+    switch (addr) {
+    case A_UART_RXD:
+        r = s->rx_fifo[s->rx_fifo_pos];
+        if (s->rx_started && s->rx_fifo_len) {
+            qemu_chr_fe_accept_input(&s->chr);

Should this be called after popping a byte from the rx fifo? That way .can_receive() will return true again.

Could you explain more, please?

Quoted 10 lines

hw/char/nrf51_uart.c line 260

Quoted 7 lines
hw/char/nrf51_uart.c new file +7 -0
+static void nrf51_uart_realize(DeviceState *dev, Error **errp)
+{
+    NRF51UARTState *s = NRF51_UART(dev);
+
+    qemu_chr_fe_set_handlers(&s->chr, uart_can_receive, uart_receive,
+                             uart_event, NULL, s, NULL, true);
+}

unrealize() should set the handlers to NULL. That way the device can be removed without leaving callbacks registered.

I don't know the reason, but almost all char devices do not implement this function. Maybe, because when you quit qemu, qemu_chr_cleanup() is called.

Best regards, Julia Suvorova.

#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] hw/char: Implement nRF51 SoC UART

Quoted 20 lines · Julia Suvorova
Quoted 10 lines · Stefan Hajnoczi
Quoted 9 lines · Julia Suvorova
hw/char/nrf51_uart.c new file +9 -0
+static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)
+{
+    NRF51UARTState *s = NRF51_UART(opaque);
+    uint64_t r;
+
+    if (!s->enabled) {
+        return 0;
+    }
+
Quoted 8 lines

hw/char/nrf51_uart.c line 49

Quoted 5 lines
hw/char/nrf51_uart.c new file +5 -0
+    switch (addr) {
+    case A_UART_RXD:
+        r = s->rx_fifo[s->rx_fifo_pos];
+        if (s->rx_started && s->rx_fifo_len) {
+            qemu_chr_fe_accept_input(&s->chr);

Should this be called after popping a byte from the rx fifo? That way .can_receive() will return true again.

Quoted 1 line

Could you explain more, please?

This calls into the chardev's ->chr_accept_input() function. That function may do anything it wants.

At this point we haven't popped a byte from our rx fifo yet, so if ->chr_accept_input() calls back into the chardev frontend (us!) it sees that we cannot receive. That's strange since we just told the backend we want to accept input!

I haven't checked if there is any code path where this can happen, but it's safer to first update internal state before letting the outside world know that we can accept more input.

Quoted 11 lines
Quoted 10 lines

hw/char/nrf51_uart.c line 260

Quoted 7 lines
hw/char/nrf51_uart.c new file +7 -0
+static void nrf51_uart_realize(DeviceState *dev, Error **errp)
+{
+    NRF51UARTState *s = NRF51_UART(dev);
+
+    qemu_chr_fe_set_handlers(&s->chr, uart_can_receive, uart_receive,
+                             uart_event, NULL, s, NULL, true);
+}

unrealize() should set the handlers to NULL. That way the device can be removed without leaving callbacks registered.

Quoted 2 lines

I don't know the reason, but almost all char devices do not implement this function. Maybe, because when you quit qemu, qemu_chr_cleanup() is called.

It's an assumption that on-board devices cannot be hot unplugged and that the machine type stays alive until QEMU terminates.

Making this assumption saves 1 call to qemu_chr_fe_set_handlers(). The cost is that we cannot safely stop the system-on-chip because its devices don't clean up properly.

Since cleanup is so trivial here I think it's worthwhile.

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] hw/char: Implement nRF51 SoC UART

Quoted 26 lines · Stefan Hajnoczi
Quoted 20 lines · Julia Suvorova
Quoted 10 lines · Stefan Hajnoczi
Quoted 9 lines · Julia Suvorova
hw/char/nrf51_uart.c new file +9 -0
+static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)
+{
+    NRF51UARTState *s = NRF51_UART(opaque);
+    uint64_t r;
+
+    if (!s->enabled) {
+        return 0;
+    }
+
Quoted 8 lines

hw/char/nrf51_uart.c line 49

Quoted 5 lines
hw/char/nrf51_uart.c new file +5 -0
+    switch (addr) {
+    case A_UART_RXD:
+        r = s->rx_fifo[s->rx_fifo_pos];
+        if (s->rx_started && s->rx_fifo_len) {
+            qemu_chr_fe_accept_input(&s->chr);

Should this be called after popping a byte from the rx fifo? That way .can_receive() will return true again.

Quoted 1 line

Could you explain more, please?

This calls into the chardev's ->chr_accept_input() function. That function may do anything it wants.

Quoted 8 lines

At this point we haven't popped a byte from our rx fifo yet, so if ->chr_accept_input() calls back into the chardev frontend (us!) it sees that we cannot receive. That's strange since we just told the backend we want to accept input!

I haven't checked if there is any code path where this can happen, but it's safer to first update internal state before letting the outside world know that we can accept more input.

Thanks, I got it. I'll change the order.

Quoted 14 lines
Quoted 11 lines
Quoted 10 lines

hw/char/nrf51_uart.c line 260

Quoted 7 lines
hw/char/nrf51_uart.c new file +7 -0
+static void nrf51_uart_realize(DeviceState *dev, Error **errp)
+{
+    NRF51UARTState *s = NRF51_UART(dev);
+
+    qemu_chr_fe_set_handlers(&s->chr, uart_can_receive, uart_receive,
+                             uart_event, NULL, s, NULL, true);
+}

unrealize() should set the handlers to NULL. That way the device can be removed without leaving callbacks registered.

Quoted 2 lines

I don't know the reason, but almost all char devices do not implement this function. Maybe, because when you quit qemu, qemu_chr_cleanup() is called.

Quoted 8 lines

It's an assumption that on-board devices cannot be hot unplugged and that the machine type stays alive until QEMU terminates.

Making this assumption saves 1 call to qemu_chr_fe_set_handlers(). The cost is that we cannot safely stop the system-on-chip because its devices don't clean up properly.

Since cleanup is so trivial here I think it's worthwhile.

Ok, I'll implement unrealize with a qemu_chr_fe_deinit() in it.

Best regards, Julia Suvorova.

#6 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] hw/char: Implement nRF51 SoC UART

In reply to Stefan Hajnoczi
Quoted 8 lines · Stefan Hajnoczi

It's an assumption that on-board devices cannot be hot unplugged and that the machine type stays alive until QEMU terminates.

Making this assumption saves 1 call to qemu_chr_fe_set_handlers(). The cost is that we cannot safely stop the system-on-chip because its devices don't clean up properly.

Since cleanup is so trivial here I think it's worthwhile.

I would be more in favour of adding an unrealize function to random always-present-never-unpluggable devices if we had better documentation of exactly what the QOM lifecycle is and what needs to be done in init/realize/unrealize/etc. As it is, I don't know myself and therefore can't review whether devices with unrealize methods get it right. And obviously the code is completely untestable because it can never run...

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] hw/char: Implement nRF51 SoC UART

Quoted 9 lines · Peter Maydell
Quoted 8 lines · Stefan Hajnoczi

It's an assumption that on-board devices cannot be hot unplugged and that the machine type stays alive until QEMU terminates.

Making this assumption saves 1 call to qemu_chr_fe_set_handlers(). The cost is that we cannot safely stop the system-on-chip because its devices don't clean up properly.

Since cleanup is so trivial here I think it's worthwhile.

Quoted 8 lines

I would be more in favour of adding an unrealize function to random always-present-never-unpluggable devices if we had better documentation of exactly what the QOM lifecycle is and what needs to be done in init/realize/unrealize/etc. As it is, I don't know myself and therefore can't review whether devices with unrealize methods get it right. And obviously the code is completely untestable because it can never run...

I agree. Don't bother with unrealize unless the device hot-unpluggable.
What matters is that init/finalize are done right but that is tested by
device-introspection-test.

Paolo

#8 lore raw

PATCH v2 2/4 hw/arm/nrf51_soc: Connect UART to nRF51 SoC

In reply to Julia Suvorova

Wire up nRF51 UART in the corresponding SoC using in-place init/realize.

Based-on: <[email protected]>

---
 hw/arm/nrf51_soc.c         | 20 ++++++++++++++++++++
 include/hw/arm/nrf51_soc.h |  3 +++
 2 files changed, 23 insertions(+)
hw/arm/nrf51_soc.c +20 -0
index 9f9649c780..8b5602f363 100644
@@ -38,9 +38,12 @@
 #define NRF51822_FLASH_SIZE     (256 * 1024)
 #define NRF51822_SRAM_SIZE      (16 * 1024)
 
+#define BASE_TO_IRQ(base) ((base >> 12) & 0x1F)
+
 static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
 {
     NRF51State *s = NRF51_SOC(dev_soc);
+    MemoryRegion *mr = NULL;
     Error *err = NULL;
 
     if (!s->board_memory) {
@@ -70,6 +73,19 @@ static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
     }
     memory_region_add_subregion(&s->container, SRAM_BASE, &s->sram);
 
+    /* UART */
+    qdev_prop_set_chr(DEVICE(&s->uart), "chardev", serial_hd(0));
+    object_property_set_bool(OBJECT(&s->uart), true, "realized", &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->uart), 0);
+    memory_region_add_subregion_overlap(&s->container, UART_BASE, mr, 0);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart), 0,
+                       qdev_get_gpio_in(DEVICE(&s->cpu),
+                                        BASE_TO_IRQ(UART_BASE)));
+
     create_unimplemented_device("nrf51_soc.io", IOMEM_BASE, IOMEM_SIZE);
     create_unimplemented_device("nrf51_soc.ficr", FICR_BASE, FICR_SIZE);
     create_unimplemented_device("nrf51_soc.private", 0xF0000000, 0x10000000);
@@ -86,6 +102,10 @@ static void nrf51_soc_init(Object *obj)
     qdev_set_parent_bus(DEVICE(&s->cpu), sysbus_get_default());
     qdev_prop_set_string(DEVICE(&s->cpu), "cpu-type", ARM_CPU_TYPE_NAME("cortex-m0"));
     qdev_prop_set_uint32(DEVICE(&s->cpu), "num-irq", 32);
+
+    object_initialize(&s->uart, sizeof(s->uart), TYPE_NRF51_UART);
+    object_property_add_child(obj, "uart", OBJECT(&s->uart), &error_abort);
+    qdev_set_parent_bus(DEVICE(&s->uart), sysbus_get_default());
 }
 
 static Property nrf51_soc_properties[] = {
include/hw/arm/nrf51_soc.h +3 -0
index e380ec26b8..46a1c1a66c 100644
@@ -13,6 +13,7 @@
 #include "qemu/osdep.h"
 #include "hw/sysbus.h"
 #include "hw/arm/armv7m.h"
+#include "hw/char/nrf51_uart.h"
 
 #define TYPE_NRF51_SOC "nrf51-soc"
 #define NRF51_SOC(obj) \
@@ -25,6 +26,8 @@ typedef struct NRF51State {
     /*< public >*/
     ARMv7MState cpu;
 
+    NRF51UARTState uart;
+
     MemoryRegion iomem;
     MemoryRegion sram;
     MemoryRegion flash;
Signature · 1 line
2.17.1
#9 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] hw/arm/nrf51_soc: Connect UART to nRF51 SoC

hw/arm/nrf51_soc.c static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp) · line 81

Quoted 10 lines · Julia Suvorova
hw/arm/nrf51_soc.c +6 -0
@@ -70,6 +73,19 @@ static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
     }
     memory_region_add_subregion(&s->container, SRAM_BASE, &s->sram);
 
+    /* UART */
+    qdev_prop_set_chr(DEVICE(&s->uart), "chardev", serial_hd(0));
+    object_property_set_bool(OBJECT(&s->uart), true, "realized", &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;

Is there cleanup missing (e.g. unrealizing sub-devices or freeing resources)?

#10 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] hw/arm/nrf51_soc: Connect UART to nRF51 SoC

In reply to Julia Suvorova
Quoted 15 lines · Julia Suvorova

Wire up nRF51 UART in the corresponding SoC using in-place init/realize.

Based-on: <[email protected]>

---
 hw/arm/nrf51_soc.c         | 20 ++++++++++++++++++++
 include/hw/arm/nrf51_soc.h |  3 +++
 2 files changed, 23 insertions(+)
hw/arm/nrf51_soc.c +0 -0
index 9f9649c780..8b5602f363 100644

hw/arm/nrf51_soc.c line 46

Quoted 9 lines
hw/arm/nrf51_soc.c +3 -0
@@ -38,9 +38,12 @@
 #define NRF51822_FLASH_SIZE     (256 * 1024)
 #define NRF51822_SRAM_SIZE      (16 * 1024)
 
+#define BASE_TO_IRQ(base) ((base >> 12) & 0x1F)
+
 static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
 {
     NRF51State *s = NRF51_SOC(dev_soc);
+    MemoryRegion *mr = NULL;

This variable is unconditionally assigned before use later, so you don't need to initialize it here. (Some compilers and analysis tools will complain that the value assigned here is never used.)

hw/arm/nrf51_soc.c static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp) · line 77

Quoted 9 lines
hw/arm/nrf51_soc.c +2 -0
     Error *err = NULL;
 
     if (!s->board_memory) {
@@ -70,6 +73,19 @@ static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
     }
     memory_region_add_subregion(&s->container, SRAM_BASE, &s->sram);
 
+    /* UART */
+    qdev_prop_set_chr(DEVICE(&s->uart), "chardev", serial_hd(0));

serial_hd() ideally is something that should only be used by board code. It's better to have the SoC object define a chardev property which the board then connects up to the serial_hd() chardev.

You can do this by calling
object_property_add_alias(obj, "serial0", OBJECT(&s->uart), "chardev",

                             &error_abort);

in the SoC's init function. That will create a property "serial0" on the SoC object that just passes through to the "chardev" property on the UART. Then in the board code you can set the SoC serial0 property to serial_hd(0).

Quoted 15 lines
hw/arm/nrf51_soc.c +11 -0
+    object_property_set_bool(OBJECT(&s->uart), true, "realized", &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->uart), 0);
+    memory_region_add_subregion_overlap(&s->container, UART_BASE, mr, 0);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart), 0,
+                       qdev_get_gpio_in(DEVICE(&s->cpu),
+                                        BASE_TO_IRQ(UART_BASE)));
+
     create_unimplemented_device("nrf51_soc.io", IOMEM_BASE, IOMEM_SIZE);
     create_unimplemented_device("nrf51_soc.ficr", FICR_BASE, FICR_SIZE);
     create_unimplemented_device("nrf51_soc.private", 0xF0000000, 0x10000000);

hw/arm/nrf51_soc.c static void nrf51_soc_init(Object *obj) · line 108

Quoted 7 lines
hw/arm/nrf51_soc.c +4 -0
@@ -86,6 +102,10 @@ static void nrf51_soc_init(Object *obj)
     qdev_set_parent_bus(DEVICE(&s->cpu), sysbus_get_default());
     qdev_prop_set_string(DEVICE(&s->cpu), "cpu-type", ARM_CPU_TYPE_NAME("cortex-m0"));
     qdev_prop_set_uint32(DEVICE(&s->cpu), "num-irq", 32);
+
+    object_initialize(&s->uart, sizeof(s->uart), TYPE_NRF51_UART);
+    object_property_add_child(obj, "uart", OBJECT(&s->uart), &error_abort);
+    qdev_set_parent_bus(DEVICE(&s->uart), sysbus_get_default());

Instead of these three lines, use sysbus_init_child_obj() (which probably didn't exist when you wrote this code). This avoids a leak of a refcount on the object.

Quoted 25 lines
hw/arm/nrf51_soc.c +0 -0
 }
 
 static Property nrf51_soc_properties[] = {
include/hw/arm/nrf51_soc.h +3 -1
index e380ec26b8..46a1c1a66c 100644
@@ -13,6 +13,7 @@
 #include "qemu/osdep.h"
 #include "hw/sysbus.h"
 #include "hw/arm/armv7m.h"
+#include "hw/char/nrf51_uart.h"
 
 #define TYPE_NRF51_SOC "nrf51-soc"
 #define NRF51_SOC(obj) \
@@ -25,6 +26,8 @@ typedef struct NRF51State {
     /*< public >*/
     ARMv7MState cpu;
 
+    NRF51UARTState uart;
+
     MemoryRegion iomem;
     MemoryRegion sram;
     MemoryRegion flash;
--
Quoted 1 line

2.17.1

thanks
-- PMM

#11 lore raw

PATCH v2 3/4 tests/boot-serial-test: Add microbit board testcase

In reply to Julia Suvorova

New mini-kernel test for nRF51 SoC UART.

---
 tests/boot-serial-test.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tests/boot-serial-test.c +19 -0
index 952a2e7ead..19714c3f87 100644
@@ -62,6 +62,24 @@ static const uint8_t kernel_aarch64[] = {
     0xfd, 0xff, 0xff, 0x17,                 /* b       -12 (loop) */
 };
 
+static const uint8_t kernel_nrf51[] = {
+    0x00, 0x00, 0x00, 0x00,                 /* Stack top address */
+    0x09, 0x00, 0x00, 0x00,                 /* Reset handler address */
+    0x04, 0x4a,                             /* ldr  r2, [pc, #16] Get ENABLE */
+    0x04, 0x21,                             /* movs r1, #4 */
+    0x11, 0x60,                             /* str  r1, [r2] */
+    0x04, 0x4a,                             /* ldr  r2, [pc, #16] Get STARTTX */
+    0x01, 0x21,                             /* movs r1, #1 */
+    0x11, 0x60,                             /* str  r1, [r2] */
+    0x03, 0x4a,                             /* ldr  r2, [pc, #12] Get TXD */
+    0x54, 0x21,                             /* movs r1, 'T' */
+    0x11, 0x60,                             /* str  r1, [r2] */
+    0xfe, 0xe7,                             /* b    . */
+    0x00, 0x25, 0x00, 0x40,                 /* 0x40002500 = UART ENABLE */
+    0x08, 0x20, 0x00, 0x40,                 /* 0x40002008 = UART STARTTX */
+    0x1c, 0x25, 0x00, 0x40                  /* 0x4000251c = UART TXD */
+};
+
 typedef struct testdef {
     const char *arch;       /* Target architecture */
     const char *machine;    /* Name of the machine */
@@ -107,6 +125,7 @@ static testdef_t tests[] = {
     { "hppa", "hppa", "", "SeaBIOS wants SYSTEM HALT" },
     { "aarch64", "virt", "-cpu cortex-a57", "TT", sizeof(kernel_aarch64),
       kernel_aarch64 },
+    { "arm", "microbit", "", "T", sizeof(kernel_nrf51), kernel_nrf51 },
 
     { NULL }
 };
Signature · 1 line
2.17.1
Acked-by on v2 3/4
#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/4] tests/boot-serial-test: Add microbit board testcase

Quoted 38 lines · Julia Suvorova via Qemu-devel

New mini-kernel test for nRF51 SoC UART.

---
 tests/boot-serial-test.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tests/boot-serial-test.c +18 -0
index 952a2e7ead..19714c3f87 100644
@@ -62,6 +62,24 @@ static const uint8_t kernel_aarch64[] = {
     0xfd, 0xff, 0xff, 0x17,                 /* b       -12 (loop) */
 };
 
+static const uint8_t kernel_nrf51[] = {
+    0x00, 0x00, 0x00, 0x00,                 /* Stack top address */
+    0x09, 0x00, 0x00, 0x00,                 /* Reset handler address */
+    0x04, 0x4a,                             /* ldr  r2, [pc, #16] Get ENABLE */
+    0x04, 0x21,                             /* movs r1, #4 */
+    0x11, 0x60,                             /* str  r1, [r2] */
+    0x04, 0x4a,                             /* ldr  r2, [pc, #16] Get STARTTX */
+    0x01, 0x21,                             /* movs r1, #1 */
+    0x11, 0x60,                             /* str  r1, [r2] */
+    0x03, 0x4a,                             /* ldr  r2, [pc, #12] Get TXD */
+    0x54, 0x21,                             /* movs r1, 'T' */
+    0x11, 0x60,                             /* str  r1, [r2] */
+    0xfe, 0xe7,                             /* b    . */
+    0x00, 0x25, 0x00, 0x40,                 /* 0x40002500 = UART ENABLE */
+    0x08, 0x20, 0x00, 0x40,                 /* 0x40002008 = UART STARTTX */
+    0x1c, 0x25, 0x00, 0x40                  /* 0x4000251c = UART TXD */
+};
+
 typedef struct testdef {
     const char *arch;       /* Target architecture */
     const char *machine;    /* Name of the machine */

tests/boot-serial-test.c static testdef_t tests[] = { · line 131

Quoted 7 lines
tests/boot-serial-test.c +1 -0
@@ -107,6 +125,7 @@ static testdef_t tests[] = {
     { "hppa", "hppa", "", "SeaBIOS wants SYSTEM HALT" },
     { "aarch64", "virt", "-cpu cortex-a57", "TT", sizeof(kernel_aarch64),
       kernel_aarch64 },
+    { "arm", "microbit", "", "T", sizeof(kernel_nrf51), kernel_nrf51 },
 
     { NULL }
 };
#14 lore raw

PATCH v2 4/4 tests/microbit-test: Check nRF51 UART functionality

In reply to Julia Suvorova

Some functional tests for:
Basic reception/transmittion
Suspending
INTEN* registers

Based-on: <[email protected]>

---
 tests/microbit-test.c | 106 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 103 insertions(+), 3 deletions(-)
tests/microbit-test.c +103 -3
index 08e2210916..8b69d83684 100644
@@ -17,7 +17,10 @@
 #include "qemu/osdep.h"
 #include "exec/hwaddr.h"
 #include "libqtest.h"
+#include "hw/char/nrf51_uart.h"
 
+#include <sys/socket.h>
+#include <sys/un.h>
 
 #define PAGE_SIZE           1024
 #define FLASH_SIZE          (256 * PAGE_SIZE)
@@ -48,7 +51,6 @@
 #define GPIO_PULLDOWN 1
 #define GPIO_PULLUP 3
 
-
 static void fill_and_erase(hwaddr base, hwaddr size, uint32_t address_reg)
 {
     /* Fill memory */
@@ -204,19 +206,117 @@ static void test_nrf51_gpio(void)
     g_assert_false(true);
 }
 
+static bool wait_for_event(uint32_t event_addr)
+{
+    int i;
+
+    for (i = 0; i < 1000; i++) {
+        if (readl(event_addr) == 1) {
+            writel(event_addr, 0x00);
+            return true;
+        }
+        g_usleep(10000);
+    }
+
+    return false;
+}
+
+static void rw_to_rxd(int sock_fd, const char *in, char *out)
+{
+    int i;
+
+    g_assert(write(sock_fd, in, strlen(in)) == strlen(in));
+    for (i = 0; i < strlen(in); i++) {
+        g_assert(wait_for_event(UART_BASE + A_UART_RXDRDY));
+        out[i] = readl(UART_BASE + A_UART_RXD);
+    }
+    out[i] = '\0';
+}
+
+static void w_to_txd(const char *in)
+{
+    int i;
+
+    for (i = 0; i < strlen(in); i++) {
+        writel(UART_BASE + A_UART_TXD, in[i]);
+        g_assert(wait_for_event(UART_BASE + A_UART_TXDRDY));
+    }
+}
+
+static void test_nrf51_uart(const void *data)
+{
+    int sock_fd = *((const int *) data);
+    char s[10];
+
+    g_assert(write(sock_fd, "c", 1) == 1);
+    g_assert(readl(UART_BASE + A_UART_RXD) == 0);
+
+    writel(UART_BASE + A_UART_ENABLE, 0x04);
+    writel(UART_BASE + A_UART_STARTRX, 0x01);
+
+    g_assert(wait_for_event(UART_BASE + A_UART_RXDRDY));
+    writel(UART_BASE + A_UART_RXDRDY, 0x00);
+    g_assert(readl(UART_BASE + A_UART_RXD) == 'c');
+
+    writel(UART_BASE + A_UART_INTENSET, 0x04);
+    g_assert(readl(UART_BASE + A_UART_INTEN) == 0x04);
+    writel(UART_BASE + A_UART_INTENCLR, 0x04);
+    g_assert(readl(UART_BASE + A_UART_INTEN) == 0x00);
+
+    rw_to_rxd(sock_fd, "hello", s);
+    g_assert(strcmp(s, "hello") == 0);
+
+    writel(UART_BASE + A_UART_STARTTX, 0x01);
+    w_to_txd("d");
+    g_assert(read(sock_fd, s, 10) == 1);
+    g_assert(s[0] == 'd');
+
+    writel(UART_BASE + A_UART_SUSPEND, 0x01);
+    writel(UART_BASE + A_UART_TXD, 'h');
+    writel(UART_BASE + A_UART_STARTTX, 0x01);
+    w_to_txd("world");
+    g_assert(read(sock_fd, s, 10) == 5);
+    g_assert(strcmp(s, "world") == 0);
+}
+
 int main(int argc, char **argv)
 {
     int ret;
+    char serialtmpdir[] = "/tmp/qtest-microbit-serial-sXXXXXX";
+    char serialtmp[40];
+    int sock_fd;
+    struct sockaddr_un addr;
+
+    g_assert(mkdtemp(serialtmpdir));
+    sprintf(serialtmp, "%s/sock", serialtmpdir);
+
+    sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
+    g_assert(sock_fd != -1);
+
+    memset(&addr, 0, sizeof(struct sockaddr_un));
+
+    addr.sun_family = AF_UNIX;
+    strncpy(addr.sun_path, serialtmp, sizeof(addr.sun_path) - 1);
 
     g_test_init(&argc, &argv, NULL);
 
-    global_qtest = qtest_startf("-machine microbit");
+    global_qtest = qtest_startf("-machine microbit "
+                                "-chardev socket,id=s0,path=%s,server,nowait "
+                                "-no-shutdown -serial chardev:s0",
+                                serialtmp);
+
+    g_assert(connect(sock_fd, (const struct sockaddr *) &addr,
+                     sizeof(struct sockaddr_un)) != -1);
 
     qtest_add_func("/microbit/nrf51/nvmc", test_nrf51_nvmc);
     qtest_add_func("/microbit/nrf51/gpio", test_nrf51_gpio);
-
+    qtest_add_data_func("/microbit/nrf51/uart", &sock_fd, test_nrf51_uart);
     ret = g_test_run();
 
     qtest_quit(global_qtest);
+
+    close(sock_fd);
+    rmdir(serialtmpdir);
+
     return ret;
 }
Signature · 1 line
2.17.1
#15 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] tests/microbit-test: Check nRF51 UART functionality

tests/microbit-test.c static void test_nrf51_gpio(void) · line 319

Quoted 6 lines · Julia Suvorova
     ret = g_test_run();

     qtest_quit(global_qtest);
tests/microbit-test.c +3 -0
+
+    close(sock_fd);
+    rmdir(serialtmpdir);

This temporary directory is leaked if the test fails. Please remove it immediately after connect().