diff --git a/tests/drivers/uart/prj.conf b/tests/drivers/uart/prj.conf deleted file mode 100644 index 7142f4666dc..00000000000 --- a/tests/drivers/uart/prj.conf +++ /dev/null @@ -1,2 +0,0 @@ -CONFIG_SERIAL=y -CONFIG_UART_INTERRUPT_DRIVEN=y \ No newline at end of file diff --git a/tests/drivers/uart/src/Makefile b/tests/drivers/uart/src/Makefile deleted file mode 100644 index 00066e15678..00000000000 --- a/tests/drivers/uart/src/Makefile +++ /dev/null @@ -1 +0,0 @@ -obj-y = main.o diff --git a/tests/drivers/uart/src/main.c b/tests/drivers/uart/src/main.c deleted file mode 100644 index 5470d49811c..00000000000 --- a/tests/drivers/uart/src/main.c +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include -#include -#include - -static const char *banner1 = "Send any character to the UART device\r\n"; -static const char *banner2 = "Character read:\r\n"; - -static volatile bool data_transmitted; -static volatile bool data_arrived; -static char new_data; - -static void write_string(struct device *dev, const char *str, int len) -{ - for (int i = 0; i < len; i++) - uart_poll_out(dev, str[i]); -} - -static void test_by_polling(struct device *dev) -{ - unsigned char data; - - write_string(dev, banner1, strlen(banner1)); - - /* Poll in the character */ - while (uart_poll_in(dev, &data) == -1) - ; - - write_string(dev, banner2, strlen(banner2)); - write_string(dev, &data, 1); - write_string(dev, "\r\n", 2); -} - -static void interrupt_handler(struct device *dev) -{ - uart_irq_update(dev); - - if (uart_irq_tx_ready(dev)) { - data_transmitted = true; - } - - if (uart_irq_rx_ready(dev)) { - uart_fifo_read(dev, &new_data, 1); - data_arrived = true; - } -} - -static void read_char_irq(struct device *dev, char *data) -{ - uart_irq_rx_enable(dev); - - data_arrived = false; - while (data_arrived == false) - ; - *data = new_data; - - uart_irq_rx_disable(dev); -} - -static void write_buf_irq(struct device *dev, const char *buf, int len) -{ - int i; - - uart_irq_tx_enable(dev); - - for (i = 0; i < len; i++) { - data_transmitted = false; - while (uart_fifo_fill(dev, &buf[i], 1) == 0) - ; - while (data_transmitted == false) - ; - } - - uart_irq_tx_disable(dev); -} - -static void test_by_irq(struct device *dev) -{ - char data; - - uart_irq_callback_set(dev, interrupt_handler); - - write_buf_irq(dev, banner1, strlen(banner1)); - read_char_irq(dev, &data); - write_buf_irq(dev, banner2, strlen(banner2)); - write_buf_irq(dev, &data, sizeof(data)); - write_buf_irq(dev, "\r\n", 2); -} - -void main(void) -{ - struct device *dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); - - test_by_polling(dev); - test_by_irq(dev); -} diff --git a/tests/drivers/uart/Makefile b/tests/drivers/uart/uart_basic_api/Makefile similarity index 55% rename from tests/drivers/uart/Makefile rename to tests/drivers/uart/uart_basic_api/Makefile index 0e0de3a6b29..c194e141542 100644 --- a/tests/drivers/uart/Makefile +++ b/tests/drivers/uart/uart_basic_api/Makefile @@ -1,4 +1,4 @@ BOARD ?= quark_se_c1000_devboard -CONF_FILE = prj.conf +CONF_FILE = prj.conf # set prj_shell.conf if debugging tc include ${ZEPHYR_BASE}/Makefile.test diff --git a/tests/drivers/uart/uart_basic_api/prj.conf b/tests/drivers/uart/uart_basic_api/prj.conf new file mode 100644 index 00000000000..e623e35dc05 --- /dev/null +++ b/tests/drivers/uart/uart_basic_api/prj.conf @@ -0,0 +1,3 @@ +CONFIG_SERIAL=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_ZTEST=y diff --git a/tests/drivers/uart/uart_basic_api/prj_shell.conf b/tests/drivers/uart/uart_basic_api/prj_shell.conf new file mode 100644 index 00000000000..5e570a71920 --- /dev/null +++ b/tests/drivers/uart/uart_basic_api/prj_shell.conf @@ -0,0 +1,6 @@ +CONFIG_SERIAL=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_ZTEST=y + +CONFIG_CONSOLE_HANDLER=y +CONFIG_CONSOLE_SHELL=y diff --git a/tests/drivers/uart/uart_basic_api/src/Makefile b/tests/drivers/uart/uart_basic_api/src/Makefile new file mode 100644 index 00000000000..9bb86f8570b --- /dev/null +++ b/tests/drivers/uart/uart_basic_api/src/Makefile @@ -0,0 +1,3 @@ +include $(ZEPHYR_BASE)/tests/Makefile.test + +obj-y += main.o test_uart_poll.o test_uart_fifo.o diff --git a/tests/drivers/uart/uart_basic_api/src/main.c b/tests/drivers/uart/uart_basic_api/src/main.c new file mode 100644 index 00000000000..3f2d4c13e74 --- /dev/null +++ b/tests/drivers/uart/uart_basic_api/src/main.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @addtogroup t_driver_uart + * @{ + * @defgroup t_uart_basic test_uart_basic_operations + * @} + */ + +#include + +#ifdef CONFIG_CONSOLE_SHELL +TC_CMD_DEFINE(test_uart_fifo_read) +TC_CMD_DEFINE(test_uart_fifo_fill) +TC_CMD_DEFINE(test_uart_poll_in) +TC_CMD_DEFINE(test_uart_poll_out) +#endif + +void test_main(void) +{ +#ifdef CONFIG_CONSOLE_SHELL + /* initialize shell commands */ + static const struct shell_cmd commands[] = { + TC_CMD_ITEM(test_uart_fifo_read), + TC_CMD_ITEM(test_uart_fifo_fill), + TC_CMD_ITEM(test_uart_poll_in), + TC_CMD_ITEM(test_uart_poll_out), + { NULL, NULL } + }; + SHELL_REGISTER("uart", commands); + shell_register_default_module("uart"); +#else + ztest_test_suite(uart_basic_test, + ztest_unit_test(test_uart_fifo_fill), + ztest_unit_test(test_uart_poll_out)); + ztest_run_test_suite(uart_basic_test); +#endif +} diff --git a/tests/drivers/uart/uart_basic_api/src/test_uart.h b/tests/drivers/uart/uart_basic_api/src/test_uart.h new file mode 100644 index 00000000000..089a23d52da --- /dev/null +++ b/tests/drivers/uart/uart_basic_api/src/test_uart.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief UART cases header file + * + * Header file for UART cases + */ + +#ifndef __TEST_UART_H__ +#define __TEST_UART_H__ + +#include +#include + +#define UART_DEVICE_NAME CONFIG_UART_CONSOLE_ON_DEV_NAME + +void test_uart_poll_out(void); +void test_uart_fifo_fill(void); +void test_uart_fifo_read(void); +void test_uart_poll_in(void); + +#endif /* __TEST_UART_H__ */ diff --git a/tests/drivers/uart/uart_basic_api/src/test_uart_fifo.c b/tests/drivers/uart/uart_basic_api/src/test_uart_fifo.c new file mode 100644 index 00000000000..f6f93f1667b --- /dev/null +++ b/tests/drivers/uart/uart_basic_api/src/test_uart_fifo.c @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* + * @addtogroup t_uart_basic + * @{ + * @defgroup t_uart_fifo test_uart_fifo + * @brief TestPurpose: verify UART works well in fifo mode + * @details + * - Test Steps + * - FIFO Output: + * -# Set UART IRQ callback using uart_irq_callback_set(). + * -# Enable UART TX IRQ using uart_irq_tx_enable(). + * -# Output the prepared data using uart_fifo_fill(). + * -# Disable UART TX IRQ using uart_irq_tx_disable(). + * -# Compare the number of characters sent out with the + * original data size. + * - FIFO Input: + * -# Set UART IRQ callback using uart_irq_callback_set(). + * -# Enable UART RX IRQ using uart_irq_rx_enable(). + * -# Wait for data sent to UART console and trigger RX IRQ. + * -# Read data from UART console using uart_fifo_read(). + * -# Disable UART TX IRQ using uart_irq_rx_disable(). + * - Expected Results + * -# When test UART FIFO output, the number of characters actually + * sent out will be equal to the original length of the characters. + * -# When test UART FIFO input, the app will wait for input from UART + * console and exit after receiving one character. + * @} + */ + +#include + +static volatile bool data_transmitted; +static volatile bool data_received; +static int char_sent; +static const char *fifo_data = "This is a FIFO test.\r\n"; + +#define DATA_SIZE strlen(fifo_data) + +static void uart_fifo_callback(struct device *dev) +{ + char recvData; + + /* Verify uart_irq_update() */ + uart_irq_update(dev); + + /* Verify uart_irq_tx_ready() */ + if (uart_irq_tx_ready(dev)) { + data_transmitted = true; + char_sent++; + } + + /* Verify uart_irq_rx_ready() */ + if (uart_irq_rx_ready(dev)) { + /* Verify uart_fifo_read() */ + uart_fifo_read(dev, &recvData, 1); + TC_PRINT("%c", recvData); + + if (recvData == '\n') { + data_received = true; + } + } +} + +static int test_fifo_read(void) +{ + struct device *uart_dev = device_get_binding(UART_DEVICE_NAME); + + /* Verify uart_irq_callback_set() */ + uart_irq_callback_set(uart_dev, uart_fifo_callback); + + /* Enable Tx/Rx interrupt before using fifo */ + /* Verify uart_irq_rx_enable() */ + uart_irq_rx_enable(uart_dev); + + TC_PRINT("Please send characters to serial console\n"); + + data_received = false; + while (data_received == false) + ; + /* Verify uart_irq_rx_disable() */ + uart_irq_rx_disable(uart_dev); + + return TC_PASS; +} + +static int test_fifo_fill(void) +{ + struct device *uart_dev = device_get_binding(UART_DEVICE_NAME); + + char_sent = 0; + + /* Verify uart_irq_callback_set() */ + uart_irq_callback_set(uart_dev, uart_fifo_callback); + + /* Enable Tx/Rx interrupt before using fifo */ + /* Verify uart_irq_tx_enable() */ + uart_irq_tx_enable(uart_dev); + + /* Verify uart_fifo_fill() */ + for (int i = 0; i < DATA_SIZE; i++) { + data_transmitted = false; + while (!uart_fifo_fill(uart_dev, &fifo_data[i], 1)) + ; + while (data_transmitted == false) + ; + } + + /* Verify uart_irq_tx_disable() */ + uart_irq_tx_disable(uart_dev); + + /* strlen() doesn't include \r\n*/ + if (char_sent - 1 == DATA_SIZE) { + return TC_PASS; + } else { + return TC_FAIL; + } + +} + +void test_uart_fifo_fill(void) +{ + assert_true(test_fifo_fill() == TC_PASS, NULL); +} + +void test_uart_fifo_read(void) +{ + assert_true(test_fifo_read() == TC_PASS, NULL); +} diff --git a/tests/drivers/uart/uart_basic_api/src/test_uart_poll.c b/tests/drivers/uart/uart_basic_api/src/test_uart_poll.c new file mode 100644 index 00000000000..93cb663f01d --- /dev/null +++ b/tests/drivers/uart/uart_basic_api/src/test_uart_poll.c @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* + * @addtogroup t_uart_basic + * @{ + * @defgroup t_uart_poll test_uart_poll + * @brief TestPurpose: verify UART works well in poll mode + * @details + * - Test Steps + * - Poll Output: + * -# Output the prepared data using uart_poll_out(), and compare + * the output characters with the original characters. + * - Poll Input: + * -# Wait for data from UART console using uart_poll_in(). User sends + * data to UART console using echo "qwer" > /dev/ttyUSB* and waits + * for uart_poll_in() exit. + * - Expected Results + * -# When test UART poll out, the return value from uart_poll_out() + * will be equal to the character sent out. + * -# When test UART poll in, the app will wait for input from UART + * console and exit after receiving '\n'. + * @} + */ + +#include + +static const char *poll_data = "This is a POLL test.\r\n"; + +static int test_poll_in(void) +{ + unsigned char recvChar; + struct device *uart_dev = device_get_binding(UART_DEVICE_NAME); + + if (!uart_dev) { + TC_PRINT("Cannot get UART device\n"); + return TC_FAIL; + } + + TC_PRINT("Please send characters to serial console\n"); + + /* Verify uart_poll_in() */ + while (1) { + while (uart_poll_in(uart_dev, &recvChar) < 0) + ; + + TC_PRINT("%c", recvChar); + + if (recvChar == '\n') { + break; + } + } + + return TC_PASS; +} + +static int test_poll_out(void) +{ + int i; + unsigned char sentChar; + struct device *uart_dev = device_get_binding(UART_DEVICE_NAME); + + if (!uart_dev) { + TC_PRINT("Cannot get UART device\n"); + return TC_FAIL; + } + + /* Verify uart_poll_out() */ + for (i = 0; i < strlen(poll_data); i++) { + sentChar = uart_poll_out(uart_dev, poll_data[i]); + + if (sentChar != poll_data[i]) { + TC_PRINT("expect send %c, actaul send %c\n", + poll_data[i], sentChar); + return TC_FAIL; + } + } + + return TC_PASS; +} + +void test_uart_poll_out(void) +{ + assert_true(test_poll_out() == TC_PASS, NULL); +} + +void test_uart_poll_in(void) +{ + assert_true(test_poll_in() == TC_PASS, NULL); +} diff --git a/tests/drivers/uart/uart_basic_api/testcase.ini b/tests/drivers/uart/uart_basic_api/testcase.ini new file mode 100644 index 00000000000..997ed122fa6 --- /dev/null +++ b/tests/drivers/uart/uart_basic_api/testcase.ini @@ -0,0 +1,11 @@ +[test_uart] +tags = drivers +arch_whitelist = x86 +platform_whitelist = quark_se_c1000_devboard arduino_101 quark_d2000_crb + +[test_uart_shell] +tags = drivers +build_only = true +arch_whitelist = x86 +extra_args = CONF_FILE=prj_shell.conf +platform_whitelist = quark_se_c1000_devboard arduino_101 quark_d2000_crb