Skip to content

Commit 6352fc9

Browse files
QiuPeiyangnashif
authored andcommitted
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 <[email protected]>
1 parent e769377 commit 6352fc9

File tree

12 files changed

+319
-107
lines changed

12 files changed

+319
-107
lines changed

tests/drivers/uart/prj.conf

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/drivers/uart/src/Makefile

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/drivers/uart/src/main.c

Lines changed: 0 additions & 103 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
BOARD ?= quark_se_c1000_devboard
2-
CONF_FILE = prj.conf
2+
CONF_FILE = prj.conf # set prj_shell.conf if debugging tc
33

44
include ${ZEPHYR_BASE}/Makefile.test
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_SERIAL=y
2+
CONFIG_UART_INTERRUPT_DRIVEN=y
3+
CONFIG_ZTEST=y
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CONFIG_SERIAL=y
2+
CONFIG_UART_INTERRUPT_DRIVEN=y
3+
CONFIG_ZTEST=y
4+
5+
CONFIG_CONSOLE_HANDLER=y
6+
CONFIG_CONSOLE_SHELL=y
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include $(ZEPHYR_BASE)/tests/Makefile.test
2+
3+
obj-y += main.o test_uart_poll.o test_uart_fifo.o
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @addtogroup t_driver_uart
9+
* @{
10+
* @defgroup t_uart_basic test_uart_basic_operations
11+
* @}
12+
*/
13+
14+
#include <test_uart.h>
15+
16+
#ifdef CONFIG_CONSOLE_SHELL
17+
TC_CMD_DEFINE(test_uart_fifo_read)
18+
TC_CMD_DEFINE(test_uart_fifo_fill)
19+
TC_CMD_DEFINE(test_uart_poll_in)
20+
TC_CMD_DEFINE(test_uart_poll_out)
21+
#endif
22+
23+
void test_main(void)
24+
{
25+
#ifdef CONFIG_CONSOLE_SHELL
26+
/* initialize shell commands */
27+
static const struct shell_cmd commands[] = {
28+
TC_CMD_ITEM(test_uart_fifo_read),
29+
TC_CMD_ITEM(test_uart_fifo_fill),
30+
TC_CMD_ITEM(test_uart_poll_in),
31+
TC_CMD_ITEM(test_uart_poll_out),
32+
{ NULL, NULL }
33+
};
34+
SHELL_REGISTER("uart", commands);
35+
shell_register_default_module("uart");
36+
#else
37+
ztest_test_suite(uart_basic_test,
38+
ztest_unit_test(test_uart_fifo_fill),
39+
ztest_unit_test(test_uart_poll_out));
40+
ztest_run_test_suite(uart_basic_test);
41+
#endif
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief UART cases header file
10+
*
11+
* Header file for UART cases
12+
*/
13+
14+
#ifndef __TEST_UART_H__
15+
#define __TEST_UART_H__
16+
17+
#include <uart.h>
18+
#include <ztest.h>
19+
20+
#define UART_DEVICE_NAME CONFIG_UART_CONSOLE_ON_DEV_NAME
21+
22+
void test_uart_poll_out(void);
23+
void test_uart_fifo_fill(void);
24+
void test_uart_fifo_read(void);
25+
void test_uart_poll_in(void);
26+
27+
#endif /* __TEST_UART_H__ */
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* @addtogroup t_uart_basic
9+
* @{
10+
* @defgroup t_uart_fifo test_uart_fifo
11+
* @brief TestPurpose: verify UART works well in fifo mode
12+
* @details
13+
* - Test Steps
14+
* - FIFO Output:
15+
* -# Set UART IRQ callback using uart_irq_callback_set().
16+
* -# Enable UART TX IRQ using uart_irq_tx_enable().
17+
* -# Output the prepared data using uart_fifo_fill().
18+
* -# Disable UART TX IRQ using uart_irq_tx_disable().
19+
* -# Compare the number of characters sent out with the
20+
* original data size.
21+
* - FIFO Input:
22+
* -# Set UART IRQ callback using uart_irq_callback_set().
23+
* -# Enable UART RX IRQ using uart_irq_rx_enable().
24+
* -# Wait for data sent to UART console and trigger RX IRQ.
25+
* -# Read data from UART console using uart_fifo_read().
26+
* -# Disable UART TX IRQ using uart_irq_rx_disable().
27+
* - Expected Results
28+
* -# When test UART FIFO output, the number of characters actually
29+
* sent out will be equal to the original length of the characters.
30+
* -# When test UART FIFO input, the app will wait for input from UART
31+
* console and exit after receiving one character.
32+
* @}
33+
*/
34+
35+
#include <test_uart.h>
36+
37+
static volatile bool data_transmitted;
38+
static volatile bool data_received;
39+
static int char_sent;
40+
static const char *fifo_data = "This is a FIFO test.\r\n";
41+
42+
#define DATA_SIZE strlen(fifo_data)
43+
44+
static void uart_fifo_callback(struct device *dev)
45+
{
46+
char recvData;
47+
48+
/* Verify uart_irq_update() */
49+
uart_irq_update(dev);
50+
51+
/* Verify uart_irq_tx_ready() */
52+
if (uart_irq_tx_ready(dev)) {
53+
data_transmitted = true;
54+
char_sent++;
55+
}
56+
57+
/* Verify uart_irq_rx_ready() */
58+
if (uart_irq_rx_ready(dev)) {
59+
/* Verify uart_fifo_read() */
60+
uart_fifo_read(dev, &recvData, 1);
61+
TC_PRINT("%c", recvData);
62+
63+
if (recvData == '\n') {
64+
data_received = true;
65+
}
66+
}
67+
}
68+
69+
static int test_fifo_read(void)
70+
{
71+
struct device *uart_dev = device_get_binding(UART_DEVICE_NAME);
72+
73+
/* Verify uart_irq_callback_set() */
74+
uart_irq_callback_set(uart_dev, uart_fifo_callback);
75+
76+
/* Enable Tx/Rx interrupt before using fifo */
77+
/* Verify uart_irq_rx_enable() */
78+
uart_irq_rx_enable(uart_dev);
79+
80+
TC_PRINT("Please send characters to serial console\n");
81+
82+
data_received = false;
83+
while (data_received == false)
84+
;
85+
/* Verify uart_irq_rx_disable() */
86+
uart_irq_rx_disable(uart_dev);
87+
88+
return TC_PASS;
89+
}
90+
91+
static int test_fifo_fill(void)
92+
{
93+
struct device *uart_dev = device_get_binding(UART_DEVICE_NAME);
94+
95+
char_sent = 0;
96+
97+
/* Verify uart_irq_callback_set() */
98+
uart_irq_callback_set(uart_dev, uart_fifo_callback);
99+
100+
/* Enable Tx/Rx interrupt before using fifo */
101+
/* Verify uart_irq_tx_enable() */
102+
uart_irq_tx_enable(uart_dev);
103+
104+
/* Verify uart_fifo_fill() */
105+
for (int i = 0; i < DATA_SIZE; i++) {
106+
data_transmitted = false;
107+
while (!uart_fifo_fill(uart_dev, &fifo_data[i], 1))
108+
;
109+
while (data_transmitted == false)
110+
;
111+
}
112+
113+
/* Verify uart_irq_tx_disable() */
114+
uart_irq_tx_disable(uart_dev);
115+
116+
/* strlen() doesn't include \r\n*/
117+
if (char_sent - 1 == DATA_SIZE) {
118+
return TC_PASS;
119+
} else {
120+
return TC_FAIL;
121+
}
122+
123+
}
124+
125+
void test_uart_fifo_fill(void)
126+
{
127+
assert_true(test_fifo_fill() == TC_PASS, NULL);
128+
}
129+
130+
void test_uart_fifo_read(void)
131+
{
132+
assert_true(test_fifo_read() == TC_PASS, NULL);
133+
}

0 commit comments

Comments
 (0)