2 * QTest testcase for Microbit board using the Nordic Semiconductor nRF51 SoC.
5 * Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf
6 * Product Spec: http://infocenter.nordicsemi.com/pdf/nRF51822_PS_v3.1.pdf
8 * Microbit Board: http://microbit.org/
10 * Copyright 2018 Steffen Görtz <contrib@steffen-goertz.de>
12 * This code is licensed under the GPL version 2 or later. See
13 * the COPYING file in the top-level directory.
17 #include "qemu/osdep.h"
18 #include "exec/hwaddr.h"
21 #include "hw/arm/nrf51.h"
22 #include "hw/gpio/nrf51_gpio.h"
23 #include "hw/timer/nrf51_timer.h"
24 #include "hw/i2c/microbit_i2c.h"
26 /* Read a byte from I2C device at @addr from register @reg */
27 static uint32_t i2c_read_byte(uint32_t addr
, uint32_t reg
)
31 writel(NRF51_TWI_BASE
+ NRF51_TWI_REG_ADDRESS
, addr
);
32 writel(NRF51_TWI_BASE
+ NRF51_TWI_TASK_STARTTX
, 1);
33 writel(NRF51_TWI_BASE
+ NRF51_TWI_REG_TXD
, reg
);
34 val
= readl(NRF51_TWI_BASE
+ NRF51_TWI_EVENT_TXDSENT
);
35 g_assert_cmpuint(val
, ==, 1);
36 writel(NRF51_TWI_BASE
+ NRF51_TWI_TASK_STOP
, 1);
38 writel(NRF51_TWI_BASE
+ NRF51_TWI_TASK_STARTRX
, 1);
39 val
= readl(NRF51_TWI_BASE
+ NRF51_TWI_EVENT_RXDREADY
);
40 g_assert_cmpuint(val
, ==, 1);
41 val
= readl(NRF51_TWI_BASE
+ NRF51_TWI_REG_RXD
);
42 writel(NRF51_TWI_BASE
+ NRF51_TWI_TASK_STOP
, 1);
47 static void test_microbit_i2c(void)
51 /* We don't program pins/irqs but at least enable the device */
52 writel(NRF51_TWI_BASE
+ NRF51_TWI_REG_ENABLE
, 5);
54 /* MMA8653 magnetometer detection */
55 val
= i2c_read_byte(0x3A, 0x0D);
56 g_assert_cmpuint(val
, ==, 0x5A);
58 val
= i2c_read_byte(0x3A, 0x0D);
59 g_assert_cmpuint(val
, ==, 0x5A);
61 /* LSM303 accelerometer detection */
62 val
= i2c_read_byte(0x3C, 0x4F);
63 g_assert_cmpuint(val
, ==, 0x40);
65 writel(NRF51_TWI_BASE
+ NRF51_TWI_REG_ENABLE
, 0);
68 static void test_nrf51_gpio(void)
71 uint32_t actual
, expected
;
76 } const reset_state
[] = {
77 {NRF51_GPIO_REG_OUT
, 0x00000000}, {NRF51_GPIO_REG_OUTSET
, 0x00000000},
78 {NRF51_GPIO_REG_OUTCLR
, 0x00000000}, {NRF51_GPIO_REG_IN
, 0x00000000},
79 {NRF51_GPIO_REG_DIR
, 0x00000000}, {NRF51_GPIO_REG_DIRSET
, 0x00000000},
80 {NRF51_GPIO_REG_DIRCLR
, 0x00000000}
83 /* Check reset state */
84 for (i
= 0; i
< ARRAY_SIZE(reset_state
); i
++) {
85 expected
= reset_state
[i
].expected
;
86 actual
= readl(NRF51_GPIO_BASE
+ reset_state
[i
].addr
);
87 g_assert_cmpuint(actual
, ==, expected
);
90 for (i
= 0; i
< NRF51_GPIO_PINS
; i
++) {
91 expected
= 0x00000002;
92 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
+ i
* 4);
93 g_assert_cmpuint(actual
, ==, expected
);
96 /* Check dir bit consistency between dir and cnf */
97 /* Check set via DIRSET */
98 expected
= 0x80000001;
99 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIRSET
, expected
);
100 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
);
101 g_assert_cmpuint(actual
, ==, expected
);
102 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
) & 0x01;
103 g_assert_cmpuint(actual
, ==, 0x01);
104 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_END
) & 0x01;
105 g_assert_cmpuint(actual
, ==, 0x01);
107 /* Check clear via DIRCLR */
108 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIRCLR
, 0x80000001);
109 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
);
110 g_assert_cmpuint(actual
, ==, 0x00000000);
111 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
) & 0x01;
112 g_assert_cmpuint(actual
, ==, 0x00);
113 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_END
) & 0x01;
114 g_assert_cmpuint(actual
, ==, 0x00);
116 /* Check set via DIR */
117 expected
= 0x80000001;
118 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
, expected
);
119 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
);
120 g_assert_cmpuint(actual
, ==, expected
);
121 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
) & 0x01;
122 g_assert_cmpuint(actual
, ==, 0x01);
123 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_END
) & 0x01;
124 g_assert_cmpuint(actual
, ==, 0x01);
127 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
, 0x00000000);
129 /* Check Input propagates */
130 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0x00);
131 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 0);
132 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
133 g_assert_cmpuint(actual
, ==, 0x00);
134 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 1);
135 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
136 g_assert_cmpuint(actual
, ==, 0x01);
137 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, -1);
138 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
139 g_assert_cmpuint(actual
, ==, 0x01);
140 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0x02);
142 /* Check pull-up working */
143 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 0);
144 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b0000);
145 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
146 g_assert_cmpuint(actual
, ==, 0x00);
147 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b1110);
148 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
149 g_assert_cmpuint(actual
, ==, 0x01);
150 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0x02);
152 /* Check pull-down working */
153 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 1);
154 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b0000);
155 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
156 g_assert_cmpuint(actual
, ==, 0x01);
157 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b0110);
158 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
159 g_assert_cmpuint(actual
, ==, 0x00);
160 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0x02);
161 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, -1);
163 /* Check Output propagates */
164 irq_intercept_out("/machine/nrf51");
165 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b0011);
166 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTSET
, 0x01);
167 g_assert_true(get_irq(0));
168 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTCLR
, 0x01);
169 g_assert_false(get_irq(0));
171 /* Check self-stimulation */
172 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b01);
173 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTSET
, 0x01);
174 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
175 g_assert_cmpuint(actual
, ==, 0x01);
177 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTCLR
, 0x01);
178 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
179 g_assert_cmpuint(actual
, ==, 0x00);
182 * Check short-circuit - generates an guest_error which must be checked
183 * manually as long as qtest can not scan qemu_log messages
185 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b01);
186 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTSET
, 0x01);
187 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 0);
190 static void timer_task(hwaddr task
)
192 writel(NRF51_TIMER_BASE
+ task
, NRF51_TRIGGER_TASK
);
195 static void timer_clear_event(hwaddr event
)
197 writel(NRF51_TIMER_BASE
+ event
, NRF51_EVENT_CLEAR
);
200 static void timer_set_bitmode(uint8_t mode
)
202 writel(NRF51_TIMER_BASE
+ NRF51_TIMER_REG_BITMODE
, mode
);
205 static void timer_set_prescaler(uint8_t prescaler
)
207 writel(NRF51_TIMER_BASE
+ NRF51_TIMER_REG_PRESCALER
, prescaler
);
210 static void timer_set_cc(size_t idx
, uint32_t value
)
212 writel(NRF51_TIMER_BASE
+ NRF51_TIMER_REG_CC0
+ idx
* 4, value
);
215 static void timer_assert_events(uint32_t ev0
, uint32_t ev1
, uint32_t ev2
,
218 g_assert(readl(NRF51_TIMER_BASE
+ NRF51_TIMER_EVENT_COMPARE_0
) == ev0
);
219 g_assert(readl(NRF51_TIMER_BASE
+ NRF51_TIMER_EVENT_COMPARE_1
) == ev1
);
220 g_assert(readl(NRF51_TIMER_BASE
+ NRF51_TIMER_EVENT_COMPARE_2
) == ev2
);
221 g_assert(readl(NRF51_TIMER_BASE
+ NRF51_TIMER_EVENT_COMPARE_3
) == ev3
);
224 static void test_nrf51_timer(void)
226 uint32_t steps_to_overflow
= 408;
229 timer_task(NRF51_TIMER_TASK_STOP
);
230 timer_task(NRF51_TIMER_TASK_CLEAR
);
232 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_0
);
233 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_1
);
234 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_2
);
235 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_3
);
237 timer_set_bitmode(NRF51_TIMER_WIDTH_16
); /* 16 MHz Timer */
238 timer_set_prescaler(0);
239 /* Swept over in first step */
241 /* Barely miss on first step */
242 timer_set_cc(1, 162);
243 /* Spot on on third step */
244 timer_set_cc(2, 480);
246 timer_assert_events(0, 0, 0, 0);
248 timer_task(NRF51_TIMER_TASK_START
);
250 timer_assert_events(1, 0, 0, 0);
252 /* Swept over on first overflow */
253 timer_set_cc(3, 114);
256 timer_assert_events(1, 1, 0, 0);
259 timer_assert_events(1, 1, 1, 0);
261 /* Wrap time until internal counter overflows */
262 while (steps_to_overflow
--) {
263 timer_assert_events(1, 1, 1, 0);
267 timer_assert_events(1, 1, 1, 1);
269 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_0
);
270 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_1
);
271 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_2
);
272 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_3
);
273 timer_assert_events(0, 0, 0, 0);
275 timer_task(NRF51_TIMER_TASK_STOP
);
277 /* Test Proposal: Stop/Shutdown */
278 /* Test Proposal: Shortcut Compare -> Clear */
279 /* Test Proposal: Shortcut Compare -> Stop */
280 /* Test Proposal: Counter Mode */
283 int main(int argc
, char **argv
)
287 g_test_init(&argc
, &argv
, NULL
);
289 global_qtest
= qtest_initf("-machine microbit");
291 qtest_add_func("/microbit/nrf51/gpio", test_nrf51_gpio
);
292 qtest_add_func("/microbit/nrf51/timer", test_nrf51_timer
);
293 qtest_add_func("/microbit/microbit/i2c", test_microbit_i2c
);
297 qtest_quit(global_qtest
);