tests: add zephyr uart driver api test case
This commit verifies the following uart driver apis: uart_irq_callback_set() uart_irq_rx_enable() uart_irq_rx_disable() uart_irq_rx_ready() uart_irq_tx_enable() uart_irq_tx_disable() uart_irq_tx_ready() uart_fifo_fill() uart_fifo_read() uart_irq_update() uart_poll_in() uart_poll_out() Change-Id: I9be9341aee4357f86a2bc49f19733fb84273e89c Signed-off-by: Qiu Peiyang <peiyangx.qiu@intel.com>
This commit is contained in:
parent
4d096a44a1
commit
34b53f181f
12 changed files with 319 additions and 107 deletions
|
@ -1,2 +0,0 @@
|
|||
CONFIG_SERIAL=y
|
||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
|
@ -1 +0,0 @@
|
|||
obj-y = main.o
|
|
@ -1,103 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <device.h>
|
||||
#include <uart.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
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);
|
||||
}
|
|
@ -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
|
3
tests/drivers/uart/uart_basic_api/prj.conf
Normal file
3
tests/drivers/uart/uart_basic_api/prj.conf
Normal file
|
@ -0,0 +1,3 @@
|
|||
CONFIG_SERIAL=y
|
||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||
CONFIG_ZTEST=y
|
6
tests/drivers/uart/uart_basic_api/prj_shell.conf
Normal file
6
tests/drivers/uart/uart_basic_api/prj_shell.conf
Normal file
|
@ -0,0 +1,6 @@
|
|||
CONFIG_SERIAL=y
|
||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||
CONFIG_ZTEST=y
|
||||
|
||||
CONFIG_CONSOLE_HANDLER=y
|
||||
CONFIG_CONSOLE_SHELL=y
|
3
tests/drivers/uart/uart_basic_api/src/Makefile
Normal file
3
tests/drivers/uart/uart_basic_api/src/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
include $(ZEPHYR_BASE)/tests/Makefile.test
|
||||
|
||||
obj-y += main.o test_uart_poll.o test_uart_fifo.o
|
42
tests/drivers/uart/uart_basic_api/src/main.c
Normal file
42
tests/drivers/uart/uart_basic_api/src/main.c
Normal file
|
@ -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 <test_uart.h>
|
||||
|
||||
#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
|
||||
}
|
27
tests/drivers/uart/uart_basic_api/src/test_uart.h
Normal file
27
tests/drivers/uart/uart_basic_api/src/test_uart.h
Normal file
|
@ -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 <uart.h>
|
||||
#include <ztest.h>
|
||||
|
||||
#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__ */
|
133
tests/drivers/uart/uart_basic_api/src/test_uart_fifo.c
Normal file
133
tests/drivers/uart/uart_basic_api/src/test_uart_fifo.c
Normal file
|
@ -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 <test_uart.h>
|
||||
|
||||
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);
|
||||
}
|
93
tests/drivers/uart/uart_basic_api/src/test_uart_poll.c
Normal file
93
tests/drivers/uart/uart_basic_api/src/test_uart_poll.c
Normal file
|
@ -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 <test_uart.h>
|
||||
|
||||
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);
|
||||
}
|
11
tests/drivers/uart/uart_basic_api/testcase.ini
Normal file
11
tests/drivers/uart/uart_basic_api/testcase.ini
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue