Skip to contentSkip to conversation

arm: Add nRF51 SoC and micro:bit machine

PATCH Superseded Joel Stanley 3 participants 9 messages Aug 3, 2018 – Aug 26, 2018

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover arm: Add nRF51 SoC and micro:bit machine

v4: Fix number of IRQs
Based-on: [email protected]

This short series implements a minimal definition of the Nordic Semiconductor nRF51, a Cortex-M0 ARM SoC, and the BBC micro:bit, a machine that will use this SoC.

This work will serve as the base for our Google Summer of Code and Outreachy interns who will work on implementing a number of features on top of this base.

I've tested this with a microbit micropython firmware, and checked that it starts running by looking at it with gdb.

I chose to keep the nrf51 and the microbit seperate, to not confuse the peripherals that are on the microbit but are not part of the nrf51, and vice versa.

Joel Stanley (3):
MAINTAINERS: Add NRF51 entry
arm: Add Nordic Semiconductor nRF51 SoC
arm: Add BBC micro:bit machine

 MAINTAINERS                     |   8 +++
 default-configs/arm-softmmu.mak |   1 +
 hw/arm/Makefile.objs            |   1 +
 hw/arm/microbit.c               |  54 +++++++++++++++
 hw/arm/nrf51_soc.c              | 119 ++++++++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h      |  42 +++++++++++
 6 files changed, 225 insertions(+)
 create mode 100644 hw/arm/microbit.c
 create mode 100644 hw/arm/nrf51_soc.c
 create mode 100644 include/hw/arm/nrf51_soc.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/3 MAINTAINERS: Add NRF51 entry

This contains the NRF51, and the machine that uses it, the BBC micro:bit.

---
v3:
fix spelling of mailing list
add stefan's reviewed-by

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index c48d9271cf15..5a0d2e327d4a 100644
@@ -656,6 +656,14 @@ F: include/hw/*/*aspeed*
 F: hw/net/ftgmac100.c
 F: include/hw/net/ftgmac100.h
 
+NRF51
+M: Joel Stanley <[email protected]>
+L: [email protected]
+S: Maintained
+F: hw/arm/nrf51_soc.c
+F: hw/arm/microbit.c
+F: include/hw/arm/nrf51_soc.h
+
 CRIS Machines
 -------------
 Axis Dev88
Signature · 1 line
2.17.1
Reviewed-by on v4 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] MAINTAINERS: Add NRF51 entry

Quoted 5 lines · Joel Stanley

This contains the NRF51, and the machine that uses it, the BBC micro:bit.

Quoted 7 lines

---
v3:
fix spelling of mailing list
add stefan's reviewed-by

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)

thanks
-- PMM

#3 lore raw

PATCH v4 2/3 arm: Add Nordic Semiconductor nRF51 SoC

In reply to Joel Stanley

The nRF51 is a Cortex-M0 microcontroller with an on-board radio module, plus other common ARM SoC peripherals.

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

This defines a basic model of the CPU and memory, with no peripherals implemented at this stage.

---
v2:
put memory as struct fileds in state structure
pass OBJECT(s) as owner, not NULL
Add missing addresses for ficr
Fix flash and sram sizes for microbit
Embed cpu object in state object an initalise it without use of armv7m_init Link to datasheet
v3:
rebase nrf51 on m0 changes
remove unused kernel_filename
clarify flash and sram size
make flash and sram size properties of the soc state
v4:
set the number of interrupts to 32

---
 default-configs/arm-softmmu.mak |   1 +
 hw/arm/Makefile.objs            |   1 +
 hw/arm/nrf51_soc.c              | 119 ++++++++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h      |  42 +++++++++++
 4 files changed, 163 insertions(+)
 create mode 100644 hw/arm/nrf51_soc.c
 create mode 100644 include/hw/arm/nrf51_soc.h
default-configs/arm-softmmu.mak +1 -0
index e704cb6e34d7..3432721d7d08 100644
@@ -102,6 +102,7 @@ CONFIG_STM32F2XX_SYSCFG=y
 CONFIG_STM32F2XX_ADC=y
 CONFIG_STM32F2XX_SPI=y
 CONFIG_STM32F205_SOC=y
+CONFIG_NRF51_SOC=y
 
 CONFIG_CMSDK_APB_TIMER=y
 CONFIG_CMSDK_APB_UART=y
hw/arm/Makefile.objs +1 -0
index b1e4f8f006aa..e31875ec69bc 100644
@@ -36,3 +36,4 @@ obj-$(CONFIG_MSF2) += msf2-soc.o msf2-som.o
 obj-$(CONFIG_IOTKIT) += iotkit.o
 obj-$(CONFIG_FSL_IMX7) += fsl-imx7.o mcimx7d-sabre.o
 obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o smmuv3.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o
hw/arm/nrf51_soc.c new file +119 -0

119 lines not on this page · raw message

include/hw/arm/nrf51_soc.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.17.1
#4 lore raw

PATCH v4 3/3 arm: Add BBC micro:bit machine

In reply to Joel Stanley

This adds the base for a machine model of the BBC micro:bit:

https://en.wikipedia.org/wiki/Micro_Bit

This is a system with a nRF51 SoC containing the main processor, with various peripherals on board.

---
v2:
- Instead of setting kernel filename property, load the image directly
- Add link to hardware overview website
v3:
- Rebase microbit on m0 changes
- Remove hard-coded flash size and retrieve from the soc
- Add Stefan's reviewed-by

---
 hw/arm/Makefile.objs |  2 +-
 hw/arm/microbit.c    | 54 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 hw/arm/microbit.c
hw/arm/Makefile.objs +1 -1
index e31875ec69bc..2798a257921d 100644
@@ -36,4 +36,4 @@ obj-$(CONFIG_MSF2) += msf2-soc.o msf2-som.o
 obj-$(CONFIG_IOTKIT) += iotkit.o
 obj-$(CONFIG_FSL_IMX7) += fsl-imx7.o mcimx7d-sabre.o
 obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o smmuv3.o
-obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o microbit.o
hw/arm/microbit.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.17.1
#5 lore raw

Re: [Qemu-arm] [PATCH v4 3/3] arm: Add BBC micro:bit machine

Quoted 52 lines · Joel Stanley

This adds the base for a machine model of the BBC micro:bit:

https://en.wikipedia.org/wiki/Micro_Bit

This is a system with a nRF51 SoC containing the main processor, with various peripherals on board.

---
v2:
- Instead of setting kernel filename property, load the image directly
- Add link to hardware overview website
v3:
- Rebase microbit on m0 changes
- Remove hard-coded flash size and retrieve from the soc
- Add Stefan's reviewed-by

---
 hw/arm/Makefile.objs |  2 +-
 hw/arm/microbit.c    | 54 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 hw/arm/microbit.c
hw/arm/Makefile.objs +1 -1
index e31875ec69bc..2798a257921d 100644
@@ -36,4 +36,4 @@ obj-$(CONFIG_MSF2) += msf2-soc.o msf2-som.o
 obj-$(CONFIG_IOTKIT) += iotkit.o
 obj-$(CONFIG_FSL_IMX7) += fsl-imx7.o mcimx7d-sabre.o
 obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o smmuv3.o
-obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o microbit.o
hw/arm/microbit.c new file +13 -0
new file mode 100644
index 000000000000..ecf64e883f4f
@@ -0,0 +1,54 @@
+/*
+ * BBC micro:bit machine
+ * http://tech.microbit.org/hardware/
+ *
+ * Copyright 2018 Joel Stanley <[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 "hw/boards.h"

hw/arm/microbit.c line 23

Quoted 10 lines
hw/arm/microbit.c new file +10 -0
+#include "hw/arm/arm.h"
+#include "exec/address-spaces.h"
+
+#include "hw/arm/nrf51_soc.h"
+
+typedef struct {
+    MachineState parent;
+
+    NRF51State nrf51;
+} MICROBITMachineState;

Can we call this "MicrobitMachineState", please? I don't think the board name is all-caps, and our convention for struct type names is CamelCase.

hw/arm/microbit.c line 25

Quoted 2 lines
hw/arm/microbit.c new file +2 -0
+
+#define TYPE_MICROBIT_MACHINE "microbit"

Should be MACHINE_TYPE_NAME("microbit")

hw/arm/microbit.c line 32

Quoted 7 lines
hw/arm/microbit.c new file +7 -0
+
+#define MICROBIT_MACHINE(obj) \
+    OBJECT_CHECK(MICROBITMachineState, obj, TYPE_MICROBIT_MACHINE)
+
+static void microbit_init(MachineState *machine)
+{
+    MICROBITMachineState *s = g_new(MICROBITMachineState, 1);

This is odd. The MICROBITMachineState is the state struct for your subclass of MachineState, and that object has already been allocated (you get a pointer to it as the argument to the init function here). So all you need to do is cast it to the right type:

      MICROBITMachineState *s = MICROBIT_MACHINE(machine);

You don't need to allocate a second copy. (You do need to get the type registration right so that you declare that the type is of size sizeof(MICROBITMachineState) rather than just sizeof(MachineState), though -- see below.)

hw/arm/microbit.c line 38

Quoted 6 lines
hw/arm/microbit.c new file +6 -0
+    MemoryRegion *system_memory = get_system_memory();
+    Object *soc;
+
+    object_initialize(&s->nrf51, sizeof(s->nrf51), TYPE_NRF51_SOC);
+    soc = OBJECT(&s->nrf51);
+    object_property_add_child(OBJECT(machine), "nrf51", soc, &error_fatal);

You should use the new function init_sysbus_child() here rather than doing separate initialize and add_child steps (we realised late in the 3.0 cycle that we had refcount leaks as a result of doing this as a 2-step process, hence the new function).

hw/arm/microbit.c line 42

Quoted 4 lines
hw/arm/microbit.c new file +4 -0
+    object_property_set_link(soc, OBJECT(system_memory),
+                             "memory", &error_abort);
+
+    object_property_set_bool(soc, true, "realized", &error_abort);

Better to use error_fatal rather than error_abort in these two calls, I think.

hw/arm/microbit.c line 54

Quoted 12 lines
hw/arm/microbit.c new file +12 -0
+
+    arm_m_profile_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+            NRF51_SOC(soc)->flash_size);
+}
+
+static void microbit_machine_init(MachineClass *mc)
+{
+    mc->desc = "BBC micro:bit";
+    mc->init = microbit_init;
+    mc->max_cpus = 1;
+}
+DEFINE_MACHINE("microbit", microbit_machine_init);

Your subclass of TYPE_MACHINE has extra state, so it can't use DEFINE_MACHINE (which creates a subclass whose instance_size is the same as the parent TYPE_MACHINE). You need to do this longhand:

static void machine_class_init(ObjectClass *oc, void *data)

{
    MachineClass *mc = MACHINE_CLASS(oc);

    mc->desc = "BBC micro:bit";
    mc->init = microbit_init;
    mc->max_cpus = 1;
}

static const TypeInfo microbit_info = {
.name = TYPE_MICROBIT_MACHINE,
.parent = TYPE_MACHINE,
.instance_size = sizeof(MICROBITMachineState), .class_init = microbit_class_init,
};

static void microbit_machine_init(void)

{
    type_register_static(&microbit_info);
}

type_init(microbit_machine_init);

(code untested but should be correct; compare against other boards.)

thanks
-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] arm: Add BBC micro:bit machine

Quoted 10 lines
Quoted 10 lines
hw/arm/microbit.c new file +9 -0
+
+    arm_m_profile_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+            NRF51_SOC(soc)->flash_size);
+}
+
+static void microbit_machine_init(MachineClass *mc)
+{
+    mc->desc = "BBC micro:bit";
+    mc->init = microbit_init;
Quoted 9 lines

hw/arm/microbit.c line 54

Quoted 3 lines
hw/arm/microbit.c new file +3 -0
+    mc->max_cpus = 1;
+}
+DEFINE_MACHINE("microbit", microbit_machine_init);

Your subclass of TYPE_MACHINE has extra state, so it can't use DEFINE_MACHINE (which creates a subclass whose instance_size is the same as the parent TYPE_MACHINE). You need to do this longhand:

Hi Peter,

this is covered in <[email protected]>

Steffen

#7 lore raw

Re: [Qemu-arm] [PATCH v4 3/3] arm: Add BBC micro:bit machine

Quoted 20 lines · Steffen Görtz
Quoted 10 lines
Quoted 10 lines
hw/arm/microbit.c new file +9 -0
+
+    arm_m_profile_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+            NRF51_SOC(soc)->flash_size);
+}
+
+static void microbit_machine_init(MachineClass *mc)
+{
+    mc->desc = "BBC micro:bit";
+    mc->init = microbit_init;
Quoted 9 lines

hw/arm/microbit.c line 54

Quoted 3 lines
hw/arm/microbit.c new file +3 -0
+    mc->max_cpus = 1;
+}
+DEFINE_MACHINE("microbit", microbit_machine_init);

Your subclass of TYPE_MACHINE has extra state, so it can't use DEFINE_MACHINE (which creates a subclass whose instance_size is the same as the parent TYPE_MACHINE). You need to do this longhand:

Quoted 3 lines

Hi Peter,

this is covered in <[email protected]>

So it is. This patch is wrong though, so the fix needs to be made by folding the relevant changes into this patch, not as a separate commit later.

PS: you don't really need a header file for the machine's struct, because nothing except the machine's .c file will ever need to include it.

thanks
-- PMM

#8 lore raw

Re: [Qemu-arm] [PATCH v4 3/3] arm: Add BBC micro:bit machine

In reply to Peter Maydell
Quoted 30 lines · Peter Maydell

hw/arm/microbit.c line 54

Quoted 7 lines
hw/arm/microbit.c new file +7 -0
+static void microbit_machine_init(MachineClass *mc)
+{
+    mc->desc = "BBC micro:bit";
+    mc->init = microbit_init;
+    mc->max_cpus = 1;
+}
+DEFINE_MACHINE("microbit", microbit_machine_init);

Your subclass of TYPE_MACHINE has extra state, so it can't use DEFINE_MACHINE (which creates a subclass whose instance_size is the same as the parent TYPE_MACHINE). You need to do this longhand:

static void machine_class_init(ObjectClass *oc, void *data)

{
    MachineClass *mc = MACHINE_CLASS(oc);

    mc->desc = "BBC micro:bit";
    mc->init = microbit_init;
    mc->max_cpus = 1;
}

static const TypeInfo microbit_info = {
.name = TYPE_MICROBIT_MACHINE,
.parent = TYPE_MACHINE,
.instance_size = sizeof(MICROBITMachineState), .class_init = microbit_class_init,
};

static void microbit_machine_init(void)

Quoted 8 lines
{
    type_register_static(&microbit_info);
}

type_init(microbit_machine_init);

(code untested but should be correct; compare against other boards.)

Thanks for spelling it out. I spent a decent chunk of time looking at other boards, and this aspect of Qemu still baffles me.