1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "test_uart.h"
8 
9 static const char *poll_data = "This is a POLL test.\r\n";
10 
test_poll_in(void)11 static int test_poll_in(void)
12 {
13 	unsigned char recv_char;
14 	const struct device *const uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
15 
16 	if (!device_is_ready(uart_dev)) {
17 		TC_PRINT("UART device not ready\n");
18 		return TC_FAIL;
19 	}
20 
21 	TC_PRINT("Please send characters to serial console\n");
22 
23 	/* Verify uart_poll_in() */
24 	while (1) {
25 		while (uart_poll_in(uart_dev, &recv_char) < 0) {
26 			/* Allow other thread/workqueue to work. */
27 			k_yield();
28 		}
29 
30 		TC_PRINT("%c", recv_char);
31 
32 		if ((recv_char == '\n') || (recv_char == '\r')) {
33 			break;
34 		}
35 	}
36 
37 	return TC_PASS;
38 }
39 
test_poll_out(void)40 static int test_poll_out(void)
41 {
42 	int i;
43 	const struct device *const uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
44 
45 	if (!device_is_ready(uart_dev)) {
46 		TC_PRINT("UART device not ready\n");
47 		return TC_FAIL;
48 	}
49 
50 	/* Verify uart_poll_out() */
51 	for (i = 0; i < strlen(poll_data); i++) {
52 		uart_poll_out(uart_dev, poll_data[i]);
53 	}
54 
55 	return TC_PASS;
56 }
57 
58 #if CONFIG_SHELL
test_uart_poll_out(void)59 void test_uart_poll_out(void)
60 #else
61 ZTEST(uart_basic_api, test_uart_poll_out)
62 #endif
63 {
64 	zassert_true(test_poll_out() == TC_PASS);
65 }
66 
67 #if CONFIG_SHELL
test_uart_poll_in(void)68 void test_uart_poll_in(void)
69 #else
70 ZTEST(uart_basic_api, test_uart_poll_in)
71 #endif
72 {
73 	zassert_true(test_poll_in() == TC_PASS);
74 }
75