Skip to contentSkip to conversation

arm: nRF51 Devices and Microbit Support

PATCH In review Steffen Görtz 5 participants 25 messages Aug 6, 2018 – Aug 21, 2018 Kbuild

lore
Share & tools
lore
#0 lore raw

cover arm: nRF51 Devices and Microbit Support

This series contains additional peripheral devices for the nRF51822 microcontroller. Furthermore it includes a device to demultiplex the row and column strobes used in embedded devices to drive 2D LED dot-matrices.

Included devices:
- Random Number Generator
- Non-volatile Memories
- General purpose I/O
- Timer

Microbit board-level Devices:
- LED Matrix

Instantiate of the devices is done in an upcoming patch series.

Based-on: [email protected]

Steffen Görtz (7):
hw/misc/nrf51_rng: Add NRF51 random number generator peripheral hw/nvram/nrf51_nvm: Add nRF51 non-volatile memories
tests: Add bbc:microbit / nRF51 test suite hw/gpio/nrf51_gpio: Add nRF51 GPIO peripheral tests/microbit-test: Add Tests for nRF51 GPIO hw/timer/nrf51_timer: Add nRF51 Timer peripheral hw/display/led_matrix: Add LED matrix display device

 Makefile.objs                   |   1 +
 hw/display/Makefile.objs        |   2 +
 hw/display/led_matrix.c         | 262 +++++++++++++++++++++
 hw/gpio/Makefile.objs           |   1 +
 hw/gpio/nrf51_gpio.c            | 305 +++++++++++++++++++++++++
 hw/gpio/trace-events            |   7 +
 hw/misc/Makefile.objs           |   1 +
 hw/misc/nrf51_rng.c             | 273 ++++++++++++++++++++++
 hw/nvram/Makefile.objs          |   1 +
 hw/nvram/nrf51_nvm.c            | 390 ++++++++++++++++++++++++++++++++
 hw/timer/Makefile.objs          |   1 +
 hw/timer/nrf51_timer.c          | 382 +++++++++++++++++++++++++++++++
 hw/timer/trace-events           |   5 +
 include/hw/display/led_matrix.h |  38 ++++
 include/hw/gpio/nrf51_gpio.h    |  57 +++++
 include/hw/misc/nrf51_rng.h     |  71 ++++++
 include/hw/nvram/nrf51_nvm.h    |  56 +++++
 include/hw/timer/nrf51_timer.h  |  63 ++++++
 tests/Makefile.include          |   2 +
 tests/microbit-test.c           | 222 ++++++++++++++++++
 20 files changed, 2140 insertions(+)
 create mode 100644 hw/display/led_matrix.c
 create mode 100644 hw/gpio/nrf51_gpio.c
 create mode 100644 hw/gpio/trace-events
 create mode 100644 hw/misc/nrf51_rng.c
 create mode 100644 hw/nvram/nrf51_nvm.c
 create mode 100644 hw/timer/nrf51_timer.c
 create mode 100644 include/hw/display/led_matrix.h
 create mode 100644 include/hw/gpio/nrf51_gpio.h
 create mode 100644 include/hw/misc/nrf51_rng.h
 create mode 100644 include/hw/nvram/nrf51_nvm.h
 create mode 100644 include/hw/timer/nrf51_timer.h
 create mode 100644 tests/microbit-test.c
Signature · 1 line
2.18.0
#1 lore raw

PATCH 1/7 hw/misc/nrf51_rng: Add NRF51 random number generator peripheral

Add a model of the NRF51 random number generator peripheral. This is a simple random generator that continuously generates new random values after startup.

Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf

---
 hw/misc/Makefile.objs       |   1 +
 hw/misc/nrf51_rng.c         | 273 ++++++++++++++++++++++++++++++++++++
 include/hw/misc/nrf51_rng.h |  71 ++++++++++
 3 files changed, 345 insertions(+)
 create mode 100644 hw/misc/nrf51_rng.c
 create mode 100644 include/hw/misc/nrf51_rng.h
hw/misc/Makefile.objs +1 -0
index 9350900845..fc3fe57e13 100644
@@ -72,3 +72,4 @@ obj-$(CONFIG_AUX) += auxbus.o
 obj-$(CONFIG_ASPEED_SOC) += aspeed_scu.o aspeed_sdmc.o
 obj-y += mmio_interface.o
 obj-$(CONFIG_MSF2) += msf2-sysreg.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_rng.o
hw/misc/nrf51_rng.c new file +273 -0

273 lines not on this page · raw message

include/hw/misc/nrf51_rng.h new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.18.0
Reviewed-by on v1 1/7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/7] hw/misc/nrf51_rng: Add NRF51 random number generator peripheral

hw/misc/nrf51_rng.c line 34

Quoted 2 lines · Steffen Görtz
hw/misc/nrf51_rng.c new file +2 -0
+#define NRF51_TRIGGER_TASK 0x01
+#define NRF51_EVENT_CLEAR  0x00

Please consider putting these generic constants into hw/arm/nrf51.h or a similar file that all nRF51 devices can include. That way code duplication is eliminated.

#3 lore raw

Re: [Qemu-devel] [PATCH 1/7] hw/misc/nrf51_rng: Add NRF51 random number generator peripheral

In reply to Steffen Görtz
Quoted 15 lines · Steffen Görtz

Add a model of the NRF51 random number generator peripheral. This is a simple random generator that continuously generates new random values after startup.

Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf

---
 hw/misc/Makefile.objs       |   1 +
 hw/misc/nrf51_rng.c         | 273 ++++++++++++++++++++++++++++++++++++
 include/hw/misc/nrf51_rng.h |  71 ++++++++++
 3 files changed, 345 insertions(+)
 create mode 100644 hw/misc/nrf51_rng.c
 create mode 100644 include/hw/misc/nrf51_rng.h
Quoted 1 line

+static void update_irq(Nrf51RNGState *s)

See remark on other patch about consistency of struct naming.

hw/misc/nrf51_rng.c line 39

Quoted 3 lines
hw/misc/nrf51_rng.c new file +3 -0
+{
+    bool irq = false;
+    irq |= s->interrupt_enabled && s->event_valrdy;

You don't need to set irq to false and then modify it; this is equivalent to
bool irq = s->interrupt_enabled && s->event_valrdy;

hw/misc/nrf51_rng.c line 223

Quoted 6 lines
hw/misc/nrf51_rng.c new file +5 -0
+    qemu_set_irq(s->irq, irq);
+}
 
+static void nrf51_rng_reset(DeviceState *dev)
+{
+    Nrf51RNGState *s = NRF51_RNG(dev);

This seems to be missing code to actually reset the fields ?

hw/misc/nrf51_rng.c line 109

Quoted 3 lines
hw/misc/nrf51_rng.c new file +3 -0
+
+    rng_update_timer(s);
+}

Otherwise the device looks good.

thanks
-- PMM

#4 lore raw

PATCH 2/7 hw/nvram/nrf51_nvm: Add nRF51 non-volatile memories

In reply to Steffen Görtz

The nRF51 contains three regions of non-volatile memory (NVM):
- CODE (R/W): contains code
- FICR (R): Factory information like code size, chip id etc.
- UICR (R/W): Changeable configuration data. Lock bits, Code protection configuration, Bootloader address, Nordic SoftRadio configuration, Firmware configuration.

Read and write access to the memories is managed by the Non-volatile memory controller.

Memory schema:
[ CPU ] -+- [ NVM, either FICR, UICR or CODE ]

          |      |
          \- [ NVMC ]
---
 hw/nvram/Makefile.objs       |   1 +
 hw/nvram/nrf51_nvm.c         | 390 +++++++++++++++++++++++++++++++++++
 include/hw/nvram/nrf51_nvm.h |  56 +++++
 3 files changed, 447 insertions(+)
 create mode 100644 hw/nvram/nrf51_nvm.c
 create mode 100644 include/hw/nvram/nrf51_nvm.h
hw/nvram/Makefile.objs +1 -0
index a912d25391..3f978e6212 100644
@@ -5,3 +5,4 @@ common-obj-y += fw_cfg.o
 common-obj-y += chrp_nvram.o
 common-obj-$(CONFIG_MAC_NVRAM) += mac_nvram.o
 obj-$(CONFIG_PSERIES) += spapr_nvram.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_nvm.o
hw/nvram/nrf51_nvm.c new file +390 -0

390 lines not on this page · raw message

include/hw/nvram/nrf51_nvm.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.18.0
#5 lore raw

Re: [Qemu-devel] [PATCH 2/7] hw/nvram/nrf51_nvm: Add nRF51 non-volatile memories

Quoted 11 lines · Steffen Görtz
hw/nvram/nrf51_nvm.c new file +11 -0
+static uint64_t uicr_read(void *opaque, hwaddr offset, unsigned int size)
+{
+    Nrf51NVMState *s = NRF51_NVM(opaque);
+
+    offset >>= 2;
+
+    return s->uicr_content[offset];
+}
+
+static void uicr_write(void *opaque, hwaddr offset, uint64_t value,
+        unsigned int size)

hw/nvram/nrf51_nvm.c line 213

Quoted 10 lines
hw/nvram/nrf51_nvm.c new file +10 -0
+{
+    Nrf51NVMState *s = NRF51_NVM(opaque);
+
+    offset >>= 2;
+
+    if (offset >= ARRAY_SIZE(s->uicr_content)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                "%s: bad read offset 0x%" HWADDR_PRIx "\n", __func__, offset);
+        return;
+    }

There is asymmetry here: uicr_read() doesn't check offset before indexing the array but uicr_write() does. Either the check is necessary or it's not.

I think this check isn't necessary since the memory region is sized appropriately:

memory_region_init_io(&s->uicr, NULL, &uicr_ops, s, "nrf51_soc.uicr",

          sizeof(s->uicr_content));

hw/nvram/nrf51_nvm.c line 343

Quoted 6 lines
hw/nvram/nrf51_nvm.c new file +6 -0
+static void nrf51_nvm_reset(DeviceState *dev)
+{
+    Nrf51NVMState *s = NRF51_NVM(dev);
+
+    memset(s->empty_page, 0xFF, s->page_size);
+}

Can this be done in ->realize()? Nothing changes the contents of empty_page, so a ->reset() function seems unnecessary.

#6 lore raw

Re: [Qemu-devel] [PATCH 2/7] hw/nvram/nrf51_nvm: Add nRF51 non-volatile memories

Hi Stefan,

thank you for your review!

Quoted 11 lines

There is asymmetry here: uicr_read() doesn't check offset before indexing the array but uicr_write() does. Either the check is necessary or it's not.

I think this check isn't necessary since the memory region is sized appropriately:

memory_region_init_io(&s->uicr, NULL, &uicr_ops, s, "nrf51_soc.uicr",

          sizeof(s->uicr_content));
Quoted 10 lines

hw/nvram/nrf51_nvm.c line 343

Quoted 6 lines
hw/nvram/nrf51_nvm.c new file +6 -0
+static void nrf51_nvm_reset(DeviceState *dev)
+{
+    Nrf51NVMState *s = NRF51_NVM(dev);
+
+    memset(s->empty_page, 0xFF, s->page_size);
+}

Can this be done in ->realize()? Nothing changes the contents of empty_page, so a ->reset() function seems unnecessary.

Addressed in next revision of this patch.

Steffen

#7 lore raw

Re: [Qemu-devel] [PATCH 2/7] hw/nvram/nrf51_nvm: Add nRF51 non-volatile memories

In reply to Steffen Görtz
Quoted 74 lines · Steffen Görtz

The nRF51 contains three regions of non-volatile memory (NVM):
- CODE (R/W): contains code
- FICR (R): Factory information like code size, chip id etc.
- UICR (R/W): Changeable configuration data. Lock bits, Code protection configuration, Bootloader address, Nordic SoftRadio configuration, Firmware configuration.

Read and write access to the memories is managed by the Non-volatile memory controller.

Memory schema:
[ CPU ] -+- [ NVM, either FICR, UICR or CODE ]

          |      |
          \- [ NVMC ]
---
 hw/nvram/Makefile.objs       |   1 +
 hw/nvram/nrf51_nvm.c         | 390 +++++++++++++++++++++++++++++++++++
 include/hw/nvram/nrf51_nvm.h |  56 +++++
 3 files changed, 447 insertions(+)
 create mode 100644 hw/nvram/nrf51_nvm.c
 create mode 100644 include/hw/nvram/nrf51_nvm.h
hw/nvram/Makefile.objs +1 -0
index a912d25391..3f978e6212 100644
@@ -5,3 +5,4 @@ common-obj-y += fw_cfg.o
 common-obj-y += chrp_nvram.o
 common-obj-$(CONFIG_MAC_NVRAM) += mac_nvram.o
 obj-$(CONFIG_PSERIES) += spapr_nvram.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_nvm.o
hw/nvram/nrf51_nvm.c new file +37 -0
new file mode 100644
index 0000000000..603dbd7ca2
@@ -0,0 +1,390 @@
+/*
+ * Nordic Semiconductor nRF51 non-volatile memory
+ *
+ * It provides an interface to erase regions in flash memory.
+ * Furthermore it provides the user and factory information registers.
+ *
+ * Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf
+ *
+ * See nRF51 reference manual and product sheet sections:
+ * + Non-Volatile Memory Controller (NVMC)
+ * + Factory Information Configuration Registers (FICR)
+ * + User Information Configuration Registers (UICR)
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu/log.h"
+#include "exec/address-spaces.h"
+#include "hw/nvram/nrf51_nvm.h"
+
+#define NRF51_NVMC_SIZE         0x1000
+
+#define NRF51_NVMC_READY        0x400
+#define NRF51_NVMC_READY_READY  0x01
+#define NRF51_NVMC_CONFIG       0x504
+#define NRF51_NVMC_CONFIG_MASK  0x03
+#define NRF51_NVMC_CONFIG_WEN   0x01
+#define NRF51_NVMC_CONFIG_EEN   0x02
+#define NRF51_NVMC_ERASEPCR1    0x508
+#define NRF51_NVMC_ERASEPCR0    0x510
+#define NRF51_NVMC_ERASEALL     0x50C
+#define NRF51_NVMC_ERASEUICR    0x514

hw/nvram/nrf51_nvm.c line 47

Quoted 10 lines
hw/nvram/nrf51_nvm.c new file +10 -0
+#define NRF51_NVMC_ERASE        0x01
+
+#define NRF51_FICR_SIZE         0x100
+
+#define NRF51_UICR_SIZE         0x100
+
+
+/* FICR Registers Assignments
+CODEPAGESIZE      0x010
+CODESIZE          0x014

This is confusing because these lines look like code because they don't have the leading " * ". Please keep multiline comments to the standard format that CODING_STYLE (now) suggests.

Quoted 43 lines
hw/nvram/nrf51_nvm.c new file +43 -0
+CLENR0            0x028
+PPFC              0x02C
+NUMRAMBLOCK       0x034
+SIZERAMBLOCKS     0x038
+SIZERAMBLOCK[0]   0x038
+SIZERAMBLOCK[1]   0x03C
+SIZERAMBLOCK[2]   0x040
+SIZERAMBLOCK[3]   0x044
+CONFIGID          0x05C
+DEVICEID[0]       0x060
+DEVICEID[1]       0x064
+ER[0]             0x080
+ER[1]             0x084
+ER[2]             0x088
+ER[3]             0x08C
+IR[0]             0x090
+IR[1]             0x094
+IR[2]             0x098
+IR[3]             0x09C
+DEVICEADDRTYPE    0x0A0
+DEVICEADDR[0]     0x0A4
+DEVICEADDR[1]     0x0A8
+OVERRIDEEN        0x0AC
+NRF_1MBIT[0]      0x0B0
+NRF_1MBIT[1]      0x0B4
+NRF_1MBIT[2]      0x0B8
+NRF_1MBIT[3]      0x0BC
+NRF_1MBIT[4]      0x0C0
+BLE_1MBIT[0]      0x0EC
+BLE_1MBIT[1]      0x0F0
+BLE_1MBIT[2]      0x0F4
+BLE_1MBIT[3]      0x0F8
+BLE_1MBIT[4]      0x0FC
+*/
+static const uint32_t ficr_content[64] = {
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000400,
+        0x00000100, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000002, 0x00002000,
+        0x00002000, 0x00002000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000003,
+        0x12345678, 0x9ABCDEF1, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,

hw/nvram/nrf51_nvm.c line 100

Quoted 10 lines
hw/nvram/nrf51_nvm.c new file +10 -0
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
+};
+
+static uint64_t ficr_read(void *opaque, hwaddr offset, unsigned int size)
+{
+    return ficr_content[offset >> 2];

We can't get here with a bogus offset because of the use of sizeof() when registering the memory region. I think that an assert() that offset is in range before doing the array dereference would be helpful though, as a defensive guard against the MR size being changed in future: it turns the consequences of that bug from "read out of bounds" to "assertion failure". Similarly with uicr.

Quoted 7 lines
hw/nvram/nrf51_nvm.c new file +6 -0
+}
 
+static const uint32_t uicr_fixture[NRF51_UICR_FIXTURE_SIZE] = {
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,

hw/nvram/nrf51_nvm.c line 191

Quoted 10 lines
hw/nvram/nrf51_nvm.c new file +10 -0
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
+        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
+};

Would this ever be anything other than "all 0xff" ? If not, might be more obvious just to use memset() to init/reset the data.

Quoted 40 lines
hw/nvram/nrf51_nvm.c new file +40 -0
+
+static uint64_t uicr_read(void *opaque, hwaddr offset, unsigned int size)
+{
+    Nrf51NVMState *s = NRF51_NVM(opaque);
+
+    offset >>= 2;
+
+    return s->uicr_content[offset];
+}
+
+static void uicr_write(void *opaque, hwaddr offset, uint64_t value,
+        unsigned int size)
+{
+    Nrf51NVMState *s = NRF51_NVM(opaque);
+
+    offset >>= 2;
+
+    if (offset >= ARRAY_SIZE(s->uicr_content)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                "%s: bad read offset 0x%" HWADDR_PRIx "\n", __func__, offset);
+        return;
+    }
+
+    s->uicr_content[offset] = value;
+}
+
+static const MemoryRegionOps uicr_ops = {
+    .read = uicr_read,
+    .write = uicr_write,
+    .impl.min_access_size = 4,
+    .impl.max_access_size = 4,
+    .impl.unaligned = false,
+};
+
+
+static uint64_t io_read(void *opaque, hwaddr offset, unsigned int size)
+{
+    Nrf51NVMState *s = NRF51_NVM(opaque);
+    uint64_t r = 0;
+

hw/nvram/nrf51_nvm.c line 241

Quoted 10 lines
hw/nvram/nrf51_nvm.c new file +10 -0
+    switch (offset) {
+    case NRF51_NVMC_READY:
+        r = NRF51_NVMC_READY_READY;
+        break;
+    case NRF51_NVMC_CONFIG:
+        r = s->config;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                "%s: bad read offset 0x%" HWADDR_PRIx "\n", __func__, offset);

Missing "break;" here (and at the end of the switches in the other functions in this file). The compiler won't complain (and the behaviour is correct) but Coverity likely will produce a warning later.

Quoted 51 lines
hw/nvram/nrf51_nvm.c new file +51 -0
+    }
+
+    return r;
+}
+
+static void io_write(void *opaque, hwaddr offset, uint64_t value,
+        unsigned int size)
+{
+    Nrf51NVMState *s = NRF51_NVM(opaque);
+
+    switch (offset) {
+    case NRF51_NVMC_CONFIG:
+        s->config = value & NRF51_NVMC_CONFIG_MASK;
+        break;
+    case NRF51_NVMC_ERASEPCR0:
+    case NRF51_NVMC_ERASEPCR1:
+        value &= ~(s->page_size - 1);
+        if (value < (s->code_size * s->page_size)) {
+            address_space_write(&s->as, value, MEMTXATTRS_UNSPECIFIED,
+                    s->empty_page, s->page_size);
+        }
+        break;
+    case NRF51_NVMC_ERASEALL:
+        if (value == NRF51_NVMC_ERASE) {
+            for (uint32_t i = 0; i < s->code_size; i++) {
+                address_space_write(&s->as, i * s->page_size,
+                MEMTXATTRS_UNSPECIFIED, s->empty_page, s->page_size);
+            }
+            memset(s->uicr_content, 0xFF, sizeof(s->uicr_content));
+        }
+        break;
+    case NRF51_NVMC_ERASEUICR:
+        if (value == NRF51_NVMC_ERASE) {
+            memset(s->uicr_content, 0xFF, sizeof(s->uicr_content));
+        }
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                "%s: bad write offset 0x%" HWADDR_PRIx "\n", __func__, offset);
+    }
+}
+
+static const MemoryRegionOps io_ops = {
+        .read = io_read,
+        .write = io_write,
+        .endianness = DEVICE_LITTLE_ENDIAN,
+};
+
+static void nrf51_nvm_init(Object *obj)
+{

hw/nvram/nrf51_nvm.c line 302

Quoted 10 lines
hw/nvram/nrf51_nvm.c new file +10 -0
+    Nrf51NVMState *s = NRF51_NVM(obj);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
+
+    memory_region_init_io(&s->mmio, obj, &io_ops, s, "nrf51_soc.nvmc",
+            NRF51_NVMC_SIZE);
+    sysbus_init_mmio(sbd, &s->mmio);
+
+    memory_region_init_io(&s->ficr, NULL, &ficr_ops, s, "nrf51_soc.ficr",
+            sizeof(ficr_content));
+    memory_region_set_readonly(&s->ficr, true);

memory_region_set_readonly() is only intended for use with RAM regions, which this is not. Usually if you want an IO region that has "ignores writes" behaviour you just give it a write function that does nothing.

hw/nvram/nrf51_nvm.c line 305

Quoted 3 lines
hw/nvram/nrf51_nvm.c new file +3 -0
+    sysbus_init_mmio(sbd, &s->ficr);
+
+    memcpy(s->uicr_content, uicr_fixture, sizeof(s->uicr_content));

The guest can change uicr_content[], so this memcpy() is a part of reset and should be in the reset function, so that it gets set back to its correct value on a reset.

Quoted 21 lines
hw/nvram/nrf51_nvm.c new file +21 -0
+    memory_region_init_io(&s->uicr, NULL, &uicr_ops, s, "nrf51_soc.uicr",
+            sizeof(s->uicr_content));
+    sysbus_init_mmio(sbd, &s->uicr);
+}
+
+static void nrf51_nvm_realize(DeviceState *dev, Error **errp)
+{
+    Nrf51NVMState *s = NRF51_NVM(dev);
+
+    if (!s->mr) {
+        error_setg(errp, "memory property was not set");
+        return;
+    }
+
+    if (s->page_size < NRF51_UICR_SIZE) {
+        error_setg(errp, "page size too small");
+        return;
+    }
+
+    s->empty_page = g_malloc(s->page_size);
+

hw/nvram/nrf51_nvm.c line 336

Quoted 10 lines
hw/nvram/nrf51_nvm.c new file +10 -0
+    address_space_init(&s->as, s->mr, "system-memory");
+}
+
+static void nrf51_nvm_unrealize(DeviceState *dev, Error **errp)
+{
+    Nrf51NVMState *s = NRF51_NVM(dev);
+
+    g_free(s->empty_page);
+    s->empty_page = NULL;
+}

This device can't be hot-unplugged, so an unrealize function is unreachable and not required.

hw/nvram/nrf51_nvm.c line 347

Quoted 11 lines
hw/nvram/nrf51_nvm.c new file +11 -0
+
+static void nrf51_nvm_reset(DeviceState *dev)
+{
+    Nrf51NVMState *s = NRF51_NVM(dev);
+
+    memset(s->empty_page, 0xFF, s->page_size);
+}
+
+
+static Property nrf51_nvm_properties[] = {
+    DEFINE_PROP_UINT16("page_size", Nrf51NVMState, page_size, 0x400),

Do the different NRF51 variants really have different NAND page sizes ?

hw/nvram/nrf51_nvm.c line 352

Quoted 5 lines
hw/nvram/nrf51_nvm.c new file +5 -0
+    DEFINE_PROP_UINT32("code_size", Nrf51NVMState, code_size, 0x100),
+    DEFINE_PROP_LINK("memory", Nrf51NVMState, mr, TYPE_MEMORY_REGION,
+                     MemoryRegion *),
+    DEFINE_PROP_END_OF_LIST(),
+};

thanks
-- PMM

#8 lore raw

Re: [Qemu-devel] [PATCH 2/7] hw/nvram/nrf51_nvm: Add nRF51 non-volatile memories

Hi Peter,

Quoted 6 lines

hw/nvram/nrf51_nvm.c line 347

Quoted 3 lines
hw/nvram/nrf51_nvm.c new file +3 -0
+
+static Property nrf51_nvm_properties[] = {
+    DEFINE_PROP_UINT16("page_size", Nrf51NVMState, page_size, 0x400),

Do the different NRF51 variants really have different NAND page sizes ?

it seems that is not the case at least for the NRF51 Series. I removed the property in favor for a nrf51.h-wide constant. I think this is NOR flash btw, because it is single-byte writable, which is usually a sign that it is NOR flash. But i could be wrong and the datasheet does not state anything. Thank you for your remarks!

Steffen

#9 lore raw

PATCH 3/7 tests: Add bbc:microbit / nRF51 test suite

In reply to Steffen Görtz

The microbit-test includes tests for the nRF51 NVMC peripheral and will host future nRF51 peripheral tests and board-level bbc:microbit tests.

---
 tests/Makefile.include |   2 +
 tests/microbit-test.c  | 126 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 128 insertions(+)
 create mode 100644 tests/microbit-test.c
tests/Makefile.include +2 -0
index 760a0f18b6..1f1206e33a 100644
@@ -378,6 +378,7 @@ check-qtest-sparc64-y += tests/boot-serial-test$(EXESUF)
 check-qtest-arm-y = tests/tmp105-test$(EXESUF)
 check-qtest-arm-y += tests/pca9552-test$(EXESUF)
 check-qtest-arm-y += tests/ds1338-test$(EXESUF)
+check-qtest-arm-y += tests/microbit-test$(EXESUF)
 check-qtest-arm-y += tests/m25p80-test$(EXESUF)
 gcov-files-arm-y += hw/misc/tmp105.c
 check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
@@ -793,6 +794,7 @@ tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y)
 tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
 tests/pca9552-test$(EXESUF): tests/pca9552-test.o $(libqos-omap-obj-y)
 tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y)
+tests/microbit-test$(EXESUF): tests/microbit-test.o
 tests/m25p80-test$(EXESUF): tests/m25p80-test.o
 tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
 tests/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y)
tests/microbit-test.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.18.0
#10 lore raw

Re: [Qemu-devel] [PATCH 3/7] tests: Add bbc:microbit / nRF51 test suite

tests/microbit-test.c line 34

Quoted 13 lines · Steffen Görtz
tests/microbit-test.c new file +13 -0
+#define PAGE_SIZE           1024
+#define FLASH_SIZE          (256 * PAGE_SIZE)
+#define FLASH_BASE          0x00000000
+#define UICR_BASE           0x10001000
+#define UICR_SIZE           0x100
+#define NVMC_BASE           0x4001E000UL
+#define NVMC_READY          0x400
+#define NVMC_CONFIG         0x504
+#define NVMC_ERASEPAGE      0x508
+#define NVMC_ERASEPCR1      0x508
+#define NVMC_ERASEALL       0x50C
+#define NVMC_ERASEPCR0      0x510
+#define NVMC_ERASEUICR      0x514

All these constants could live in include/hw/arm/nrf51.h. That way tests do not need to duplicate them. As more devices are implemented we can expect this list to grow.

Quoted 1 line

+static void fill_and_erase(hwaddr base, hwaddr size, uint32_t address_reg)

Why hwaddr? writel() and friends use uint64_t.

#11 lore raw

Re: [Qemu-devel] [PATCH 3/7] tests: Add bbc:microbit / nRF51 test suite

Quoted 11 lines · Stefan Hajnoczi
Quoted 9 lines · Steffen Görtz
tests/microbit-test.c new file +9 -0
+#define PAGE_SIZE           1024
+#define FLASH_SIZE          (256 * PAGE_SIZE)
+#define FLASH_BASE          0x00000000
+#define UICR_BASE           0x10001000
+#define UICR_SIZE           0x100
+#define NVMC_BASE           0x4001E000UL
+#define NVMC_READY          0x400
+#define NVMC_CONFIG         0x504
+#define NVMC_ERASEPAGE      0x508
Quoted 8 lines

tests/microbit-test.c line 34

Quoted 4 lines
tests/microbit-test.c new file +4 -0
+#define NVMC_ERASEPCR1      0x508
+#define NVMC_ERASEALL       0x50C
+#define NVMC_ERASEPCR0      0x510
+#define NVMC_ERASEUICR      0x514

All these constants could live in include/hw/arm/nrf51.h. That way tests do not need to duplicate them. As more devices are implemented we can expect this list to grow.

Maybe it's better to move them to device headers? include/hw/arm/nrf51.h has to include them in any case to use device types.

Best regards, Julia Suvorova.

#12 lore raw

Re: [Qemu-devel] [PATCH 3/7] tests: Add bbc:microbit / nRF51 test suite

Quoted 25 lines · Julia Suvorova
Quoted 14 lines · Stefan Hajnoczi
Quoted 11 lines · Steffen Görtz
tests/microbit-test.c new file +10 -0
+#define PAGE_SIZE           1024
+#define FLASH_SIZE          (256 * PAGE_SIZE)
+#define FLASH_BASE          0x00000000
+#define UICR_BASE           0x10001000
+#define UICR_SIZE           0x100
+#define NVMC_BASE           0x4001E000UL
+#define NVMC_READY          0x400
+#define NVMC_CONFIG         0x504
+#define NVMC_ERASEPAGE      0x508
+#define NVMC_ERASEPCR1      0x508
Quoted 8 lines

tests/microbit-test.c line 34

Quoted 3 lines
tests/microbit-test.c new file +3 -0
+#define NVMC_ERASEALL       0x50C
+#define NVMC_ERASEPCR0      0x510
+#define NVMC_ERASEUICR      0x514

All these constants could live in include/hw/arm/nrf51.h. That way tests do not need to duplicate them. As more devices are implemented we can expect this list to grow.

Quoted 3 lines

Maybe it's better to move them to device headers? include/hw/arm/nrf51.h has to include them in any case to use device types.

That would be fine. Each device (uart, rng, etc) would have it's own include/hw/... header file and test cases can include that file to get the constants/structs.

Stefan

#13 lore raw

PATCH 4/7 hw/gpio/nrf51_gpio: Add nRF51 GPIO peripheral

In reply to Steffen Görtz

This adds a model of the nRF51 GPIO peripheral.

Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf

The nRF51 series microcontrollers support up to 32 GPIO pins in various configurations. The pins can be used as input pins with pull-ups or pull-down. Furthermore, three different output driver modes per level are
available (disconnected, standard, high-current).

The GPIO-Peripheral has a mechanism for detecting level changes which is not featured in this model.

---
 Makefile.objs                |   1 +
 hw/gpio/Makefile.objs        |   1 +
 hw/gpio/nrf51_gpio.c         | 305 +++++++++++++++++++++++++++++++++++
 hw/gpio/trace-events         |   7 +
 include/hw/gpio/nrf51_gpio.h |  57 +++++++
 5 files changed, 371 insertions(+)
 create mode 100644 hw/gpio/nrf51_gpio.c
 create mode 100644 hw/gpio/trace-events
 create mode 100644 include/hw/gpio/nrf51_gpio.h
Makefile.objs +1 -0
index 7a9828da28..506ae4eae0 100644
@@ -241,6 +241,7 @@ trace-events-subdirs += hw/usb
 trace-events-subdirs += hw/vfio
 trace-events-subdirs += hw/virtio
 trace-events-subdirs += hw/xen
+trace-events-subdirs += hw/gpio
 trace-events-subdirs += io
 trace-events-subdirs += linux-user
 trace-events-subdirs += migration
hw/gpio/Makefile.objs +1 -0
index fa0a72e6d0..e5da0cb54f 100644
@@ -8,3 +8,4 @@ common-obj-$(CONFIG_GPIO_KEY) += gpio_key.o
 obj-$(CONFIG_OMAP) += omap_gpio.o
 obj-$(CONFIG_IMX) += imx_gpio.o
 obj-$(CONFIG_RASPI) += bcm2835_gpio.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_gpio.o
hw/gpio/nrf51_gpio.c new file +305 -0

305 lines not on this page · raw message

hw/gpio/trace-events new file +7 -0
new file mode 100644
index 0000000000..11486b09b9
@@ -0,0 +1,7 @@
+# See docs/devel/tracing.txt for syntax documentation.
+
+# hw/gpio/nrf51_gpio.c
+nrf51_gpio_read(uint64_t offset, uint64_t r) "offset 0x%" PRIx64 " value 0x%" PRIx64
+nrf51_gpio_write(uint64_t offset, uint64_t value) "offset 0x%" PRIx64 " value 0x%" PRIx64
+nrf51_gpio_set(int64_t line, int64_t value) "line %" PRIi64 " value 0x%" PRIi64
+nrf51_gpio_update_output_irq(int64_t line, int64_t value) "line %" PRIi64 " value 0x%" PRIi64
\ No newline at end of file
include/hw/gpio/nrf51_gpio.h new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.18.0
Reviewed-by on v1 4/7
#14 lore raw

Re: [Qemu-devel] [PATCH 4/7] hw/gpio/nrf51_gpio: Add nRF51 GPIO peripheral

Quoted 13 lines · Steffen Görtz

This adds a model of the nRF51 GPIO peripheral.

Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf

The nRF51 series microcontrollers support up to 32 GPIO pins in various configurations. The pins can be used as input pins with pull-ups or pull-down. Furthermore, three different output driver modes per level are
available (disconnected, standard, high-current).

The GPIO-Peripheral has a mechanism for detecting level changes which is not featured in this model.

Quoted 10 lines
---
 Makefile.objs                |   1 +
 hw/gpio/Makefile.objs        |   1 +
 hw/gpio/nrf51_gpio.c         | 305 +++++++++++++++++++++++++++++++++++
 hw/gpio/trace-events         |   7 +
 include/hw/gpio/nrf51_gpio.h |  57 +++++++
 5 files changed, 371 insertions(+)
 create mode 100644 hw/gpio/nrf51_gpio.c
 create mode 100644 hw/gpio/trace-events
 create mode 100644 include/hw/gpio/nrf51_gpio.h
#15 lore raw

Re: [Qemu-devel] [PATCH 4/7] hw/gpio/nrf51_gpio: Add nRF51 GPIO peripheral

In reply to Steffen Görtz
Quoted 9 lines · Steffen Görtz

This adds a model of the nRF51 GPIO peripheral.

Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf

The nRF51 series microcontrollers support up to 32 GPIO pins in various configurations. The pins can be used as input pins with pull-ups or pull-down. Furthermore, three different output driver modes per level are
available (disconnected, standard, high-current).

Quoted 4 lines

The GPIO-Peripheral has a mechanism for detecting level changes which is not featured in this model.

I have a couple of style nits below but otherwise this looks good.

Quoted 6 lines
hw/gpio/nrf51_gpio.c new file +6 -0
+#define NRF51_GPIO_SIZE 0x1000
+
+#define NRF51_GPIO_REG_OUT          0x504
+#define NRF51_GPIO_REG_OUTSET       0x508
+#define NRF51_GPIO_REG_OUTCLR       0x50C
+#define NRF51_GPIO_REG_IN           0x510

hw/gpio/nrf51_gpio.c line 33

Quoted 10 lines
hw/gpio/nrf51_gpio.c new file +10 -0
+#define NRF51_GPIO_REG_DIR          0x514
+#define NRF51_GPIO_REG_DIRSET       0x518
+#define NRF51_GPIO_REG_DIRCLR       0x51C
+#define NRF51_GPIO_REG_CNF_START    0x700
+#define NRF51_GPIO_REG_CNF_END      0x77F
+
+#define GPIO_PULLDOWN 1
+#define GPIO_PULLUP 3
+
+/**

"/**" is for doc-comment format comments. Generally we only need those for functions that are global, not file-local ones. If you want to do a doc-comment, then you want to do it in the right syntax, with description of input arguments and result (eg see include/qemu/bitops.h for some examples). If not, just start with "/*".

Quoted 46 lines

+ * Check if the output driver is connected to the direction switch
+ * given the current configuration and logic level.
+ * It is not differentiated between standard and "high"(-power) drive modes.
+ */

hw/gpio/nrf51_gpio.c new file +42 -0
+static bool is_connected(uint32_t config, uint32_t level)
+{
+    bool state;
+    uint32_t drive_config = extract32(config, 8, 3);
+
+    switch (drive_config) {
+    case 0 ... 3:
+        state = true;
+        break;
+    case 4 ... 5:
+        state = level != 0;
+        break;
+    case 6 ... 7:
+        state = level == 0;
+        break;
+    }
+
+    return state;
+}
+
+static void update_output_irq(Nrf51GPIOState *s, size_t i,
+                              bool connected, bool level)
+{
+    int64_t irq_level = connected ? level : -1;
+    bool old_connected = extract32(s->old_out_connected, i, 1);
+    bool old_level = extract32(s->old_out, i, 1);
+
+    if ((old_connected != connected) || (old_level != level)) {
+        qemu_set_irq(s->output[i], irq_level);
+        trace_nrf51_gpio_update_output_irq(i, irq_level);
+    }
+
+    s->old_out = deposit32(s->old_out, i, 1, level);
+    s->old_out_connected = deposit32(s->old_out_connected, i, 1, connected);
+}
+
+static void update_state(Nrf51GPIOState *s)
+{
+    uint32_t pull;
+    bool connected_out, dir, connected_in, out, input;
+
+    for (size_t i = 0; i < NRF51_GPIO_PINS; i++) {

hw/gpio/nrf51_gpio.c line 89

Quoted 10 lines
hw/gpio/nrf51_gpio.c new file +10 -0
+        pull = extract32(s->cnf[i], 2, 2);
+        dir = extract32(s->cnf[i], 0, 1);
+        connected_in = extract32(s->in_mask, i, 1);
+        out = extract32(s->out, i, 1);
+        input = !extract32(s->cnf[i], 1, 1);
+        connected_out = is_connected(s->cnf[i], out) && dir;
+
+        update_output_irq(s, i, connected_out, out);
+
+        /** Pin both driven externally and internally */

Inline comments like these should definitely not have the doc-comment starting indicator.

Quoted 10 lines
hw/gpio/nrf51_gpio.c new file +3 -0
+        if (connected_out && connected_in) {
+            qemu_log_mask(LOG_GUEST_ERROR, "GPIO pin %zu short circuited\n", i);
+        }
hw/gpio/trace-events new file +0 -0
new file mode 100644
index 0000000000..11486b09b9

hw/gpio/trace-events line 7

Quoted 8 lines
hw/gpio/trace-events new file +7 -0
@@ -0,0 +1,7 @@
+# See docs/devel/tracing.txt for syntax documentation.
+
+# hw/gpio/nrf51_gpio.c
+nrf51_gpio_read(uint64_t offset, uint64_t r) "offset 0x%" PRIx64 " value 0x%" PRIx64
+nrf51_gpio_write(uint64_t offset, uint64_t value) "offset 0x%" PRIx64 " value 0x%" PRIx64
+nrf51_gpio_set(int64_t line, int64_t value) "line %" PRIi64 " value 0x%" PRIi64
+nrf51_gpio_update_output_irq(int64_t line, int64_t value) "line %" PRIi64 " value 0x%" PRIi64
\ No newline at end of file

You want to make sure there's a newline at the end of the file...

thanks
-- PMM

#16 lore raw

PATCH 5/7 tests/microbit-test: Add Tests for nRF51 GPIO

In reply to Steffen Görtz

The test suite for the nRF51 GPIO peripheral for now only tests initial state. Additionally a set of tests testing an implementation detail of the model are included.

---
 tests/microbit-test.c | 96 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
tests/microbit-test.c +96 -0
index 9f83063cf3..cc36171734 100644
@@ -33,6 +33,21 @@
 #define NVMC_ERASEPCR0      0x510
 #define NVMC_ERASEUICR      0x514
 
+#define GPIO_BASE         0x50000000
+#define GPIO_OUT          0x504
+#define GPIO_OUTSET       0x508
+#define GPIO_OUTCLR       0x50C
+#define GPIO_IN           0x510
+#define GPIO_DIR          0x514
+#define GPIO_DIRSET       0x518
+#define GPIO_DIRCLR       0x51C
+#define GPIO_CNF_START    0x700
+#define GPIO_CNF_END      0x77F
+#define GPIO_PINS 32
+
+#define GPIO_PULLDOWN 1
+#define GPIO_PULLUP 3
+
 
 static void fill_and_erase(hwaddr base, hwaddr size, uint32_t address_reg)
 {
@@ -109,6 +124,86 @@ static void test_nrf51_nvmc(void)
     }
 }
 
+static void test_nrf51_gpio(void)
+{
+    size_t i;
+    uint32_t actual, expected;
+
+    struct {
+        hwaddr addr;
+        uint32_t expected;
+    } reset_state[] = {
+            {GPIO_OUT, 0x00000000}, {GPIO_OUTSET, 0x00000000},
+            {GPIO_OUTCLR, 0x00000000}, {GPIO_IN, 0x00000000},
+            {GPIO_DIR, 0x00000000}, {GPIO_DIRSET, 0x00000000},
+            {GPIO_DIRCLR, 0x00000000}
+    };
+
+    /** Check reset state **/
+    for (i = 0; i < ARRAY_SIZE(reset_state); i++) {
+        expected = reset_state[i].expected;
+        actual = readl(GPIO_BASE + reset_state[i].addr);
+        g_assert_cmpuint(actual, ==, expected);
+    }
+
+    for (i = 0; i < GPIO_PINS; i++) {
+        expected = 0x00000002;
+        actual = readl(GPIO_BASE + GPIO_CNF_START + i * 4);
+        g_assert_cmpuint(actual, ==, expected);
+    }
+
+    /** Check dir bit consistency between dir and cnf **/
+    /* Check set via DIRSET */
+    expected = 0x80000001;
+    writel(GPIO_BASE + GPIO_DIRSET, expected);
+    actual = readl(GPIO_BASE + GPIO_DIR);
+    g_assert_cmpuint(actual, ==, expected);
+    actual = readl(GPIO_BASE + GPIO_CNF_START) & 0x01;
+    g_assert_cmpuint(actual, ==, 0x01);
+    actual = readl(GPIO_BASE + GPIO_CNF_END) & 0x01;
+    g_assert_cmpuint(actual, ==, 0x01);
+
+    /* Check clear via DIRCLR */
+    writel(GPIO_BASE + GPIO_DIRCLR, 0x80000001);
+    actual = readl(GPIO_BASE + GPIO_DIR);
+    g_assert_cmpuint(actual, ==, 0x00000000);
+    actual = readl(GPIO_BASE + GPIO_CNF_START) & 0x01;
+    g_assert_cmpuint(actual, ==, 0x00);
+    actual = readl(GPIO_BASE + GPIO_CNF_END) & 0x01;
+    g_assert_cmpuint(actual, ==, 0x00);
+
+    /* Check set via DIR */
+    expected = 0x80000001;
+    writel(GPIO_BASE + GPIO_DIR, expected);
+    actual = readl(GPIO_BASE + GPIO_DIR);
+    g_assert_cmpuint(actual, ==, expected);
+    actual = readl(GPIO_BASE + GPIO_CNF_START) & 0x01;
+    g_assert_cmpuint(actual, ==, 0x01);
+    actual = readl(GPIO_BASE + GPIO_CNF_END) & 0x01;
+    g_assert_cmpuint(actual, ==, 0x01);
+
+    /* Reset DIR */
+    writel(GPIO_BASE + GPIO_DIR, 0x00000000);
+
+    /* Check Input propagates */
+    g_assert_false(true);
+
+    /* Check pull-up working */
+    g_assert_false(true);
+
+    /* Check pull-down working */
+    g_assert_false(true);
+
+    /* Check Output propagates */
+    g_assert_false(true);
+
+    /* Check self-stimulation */
+    g_assert_false(true);
+
+    /* Check short-circuit */
+    g_assert_false(true);
+}
+
 int main(int argc, char **argv)
 {
     int ret;
@@ -118,6 +213,7 @@ int main(int argc, char **argv)
     global_qtest = qtest_startf("-machine microbit");
 
     qtest_add_func("/microbit/nrf51/nvmc", test_nrf51_nvmc);
+    qtest_add_func("/microbit/nrf51/gpio", test_nrf51_gpio);
 
     ret = g_test_run();
 
Signature · 1 line
2.18.0
#17 lore raw

Re: [Qemu-devel] [PATCH 5/7] tests/microbit-test: Add Tests for nRF51 GPIO

Review

Quoted 2 lines · Steffen Görtz
(quoted diff) +2 -0
+    /* Check Input propagates */
+    g_assert_false(true);

Each patch must compile and run successfully. This test case will fail here. Please don't do this - even if a later patch will fill in the missing part.

The reason for this requirement is that git-bisect(1) must work on any commit. If the some commits don't compile or pass tests then it makes git-bisect(1) much harder or impossible to use.

Stefan

#18 lore raw

PATCH 6/7 hw/timer/nrf51_timer: Add nRF51 Timer peripheral

In reply to Steffen Görtz

This patch adds the model for the nRF51 timer peripheral. Currently, only the TIMER mode is implemented.

---
 hw/timer/Makefile.objs         |   1 +
 hw/timer/nrf51_timer.c         | 382 +++++++++++++++++++++++++++++++++
 hw/timer/trace-events          |   5 +
 include/hw/timer/nrf51_timer.h |  63 ++++++
 4 files changed, 451 insertions(+)
 create mode 100644 hw/timer/nrf51_timer.c
 create mode 100644 include/hw/timer/nrf51_timer.h
hw/timer/Makefile.objs +1 -0
index e16b2b913c..18deca761e 100644
@@ -23,6 +23,7 @@ common-obj-$(CONFIG_IMX) += imx_gpt.o
 common-obj-$(CONFIG_LM32) += lm32_timer.o
 common-obj-$(CONFIG_MILKYMIST) += milkymist-sysctl.o
 common-obj-$(CONFIG_XLNX_ZYNQMP) += xlnx-zynqmp-rtc.o
+common-obj-$(CONFIG_NRF51_SOC) += nrf51_timer.o
 
 obj-$(CONFIG_ALTERA_TIMER) += altera_timer.o
 obj-$(CONFIG_EXYNOS4) += exynos4210_mct.o
hw/timer/nrf51_timer.c new file +382 -0

382 lines not on this page · raw message

hw/timer/trace-events +5 -0
index e6e042fddb..91c20540a3 100644
@@ -63,3 +63,8 @@ cmsdk_apb_timer_reset(void) "CMSDK APB timer: reset"
 
 # hw/timer/xlnx-zynqmp-rtc.c
 xlnx_zynqmp_rtc_gettime(int year, int month, int day, int hour, int min, int sec) "Get time from host: %d-%d-%d %2d:%02d:%02d"
+
+# hw/timer/nrf51_timer.c
+nrf51_timer_read(uint64_t addr, uint32_t value, unsigned size) "read addr 0x%" PRIx64 " data 0x%" PRIx32 " size %u"
+nrf51_timer_write(uint64_t addr, uint32_t value, unsigned size) "write addr 0x%" PRIx64 " data 0x%" PRIx32 " size %u"
+
include/hw/timer/nrf51_timer.h new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.18.0
#19 lore raw

Re: [Qemu-devel] [PATCH 6/7] hw/timer/nrf51_timer: Add nRF51 Timer peripheral

hw/timer/nrf51_timer.c line 51

Quoted 5 lines · Steffen Görtz
hw/timer/nrf51_timer.c new file +5 -0
+/* Trigger */
+#define NRF51_TRIGGER_TASK 0x01
+
+/* Events */
+#define NRF51_EVENT_CLEAR  0x00

NRF51_TRIGGER_TASK and NRF51_EVENT_CLEAR are generic nRF51 SoC constants. Please put them in a header to avoid duplication.

hw/timer/nrf51_timer.c line 318

Quoted 5 lines
hw/timer/nrf51_timer.c new file +5 -0
+static void nrf51_timer_reset(DeviceState *dev)
+{
+    Nrf51TimerState *s = NRF51_TIMER(dev);
+
+    s->runstate = NRF51_TIMER_STOPPED;

This leaves the timer pending if it was scheduled and then reset is called. I think timer_del() is necessary.

Quoted 8 lines
include/hw/timer/nrf51_timer.h new file +2 -0
new file mode 100644
index 0000000000..a1278f17bd
@@ -0,0 +1,63 @@
+/*
+* nRF51 System-on-Chip Timer peripheral

include/hw/timer/nrf51_timer.h line 12

Quoted 10 lines
include/hw/timer/nrf51_timer.h new file +10 -0
+ *
+ * Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf
+ * Product Spec: http://infocenter.nordicsemi.com/pdf/nRF51822_PS_v3.1.pdf
+ *
+ * QEMU interface:
+ * + sysbus MMIO regions 0: GPIO registers
+ * + sysbus irq
+ *
+ * Accuracy of the peripheral model:
+ * + Only TIMER mode is implemented, COUNTER mode is not implemented.

This information was in a doc comment above the device state struct in other patches you've sent. Here it's at the top of the file. For consistency please put it in a doc comment.

include/hw/timer/nrf51_timer.h line 32

Quoted 4 lines
include/hw/timer/nrf51_timer.h new file +4 -0
+typedef enum {
+    NRF51_TIMER_STOPPED = 0,
+    NRF51_TIMER_RUNNING
+} Nrf51TimerRunstate;

This is basically a bool and the Nrf51TimerState->runstate field is uint8_t. The code can be simplified by dropping the enum and changing the field to "bool running".

include/hw/timer/nrf51_timer.h line 37

Quoted 4 lines
include/hw/timer/nrf51_timer.h new file +4 -0
+typedef enum {
+    NRF51_TIMER_TIMER = 0,
+    NRF51_TIMER_COUNTER = 1
+} Nrf51TimerMode;

Since Nrf51TimerMode isn't used in this header file (the Nrf51TimerState->mode field is uint32_t), this enum is internal to the device implementation. It can be moved to the .c file.

#20 lore raw

PATCH 7/7 hw/display/led_matrix: Add LED matrix display device

In reply to Steffen Görtz

The LEDs are not individually connected to the output
pins of the microcontroller. Instead, the LEDs share pins for rows and columns.
The pattern for a row or a column is only displayed for a short moment. The slowness of the human eye results in a complete and flicker-free image (persistence of vision). More information can be found here: https://en.wikipedia.org/wiki/Multiplexed_display .

This device demultiplexes the dot-matrix pattern to a grayscale image as it would be perceived by the human eye.
The number of rows, columns and the refresh period can be configured.

At the moment it is assumed that the LEDs are connected in forward direction from rows to columns.

The demultiplexed LEDs are drawn to the canvas of a graphics console. In the future it is planed to send QMP events with the updated matrix image.

---
 hw/display/Makefile.objs        |   2 +
 hw/display/led_matrix.c         | 262 ++++++++++++++++++++++++++++++++
 include/hw/display/led_matrix.h |  38 +++++
 3 files changed, 302 insertions(+)
 create mode 100644 hw/display/led_matrix.c
 create mode 100644 include/hw/display/led_matrix.h
hw/display/Makefile.objs +2 -0
index fb8408c6d0..c423f1eb1a 100644
@@ -23,6 +23,8 @@ common-obj-$(CONFIG_FRAMEBUFFER) += framebuffer.o
 common-obj-$(CONFIG_MILKYMIST) += milkymist-vgafb.o
 common-obj-$(CONFIG_ZAURUS) += tc6393xb.o
 
+common-obj-$(CONFIG_NRF51_SOC) += led_matrix.o
+
 common-obj-$(CONFIG_MILKYMIST_TMU2) += milkymist-tmu2.o
 milkymist-tmu2.o-cflags := $(X11_CFLAGS)
 milkymist-tmu2.o-libs := $(X11_LIBS)
hw/display/led_matrix.c new file +262 -0

262 lines not on this page · raw message

include/hw/display/led_matrix.h new file +38 -0
new file mode 100644
index 0000000000..4a43b69f5b
@@ -0,0 +1,38 @@
+/*
+ * LED Matrix Demultiplexer
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ */
+#ifndef LED_MATRIX_H
+#define LED_MATRIX_H
+
+#include "hw/sysbus.h"
+#include "qemu/timer.h"
+#define TYPE_LED_MATRIX "led_matrix"
+#define LED_MATRIX(obj) OBJECT_CHECK(LEDMatrixState, (obj), TYPE_LED_MATRIX)
+
+typedef struct LEDMatrixState {
+    SysBusDevice parent_obj;
+
+    QemuConsole *con;
+    bool redraw;
+
+    uint32_t refresh_period; /* refresh period in ms */
+    uint8_t nrows;
+    uint8_t ncols;
+    bool strobe_row;
+
+    QEMUTimer timer;
+    int64_t timestamp;
+
+    uint64_t row;
+    uint64_t col;
+    int64_t *led_working_dc; /* Current LED duty cycle acquisition */
+    int64_t *led_frame_dc; /* Last complete LED duty cycle acquisition */
+} LEDMatrixState;
+
+
+#endif
Signature · 1 line
2.18.0
#21 lore raw

Re: [Qemu-devel] [PATCH 7/7] hw/display/led_matrix: Add LED matrix display device

hw/display/led_matrix.c line 49

Quoted 7 lines · Steffen Görtz
hw/display/led_matrix.c new file +7 -0
+static void led_timer_expire(void *opaque)
+{
+    LEDMatrixState *s = LED_MATRIX(opaque);
+/*
+    uint8_t divider = s->strobe_row ? s->nrows : s->ncols;
+    int64_t max_on = (s->refresh_period * 1000) / divider;
+*/

Please remove dead code.

hw/display/led_matrix.c line 60

Quoted 11 lines
hw/display/led_matrix.c new file +11 -0
+
+    update_on_times(s);
+
+    memcpy(s->led_frame_dc, s->led_working_dc,
+           sizeof(int64_t) * s->nrows * s->ncols);
+    memset(s->led_working_dc, 0x00, sizeof(int64_t) * s->nrows * s->ncols);
+
+    timer_mod(&s->timer,
+            qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + s->refresh_period);
+    s->redraw = true;
+}

Have you considered an approach based on the access pattern rather than a timer (sampling)?

Sampling-based approaches are more CPU intensive and may result in artifacts if the guest doesn't update pins at exactly the refresh rate.

The access pattern approach assumes that the rows/columns are scanned in a certain way. It uses the guest's pin accesses as the event for checking the matrix state. It's more efficient, doesn't suffer from sampling artifacts, but requires knowledge of how rows/columns are scanned.

Quoted 10 lines
hw/display/led_matrix.c new file +10 -0
+static void draw_pixel(DisplaySurface *ds, int x, int y, uint32_t color)
+{
+    int bpp;
+    uint8_t *d;
+    bpp = (surface_bits_per_pixel(ds) + 7) >> 3;
+    d = surface_data(ds) + surface_stride(ds) * y + bpp * x;
+    switch (bpp) {
+    case 1:
+        *((uint8_t *) d) = color;
+        d++;

hw/display/led_matrix.c line 99

Quoted 10 lines
hw/display/led_matrix.c new file +10 -0
+        break;
+    case 2:
+        *((uint16_t *) d) = color;
+        d += 2;
+        break;
+    case 4:
+        *((uint32_t *) d) = color;
+        d += 4;
+        break;
+    }

There is no need to increment d.

hw/display/led_matrix.c line 167

Quoted 3 lines
hw/display/led_matrix.c new file +3 -0
+    for (x = 0; x < s->nrows ; x++) {
+        for (y = 0; y < s->ncols; y++) {
+            idx = x * s->ncols + y;

I'm confused by the naming here. "rows" is height/vertical/y. "columns" is width/horizontal/x. Why is "x" used for vertical and "y" for horizontal?

hw/display/led_matrix.c line 171

Quoted 4 lines
hw/display/led_matrix.c new file +4 -0
+            red = (s->led_frame_dc[idx] * 0xFF) / (s->refresh_period * 1000);
+            color_led = colorfunc(red, 0x00, 0x00);
+
+            draw_box(surface, idx * 10, 0, 5, 10, color_led);

Are the LEDs layed out horizontally? The y coordinate is hardcoded to
0. I thought this would be a 2D LED matrix.

hw/display/led_matrix.c line 227

Quoted 6 lines
hw/display/led_matrix.c new file +6 -0
+static void led_matrix_unrealize(DeviceState *dev, Error **errp)
+{
+    LEDMatrixState *s = LED_MATRIX(dev);
+
+    g_free(s->led_working_dc);
+    s->led_working_dc = NULL;

led_frame_dc is leaked.

graphic_console_close() is missing.

Quoted 13 lines
include/hw/display/led_matrix.h new file +7 -0
new file mode 100644
index 0000000000..4a43b69f5b
@@ -0,0 +1,38 @@
+/*
+ * LED Matrix Demultiplexer
+ *
+ * Copyright 2018 Steffen Görtz <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.

include/hw/display/led_matrix.h line 17

Quoted 10 lines
include/hw/display/led_matrix.h new file +10 -0
+ */
+#ifndef LED_MATRIX_H
+#define LED_MATRIX_H
+
+#include "hw/sysbus.h"
+#include "qemu/timer.h"
+#define TYPE_LED_MATRIX "led_matrix"
+#define LED_MATRIX(obj) OBJECT_CHECK(LEDMatrixState, (obj), TYPE_LED_MATRIX)
+
+typedef struct LEDMatrixState {

There is too little documentation here for a generic device that could be reused by other boards. Please describe the purpose and functionality of this device.

#22 lore raw
In reply to Steffen Görtz
Quoted 11 lines · Steffen Görtz

This series contains additional peripheral devices for the nRF51822 microcontroller. Furthermore it includes a device to demultiplex the row and column strobes used in embedded devices to drive 2D LED dot-matrices.

Included devices:
- Random Number Generator
- Non-volatile Memories
- General purpose I/O
- Timer

Quoted 4 lines

Microbit board-level Devices:
- LED Matrix

Instantiate of the devices is done in an upcoming patch series.

Thanks for gathering together these patches in one series. This and the other Cortex-M0-related patchsets are on my queue to review, but I won't be able to get to them for a little bit as I'm currently dealing with 3.0 release work. Hopefully by next week that will have calmed down.

thanks
-- PMM

#23 lore raw
Quoted 11 lines
Quoted 5 lines

Microbit board-level Devices:
- LED Matrix

Instantiate of the devices is done in an upcoming patch series.

Thanks for gathering together these patches in one series. This and the other Cortex-M0-related patchsets are on my queue to review, but I won't be able to get to them for a little bit as I'm currently dealing with 3.0 release work. Hopefully by next week that will have calmed down.

Great! Thank you!

Steffen

#24 lore raw
In reply to Peter Maydell
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Steffen Görtz

This series contains additional peripheral devices for the nRF51822 microcontroller. Furthermore it includes a device to demultiplex the row and column strobes used in embedded devices to drive 2D LED dot-matrices.

Included devices:
- Random Number Generator
- Non-volatile Memories
- General purpose I/O
- Timer

Microbit board-level Devices:
- LED Matrix

Quoted 7 lines
Quoted 1 line

Instantiate of the devices is done in an upcoming patch series.

Thanks for gathering together these patches in one series. This and the other Cortex-M0-related patchsets are on my queue to review, but I won't be able to get to them for a little bit as I'm currently dealing with 3.0 release work. Hopefully by next week that will have calmed down.

I've now gone through and left some review comments. (Patches where I haven't replied I thought were covered by Stefan's review.)

thanks
-- PMM