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(105)), 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[10] __used __NOCACHE;
349 static __aligned(32) uint8_t chained_read_buf_1[10] __used __NOCACHE;
350 static __aligned(32) uint8_t chained_cpy_buf[10] __used __NOCACHE;
351 #else
352 ZTEST_BMEM uint8_t chained_read_buf_0[10];
353 ZTEST_BMEM uint8_t chained_read_buf_1[10];
354 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 	 __aligned(32) 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 	 __aligned(32) uint8_t tx_buf[4];
493 #endif /* NOCACHE_MEM */
494 
495 	zassert_equal(uart_rx_enable(uart_dev, double_buffer[0], sizeof(double_buffer[0]),
496 				     25 * USEC_PER_MSEC),
497 		      0, "Failed to enable receiving");
498 
499 	for (int i = 0; i < 100; i++) {
500 		snprintf(tx_buf, sizeof(tx_buf), "%03d", i);
501 		uart_tx(uart_dev, tx_buf, sizeof(tx_buf), 100 * USEC_PER_MSEC);
502 		zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0,
503 			      "TX_DONE timeout");
504 		zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0,
505 			      "RX_RDY timeout");
506 		zassert_equal(memcmp(tx_buf, read_ptr, sizeof(tx_buf)),
507 			      0,
508 			      "Buffers not equal");
509 	}
510 	uart_rx_disable(uart_dev);
511 	zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
512 		      "RX_DISABLED timeout");
513 }
514 
515 #if NOCACHE_MEM
516 static __aligned(32) uint8_t test_read_abort_rx_buf[2][100] __used __NOCACHE;
517 static __aligned(32) uint8_t test_read_abort_read_buf[100] __used __NOCACHE;
518 #else
519 static ZTEST_BMEM uint8_t test_read_abort_rx_buf[2][100];
520 static ZTEST_BMEM uint8_t test_read_abort_read_buf[100];
521 #endif /* NOCACHE_MEM */
522 static ZTEST_BMEM int test_read_abort_rx_cnt;
523 static ZTEST_BMEM bool test_read_abort_rx_buf_req_once;
524 
test_read_abort_callback(const struct device * dev,struct uart_event * evt,void * user_data)525 static void test_read_abort_callback(const struct device *dev,
526 			      struct uart_event *evt, void *user_data)
527 {
528 	int err;
529 
530 	ARG_UNUSED(dev);
531 
532 	switch (evt->type) {
533 	case UART_TX_DONE:
534 		k_sem_give(&tx_done);
535 		break;
536 	case UART_RX_BUF_REQUEST:
537 	{
538 		if (!test_read_abort_rx_buf_req_once) {
539 			k_sem_give(&rx_buf_coherency);
540 			uart_rx_buf_rsp(dev,
541 					test_read_abort_rx_buf[1],
542 					sizeof(test_read_abort_rx_buf[1]));
543 			test_read_abort_rx_buf_req_once = true;
544 		}
545 		break;
546 	}
547 	case UART_RX_RDY:
548 		memcpy(&test_read_abort_read_buf[test_read_abort_rx_cnt],
549 		       &evt->data.rx.buf[evt->data.rx.offset],
550 		       evt->data.rx.len);
551 		test_read_abort_rx_cnt += evt->data.rx.len;
552 		k_sem_give(&rx_rdy);
553 		break;
554 	case UART_RX_BUF_RELEASED:
555 		k_sem_give(&rx_buf_released);
556 		err = k_sem_take(&rx_buf_coherency, K_NO_WAIT);
557 		failed_in_isr |= (err < 0);
558 		break;
559 	case UART_RX_DISABLED:
560 		err = k_sem_take(&rx_buf_released, K_NO_WAIT);
561 		failed_in_isr |= (err < 0);
562 		k_sem_give(&rx_disabled);
563 		break;
564 	default:
565 		break;
566 	}
567 }
568 
read_abort_timeout(struct k_timer * timer)569 static void read_abort_timeout(struct k_timer *timer)
570 {
571 	int err;
572 
573 	err = uart_rx_disable(uart_dev);
574 	zassert_equal(err, 0, "Unexpected err:%d", err);
575 }
576 
read_abort_setup(void)577 static void *read_abort_setup(void)
578 {
579 	static int idx;
580 
581 	uart_async_test_init(idx++);
582 
583 	test_read_abort_rx_buf_req_once = false;
584 	failed_in_isr = false;
585 	uart_callback_set(uart_dev, test_read_abort_callback, NULL);
586 
587 	return NULL;
588 }
589 
ZTEST_USER(uart_async_read_abort,test_read_abort)590 ZTEST_USER(uart_async_read_abort, test_read_abort)
591 {
592 	struct uart_config cfg;
593 	int err;
594 	uint32_t t_us;
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 	 __aligned(32) uint8_t rx_buf[100];
600 	 __aligned(32) 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 	err = uart_config_get(uart_dev, &cfg);
607 	zassert_equal(err, 0);
608 
609 	/* Lets aim to abort after transmitting ~20 bytes (200 bauds) */
610 	t_us = (20 * 10 * 1000000) / cfg.baudrate;
611 
612 	err = uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), 50 * USEC_PER_MSEC);
613 	zassert_equal(err, 0);
614 	k_sem_give(&rx_buf_coherency);
615 
616 	err = uart_tx(uart_dev, tx_buf, 5, 100 * USEC_PER_MSEC);
617 	zassert_equal(err, 0);
618 	zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
619 	zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
620 	zassert_equal(memcmp(tx_buf, rx_buf, 5), 0, "Buffers not equal");
621 
622 	err = uart_tx(uart_dev, tx_buf, 95, 100 * USEC_PER_MSEC);
623 	zassert_equal(err, 0);
624 
625 	k_timer_start(&read_abort_timer, K_USEC(t_us), K_NO_WAIT);
626 
627 	/* RX will be aborted from k_timer timeout */
628 
629 	zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
630 	zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
631 		      "RX_DISABLED timeout");
632 	zassert_false(failed_in_isr, "Unexpected order of uart events");
633 	zassert_not_equal(memcmp(tx_buf, test_read_abort_read_buf, 100), 0, "Buffers equal");
634 
635 	/* Read out possible other RX bytes
636 	 * that may affect following test on RX
637 	 */
638 	uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), 50 * USEC_PER_MSEC);
639 	while (k_sem_take(&rx_rdy, K_MSEC(1000)) != -EAGAIN) {
640 		;
641 	}
642 	uart_rx_disable(uart_dev);
643 	k_msleep(10);
644 	zassert_not_equal(k_sem_take(&rx_buf_coherency, K_NO_WAIT), 0,
645 			"All provided buffers are released");
646 
647 }
648 
649 static ZTEST_BMEM volatile size_t sent;
650 static ZTEST_BMEM volatile size_t received;
651 #if NOCACHE_MEM
652 static __aligned(32) uint8_t test_rx_buf[2][100] __used __NOCACHE;
653 #else
654 static ZTEST_BMEM uint8_t test_rx_buf[2][100];
655 #endif /* NOCACHE_MEM */
656 
test_write_abort_callback(const struct device * dev,struct uart_event * evt,void * user_data)657 static void test_write_abort_callback(const struct device *dev,
658 			       struct uart_event *evt, void *user_data)
659 {
660 	ARG_UNUSED(dev);
661 
662 	switch (evt->type) {
663 	case UART_TX_DONE:
664 		k_sem_give(&tx_done);
665 		break;
666 	case UART_TX_ABORTED:
667 		sent = evt->data.tx.len;
668 		k_sem_give(&tx_aborted);
669 		break;
670 	case UART_RX_RDY:
671 		received = evt->data.rx.len;
672 		k_sem_give(&rx_rdy);
673 		break;
674 	case UART_RX_BUF_REQUEST:
675 		uart_rx_buf_rsp(dev, test_rx_buf[1], sizeof(test_rx_buf[1]));
676 		break;
677 	case UART_RX_BUF_RELEASED:
678 		k_sem_give(&rx_buf_released);
679 		break;
680 	case UART_RX_DISABLED:
681 		k_sem_give(&rx_disabled);
682 		break;
683 	default:
684 		break;
685 	}
686 }
687 
write_abort_setup(void)688 static void *write_abort_setup(void)
689 {
690 	static int idx;
691 
692 	uart_async_test_init(idx++);
693 
694 	uart_callback_set(uart_dev, test_write_abort_callback, NULL);
695 
696 	return NULL;
697 }
698 
ZTEST_USER(uart_async_write_abort,test_write_abort)699 ZTEST_USER(uart_async_write_abort, test_write_abort)
700 {
701 #if NOCACHE_MEM
702 	static __aligned(32) uint8_t tx_buf[100] __used __NOCACHE;
703 #else
704 	 __aligned(32) uint8_t tx_buf[100];
705 #endif /* NOCACHE_MEM */
706 
707 	memset(test_rx_buf, 0, sizeof(test_rx_buf));
708 	memset(tx_buf, 1, sizeof(tx_buf));
709 
710 	uart_rx_enable(uart_dev, test_rx_buf[0], sizeof(test_rx_buf[0]), 50 * USEC_PER_MSEC);
711 
712 	uart_tx(uart_dev, tx_buf, 5, 100 * USEC_PER_MSEC);
713 	zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
714 	zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
715 	zassert_equal(memcmp(tx_buf, test_rx_buf, 5), 0, "Buffers not equal");
716 
717 	uart_tx(uart_dev, tx_buf, 95, 100 * USEC_PER_MSEC);
718 	uart_tx_abort(uart_dev);
719 	zassert_equal(k_sem_take(&tx_aborted, K_MSEC(100)), 0,
720 		      "TX_ABORTED timeout");
721 	if (sent != 0) {
722 		zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0,
723 			      "RX_RDY timeout");
724 		k_sleep(K_MSEC(30));
725 		zassert_equal(sent, received, "Sent is not equal to received.");
726 	}
727 	uart_rx_disable(uart_dev);
728 	zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
729 		      0,
730 		      "RX_BUF_RELEASED timeout");
731 	zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
732 		      "RX_DISABLED timeout");
733 }
734 
735 
test_forever_timeout_callback(const struct device * dev,struct uart_event * evt,void * user_data)736 static void test_forever_timeout_callback(const struct device *dev,
737 				   struct uart_event *evt, void *user_data)
738 {
739 	ARG_UNUSED(dev);
740 
741 	switch (evt->type) {
742 	case UART_TX_DONE:
743 		k_sem_give(&tx_done);
744 		break;
745 	case UART_TX_ABORTED:
746 		sent = evt->data.tx.len;
747 		k_sem_give(&tx_aborted);
748 		break;
749 	case UART_RX_RDY:
750 		received = evt->data.rx.len;
751 		k_sem_give(&rx_rdy);
752 		break;
753 	case UART_RX_BUF_RELEASED:
754 		k_sem_give(&rx_buf_released);
755 		break;
756 	case UART_RX_DISABLED:
757 		k_sem_give(&rx_disabled);
758 		break;
759 	default:
760 		break;
761 	}
762 }
763 
forever_timeout_setup(void)764 static void *forever_timeout_setup(void)
765 {
766 	static int idx;
767 
768 	uart_async_test_init(idx++);
769 
770 	uart_callback_set(uart_dev, test_forever_timeout_callback, NULL);
771 
772 	return NULL;
773 }
774 
ZTEST_USER(uart_async_timeout,test_forever_timeout)775 ZTEST_USER(uart_async_timeout, test_forever_timeout)
776 {
777 #if NOCACHE_MEM
778 	static __aligned(32) uint8_t rx_buf[100] __used __NOCACHE;
779 	static __aligned(32) uint8_t tx_buf[100] __used __NOCACHE;
780 #else
781 	 __aligned(32) uint8_t rx_buf[100];
782 	 __aligned(32) uint8_t tx_buf[100];
783 #endif /* NOCACHE_MEM */
784 
785 	memset(rx_buf, 0, sizeof(rx_buf));
786 	memset(tx_buf, 1, sizeof(tx_buf));
787 
788 	uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), SYS_FOREVER_US);
789 
790 	uart_tx(uart_dev, tx_buf, 5, SYS_FOREVER_US);
791 	zassert_not_equal(k_sem_take(&tx_aborted, K_MSEC(1000)), 0,
792 			  "TX_ABORTED timeout");
793 	zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
794 	zassert_not_equal(k_sem_take(&rx_rdy, K_MSEC(1000)), 0,
795 			  "RX_RDY timeout");
796 
797 	uart_tx(uart_dev, tx_buf, 95, SYS_FOREVER_US);
798 
799 	zassert_not_equal(k_sem_take(&tx_aborted, K_MSEC(1000)), 0,
800 			  "TX_ABORTED timeout");
801 	zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
802 	zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
803 
804 
805 	zassert_equal(memcmp(tx_buf, rx_buf, 100), 0, "Buffers not equal");
806 
807 	uart_rx_disable(uart_dev);
808 	zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
809 		      0,
810 		      "RX_BUF_RELEASED timeout");
811 	zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
812 		      "RX_DISABLED timeout");
813 }
814 
815 
816 #if NOCACHE_MEM
817 static const uint8_t chained_write_tx_bufs[2][10] = {"Message 1", "Message 2"};
818 #else
819 static ZTEST_DMEM uint8_t chained_write_tx_bufs[2][10] = {"Message 1", "Message 2"};
820 #endif /* NOCACHE_MEM */
821 static ZTEST_DMEM bool chained_write_next_buf = true;
822 static ZTEST_BMEM volatile uint8_t tx_sent;
823 
test_chained_write_callback(const struct device * dev,struct uart_event * evt,void * user_data)824 static void test_chained_write_callback(const struct device *dev,
825 				 struct uart_event *evt, void *user_data)
826 {
827 	switch (evt->type) {
828 	case UART_TX_DONE:
829 		if (chained_write_next_buf) {
830 			uart_tx(dev, chained_write_tx_bufs[1], 10, 100 * USEC_PER_MSEC);
831 			chained_write_next_buf = false;
832 		}
833 		tx_sent = 1;
834 		k_sem_give(&tx_done);
835 		break;
836 	case UART_TX_ABORTED:
837 		sent = evt->data.tx.len;
838 		k_sem_give(&tx_aborted);
839 		break;
840 	case UART_RX_RDY:
841 		received = evt->data.rx.len;
842 		k_sem_give(&rx_rdy);
843 		break;
844 	case UART_RX_BUF_RELEASED:
845 		k_sem_give(&rx_buf_released);
846 		break;
847 	case UART_RX_DISABLED:
848 		k_sem_give(&rx_disabled);
849 		break;
850 	default:
851 		break;
852 	}
853 }
854 
chained_write_setup(void)855 static void *chained_write_setup(void)
856 {
857 	static int idx;
858 
859 	uart_async_test_init(idx++);
860 
861 	tx_sent = 0;
862 	chained_write_next_buf = true;
863 	uart_callback_set(uart_dev, test_chained_write_callback, NULL);
864 
865 	return NULL;
866 }
867 
ZTEST_USER(uart_async_chain_write,test_chained_write)868 ZTEST_USER(uart_async_chain_write, test_chained_write)
869 {
870 #if NOCACHE_MEM
871 	static __aligned(32) uint8_t rx_buf[20] __used __NOCACHE;
872 #else
873 	 __aligned(32) uint8_t rx_buf[20];
874 #endif /* NOCACHE_MEM */
875 
876 	memset(rx_buf, 0, sizeof(rx_buf));
877 
878 	uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), 50 * USEC_PER_MSEC);
879 
880 	uart_tx(uart_dev, chained_write_tx_bufs[0], 10, 100 * USEC_PER_MSEC);
881 	zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
882 	zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
883 	zassert_equal(chained_write_next_buf, false, "Sent no message");
884 	zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
885 	zassert_equal(memcmp(chained_write_tx_bufs[0], rx_buf, 10),
886 		      0,
887 		      "Buffers not equal");
888 	zassert_equal(memcmp(chained_write_tx_bufs[1], rx_buf + 10, 10),
889 		      0,
890 		      "Buffers not equal");
891 
892 	uart_rx_disable(uart_dev);
893 	zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
894 		      0,
895 		      "RX_BUF_RELEASED timeout");
896 	zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
897 		      "RX_DISABLED timeout");
898 }
899 
900 #define RX_LONG_BUFFER CONFIG_TEST_LONG_BUFFER_SIZE
901 #define TX_LONG_BUFFER (CONFIG_TEST_LONG_BUFFER_SIZE - 8)
902 
903 #if NOCACHE_MEM
904 static __aligned(32) uint8_t long_rx_buf[RX_LONG_BUFFER] __used __NOCACHE;
905 static __aligned(32) uint8_t long_rx_buf2[RX_LONG_BUFFER] __used __NOCACHE;
906 static __aligned(32) uint8_t long_tx_buf[TX_LONG_BUFFER] __used __NOCACHE;
907 #else
908 static ZTEST_BMEM uint8_t long_rx_buf[RX_LONG_BUFFER];
909 static ZTEST_BMEM uint8_t long_rx_buf2[RX_LONG_BUFFER];
910 static ZTEST_BMEM uint8_t long_tx_buf[TX_LONG_BUFFER];
911 #endif /* NOCACHE_MEM */
912 static ZTEST_BMEM volatile uint8_t evt_num;
913 static ZTEST_BMEM size_t long_received[2];
914 static ZTEST_BMEM uint8_t *long_next_buffer;
915 
test_long_buffers_callback(const struct device * dev,struct uart_event * evt,void * user_data)916 static void test_long_buffers_callback(const struct device *dev,
917 				struct uart_event *evt, void *user_data)
918 {
919 
920 	switch (evt->type) {
921 	case UART_TX_DONE:
922 		k_sem_give(&tx_done);
923 		break;
924 	case UART_TX_ABORTED:
925 		sent = evt->data.tx.len;
926 		k_sem_give(&tx_aborted);
927 		break;
928 	case UART_RX_RDY:
929 		long_received[evt_num] = evt->data.rx.len;
930 		evt_num++;
931 		k_sem_give(&rx_rdy);
932 		break;
933 	case UART_RX_BUF_RELEASED:
934 		k_sem_give(&rx_buf_released);
935 		break;
936 	case UART_RX_DISABLED:
937 		k_sem_give(&rx_disabled);
938 		break;
939 	case UART_RX_BUF_REQUEST:
940 		uart_rx_buf_rsp(dev, long_next_buffer, RX_LONG_BUFFER);
941 		long_next_buffer = (long_next_buffer == long_rx_buf2) ? long_rx_buf : long_rx_buf2;
942 		break;
943 	default:
944 		break;
945 	}
946 }
947 
long_buffers_setup(void)948 static void *long_buffers_setup(void)
949 {
950 	static int idx;
951 
952 	uart_async_test_init(idx++);
953 
954 	evt_num = 0;
955 	long_next_buffer = long_rx_buf2;
956 	uart_callback_set(uart_dev, test_long_buffers_callback, NULL);
957 
958 	return NULL;
959 }
960 
ZTEST_USER(uart_async_long_buf,test_long_buffers)961 ZTEST_USER(uart_async_long_buf, test_long_buffers)
962 {
963 	size_t tx_len1 = TX_LONG_BUFFER / 2;
964 	size_t tx_len2 = TX_LONG_BUFFER;
965 
966 	memset(long_rx_buf, 0, sizeof(long_rx_buf));
967 	memset(long_tx_buf, 1, sizeof(long_tx_buf));
968 
969 	uart_rx_enable(uart_dev, long_rx_buf, sizeof(long_rx_buf), 10 * USEC_PER_MSEC);
970 
971 	uart_tx(uart_dev, long_tx_buf, tx_len1, 200 * USEC_PER_MSEC);
972 	zassert_equal(k_sem_take(&tx_done, K_MSEC(200)), 0, "TX_DONE timeout");
973 	zassert_equal(k_sem_take(&rx_rdy, K_MSEC(200)), 0, "RX_RDY timeout");
974 	zassert_equal(long_received[0], tx_len1, "Wrong number of bytes received.");
975 	zassert_equal(memcmp(long_tx_buf, long_rx_buf, tx_len1),
976 		      0,
977 		      "Buffers not equal");
978 	k_msleep(10);
979 	/* Check if instance is releasing a buffer after the timeout. */
980 	bool release_on_timeout = k_sem_take(&rx_buf_released, K_NO_WAIT) == 0;
981 
982 	evt_num = 0;
983 	uart_tx(uart_dev, long_tx_buf, tx_len2, 200 * USEC_PER_MSEC);
984 	zassert_equal(k_sem_take(&tx_done, K_MSEC(200)), 0, "TX_DONE timeout");
985 	zassert_equal(k_sem_take(&rx_rdy, K_MSEC(200)), 0, "RX_RDY timeout");
986 
987 	if (release_on_timeout) {
988 		zassert_equal(long_received[0], tx_len2, "Wrong number of bytes received.");
989 		zassert_equal(memcmp(long_tx_buf, long_rx_buf2, long_received[0]), 0,
990 			      "Buffers not equal");
991 	} else {
992 		zassert_equal(k_sem_take(&rx_rdy, K_MSEC(200)), 0, "RX_RDY timeout");
993 		zassert_equal(long_received[0], RX_LONG_BUFFER - tx_len1,
994 				"Wrong number of bytes received.");
995 		zassert_equal(long_received[1], tx_len2 - (RX_LONG_BUFFER - tx_len1),
996 				"Wrong number of bytes received.");
997 		zassert_equal(memcmp(long_tx_buf, long_rx_buf + tx_len1, long_received[0]), 0,
998 			      "Buffers not equal");
999 		zassert_equal(memcmp(long_tx_buf, long_rx_buf2, long_received[1]), 0,
1000 			      "Buffers not equal");
1001 	}
1002 
1003 	uart_rx_disable(uart_dev);
1004 	zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)),
1005 		      0,
1006 		      "RX_BUF_RELEASED timeout");
1007 	zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
1008 		      "RX_DISABLED timeout");
1009 }
1010 
1011 ZTEST_SUITE(uart_async_single_read, NULL, single_read_setup,
1012 		NULL, NULL, NULL);
1013 
1014 ZTEST_SUITE(uart_async_multi_rx, NULL, multiple_rx_enable_setup,
1015 		NULL, NULL, NULL);
1016 
1017 ZTEST_SUITE(uart_async_chain_read, NULL, chained_read_setup,
1018 		NULL, NULL, NULL);
1019 
1020 ZTEST_SUITE(uart_async_double_buf, NULL, double_buffer_setup,
1021 		NULL, NULL, NULL);
1022 
1023 ZTEST_SUITE(uart_async_read_abort, NULL, read_abort_setup,
1024 		NULL, NULL, NULL);
1025 
1026 ZTEST_SUITE(uart_async_chain_write, NULL, chained_write_setup,
1027 		NULL, NULL, NULL);
1028 
1029 ZTEST_SUITE(uart_async_long_buf, NULL, long_buffers_setup,
1030 		NULL, NULL, NULL);
1031 
1032 ZTEST_SUITE(uart_async_write_abort, NULL, write_abort_setup,
1033 		NULL, NULL, NULL);
1034 
1035 ZTEST_SUITE(uart_async_timeout, NULL, forever_timeout_setup,
1036 		NULL, NULL, NULL);
1037 
test_main(void)1038 void test_main(void)
1039 {
1040 	/* Run all suites for each dut UART. Setup function for each suite is picking
1041 	 * next UART from the array.
1042 	 */
1043 	ztest_run_all(NULL, false, ARRAY_SIZE(duts), 1);
1044 	ztest_verify_all_test_suites_ran();
1045 }
1046