1 /*
2 * Copyright (c) 2019 Nordic Semiconductor ASA
3 * Copyright (c) 2024 STMicroelectronics
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include "test_uart.h"
9
10 #if defined(CONFIG_DCACHE) && defined(CONFIG_DT_DEFINED_NOCACHE)
11 #define __NOCACHE __attribute__ ((__section__(CONFIG_DT_DEFINED_NOCACHE_NAME)))
12 #define NOCACHE_MEM 1
13 #elif defined(CONFIG_DCACHE) && defined(CONFIG_NOCACHE_MEMORY)
14 #define __NOCACHE __nocache
15 #define NOCACHE_MEM 1
16 #else
17 #define NOCACHE_MEM 0
18 #endif /* CONFIG_NOCACHE_MEMORY */
19
20 K_SEM_DEFINE(tx_done, 0, 1);
21 K_SEM_DEFINE(tx_aborted, 0, 1);
22 K_SEM_DEFINE(rx_rdy, 0, 1);
23 K_SEM_DEFINE(rx_buf_coherency, 0, 255);
24 K_SEM_DEFINE(rx_buf_released, 0, 1);
25 K_SEM_DEFINE(rx_disabled, 0, 1);
26
27 static ZTEST_BMEM volatile bool failed_in_isr;
28
29 struct dut_data {
30 const struct device *dev;
31 const char *name;
32 };
33
34 ZTEST_DMEM struct dut_data duts[] = {
35 {
36 .dev = DEVICE_DT_GET(UART_NODE),
37 .name = DT_NODE_FULL_NAME(UART_NODE),
38 },
39 #if DT_NODE_EXISTS(DT_NODELABEL(dut2)) && DT_NODE_HAS_STATUS(DT_NODELABEL(dut2), okay)
40 {
41 .dev = DEVICE_DT_GET(DT_NODELABEL(dut2)),
42 .name = DT_NODE_FULL_NAME(DT_NODELABEL(dut2)),
43 },
44 #endif
45 };
46
47 static ZTEST_BMEM const struct device *uart_dev;
48 static ZTEST_BMEM const char *uart_name;
49
50 static void read_abort_timeout(struct k_timer *timer);
51 static K_TIMER_DEFINE(read_abort_timer, read_abort_timeout, NULL);
52
53
54 #ifdef CONFIG_USERSPACE
set_permissions(void)55 static void set_permissions(void)
56 {
57 k_thread_access_grant(k_current_get(), &tx_done, &tx_aborted,
58 &rx_rdy, &rx_buf_coherency, &rx_buf_released,
59 &rx_disabled, uart_dev, &read_abort_timer);
60
61 for (size_t i = 0; i < ARRAY_SIZE(duts); i++) {
62 k_thread_access_grant(k_current_get(), duts[i].dev);
63 }
64 }
65 #endif
66
uart_async_test_init(int idx)67 static void uart_async_test_init(int idx)
68 {
69 static bool initialized;
70
71 uart_dev = duts[idx].dev;
72 uart_name = duts[idx].name;
73
74 __ASSERT_NO_MSG(device_is_ready(uart_dev));
75 TC_PRINT("UART instance:%s\n", uart_name);
76 uart_rx_disable(uart_dev);
77 uart_tx_abort(uart_dev);
78 k_sem_reset(&tx_done);
79 k_sem_reset(&tx_aborted);
80 k_sem_reset(&rx_rdy);
81 k_sem_reset(&rx_buf_coherency);
82 k_sem_reset(&rx_buf_released);
83 k_sem_reset(&rx_disabled);
84
85 #ifdef CONFIG_UART_WIDE_DATA
86 const struct uart_config uart_cfg = {
87 .baudrate = 115200,
88 .parity = UART_CFG_PARITY_NONE,
89 .stop_bits = UART_CFG_STOP_BITS_1,
90 .data_bits = UART_CFG_DATA_BITS_9,
91 .flow_ctrl = UART_CFG_FLOW_CTRL_NONE
92 };
93 __ASSERT_NO_MSG(uart_configure(uart_dev, &uart_cfg) == 0);
94 #endif
95
96 if (!initialized) {
97 initialized = true;
98 #ifdef CONFIG_USERSPACE
99 set_permissions();
100 #endif
101 }
102
103 }
104
105 struct test_data {
106 volatile uint32_t tx_aborted_count;
107 __aligned(32) uint8_t rx_first_buffer[10];
108 uint32_t recv_bytes_first_buffer;
109 __aligned(32) uint8_t rx_second_buffer[5];
110 uint32_t recv_bytes_second_buffer;
111 bool supply_second_buffer;
112 };
113
114 #if NOCACHE_MEM
115 static struct test_data tdata __used __NOCACHE;
116 #else
117 static ZTEST_BMEM struct test_data tdata;
118 #endif /* NOCACHE_MEM */
119
test_single_read_callback(const struct device * dev,struct uart_event * evt,void * user_data)120 static void test_single_read_callback(const struct device *dev,
121 struct uart_event *evt, void *user_data)
122 {
123 ARG_UNUSED(dev);
124 struct test_data *data = (struct test_data *)user_data;
125
126 switch (evt->type) {
127 case UART_TX_DONE:
128 k_sem_give(&tx_done);
129 break;
130 case UART_TX_ABORTED:
131 data->tx_aborted_count++;
132 break;
133 case UART_RX_RDY:
134 if ((uintptr_t)evt->data.rx.buf < (uintptr_t)tdata.rx_second_buffer) {
135 data->recv_bytes_first_buffer += evt->data.rx.len;
136 } else {
137 data->recv_bytes_second_buffer += evt->data.rx.len;
138 }
139 k_sem_give(&rx_rdy);
140 break;
141 case UART_RX_BUF_RELEASED:
142 k_sem_give(&rx_buf_released);
143 break;
144 case UART_RX_BUF_REQUEST:
145 if (data->supply_second_buffer) {
146 /* Reply to one buffer request. */
147 uart_rx_buf_rsp(dev, data->rx_second_buffer,
148 sizeof(data->rx_second_buffer));
149 data->supply_second_buffer = false;
150 }
151 break;
152 case UART_RX_DISABLED:
153 k_sem_give(&rx_disabled);
154 break;
155 default:
156 break;
157 }
158 }
159
160 static ZTEST_BMEM volatile uint32_t tx_aborted_count;
161
single_read_setup(void)162 static void *single_read_setup(void)
163 {
164 static int idx;
165
166 uart_async_test_init(idx++);
167
168 memset(&tdata, 0, sizeof(tdata));
169 tdata.supply_second_buffer = true;
170 uart_callback_set(uart_dev,
171 test_single_read_callback,
172 (void *) &tdata);
173
174 return NULL;
175 }
176
tdata_check_recv_buffers(const uint8_t * tx_buf,uint32_t sent_bytes)177 static void tdata_check_recv_buffers(const uint8_t *tx_buf, uint32_t sent_bytes)
178 {
179 uint32_t recv_bytes_total;
180
181 recv_bytes_total = tdata.recv_bytes_first_buffer + tdata.recv_bytes_second_buffer;
182 zassert_equal(recv_bytes_total, sent_bytes, "Incorrect number of bytes received");
183
184 zassert_equal(memcmp(tx_buf, tdata.rx_first_buffer, tdata.recv_bytes_first_buffer), 0,
185 "Invalid data received in first buffer");
186 zassert_equal(memcmp(tx_buf + tdata.recv_bytes_first_buffer, tdata.rx_second_buffer,
187 tdata.recv_bytes_second_buffer),
188 0, "Invalid data received in second buffer");
189
190 /* check that the remaining bytes in the buffers are zero */
191 for (int i = tdata.recv_bytes_first_buffer; i < sizeof(tdata.rx_first_buffer); i++) {
192 zassert_equal(tdata.rx_first_buffer[i], 0,
193 "Received extra data to the first buffer");
194 }
195
196 for (int i = tdata.recv_bytes_second_buffer; i < sizeof(tdata.rx_second_buffer); i++) {
197 zassert_equal(tdata.rx_second_buffer[i], 0,
198 "Received extra data to the second buffer");
199 }
200 }
201
ZTEST_USER(uart_async_single_read,test_single_read)202 ZTEST_USER(uart_async_single_read, test_single_read)
203 {
204 /* Check also if sending from read only memory (e.g. flash) works. */
205 static const uint8_t tx_buf[] = "0123456789";
206 uint32_t sent_bytes = 0;
207
208 zassert_not_equal(memcmp(tx_buf, tdata.rx_first_buffer, 5), 0,
209 "Initial buffer check failed");
210
211 uart_rx_enable(uart_dev, tdata.rx_first_buffer, 10, 50 * USEC_PER_MSEC);
212 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
213 "RX_RDY not expected at this point");
214
215 uart_tx(uart_dev, tx_buf, 5, 100 * USEC_PER_MSEC);
216 sent_bytes += 5;
217
218 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
219 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
220 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
221 "Extra RX_RDY received");
222
223 tdata_check_recv_buffers(tx_buf, sent_bytes);
224
225 uart_tx(uart_dev, tx_buf + sent_bytes, 5, 100 * USEC_PER_MSEC);
226 sent_bytes += 5;
227
228 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
229 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
230 zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
231 0,
232 "RX_BUF_RELEASED timeout");
233 uart_rx_disable(uart_dev);
234
235 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(1000)), 0,
236 "RX_DISABLED timeout");
237 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
238 "Extra RX_RDY received");
239
240 tdata_check_recv_buffers(tx_buf, sent_bytes);
241
242 zassert_equal(tdata.tx_aborted_count, 0, "TX aborted triggered");
243 }
244
multiple_rx_enable_setup(void)245 static void *multiple_rx_enable_setup(void)
246 {
247 static int idx;
248
249 uart_async_test_init(idx++);
250
251 memset(&tdata, 0, sizeof(tdata));
252 /* Reuse the callback from the single_read test case, as this test case
253 * does not need anything extra in this regard.
254 */
255 uart_callback_set(uart_dev,
256 test_single_read_callback,
257 (void *)&tdata);
258
259 return NULL;
260 }
261
ZTEST_USER(uart_async_multi_rx,test_multiple_rx_enable)262 ZTEST_USER(uart_async_multi_rx, test_multiple_rx_enable)
263 {
264 /* Check also if sending from read only memory (e.g. flash) works. */
265 static const uint8_t tx_buf[] = "test";
266 const uint32_t rx_buf_size = sizeof(tx_buf);
267 int ret;
268
269 BUILD_ASSERT(sizeof(tx_buf) <= sizeof(tdata.rx_first_buffer), "Invalid buf size");
270
271 /* Enable RX without a timeout. */
272 ret = uart_rx_enable(uart_dev, tdata.rx_first_buffer, rx_buf_size, SYS_FOREVER_US);
273 zassert_equal(ret, 0, "uart_rx_enable failed");
274 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
275 "RX_RDY not expected at this point");
276 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), -EAGAIN,
277 "RX_DISABLED not expected at this point");
278
279 /* Disable RX before any data has been received. */
280 ret = uart_rx_disable(uart_dev);
281 zassert_equal(ret, 0, "uart_rx_disable failed");
282 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
283 "RX_RDY not expected at this point");
284 zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)), 0,
285 "RX_BUF_RELEASED timeout");
286 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
287 "RX_DISABLED timeout");
288
289 k_sem_reset(&rx_buf_released);
290 k_sem_reset(&rx_disabled);
291
292 /* Check that RX can be reenabled after "manual" disabling. */
293 ret = uart_rx_enable(uart_dev, tdata.rx_first_buffer, rx_buf_size,
294 50 * USEC_PER_MSEC);
295 zassert_equal(ret, 0, "uart_rx_enable failed");
296 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
297 "RX_RDY not expected at this point");
298
299 /* Send enough data to completely fill RX buffer, so that RX ends. */
300 ret = uart_tx(uart_dev, tx_buf, sizeof(tx_buf), 100 * USEC_PER_MSEC);
301 zassert_equal(ret, 0, "uart_tx failed");
302 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
303 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
304 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
305 "Extra RX_RDY received");
306 zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)), 0,
307 "RX_BUF_RELEASED timeout");
308 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
309 "RX_DISABLED timeout");
310 zassert_equal(tx_aborted_count, 0, "Unexpected TX abort");
311
312 tdata_check_recv_buffers(tx_buf, sizeof(tx_buf));
313
314 k_sem_reset(&rx_rdy);
315 k_sem_reset(&rx_buf_released);
316 k_sem_reset(&rx_disabled);
317 k_sem_reset(&tx_done);
318
319 memset(&tdata, 0, sizeof(tdata));
320
321 /* Check that RX can be reenabled after automatic disabling. */
322 ret = uart_rx_enable(uart_dev, tdata.rx_first_buffer, rx_buf_size,
323 50 * USEC_PER_MSEC);
324 zassert_equal(ret, 0, "uart_rx_enable failed");
325 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
326 "RX_RDY not expected at this point");
327
328 /* Fill RX buffer again to confirm that RX still works properly. */
329 ret = uart_tx(uart_dev, tx_buf, sizeof(tx_buf), 100 * USEC_PER_MSEC);
330 zassert_equal(ret, 0, "uart_tx failed");
331 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
332 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
333 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
334 "Extra RX_RDY received");
335 zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)), 0,
336 "RX_BUF_RELEASED timeout");
337 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
338 "RX_DISABLED timeout");
339 zassert_equal(tx_aborted_count, 0, "Unexpected TX abort");
340
341 tdata_check_recv_buffers(tx_buf, sizeof(tx_buf));
342 }
343
344 #if NOCACHE_MEM
345 /* To ensure 32-bit alignment of the buffer array,
346 * the two arrays are defined instead using an array of arrays
347 */
348 static __aligned(32) uint8_t chained_read_buf_0[8] __used __NOCACHE;
349 static __aligned(32) uint8_t chained_read_buf_1[8] __used __NOCACHE;
350 static __aligned(32) uint8_t chained_cpy_buf[10] __used __NOCACHE;
351 #else
352 static ZTEST_BMEM uint8_t chained_read_buf_0[8];
353 static ZTEST_BMEM uint8_t chained_read_buf_1[8];
354 static ZTEST_BMEM uint8_t chained_cpy_buf[10];
355 #endif /* NOCACHE_MEM */
356 static ZTEST_BMEM volatile uint8_t rx_data_idx;
357 static ZTEST_BMEM uint8_t rx_buf_idx;
358
359 static ZTEST_BMEM uint8_t *read_ptr;
360
361 static uint8_t *chained_read_buf[2] = {chained_read_buf_0, chained_read_buf_1};
362
test_chained_read_callback(const struct device * dev,struct uart_event * evt,void * user_data)363 static void test_chained_read_callback(const struct device *dev,
364 struct uart_event *evt, void *user_data)
365 {
366 int err;
367
368 switch (evt->type) {
369 case UART_TX_DONE:
370 k_sem_give(&tx_done);
371 break;
372 case UART_RX_RDY:
373 zassert_true(rx_data_idx + evt->data.rx.len <= sizeof(chained_cpy_buf));
374 memcpy(&chained_cpy_buf[rx_data_idx],
375 &evt->data.rx.buf[evt->data.rx.offset],
376 evt->data.rx.len);
377 rx_data_idx += evt->data.rx.len;
378 break;
379 case UART_RX_BUF_REQUEST:
380 err = uart_rx_buf_rsp(dev, chained_read_buf[rx_buf_idx],
381 sizeof(chained_read_buf_0));
382 zassert_equal(err, 0);
383 rx_buf_idx = !rx_buf_idx ? 1 : 0;
384 break;
385 case UART_RX_DISABLED:
386 k_sem_give(&rx_disabled);
387 break;
388 default:
389 break;
390 }
391
392 }
393
chained_read_setup(void)394 static void *chained_read_setup(void)
395 {
396 static int idx;
397
398 uart_async_test_init(idx++);
399
400 uart_callback_set(uart_dev, test_chained_read_callback, NULL);
401
402 return NULL;
403 }
404
ZTEST_USER(uart_async_chain_read,test_chained_read)405 ZTEST_USER(uart_async_chain_read, test_chained_read)
406 {
407 #if NOCACHE_MEM
408 static __aligned(32) uint8_t tx_buf[10] __used __NOCACHE;
409 #else
410 uint8_t tx_buf[10];
411 #endif /* NOCACHE_MEM */
412 int iter = 6;
413 uint32_t rx_timeout_ms = 50;
414 int err;
415
416 err = uart_rx_enable(uart_dev, chained_read_buf[rx_buf_idx++], sizeof(chained_read_buf_0),
417 rx_timeout_ms * USEC_PER_MSEC);
418 zassert_equal(err, 0);
419 rx_data_idx = 0;
420
421 for (int i = 0; i < iter; i++) {
422 zassert_not_equal(k_sem_take(&rx_disabled, K_MSEC(10)),
423 0,
424 "RX_DISABLED occurred");
425 snprintf(tx_buf, sizeof(tx_buf), "Message %d", i);
426 uart_tx(uart_dev, tx_buf, sizeof(tx_buf), 100 * USEC_PER_MSEC);
427 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0,
428 "TX_DONE timeout");
429 k_msleep(rx_timeout_ms + 10);
430 zassert_equal(rx_data_idx, sizeof(tx_buf),
431 "Unexpected amount of data received %d exp:%d",
432 rx_data_idx, sizeof(tx_buf));
433 zassert_equal(memcmp(tx_buf, chained_cpy_buf, sizeof(tx_buf)), 0,
434 "Buffers not equal exp %s, real %s", tx_buf, chained_cpy_buf);
435 rx_data_idx = 0;
436 }
437 uart_rx_disable(uart_dev);
438 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
439 "RX_DISABLED timeout");
440 }
441
442 #if NOCACHE_MEM
443 static __aligned(32) uint8_t double_buffer[2][12] __used __NOCACHE;
444 #else
445 static ZTEST_BMEM uint8_t double_buffer[2][12];
446 #endif /* NOCACHE_MEM */
447 static ZTEST_DMEM uint8_t *next_buf = double_buffer[1];
448
test_double_buffer_callback(const struct device * dev,struct uart_event * evt,void * user_data)449 static void test_double_buffer_callback(const struct device *dev,
450 struct uart_event *evt, void *user_data)
451 {
452 switch (evt->type) {
453 case UART_TX_DONE:
454 k_sem_give(&tx_done);
455 break;
456 case UART_RX_RDY:
457 read_ptr = evt->data.rx.buf + evt->data.rx.offset;
458 k_sem_give(&rx_rdy);
459 break;
460 case UART_RX_BUF_REQUEST:
461 uart_rx_buf_rsp(dev, next_buf, sizeof(double_buffer[0]));
462 break;
463 case UART_RX_BUF_RELEASED:
464 next_buf = evt->data.rx_buf.buf;
465 k_sem_give(&rx_buf_released);
466 break;
467 case UART_RX_DISABLED:
468 k_sem_give(&rx_disabled);
469 break;
470 default:
471 break;
472 }
473
474 }
475
double_buffer_setup(void)476 static void *double_buffer_setup(void)
477 {
478 static int idx;
479
480 uart_async_test_init(idx++);
481
482 uart_callback_set(uart_dev, test_double_buffer_callback, NULL);
483
484 return NULL;
485 }
486
ZTEST_USER(uart_async_double_buf,test_double_buffer)487 ZTEST_USER(uart_async_double_buf, test_double_buffer)
488 {
489 #if NOCACHE_MEM
490 static __aligned(32) uint8_t tx_buf[4] __used __NOCACHE;
491 #else
492 uint8_t tx_buf[4];
493 #endif /* NOCACHE_MEM */
494
495 zassert_equal(uart_rx_enable(uart_dev,
496 double_buffer[0],
497 sizeof(double_buffer[0]),
498 50 * USEC_PER_MSEC),
499 0,
500 "Failed to enable receiving");
501
502 for (int i = 0; i < 100; i++) {
503 snprintf(tx_buf, sizeof(tx_buf), "%03d", i);
504 uart_tx(uart_dev, tx_buf, sizeof(tx_buf), 100 * USEC_PER_MSEC);
505 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0,
506 "TX_DONE timeout");
507 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0,
508 "RX_RDY timeout");
509 zassert_equal(memcmp(tx_buf, read_ptr, sizeof(tx_buf)),
510 0,
511 "Buffers not equal");
512 }
513 uart_rx_disable(uart_dev);
514 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
515 "RX_DISABLED timeout");
516 }
517
518 #if NOCACHE_MEM
519 static __aligned(32) uint8_t test_read_abort_rx_buf[2][100] __used __NOCACHE;
520 static __aligned(32) uint8_t test_read_abort_read_buf[100] __used __NOCACHE;
521 #else
522 static ZTEST_BMEM uint8_t test_read_abort_rx_buf[2][100];
523 static ZTEST_BMEM uint8_t test_read_abort_read_buf[100];
524 #endif /* NOCACHE_MEM */
525 static ZTEST_BMEM int test_read_abort_rx_cnt;
526 static ZTEST_BMEM bool test_read_abort_rx_buf_req_once;
527
test_read_abort_callback(const struct device * dev,struct uart_event * evt,void * user_data)528 static void test_read_abort_callback(const struct device *dev,
529 struct uart_event *evt, void *user_data)
530 {
531 int err;
532
533 ARG_UNUSED(dev);
534
535 switch (evt->type) {
536 case UART_TX_DONE:
537 k_sem_give(&tx_done);
538 break;
539 case UART_RX_BUF_REQUEST:
540 {
541 if (!test_read_abort_rx_buf_req_once) {
542 k_sem_give(&rx_buf_coherency);
543 uart_rx_buf_rsp(dev,
544 test_read_abort_rx_buf[1],
545 sizeof(test_read_abort_rx_buf[1]));
546 test_read_abort_rx_buf_req_once = true;
547 }
548 break;
549 }
550 case UART_RX_RDY:
551 memcpy(&test_read_abort_read_buf[test_read_abort_rx_cnt],
552 &evt->data.rx.buf[evt->data.rx.offset],
553 evt->data.rx.len);
554 test_read_abort_rx_cnt += evt->data.rx.len;
555 k_sem_give(&rx_rdy);
556 break;
557 case UART_RX_BUF_RELEASED:
558 k_sem_give(&rx_buf_released);
559 err = k_sem_take(&rx_buf_coherency, K_NO_WAIT);
560 failed_in_isr |= (err < 0);
561 break;
562 case UART_RX_DISABLED:
563 err = k_sem_take(&rx_buf_released, K_NO_WAIT);
564 failed_in_isr |= (err < 0);
565 k_sem_give(&rx_disabled);
566 break;
567 default:
568 break;
569 }
570 }
571
read_abort_timeout(struct k_timer * timer)572 static void read_abort_timeout(struct k_timer *timer)
573 {
574 int err;
575
576 err = uart_rx_disable(uart_dev);
577 zassert_equal(err, 0, "Unexpected err:%d", err);
578 }
579
read_abort_setup(void)580 static void *read_abort_setup(void)
581 {
582 static int idx;
583
584 uart_async_test_init(idx++);
585
586 test_read_abort_rx_buf_req_once = false;
587 failed_in_isr = false;
588 uart_callback_set(uart_dev, test_read_abort_callback, NULL);
589
590 return NULL;
591 }
592
ZTEST_USER(uart_async_read_abort,test_read_abort)593 ZTEST_USER(uart_async_read_abort, test_read_abort)
594 {
595 #if NOCACHE_MEM
596 static __aligned(32) uint8_t rx_buf[100] __used __NOCACHE;
597 static __aligned(32) uint8_t tx_buf[100] __used __NOCACHE;
598 #else
599 uint8_t rx_buf[100];
600 uint8_t tx_buf[100];
601 #endif /* NOCACHE_MEM */
602
603 memset(rx_buf, 0, sizeof(rx_buf));
604 memset(tx_buf, 1, sizeof(tx_buf));
605
606 uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), 50 * USEC_PER_MSEC);
607 k_sem_give(&rx_buf_coherency);
608
609 uart_tx(uart_dev, tx_buf, 5, 100 * USEC_PER_MSEC);
610 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
611 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
612 zassert_equal(memcmp(tx_buf, rx_buf, 5), 0, "Buffers not equal");
613
614 uart_tx(uart_dev, tx_buf, 95, 100 * USEC_PER_MSEC);
615
616 k_timer_start(&read_abort_timer, K_USEC(300), K_NO_WAIT);
617
618 /* RX will be aborted from k_timer timeout */
619
620 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
621 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
622 "RX_DISABLED timeout");
623 zassert_false(failed_in_isr, "Unexpected order of uart events");
624 zassert_not_equal(memcmp(tx_buf, test_read_abort_read_buf, 100), 0, "Buffers equal");
625
626 /* Read out possible other RX bytes
627 * that may affect following test on RX
628 */
629 uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), 50 * USEC_PER_MSEC);
630 while (k_sem_take(&rx_rdy, K_MSEC(1000)) != -EAGAIN) {
631 ;
632 }
633 uart_rx_disable(uart_dev);
634 k_msleep(10);
635 zassert_not_equal(k_sem_take(&rx_buf_coherency, K_NO_WAIT), 0,
636 "All provided buffers are released");
637
638 }
639
640 static ZTEST_BMEM volatile size_t sent;
641 static ZTEST_BMEM volatile size_t received;
642 #if NOCACHE_MEM
643 static __aligned(32) uint8_t test_rx_buf[2][100] __used __NOCACHE;
644 #else
645 static ZTEST_BMEM uint8_t test_rx_buf[2][100];
646 #endif /* NOCACHE_MEM */
647
test_write_abort_callback(const struct device * dev,struct uart_event * evt,void * user_data)648 static void test_write_abort_callback(const struct device *dev,
649 struct uart_event *evt, void *user_data)
650 {
651 ARG_UNUSED(dev);
652
653 switch (evt->type) {
654 case UART_TX_DONE:
655 k_sem_give(&tx_done);
656 break;
657 case UART_TX_ABORTED:
658 sent = evt->data.tx.len;
659 k_sem_give(&tx_aborted);
660 break;
661 case UART_RX_RDY:
662 received = evt->data.rx.len;
663 k_sem_give(&rx_rdy);
664 break;
665 case UART_RX_BUF_REQUEST:
666 uart_rx_buf_rsp(dev, test_rx_buf[1], sizeof(test_rx_buf[1]));
667 break;
668 case UART_RX_BUF_RELEASED:
669 k_sem_give(&rx_buf_released);
670 break;
671 case UART_RX_DISABLED:
672 k_sem_give(&rx_disabled);
673 break;
674 default:
675 break;
676 }
677 }
678
write_abort_setup(void)679 static void *write_abort_setup(void)
680 {
681 static int idx;
682
683 uart_async_test_init(idx++);
684
685 uart_callback_set(uart_dev, test_write_abort_callback, NULL);
686
687 return NULL;
688 }
689
ZTEST_USER(uart_async_write_abort,test_write_abort)690 ZTEST_USER(uart_async_write_abort, test_write_abort)
691 {
692 #if NOCACHE_MEM
693 static __aligned(32) uint8_t tx_buf[100] __used __NOCACHE;
694 #else
695 uint8_t tx_buf[100];
696 #endif /* NOCACHE_MEM */
697
698 memset(test_rx_buf, 0, sizeof(test_rx_buf));
699 memset(tx_buf, 1, sizeof(tx_buf));
700
701 uart_rx_enable(uart_dev, test_rx_buf[0], sizeof(test_rx_buf[0]), 50 * USEC_PER_MSEC);
702
703 uart_tx(uart_dev, tx_buf, 5, 100 * USEC_PER_MSEC);
704 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
705 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
706 zassert_equal(memcmp(tx_buf, test_rx_buf, 5), 0, "Buffers not equal");
707
708 uart_tx(uart_dev, tx_buf, 95, 100 * USEC_PER_MSEC);
709 uart_tx_abort(uart_dev);
710 zassert_equal(k_sem_take(&tx_aborted, K_MSEC(100)), 0,
711 "TX_ABORTED timeout");
712 if (sent != 0) {
713 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0,
714 "RX_RDY timeout");
715 zassert_equal(sent, received, "Sent is not equal to received.");
716 }
717 uart_rx_disable(uart_dev);
718 zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
719 0,
720 "RX_BUF_RELEASED timeout");
721 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
722 "RX_DISABLED timeout");
723 }
724
725
test_forever_timeout_callback(const struct device * dev,struct uart_event * evt,void * user_data)726 static void test_forever_timeout_callback(const struct device *dev,
727 struct uart_event *evt, void *user_data)
728 {
729 ARG_UNUSED(dev);
730
731 switch (evt->type) {
732 case UART_TX_DONE:
733 k_sem_give(&tx_done);
734 break;
735 case UART_TX_ABORTED:
736 sent = evt->data.tx.len;
737 k_sem_give(&tx_aborted);
738 break;
739 case UART_RX_RDY:
740 received = evt->data.rx.len;
741 k_sem_give(&rx_rdy);
742 break;
743 case UART_RX_BUF_RELEASED:
744 k_sem_give(&rx_buf_released);
745 break;
746 case UART_RX_DISABLED:
747 k_sem_give(&rx_disabled);
748 break;
749 default:
750 break;
751 }
752 }
753
forever_timeout_setup(void)754 static void *forever_timeout_setup(void)
755 {
756 static int idx;
757
758 uart_async_test_init(idx++);
759
760 uart_callback_set(uart_dev, test_forever_timeout_callback, NULL);
761
762 return NULL;
763 }
764
ZTEST_USER(uart_async_timeout,test_forever_timeout)765 ZTEST_USER(uart_async_timeout, test_forever_timeout)
766 {
767 #if NOCACHE_MEM
768 static __aligned(32) uint8_t rx_buf[100] __used __NOCACHE;
769 static __aligned(32) uint8_t tx_buf[100] __used __NOCACHE;
770 #else
771 uint8_t rx_buf[100];
772 uint8_t tx_buf[100];
773 #endif /* NOCACHE_MEM */
774
775 memset(rx_buf, 0, sizeof(rx_buf));
776 memset(tx_buf, 1, sizeof(tx_buf));
777
778 uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), SYS_FOREVER_US);
779
780 uart_tx(uart_dev, tx_buf, 5, SYS_FOREVER_US);
781 zassert_not_equal(k_sem_take(&tx_aborted, K_MSEC(1000)), 0,
782 "TX_ABORTED timeout");
783 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
784 zassert_not_equal(k_sem_take(&rx_rdy, K_MSEC(1000)), 0,
785 "RX_RDY timeout");
786
787 uart_tx(uart_dev, tx_buf, 95, SYS_FOREVER_US);
788
789 zassert_not_equal(k_sem_take(&tx_aborted, K_MSEC(1000)), 0,
790 "TX_ABORTED timeout");
791 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
792 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
793
794
795 zassert_equal(memcmp(tx_buf, rx_buf, 100), 0, "Buffers not equal");
796
797 uart_rx_disable(uart_dev);
798 zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
799 0,
800 "RX_BUF_RELEASED timeout");
801 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
802 "RX_DISABLED timeout");
803 }
804
805
806 #if NOCACHE_MEM
807 static const uint8_t chained_write_tx_bufs[2][10] = {"Message 1", "Message 2"};
808 #else
809 static ZTEST_DMEM uint8_t chained_write_tx_bufs[2][10] = {"Message 1", "Message 2"};
810 #endif /* NOCACHE_MEM */
811 static ZTEST_DMEM bool chained_write_next_buf = true;
812 static ZTEST_BMEM volatile uint8_t tx_sent;
813
test_chained_write_callback(const struct device * dev,struct uart_event * evt,void * user_data)814 static void test_chained_write_callback(const struct device *dev,
815 struct uart_event *evt, void *user_data)
816 {
817 switch (evt->type) {
818 case UART_TX_DONE:
819 if (chained_write_next_buf) {
820 uart_tx(dev, chained_write_tx_bufs[1], 10, 100 * USEC_PER_MSEC);
821 chained_write_next_buf = false;
822 }
823 tx_sent = 1;
824 k_sem_give(&tx_done);
825 break;
826 case UART_TX_ABORTED:
827 sent = evt->data.tx.len;
828 k_sem_give(&tx_aborted);
829 break;
830 case UART_RX_RDY:
831 received = evt->data.rx.len;
832 k_sem_give(&rx_rdy);
833 break;
834 case UART_RX_BUF_RELEASED:
835 k_sem_give(&rx_buf_released);
836 break;
837 case UART_RX_DISABLED:
838 k_sem_give(&rx_disabled);
839 break;
840 default:
841 break;
842 }
843 }
844
chained_write_setup(void)845 static void *chained_write_setup(void)
846 {
847 static int idx;
848
849 uart_async_test_init(idx++);
850
851 tx_sent = 0;
852 chained_write_next_buf = true;
853 uart_callback_set(uart_dev, test_chained_write_callback, NULL);
854
855 return NULL;
856 }
857
ZTEST_USER(uart_async_chain_write,test_chained_write)858 ZTEST_USER(uart_async_chain_write, test_chained_write)
859 {
860 #if NOCACHE_MEM
861 static __aligned(32) uint8_t rx_buf[20] __used __NOCACHE;
862 #else
863 uint8_t rx_buf[20];
864 #endif /* NOCACHE_MEM */
865
866 memset(rx_buf, 0, sizeof(rx_buf));
867
868 uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), 50 * USEC_PER_MSEC);
869
870 uart_tx(uart_dev, chained_write_tx_bufs[0], 10, 100 * USEC_PER_MSEC);
871 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
872 zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
873 zassert_equal(chained_write_next_buf, false, "Sent no message");
874 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
875 zassert_equal(memcmp(chained_write_tx_bufs[0], rx_buf, 10),
876 0,
877 "Buffers not equal");
878 zassert_equal(memcmp(chained_write_tx_bufs[1], rx_buf + 10, 10),
879 0,
880 "Buffers not equal");
881
882 uart_rx_disable(uart_dev);
883 zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
884 0,
885 "RX_BUF_RELEASED timeout");
886 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
887 "RX_DISABLED timeout");
888 }
889
890 #define RX_LONG_BUFFER CONFIG_TEST_LONG_BUFFER_SIZE
891 #define TX_LONG_BUFFER (CONFIG_TEST_LONG_BUFFER_SIZE - 8)
892
893 #if NOCACHE_MEM
894 static __aligned(32) uint8_t long_rx_buf[RX_LONG_BUFFER] __used __NOCACHE;
895 static __aligned(32) uint8_t long_rx_buf2[RX_LONG_BUFFER] __used __NOCACHE;
896 static __aligned(32) uint8_t long_tx_buf[TX_LONG_BUFFER] __used __NOCACHE;
897 #else
898 static ZTEST_BMEM uint8_t long_rx_buf[RX_LONG_BUFFER];
899 static ZTEST_BMEM uint8_t long_rx_buf2[RX_LONG_BUFFER];
900 static ZTEST_BMEM uint8_t long_tx_buf[TX_LONG_BUFFER];
901 #endif /* NOCACHE_MEM */
902 static ZTEST_BMEM volatile uint8_t evt_num;
903 static ZTEST_BMEM size_t long_received[2];
904 static ZTEST_BMEM uint8_t *long_next_buffer;
905
test_long_buffers_callback(const struct device * dev,struct uart_event * evt,void * user_data)906 static void test_long_buffers_callback(const struct device *dev,
907 struct uart_event *evt, void *user_data)
908 {
909
910 switch (evt->type) {
911 case UART_TX_DONE:
912 k_sem_give(&tx_done);
913 break;
914 case UART_TX_ABORTED:
915 sent = evt->data.tx.len;
916 k_sem_give(&tx_aborted);
917 break;
918 case UART_RX_RDY:
919 long_received[evt_num] = evt->data.rx.len;
920 evt_num++;
921 k_sem_give(&rx_rdy);
922 break;
923 case UART_RX_BUF_RELEASED:
924 k_sem_give(&rx_buf_released);
925 break;
926 case UART_RX_DISABLED:
927 k_sem_give(&rx_disabled);
928 break;
929 case UART_RX_BUF_REQUEST:
930 uart_rx_buf_rsp(dev, long_next_buffer, RX_LONG_BUFFER);
931 long_next_buffer = (long_next_buffer == long_rx_buf2) ? long_rx_buf : long_rx_buf2;
932 break;
933 default:
934 break;
935 }
936 }
937
long_buffers_setup(void)938 static void *long_buffers_setup(void)
939 {
940 static int idx;
941
942 uart_async_test_init(idx++);
943
944 evt_num = 0;
945 long_next_buffer = long_rx_buf2;
946 uart_callback_set(uart_dev, test_long_buffers_callback, NULL);
947
948 return NULL;
949 }
950
ZTEST_USER(uart_async_long_buf,test_long_buffers)951 ZTEST_USER(uart_async_long_buf, test_long_buffers)
952 {
953 size_t tx_len1 = TX_LONG_BUFFER / 2;
954 size_t tx_len2 = TX_LONG_BUFFER;
955
956 memset(long_rx_buf, 0, sizeof(long_rx_buf));
957 memset(long_tx_buf, 1, sizeof(long_tx_buf));
958
959 uart_rx_enable(uart_dev, long_rx_buf, sizeof(long_rx_buf), 10 * USEC_PER_MSEC);
960
961 uart_tx(uart_dev, long_tx_buf, tx_len1, 200 * USEC_PER_MSEC);
962 zassert_equal(k_sem_take(&tx_done, K_MSEC(200)), 0, "TX_DONE timeout");
963 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(200)), 0, "RX_RDY timeout");
964 zassert_equal(long_received[0], tx_len1, "Wrong number of bytes received.");
965 zassert_equal(memcmp(long_tx_buf, long_rx_buf, tx_len1),
966 0,
967 "Buffers not equal");
968 k_msleep(10);
969 /* Check if instance is releasing a buffer after the timeout. */
970 bool release_on_timeout = k_sem_take(&rx_buf_released, K_NO_WAIT) == 0;
971
972 evt_num = 0;
973 uart_tx(uart_dev, long_tx_buf, tx_len2, 200 * USEC_PER_MSEC);
974 zassert_equal(k_sem_take(&tx_done, K_MSEC(200)), 0, "TX_DONE timeout");
975 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(200)), 0, "RX_RDY timeout");
976
977 if (release_on_timeout) {
978 zassert_equal(long_received[0], tx_len2, "Wrong number of bytes received.");
979 zassert_equal(memcmp(long_tx_buf, long_rx_buf2, long_received[0]), 0,
980 "Buffers not equal");
981 } else {
982 zassert_equal(k_sem_take(&rx_rdy, K_MSEC(200)), 0, "RX_RDY timeout");
983 zassert_equal(long_received[0], RX_LONG_BUFFER - tx_len1,
984 "Wrong number of bytes received.");
985 zassert_equal(long_received[1], tx_len2 - (RX_LONG_BUFFER - tx_len1),
986 "Wrong number of bytes received.");
987 zassert_equal(memcmp(long_tx_buf, long_rx_buf + tx_len1, long_received[0]), 0,
988 "Buffers not equal");
989 zassert_equal(memcmp(long_tx_buf, long_rx_buf2, long_received[1]), 0,
990 "Buffers not equal");
991 }
992
993 uart_rx_disable(uart_dev);
994 zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
995 0,
996 "RX_BUF_RELEASED timeout");
997 zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
998 "RX_DISABLED timeout");
999 }
1000
1001 ZTEST_SUITE(uart_async_single_read, NULL, single_read_setup,
1002 NULL, NULL, NULL);
1003
1004 ZTEST_SUITE(uart_async_multi_rx, NULL, multiple_rx_enable_setup,
1005 NULL, NULL, NULL);
1006
1007 ZTEST_SUITE(uart_async_chain_read, NULL, chained_read_setup,
1008 NULL, NULL, NULL);
1009
1010 ZTEST_SUITE(uart_async_double_buf, NULL, double_buffer_setup,
1011 NULL, NULL, NULL);
1012
1013 ZTEST_SUITE(uart_async_read_abort, NULL, read_abort_setup,
1014 NULL, NULL, NULL);
1015
1016 ZTEST_SUITE(uart_async_chain_write, NULL, chained_write_setup,
1017 NULL, NULL, NULL);
1018
1019 ZTEST_SUITE(uart_async_long_buf, NULL, long_buffers_setup,
1020 NULL, NULL, NULL);
1021
1022 ZTEST_SUITE(uart_async_write_abort, NULL, write_abort_setup,
1023 NULL, NULL, NULL);
1024
1025 ZTEST_SUITE(uart_async_timeout, NULL, forever_timeout_setup,
1026 NULL, NULL, NULL);
1027
test_main(void)1028 void test_main(void)
1029 {
1030 /* Run all suites for each dut UART. Setup function for each suite is picking
1031 * next UART from the array.
1032 */
1033 ztest_run_all(NULL, false, ARRAY_SIZE(duts), 1);
1034 ztest_verify_all_test_suites_ran();
1035 }
1036