1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*
* QTest testcase for STML4X5_USART
*
* Copyright (c) 2023 Arnaud Minier <arnaud.minier@telecom-paris.fr>
* Copyright (c) 2023 Inès Varhol <ines.varhol@telecom-paris.fr>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "libqtest.h"
#include "hw/misc/stm32l4x5_rcc_internals.h"
#include "hw/registerfields.h"
#include "stm32l4x5.h"
#define RCC_BASE_ADDR 0x40021000
/* Use USART 1 ADDR, assume the others work the same */
#define USART1_BASE_ADDR 0x40013800
/* See stm32l4x5_usart for definitions */
REG32(CR1, 0x00)
FIELD(CR1, M1, 28, 1)
FIELD(CR1, OVER8, 15, 1)
FIELD(CR1, M0, 12, 1)
FIELD(CR1, PCE, 10, 1)
FIELD(CR1, TXEIE, 7, 1)
FIELD(CR1, RXNEIE, 5, 1)
FIELD(CR1, TE, 3, 1)
FIELD(CR1, RE, 2, 1)
FIELD(CR1, UE, 0, 1)
REG32(CR2, 0x04)
REG32(CR3, 0x08)
FIELD(CR3, OVRDIS, 12, 1)
REG32(BRR, 0x0C)
REG32(GTPR, 0x10)
REG32(RTOR, 0x14)
REG32(RQR, 0x18)
REG32(ISR, 0x1C)
FIELD(ISR, REACK, 22, 1)
FIELD(ISR, TEACK, 21, 1)
FIELD(ISR, TXE, 7, 1)
FIELD(ISR, RXNE, 5, 1)
FIELD(ISR, ORE, 3, 1)
REG32(ICR, 0x20)
REG32(RDR, 0x24)
REG32(TDR, 0x28)
#define NVIC_ISPR1 0XE000E204
#define NVIC_ICPR1 0xE000E284
#define USART1_IRQ 37
static bool check_nvic_pending(QTestState *qts, unsigned int n)
{
/* No USART interrupts are less than 32 */
assert(n > 32);
n -= 32;
return qtest_readl(qts, NVIC_ISPR1) & (1 << n);
}
static bool clear_nvic_pending(QTestState *qts, unsigned int n)
{
/* No USART interrupts are less than 32 */
assert(n > 32);
n -= 32;
qtest_writel(qts, NVIC_ICPR1, (1 << n));
return true;
}
/*
* Wait indefinitely for the flag to be updated.
* If this is run on a slow CI runner,
* the meson harness will timeout after 10 minutes for us.
*/
static bool usart_wait_for_flag(QTestState *qts, uint32_t event_addr,
uint32_t flag)
{
while (true) {
if ((qtest_readl(qts, event_addr) & flag)) {
return true;
}
g_usleep(1000);
}
return false;
}
static void usart_receive_string(QTestState *qts, int sock_fd, const char *in,
char *out)
{
int i, in_len = strlen(in);
g_assert_true(send(sock_fd, in, in_len, 0) == in_len);
for (i = 0; i < in_len; i++) {
g_assert_true(usart_wait_for_flag(qts,
USART1_BASE_ADDR + A_ISR, R_ISR_RXNE_MASK));
out[i] = qtest_readl(qts, USART1_BASE_ADDR + A_RDR);
}
out[i] = '\0';
}
static void usart_send_string(QTestState *qts, const char *in)
{
int i, in_len = strlen(in);
for (i = 0; i < in_len; i++) {
qtest_writel(qts, USART1_BASE_ADDR + A_TDR, in[i]);
g_assert_true(usart_wait_for_flag(qts,
USART1_BASE_ADDR + A_ISR, R_ISR_TXE_MASK));
}
}
/* Init the RCC clocks to run at 80 MHz */
static void init_clocks(QTestState *qts)
{
uint32_t value;
/* MSIRANGE can be set only when MSI is OFF or READY */
qtest_writel(qts, (RCC_BASE_ADDR + A_CR), R_CR_MSION_MASK);
/* Clocking from MSI, in case MSI was not the default source */
qtest_writel(qts, (RCC_BASE_ADDR + A_CFGR), 0);
/*
* Update PLL and set MSI as the source clock.
* PLLM = 1 --> 000
* PLLN = 40 --> 40
* PPLLR = 2 --> 00
* PLLDIV = unused, PLLP = unused (SAI3), PLLQ = unused (48M1)
* SRC = MSI --> 01
*/
qtest_writel(qts, (RCC_BASE_ADDR + A_PLLCFGR), R_PLLCFGR_PLLREN_MASK |
(40 << R_PLLCFGR_PLLN_SHIFT) |
(0b01 << R_PLLCFGR_PLLSRC_SHIFT));
/* PLL activation */
value = qtest_readl(qts, (RCC_BASE_ADDR + A_CR));
qtest_writel(qts, (RCC_BASE_ADDR + A_CR), value | R_CR_PLLON_MASK);
/* RCC_CFGR is OK by defaut */
qtest_writel(qts, (RCC_BASE_ADDR + A_CFGR), 0);
/* CCIPR : no periph clock by default */
qtest_writel(qts, (RCC_BASE_ADDR + A_CCIPR), 0);
/* Switches on the PLL clock source */
value = qtest_readl(qts, (RCC_BASE_ADDR + A_CFGR));
qtest_writel(qts, (RCC_BASE_ADDR + A_CFGR), (value & ~R_CFGR_SW_MASK) |
(0b11 << R_CFGR_SW_SHIFT));
/* Enable SYSCFG clock enabled */
qtest_writel(qts, (RCC_BASE_ADDR + A_APB2ENR), R_APB2ENR_SYSCFGEN_MASK);
/* Enable the IO port B clock (See p.252) */
qtest_writel(qts, (RCC_BASE_ADDR + A_AHB2ENR), R_AHB2ENR_GPIOBEN_MASK);
/* Enable the clock for USART1 (cf p.259) */
/* We rewrite SYSCFGEN to not disable it */
qtest_writel(qts, (RCC_BASE_ADDR + A_APB2ENR),
R_APB2ENR_SYSCFGEN_MASK | R_APB2ENR_USART1EN_MASK);
/* TODO: Enable usart via gpio */
/* Set PCLK as the clock for USART1(cf p.272) i.e. reset both bits */
qtest_writel(qts, (RCC_BASE_ADDR + A_CCIPR), 0);
/* Reset USART1 (see p.249) */
qtest_writel(qts, (RCC_BASE_ADDR + A_APB2RSTR), 1 << 14);
qtest_writel(qts, (RCC_BASE_ADDR + A_APB2RSTR), 0);
}
static void init_uart(QTestState *qts)
{
uint32_t cr1;
init_clocks(qts);
/*
* For 115200 bauds, see p.1349.
* The clock has a frequency of 80Mhz,
* for 115200, we have to put a divider of 695 = 0x2B7.
*/
qtest_writel(qts, (USART1_BASE_ADDR + A_BRR), 0x2B7);
/*
* Set the oversampling by 16,
* disable the parity control and
* set the word length to 8. (cf p.1377)
*/
cr1 = qtest_readl(qts, (USART1_BASE_ADDR + A_CR1));
cr1 &= ~(R_CR1_M1_MASK | R_CR1_M0_MASK | R_CR1_OVER8_MASK | R_CR1_PCE_MASK);
qtest_writel(qts, (USART1_BASE_ADDR + A_CR1), cr1);
/* Enable the transmitter, the receiver and the USART. */
qtest_writel(qts, (USART1_BASE_ADDR + A_CR1),
cr1 | R_CR1_UE_MASK | R_CR1_RE_MASK | R_CR1_TE_MASK);
}
static void test_write_read(void)
{
QTestState *qts = qtest_init("-M b-l475e-iot01a");
/* Test that we can write and retrieve a value from the device */
qtest_writel(qts, USART1_BASE_ADDR + A_TDR, 0xFFFFFFFF);
const uint32_t tdr = qtest_readl(qts, USART1_BASE_ADDR + A_TDR);
g_assert_cmpuint(tdr, ==, 0x000001FF);
qtest_quit(qts);
}
static void test_receive_char(void)
{
int sock_fd;
uint32_t cr1;
QTestState *qts = qtest_init_with_serial("-M b-l475e-iot01a", &sock_fd);
init_uart(qts);
/* Try without initializing IRQ */
g_assert_true(send(sock_fd, "a", 1, 0) == 1);
usart_wait_for_flag(qts, USART1_BASE_ADDR + A_ISR, R_ISR_RXNE_MASK);
g_assert_cmphex(qtest_readl(qts, USART1_BASE_ADDR + A_RDR), ==, 'a');
g_assert_false(check_nvic_pending(qts, USART1_IRQ));
/* Now with the IRQ */
cr1 = qtest_readl(qts, (USART1_BASE_ADDR + A_CR1));
cr1 |= R_CR1_RXNEIE_MASK;
qtest_writel(qts, USART1_BASE_ADDR + A_CR1, cr1);
g_assert_true(send(sock_fd, "b", 1, 0) == 1);
usart_wait_for_flag(qts, USART1_BASE_ADDR + A_ISR, R_ISR_RXNE_MASK);
g_assert_cmphex(qtest_readl(qts, USART1_BASE_ADDR + A_RDR), ==, 'b');
g_assert_true(check_nvic_pending(qts, USART1_IRQ));
clear_nvic_pending(qts, USART1_IRQ);
close(sock_fd);
qtest_quit(qts);
}
static void test_send_char(void)
{
int sock_fd;
char s[1];
uint32_t cr1;
QTestState *qts = qtest_init_with_serial("-M b-l475e-iot01a", &sock_fd);
init_uart(qts);
/* Try without initializing IRQ */
qtest_writel(qts, USART1_BASE_ADDR + A_TDR, 'c');
g_assert_true(recv(sock_fd, s, 1, 0) == 1);
g_assert_cmphex(s[0], ==, 'c');
g_assert_false(check_nvic_pending(qts, USART1_IRQ));
/* Now with the IRQ */
cr1 = qtest_readl(qts, (USART1_BASE_ADDR + A_CR1));
cr1 |= R_CR1_TXEIE_MASK;
qtest_writel(qts, USART1_BASE_ADDR + A_CR1, cr1);
qtest_writel(qts, USART1_BASE_ADDR + A_TDR, 'd');
g_assert_true(recv(sock_fd, s, 1, 0) == 1);
g_assert_cmphex(s[0], ==, 'd');
g_assert_true(check_nvic_pending(qts, USART1_IRQ));
clear_nvic_pending(qts, USART1_IRQ);
close(sock_fd);
qtest_quit(qts);
}
static void test_receive_str(void)
{
int sock_fd;
char s[10];
QTestState *qts = qtest_init_with_serial("-M b-l475e-iot01a", &sock_fd);
init_uart(qts);
usart_receive_string(qts, sock_fd, "hello", s);
g_assert_true(memcmp(s, "hello", 5) == 0);
close(sock_fd);
qtest_quit(qts);
}
static void test_send_str(void)
{
int sock_fd;
char s[10];
QTestState *qts = qtest_init_with_serial("-M b-l475e-iot01a", &sock_fd);
init_uart(qts);
usart_send_string(qts, "world");
g_assert_true(recv(sock_fd, s, 10, 0) == 5);
g_assert_true(memcmp(s, "world", 5) == 0);
close(sock_fd);
qtest_quit(qts);
}
static void test_ack(void)
{
uint32_t cr1;
uint32_t isr;
QTestState *qts = qtest_init("-M b-l475e-iot01a");
init_uart(qts);
cr1 = qtest_readl(qts, (USART1_BASE_ADDR + A_CR1));
/* Disable the transmitter and receiver. */
qtest_writel(qts, (USART1_BASE_ADDR + A_CR1),
cr1 & ~(R_CR1_RE_MASK | R_CR1_TE_MASK));
/* Test ISR ACK for transmitter and receiver disabled */
isr = qtest_readl(qts, (USART1_BASE_ADDR + A_ISR));
g_assert_false(isr & R_ISR_TEACK_MASK);
g_assert_false(isr & R_ISR_REACK_MASK);
/* Enable the transmitter and receiver. */
qtest_writel(qts, (USART1_BASE_ADDR + A_CR1),
cr1 | (R_CR1_RE_MASK | R_CR1_TE_MASK));
/* Test ISR ACK for transmitter and receiver disabled */
isr = qtest_readl(qts, (USART1_BASE_ADDR + A_ISR));
g_assert_true(isr & R_ISR_TEACK_MASK);
g_assert_true(isr & R_ISR_REACK_MASK);
qtest_quit(qts);
}
static void check_clock(QTestState *qts, const char *path, uint32_t rcc_reg,
uint32_t reg_offset)
{
g_assert_cmpuint(get_clock_period(qts, path), ==, 0);
qtest_writel(qts, rcc_reg, qtest_readl(qts, rcc_reg) | (0x1 << reg_offset));
g_assert_cmpuint(get_clock_period(qts, path), ==, SYSCLK_PERIOD);
}
static void test_clock_enable(void)
{
/*
* For each USART device, enable its clock in RCC
* and check that its clock frequency is SYSCLK_PERIOD
*/
QTestState *qts = qtest_init("-M b-l475e-iot01a");
check_clock(qts, "machine/soc/usart[0]/clk", RCC_APB2ENR, 14);
check_clock(qts, "machine/soc/usart[1]/clk", RCC_APB1ENR1, 17);
check_clock(qts, "machine/soc/usart[2]/clk", RCC_APB1ENR1, 18);
check_clock(qts, "machine/soc/uart[0]/clk", RCC_APB1ENR1, 19);
check_clock(qts, "machine/soc/uart[1]/clk", RCC_APB1ENR1, 20);
check_clock(qts, "machine/soc/lpuart1/clk", RCC_APB1ENR2, 0);
qtest_quit(qts);
}
int main(int argc, char **argv)
{
int ret;
g_test_init(&argc, &argv, NULL);
g_test_set_nonfatal_assertions();
qtest_add_func("stm32l4x5/usart/write_read", test_write_read);
qtest_add_func("stm32l4x5/usart/receive_char", test_receive_char);
qtest_add_func("stm32l4x5/usart/send_char", test_send_char);
qtest_add_func("stm32l4x5/usart/receive_str", test_receive_str);
qtest_add_func("stm32l4x5/usart/send_str", test_send_str);
qtest_add_func("stm32l4x5/usart/ack", test_ack);
qtest_add_func("stm32l4x5/usart/clock_enable", test_clock_enable);
ret = g_test_run();
return ret;
}
|