1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * @addtogroup t_uart_basic
9  * @{
10  * @defgroup t_uart_config test_uart_config
11  * @brief TestPurpose: verify UART configure API settings
12  * @details
13  * - Test Steps
14  *   - Configure: test_uart_configure( )
15  *   - Configure Get: test_uart_config_get( )
16  * - Expected Results
17  *   -# When test UART CONFIG Configure, the value of configurations actually
18  *      set will be equal to the original configuration values (from device
19  *      tree or run-time configuration to modify those loaded initially from
20  *      device tree)
21  *   -# When test UART CONFIG Configure Get, the app will get/retrieve the
22  *      value of configurations stored at location and to be passed to UART
23  *      CONFIG Configure
24  * @}
25  */
26 
27 #include "test_uart.h"
28 const struct uart_config uart_cfg = {
29 		.baudrate = DT_PROP_OR(DT_CHOSEN(zephyr_console), current_speed, 115200),
30 		.parity = UART_CFG_PARITY_NONE,
31 		.stop_bits = UART_CFG_STOP_BITS_1,
32 		.data_bits = UART_CFG_DATA_BITS_8,
33 		.flow_ctrl = UART_CFG_FLOW_CTRL_NONE
34 	};
35 
test_configure(void)36 static int test_configure(void)
37 {
38 	const struct device *const uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
39 
40 	if (!device_is_ready(uart_dev)) {
41 		TC_PRINT("UART device not ready\n");
42 		return TC_FAIL;
43 	}
44 
45 	/* Verify configure() - set device configuration using data in cfg */
46 	int ret = uart_configure(uart_dev, &uart_cfg);
47 
48 	if (ret == -ENOSYS) {
49 		return TC_SKIP;
50 	}
51 
52 	/* 0 if successful, - error code otherwise */
53 	return (ret == 0) ? TC_PASS : TC_FAIL;
54 
55 }
56 
57 /* test UART configure get (retrieve configuration) */
test_config_get(void)58 static int test_config_get(void)
59 {
60 	struct uart_config uart_cfg_check;
61 	const struct device *const uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
62 
63 	if (!device_is_ready(uart_dev)) {
64 		TC_PRINT("UART device not ready\n");
65 		return TC_FAIL;
66 	}
67 
68 	TC_PRINT("This is a configure_get test.\n");
69 
70 	/* Verify configure() - set device configuration using data in cfg */
71 	/* 0 if successful, - error code otherwise */
72 	int ret = uart_configure(uart_dev, &uart_cfg);
73 
74 	if (ret == -ENOSYS) {
75 		return TC_SKIP;
76 	}
77 
78 	zassert_true(ret == 0, "set config error");
79 
80 	/* Verify config_get() - get device configuration, put in cfg */
81 	/* 0 if successful, - error code otherwise */
82 	/* so get the configurations from the device and check */
83 	ret = uart_config_get(uart_dev, &uart_cfg_check);
84 	zassert_true(ret == 0, "get config error");
85 
86 	/* Confirm the values from device are the values put in cfg*/
87 	if (memcmp(&uart_cfg, &uart_cfg_check, sizeof(uart_cfg)) != 0) {
88 		return TC_FAIL;
89 	} else {
90 		return TC_PASS;
91 	}
92 }
93 
94 #if CONFIG_SHELL
test_uart_configure(void)95 void test_uart_configure(void)
96 #else
97 ZTEST(uart_basic_api, test_uart_configure)
98 #endif
99 {
100 	int ret = test_configure();
101 
102 	zassert_true((ret == TC_PASS) || (ret == TC_SKIP));
103 }
104 
105 #if CONFIG_SHELL
test_uart_config_get(void)106 void test_uart_config_get(void)
107 #else
108 ZTEST(uart_basic_api, test_uart_config_get)
109 #endif
110 {
111 	int ret = test_config_get();
112 
113 	zassert_true((ret == TC_PASS) || (ret == TC_SKIP));
114 }
115