Skip to contentSkip to conversation

arm: Stub out NRF51 TWI magnetometer/accelerometer detection

PATCH In review Stefan Hajnoczi 5 participants 10 messages Jan 10, 2019 – Jan 22, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover arm: Stub out NRF51 TWI magnetometer/accelerometer detection

v2:
* Move stub code into a separate device [Peter]
* Instantiate stub from microbit board instead of nRF51 SoC since this is microbit-specific. Other boards using the nRF51 wouldn't necessarily want to see these TWI/I2C devices.
* Add test case

This series stubs out the microbit's TWI/I2C devices so the microbit-dal firmware boots successfully. The accelerometer and magnetometer are probed at startup. If they are not found the firmware panics.

Stefan Hajnoczi (1):
tests/microbit-test: add TWI stub device test

Steffen Görtz (1):
arm: Stub out NRF51 TWI magnetometer/accelerometer detection

 hw/i2c/Makefile.objs          |   1 +
 include/hw/arm/nrf51.h        |   2 +
 include/hw/arm/nrf51_soc.h    |   1 +
 include/hw/i2c/microbit_i2c.h |  42 +++++++++++
 hw/arm/microbit.c             |  15 ++++
 hw/i2c/microbit_i2c.c         | 127 ++++++++++++++++++++++++++++++++++
 tests/microbit-test.c         |  44 ++++++++++++
 7 files changed, 232 insertions(+)
 create mode 100644 include/hw/i2c/microbit_i2c.h
 create mode 100644 hw/i2c/microbit_i2c.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/2 arm: Stub out NRF51 TWI magnetometer/accelerometer detection

From: Steffen Görtz <[email protected]>

Recent microbit firmwares panic if the TWI magnetometer/accelerometer devices are not detected during startup. We don't implement TWI (I2C) so let's stub out these devices just to let the firmware boot.

Signed-off by: Steffen Görtz <[email protected]>

---
 hw/i2c/Makefile.objs          |   1 +
 include/hw/arm/nrf51.h        |   2 +
 include/hw/arm/nrf51_soc.h    |   1 +
 include/hw/i2c/microbit_i2c.h |  42 +++++++++++
 hw/arm/microbit.c             |  15 ++++
 hw/i2c/microbit_i2c.c         | 127 ++++++++++++++++++++++++++++++++++
 6 files changed, 188 insertions(+)
 create mode 100644 include/hw/i2c/microbit_i2c.h
 create mode 100644 hw/i2c/microbit_i2c.c
hw/i2c/Makefile.objs +1 -0
index 37cacde978..82e747e1cd 100644
@@ -7,5 +7,6 @@ common-obj-$(CONFIG_BITBANG_I2C) += bitbang_i2c.o
 common-obj-$(CONFIG_EXYNOS4) += exynos4210_i2c.o
 common-obj-$(CONFIG_IMX_I2C) += imx_i2c.o
 common-obj-$(CONFIG_ASPEED_SOC) += aspeed_i2c.o
+common-obj-$(CONFIG_NRF51_SOC) += microbit_i2c.o
 obj-$(CONFIG_OMAP) += omap_i2c.o
 obj-$(CONFIG_PPC4XX) += ppc4xx_i2c.o
include/hw/arm/nrf51.h +2 -0
index 175bb6c301..1008fee6c9 100644
@@ -25,6 +25,8 @@
 #define NRF51_IOMEM_SIZE      0x20000000
 
 #define NRF51_UART_BASE       0x40002000
+#define NRF51_TWI_BASE        0x40003000
+#define NRF51_TWI_SIZE        0x00001000
 #define NRF51_TIMER_BASE      0x40008000
 #define NRF51_TIMER_SIZE      0x00001000
 #define NRF51_RNG_BASE        0x4000D000
include/hw/arm/nrf51_soc.h +1 -0
index e06f0304b4..fbdefc07e4 100644
@@ -39,6 +39,7 @@ typedef struct NRF51State {
     MemoryRegion sram;
     MemoryRegion flash;
     MemoryRegion clock;
+    MemoryRegion twi;
 
     uint32_t sram_size;
     uint32_t flash_size;
include/hw/i2c/microbit_i2c.h new file +42 -0

42 lines not on this page · raw message

hw/arm/microbit.c +15 -0
index a734e7f650..84d1b8dd2b 100644
@@ -16,11 +16,13 @@
 #include "exec/address-spaces.h"
 
 #include "hw/arm/nrf51_soc.h"
+#include "hw/i2c/microbit_i2c.h"
 
 typedef struct {
     MachineState parent;
 
     NRF51State nrf51;
+    MicrobitI2CState i2c;
 } MicrobitMachineState;
 
 #define TYPE_MICROBIT_MACHINE MACHINE_TYPE_NAME("microbit")
@@ -32,7 +34,9 @@ static void microbit_init(MachineState *machine)
 {
     MicrobitMachineState *s = MICROBIT_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
+    MemoryRegion *mr;
     Object *soc = OBJECT(&s->nrf51);
+    Object *i2c = OBJECT(&s->i2c);
 
     sysbus_init_child_obj(OBJECT(machine), "nrf51", soc, sizeof(s->nrf51),
                           TYPE_NRF51_SOC);
@@ -41,6 +45,17 @@ static void microbit_init(MachineState *machine)
                              &error_fatal);
     object_property_set_bool(soc, true, "realized", &error_fatal);
 
+    /* Overlap the TWI stub device into the SoC.  This is a microbit-specific
+     * hack until we implement the nRF51 TWI controller properly and the
+     * magnetometer/accelerometer devices.
+     */
+    sysbus_init_child_obj(OBJECT(machine), "microbit.twi", i2c,
+                          sizeof(s->i2c), TYPE_MICROBIT_I2C);
+    object_property_set_bool(i2c, true, "realized", &error_fatal);
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(i2c), 0);
+    memory_region_add_subregion_overlap(&s->nrf51.container, NRF51_TWI_BASE,
+                                        mr, -1);
+
     armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
                        NRF51_SOC(soc)->flash_size);
 }
hw/i2c/microbit_i2c.c new file +127 -0

127 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw
Quoted 38 lines · Stefan Hajnoczi

From: Steffen Görtz <[email protected]>

Recent microbit firmwares panic if the TWI magnetometer/accelerometer devices are not detected during startup. We don't implement TWI (I2C) so let's stub out these devices just to let the firmware boot.

hw/arm/microbit.c +4 -0
@@ -16,11 +16,13 @@
 #include "exec/address-spaces.h"
 
 #include "hw/arm/nrf51_soc.h"
+#include "hw/i2c/microbit_i2c.h"
 
 typedef struct {
     MachineState parent;
 
     NRF51State nrf51;
+    MicrobitI2CState i2c;
 } MicrobitMachineState;
 
 #define TYPE_MICROBIT_MACHINE MACHINE_TYPE_NAME("microbit")
@@ -32,7 +34,9 @@ static void microbit_init(MachineState *machine)
 {
     MicrobitMachineState *s = MICROBIT_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
+    MemoryRegion *mr;
     Object *soc = OBJECT(&s->nrf51);
+    Object *i2c = OBJECT(&s->i2c);
 
     sysbus_init_child_obj(OBJECT(machine), "nrf51", soc, sizeof(s->nrf51),
                           TYPE_NRF51_SOC);
@@ -41,6 +45,17 @@ static void microbit_init(MachineState *machine)
                              &error_fatal);
     object_property_set_bool(soc, true, "realized", &error_fatal);
 

hw/arm/microbit.c static void microbit_init(MachineState *machine) · line 57

Quoted 10 lines
hw/arm/microbit.c +10 -0
+    /* Overlap the TWI stub device into the SoC.  This is a microbit-specific
+     * hack until we implement the nRF51 TWI controller properly and the
+     * magnetometer/accelerometer devices.
+     */
+    sysbus_init_child_obj(OBJECT(machine), "microbit.twi", i2c,
+                          sizeof(s->i2c), TYPE_MICROBIT_I2C);
+    object_property_set_bool(i2c, true, "realized", &error_fatal);
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(i2c), 0);
+    memory_region_add_subregion_overlap(&s->nrf51.container, NRF51_TWI_BASE,
+                                        mr, -1);

This is an SoC device, right? Why are we creating it here in the board code rather than in the SoC object's code ?

thanks
-- PMM

#3 lore raw
Quoted 40 lines · Peter Maydell
Quoted 38 lines · Stefan Hajnoczi

From: Steffen Görtz <[email protected]>

Recent microbit firmwares panic if the TWI magnetometer/accelerometer devices are not detected during startup. We don't implement TWI (I2C) so let's stub out these devices just to let the firmware boot.

hw/arm/microbit.c +4 -0
@@ -16,11 +16,13 @@
 #include "exec/address-spaces.h"
 
 #include "hw/arm/nrf51_soc.h"
+#include "hw/i2c/microbit_i2c.h"
 
 typedef struct {
     MachineState parent;
 
     NRF51State nrf51;
+    MicrobitI2CState i2c;
 } MicrobitMachineState;
 
 #define TYPE_MICROBIT_MACHINE MACHINE_TYPE_NAME("microbit")
@@ -32,7 +34,9 @@ static void microbit_init(MachineState *machine)
 {
     MicrobitMachineState *s = MICROBIT_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
+    MemoryRegion *mr;
     Object *soc = OBJECT(&s->nrf51);
+    Object *i2c = OBJECT(&s->i2c);
 
     sysbus_init_child_obj(OBJECT(machine), "nrf51", soc, sizeof(s->nrf51),
                           TYPE_NRF51_SOC);
@@ -41,6 +45,17 @@ static void microbit_init(MachineState *machine)
                              &error_fatal);
     object_property_set_bool(soc, true, "realized", &error_fatal);
 
Quoted 13 lines

hw/arm/microbit.c static void microbit_init(MachineState *machine) · line 57

Quoted 10 lines
hw/arm/microbit.c +10 -0
+    /* Overlap the TWI stub device into the SoC.  This is a microbit-specific
+     * hack until we implement the nRF51 TWI controller properly and the
+     * magnetometer/accelerometer devices.
+     */
+    sysbus_init_child_obj(OBJECT(machine), "microbit.twi", i2c,
+                          sizeof(s->i2c), TYPE_MICROBIT_I2C);
+    object_property_set_bool(i2c, true, "realized", &error_fatal);
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(i2c), 0);
+    memory_region_add_subregion_overlap(&s->nrf51.container, NRF51_TWI_BASE,
+                                        mr, -1);

This is an SoC device, right? Why are we creating it here in the board code rather than in the SoC object's code ?

Yes, this is a microbit-specific hack. We don't implement a full TWI/I2C bus for the nRF51 SoC. This implementation is just aimed at stubbing out the microbit-specific accelerometer/magnetometer devices.

Therefore I decided it was cleanest to place it in the microbit machine type instead of dirtying the nRF51 SoC with a microbit hack.

If the I2C passthrough GSoC 2019 project happens we'll get proper nRF51 TWI/I2C controller emulation and can eliminate this stub. But for now this allows the microbit to boot and doesn't mess up any other potential machine types that will use the nRF51 SoC.

Stefan

#4 lore raw

PATCH v2 2/2 tests/microbit-test: add TWI stub device test

In reply to Stefan Hajnoczi

This test verifies that we read back the expected I2C WHO_AM_I register values for the accelerometer/magnetometer.

---
 tests/microbit-test.c | 44 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
tests/microbit-test.c +44 -0
index 0c125535f6..dcdc0cd41a 100644
@@ -21,6 +21,49 @@
 #include "hw/arm/nrf51.h"
 #include "hw/gpio/nrf51_gpio.h"
 #include "hw/timer/nrf51_timer.h"
+#include "hw/i2c/microbit_i2c.h"
+
+/* Read a byte from I2C device at @addr from register @reg */
+static uint32_t i2c_read_byte(uint32_t addr, uint32_t reg)
+{
+    uint32_t val;
+
+    writel(NRF51_TWI_BASE + NRF51_TWI_REG_ADDRESS, addr);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STARTTX, 1);
+    writel(NRF51_TWI_BASE + NRF51_TWI_REG_TXD, reg);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_EVENT_TXDSENT);
+    g_assert_cmpuint(val, ==, 1);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STOP, 1);
+
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STARTRX, 1);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_EVENT_RXDREADY);
+    g_assert_cmpuint(val, ==, 1);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_REG_RXD);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STOP, 1);
+
+    return val;
+}
+
+static void test_microbit_i2c(void)
+{
+    uint32_t val;
+
+    /* We don't program pins/irqs but at least enable the device */
+    writel(NRF51_TWI_BASE + NRF51_TWI_REG_ENABLE, 5);
+
+    /* MMA8653 magnetometer detection */
+    val = i2c_read_byte(0x3A, 0x0D);
+    g_assert_cmpuint(val, ==, 0x5A);
+
+    val = i2c_read_byte(0x3A, 0x0D);
+    g_assert_cmpuint(val, ==, 0x5A);
+
+    /* LSM303 accelerometer detection */
+    val = i2c_read_byte(0x3C, 0x4F);
+    g_assert_cmpuint(val, ==, 0x40);
+
+    writel(NRF51_TWI_BASE + NRF51_TWI_REG_ENABLE, 0);
+}
 
 static void test_nrf51_gpio(void)
 {
@@ -247,6 +290,7 @@ int main(int argc, char **argv)
 
     qtest_add_func("/microbit/nrf51/gpio", test_nrf51_gpio);
     qtest_add_func("/microbit/nrf51/timer", test_nrf51_timer);
+    qtest_add_func("/microbit/microbit/i2c", test_microbit_i2c);
 
     ret = g_test_run();
 
Signature · 1 line
2.20.1
#5 lore raw

Re: [Qemu-arm] [PATCH v2 2/2] tests/microbit-test: add TWI stub device test

Quoted 25 lines · Stefan Hajnoczi

This test verifies that we read back the expected I2C WHO_AM_I register values for the accelerometer/magnetometer.

---
 tests/microbit-test.c | 44 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
tests/microbit-test.c +9 -0
index 0c125535f6..dcdc0cd41a 100644
@@ -21,6 +21,49 @@
 #include "hw/arm/nrf51.h"
 #include "hw/gpio/nrf51_gpio.h"
 #include "hw/timer/nrf51_timer.h"
+#include "hw/i2c/microbit_i2c.h"
+
+/* Read a byte from I2C device at @addr from register @reg */
+static uint32_t i2c_read_byte(uint32_t addr, uint32_t reg)
+{
+    uint32_t val;
+
+    writel(NRF51_TWI_BASE + NRF51_TWI_REG_ADDRESS, addr);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STARTTX, 1);

tests/microbit-test.c line 42

Quoted 10 lines
tests/microbit-test.c +10 -0
+    writel(NRF51_TWI_BASE + NRF51_TWI_REG_TXD, reg);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_EVENT_TXDSENT);
+    g_assert_cmpuint(val, ==, 1);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STOP, 1);
+
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STARTRX, 1);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_EVENT_RXDREADY);
+    g_assert_cmpuint(val, ==, 1);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_REG_RXD);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STOP, 1);

Any chance that you could use qemu_writel() right from the start here? That will make it easier to finally get rid of global_qtest one day...

Thomas

#6 lore raw

Re: [Qemu-arm] [PATCH v2 2/2] tests/microbit-test: add TWI stub device test

Quoted 31 lines · Thomas Huth
Quoted 30 lines · Stefan Hajnoczi

This test verifies that we read back the expected I2C WHO_AM_I register values for the accelerometer/magnetometer.

---
  tests/microbit-test.c | 44 +++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 44 insertions(+)
tests/microbit-test.c +14 -0
index 0c125535f6..dcdc0cd41a 100644
@@ -21,6 +21,49 @@
  #include "hw/arm/nrf51.h"
  #include "hw/gpio/nrf51_gpio.h"
  #include "hw/timer/nrf51_timer.h"
+#include "hw/i2c/microbit_i2c.h"
+
+/* Read a byte from I2C device at @addr from register @reg */
+static uint32_t i2c_read_byte(uint32_t addr, uint32_t reg)
+{
+    uint32_t val;
+
+    writel(NRF51_TWI_BASE + NRF51_TWI_REG_ADDRESS, addr);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STARTTX, 1);
+    writel(NRF51_TWI_BASE + NRF51_TWI_REG_TXD, reg);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_EVENT_TXDSENT);
+    g_assert_cmpuint(val, ==, 1);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STOP, 1);
+
Quoted 8 lines

tests/microbit-test.c line 42

Quoted 5 lines
tests/microbit-test.c +5 -0
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STARTRX, 1);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_EVENT_RXDREADY);
+    g_assert_cmpuint(val, ==, 1);
+    val = readl(NRF51_TWI_BASE + NRF51_TWI_REG_RXD);
+    writel(NRF51_TWI_BASE + NRF51_TWI_TASK_STOP, 1);

Any chance that you could use qemu_writel() right from the start here? That will make it easier to finally get rid of global_qtest one day...

I'll send a patch soon to fix it.
But this test can be merged without corrections.

Best regards, Julia Suvorova.

#7 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH v2 2/2] tests/microbit-test: add TWI stub device test

Quoted 7 lines · Julia Suvorova via Qemu-devel
Quoted 3 lines · Thomas Huth

On 2019-01-10 10:40, Stefan Hajnoczi wrote:
Any chance that you could use qemu_writel() right from the start here? That will make it easier to finally get rid of global_qtest one day...

I'll send a patch soon to fix it.
But this test can be merged without corrections.

Thank you, Julia!

Stefan

#8 lore raw
In reply to Stefan Hajnoczi
Quoted 18 lines · Stefan Hajnoczi

v2:
* Move stub code into a separate device [Peter]
* Instantiate stub from microbit board instead of nRF51 SoC since this is microbit-specific. Other boards using the nRF51 wouldn't necessarily want to see these TWI/I2C devices.
* Add test case

This series stubs out the microbit's TWI/I2C devices so the microbit-dal firmware boots successfully. The accelerometer and magnetometer are probed at startup. If they are not found the firmware panics.

Stefan Hajnoczi (1):
tests/microbit-test: add TWI stub device test

Steffen Görtz (1):
arm: Stub out NRF51 TWI magnetometer/accelerometer detection

Quoted 10 lines
 hw/i2c/Makefile.objs          |   1 +
 include/hw/arm/nrf51.h        |   2 +
 include/hw/arm/nrf51_soc.h    |   1 +
 include/hw/i2c/microbit_i2c.h |  42 +++++++++++
 hw/arm/microbit.c             |  15 ++++
 hw/i2c/microbit_i2c.c         | 127 ++++++++++++++++++++++++++++++++++
 tests/microbit-test.c         |  44 ++++++++++++
 7 files changed, 232 insertions(+)
 create mode 100644 include/hw/i2c/microbit_i2c.h
 create mode 100644 hw/i2c/microbit_i2c.c

Applied to target-arm.next, thanks. Can you provide a MAINTAINERS file update for the new files, please? (You can send that separately or stick it in another nrf51 series if you're sending one.)

thanks
-- PMM

#9 lore raw
Quoted 30 lines · Peter Maydell
Quoted 18 lines · Stefan Hajnoczi

v2:
* Move stub code into a separate device [Peter]
* Instantiate stub from microbit board instead of nRF51 SoC since this is microbit-specific. Other boards using the nRF51 wouldn't necessarily want to see these TWI/I2C devices.
* Add test case

This series stubs out the microbit's TWI/I2C devices so the microbit-dal firmware boots successfully. The accelerometer and magnetometer are probed at startup. If they are not found the firmware panics.

Stefan Hajnoczi (1):
tests/microbit-test: add TWI stub device test

Steffen Görtz (1):
arm: Stub out NRF51 TWI magnetometer/accelerometer detection

Quoted 10 lines
 hw/i2c/Makefile.objs          |   1 +
 include/hw/arm/nrf51.h        |   2 +
 include/hw/arm/nrf51_soc.h    |   1 +
 include/hw/i2c/microbit_i2c.h |  42 +++++++++++
 hw/arm/microbit.c             |  15 ++++
 hw/i2c/microbit_i2c.c         | 127 ++++++++++++++++++++++++++++++++++
 tests/microbit-test.c         |  44 ++++++++++++
 7 files changed, 232 insertions(+)
 create mode 100644 include/hw/i2c/microbit_i2c.h
 create mode 100644 hw/i2c/microbit_i2c.c
Quoted 4 lines

Applied to target-arm.next, thanks. Can you provide a MAINTAINERS file update for the new files, please? (You can send that separately or stick it in another nrf51 series if you're sending one.)

Sure, will do.

Stefan