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"
25 static void test_nrf51_gpio(void)
28 uint32_t actual
, expected
;
33 } const reset_state
[] = {
34 {NRF51_GPIO_REG_OUT
, 0x00000000}, {NRF51_GPIO_REG_OUTSET
, 0x00000000},
35 {NRF51_GPIO_REG_OUTCLR
, 0x00000000}, {NRF51_GPIO_REG_IN
, 0x00000000},
36 {NRF51_GPIO_REG_DIR
, 0x00000000}, {NRF51_GPIO_REG_DIRSET
, 0x00000000},
37 {NRF51_GPIO_REG_DIRCLR
, 0x00000000}
40 /* Check reset state */
41 for (i
= 0; i
< ARRAY_SIZE(reset_state
); i
++) {
42 expected
= reset_state
[i
].expected
;
43 actual
= readl(NRF51_GPIO_BASE
+ reset_state
[i
].addr
);
44 g_assert_cmpuint(actual
, ==, expected
);
47 for (i
= 0; i
< NRF51_GPIO_PINS
; i
++) {
48 expected
= 0x00000002;
49 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
+ i
* 4);
50 g_assert_cmpuint(actual
, ==, expected
);
53 /* Check dir bit consistency between dir and cnf */
54 /* Check set via DIRSET */
55 expected
= 0x80000001;
56 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIRSET
, expected
);
57 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
);
58 g_assert_cmpuint(actual
, ==, expected
);
59 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
) & 0x01;
60 g_assert_cmpuint(actual
, ==, 0x01);
61 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_END
) & 0x01;
62 g_assert_cmpuint(actual
, ==, 0x01);
64 /* Check clear via DIRCLR */
65 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIRCLR
, 0x80000001);
66 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
);
67 g_assert_cmpuint(actual
, ==, 0x00000000);
68 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
) & 0x01;
69 g_assert_cmpuint(actual
, ==, 0x00);
70 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_END
) & 0x01;
71 g_assert_cmpuint(actual
, ==, 0x00);
73 /* Check set via DIR */
74 expected
= 0x80000001;
75 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
, expected
);
76 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
);
77 g_assert_cmpuint(actual
, ==, expected
);
78 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
) & 0x01;
79 g_assert_cmpuint(actual
, ==, 0x01);
80 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_END
) & 0x01;
81 g_assert_cmpuint(actual
, ==, 0x01);
84 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_DIR
, 0x00000000);
86 /* Check Input propagates */
87 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0x00);
88 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 0);
89 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
90 g_assert_cmpuint(actual
, ==, 0x00);
91 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 1);
92 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
93 g_assert_cmpuint(actual
, ==, 0x01);
94 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, -1);
95 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
96 g_assert_cmpuint(actual
, ==, 0x01);
97 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0x02);
99 /* Check pull-up working */
100 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 0);
101 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b0000);
102 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
103 g_assert_cmpuint(actual
, ==, 0x00);
104 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b1110);
105 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
106 g_assert_cmpuint(actual
, ==, 0x01);
107 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0x02);
109 /* Check pull-down working */
110 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 1);
111 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b0000);
112 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
113 g_assert_cmpuint(actual
, ==, 0x01);
114 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b0110);
115 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
116 g_assert_cmpuint(actual
, ==, 0x00);
117 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0x02);
118 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, -1);
120 /* Check Output propagates */
121 irq_intercept_out("/machine/nrf51");
122 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b0011);
123 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTSET
, 0x01);
124 g_assert_true(get_irq(0));
125 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTCLR
, 0x01);
126 g_assert_false(get_irq(0));
128 /* Check self-stimulation */
129 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b01);
130 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTSET
, 0x01);
131 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
132 g_assert_cmpuint(actual
, ==, 0x01);
134 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTCLR
, 0x01);
135 actual
= readl(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_IN
) & 0x01;
136 g_assert_cmpuint(actual
, ==, 0x00);
139 * Check short-circuit - generates an guest_error which must be checked
140 * manually as long as qtest can not scan qemu_log messages
142 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_CNF_START
, 0b01);
143 writel(NRF51_GPIO_BASE
+ NRF51_GPIO_REG_OUTSET
, 0x01);
144 qtest_set_irq_in(global_qtest
, "/machine/nrf51", "unnamed-gpio-in", 0, 0);
147 static void timer_task(hwaddr task
)
149 writel(NRF51_TIMER_BASE
+ task
, NRF51_TRIGGER_TASK
);
152 static void timer_clear_event(hwaddr event
)
154 writel(NRF51_TIMER_BASE
+ event
, NRF51_EVENT_CLEAR
);
157 static void timer_set_bitmode(uint8_t mode
)
159 writel(NRF51_TIMER_BASE
+ NRF51_TIMER_REG_BITMODE
, mode
);
162 static void timer_set_prescaler(uint8_t prescaler
)
164 writel(NRF51_TIMER_BASE
+ NRF51_TIMER_REG_PRESCALER
, prescaler
);
167 static void timer_set_cc(size_t idx
, uint32_t value
)
169 writel(NRF51_TIMER_BASE
+ NRF51_TIMER_REG_CC0
+ idx
* 4, value
);
172 static void timer_assert_events(uint32_t ev0
, uint32_t ev1
, uint32_t ev2
,
175 g_assert(readl(NRF51_TIMER_BASE
+ NRF51_TIMER_EVENT_COMPARE_0
) == ev0
);
176 g_assert(readl(NRF51_TIMER_BASE
+ NRF51_TIMER_EVENT_COMPARE_1
) == ev1
);
177 g_assert(readl(NRF51_TIMER_BASE
+ NRF51_TIMER_EVENT_COMPARE_2
) == ev2
);
178 g_assert(readl(NRF51_TIMER_BASE
+ NRF51_TIMER_EVENT_COMPARE_3
) == ev3
);
181 static void test_nrf51_timer(void)
183 uint32_t steps_to_overflow
= 408;
186 timer_task(NRF51_TIMER_TASK_STOP
);
187 timer_task(NRF51_TIMER_TASK_CLEAR
);
189 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_0
);
190 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_1
);
191 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_2
);
192 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_3
);
194 timer_set_bitmode(NRF51_TIMER_WIDTH_16
); /* 16 MHz Timer */
195 timer_set_prescaler(0);
196 /* Swept over in first step */
198 /* Barely miss on first step */
199 timer_set_cc(1, 162);
200 /* Spot on on third step */
201 timer_set_cc(2, 480);
203 timer_assert_events(0, 0, 0, 0);
205 timer_task(NRF51_TIMER_TASK_START
);
207 timer_assert_events(1, 0, 0, 0);
209 /* Swept over on first overflow */
210 timer_set_cc(3, 114);
213 timer_assert_events(1, 1, 0, 0);
216 timer_assert_events(1, 1, 1, 0);
218 /* Wrap time until internal counter overflows */
219 while (steps_to_overflow
--) {
220 timer_assert_events(1, 1, 1, 0);
224 timer_assert_events(1, 1, 1, 1);
226 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_0
);
227 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_1
);
228 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_2
);
229 timer_clear_event(NRF51_TIMER_EVENT_COMPARE_3
);
230 timer_assert_events(0, 0, 0, 0);
232 timer_task(NRF51_TIMER_TASK_STOP
);
234 /* Test Proposal: Stop/Shutdown */
235 /* Test Proposal: Shortcut Compare -> Clear */
236 /* Test Proposal: Shortcut Compare -> Stop */
237 /* Test Proposal: Counter Mode */
240 int main(int argc
, char **argv
)
244 g_test_init(&argc
, &argv
, NULL
);
246 global_qtest
= qtest_initf("-machine microbit");
248 qtest_add_func("/microbit/nrf51/gpio", test_nrf51_gpio
);
249 qtest_add_func("/microbit/nrf51/timer", test_nrf51_timer
);
253 qtest_quit(global_qtest
);