Skip to content

Commit 18fc88d

Browse files
ananglnashif
authored andcommitted
tests: uart_async_api: Add multiple_rx_enable test case
Add a test case that ensures that uart_rx_enable() can be successfully called after RX is disabled with uart_rx_disable() and also when it is disabled automatically after the provided RX buffer is filled up. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent c80589a commit 18fc88d

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

tests/drivers/uart/uart_async_api/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ void test_main(void)
2424
ztest_test_suite(uart_async_test,
2525
ztest_unit_test(test_single_read_setup),
2626
ztest_user_unit_test(test_single_read),
27+
ztest_unit_test(test_multiple_rx_enable_setup),
28+
ztest_user_unit_test(test_multiple_rx_enable),
2729
ztest_unit_test(test_chained_read_setup),
2830
ztest_user_unit_test(test_chained_read),
2931
ztest_unit_test(test_double_buffer_setup),

tests/drivers/uart/uart_async_api/src/test_uart.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
void init_test(void);
5656

5757
void test_single_read(void);
58+
void test_multiple_rx_enable(void);
5859
void test_chained_read(void);
5960
void test_double_buffer(void);
6061
void test_read_abort(void);
@@ -64,6 +65,7 @@ void test_long_buffers(void);
6465
void test_chained_write(void);
6566

6667
void test_single_read_setup(void);
68+
void test_multiple_rx_enable_setup(void);
6769
void test_chained_read_setup(void);
6870
void test_double_buffer_setup(void);
6971
void test_read_abort_setup(void);

tests/drivers/uart/uart_async_api/src/test_uart_async.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,104 @@ void test_single_read(void)
103103
zassert_equal(tx_aborted_count, 0, "TX aborted triggered");
104104
}
105105

106+
void test_multiple_rx_enable_setup(void)
107+
{
108+
tx_aborted_count = 0;
109+
110+
/* Reuse the callback from the single_read test case, as this test case
111+
* does not need anything extra in this regard.
112+
*/
113+
uart_callback_set(uart_dev,
114+
test_single_read_callback,
115+
(void *)&tx_aborted_count);
116+
117+
k_sem_reset(&rx_rdy);
118+
k_sem_reset(&rx_buf_released);
119+
k_sem_reset(&rx_disabled);
120+
k_sem_reset(&tx_done);
121+
}
122+
123+
void test_multiple_rx_enable(void)
124+
{
125+
/* Check also if sending from read only memory (e.g. flash) works. */
126+
static const uint8_t tx_buf[] = "test";
127+
uint8_t rx_buf[sizeof(tx_buf)] = {0};
128+
int ret;
129+
130+
/* Enable RX without a timeout. */
131+
ret = uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), SYS_FOREVER_US);
132+
zassert_equal(ret, 0, "uart_rx_enable failed");
133+
zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
134+
"RX_RDY not expected at this point");
135+
zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), -EAGAIN,
136+
"RX_DISABLED not expected at this point");
137+
138+
/* Disable RX before any data has been received. */
139+
ret = uart_rx_disable(uart_dev);
140+
zassert_equal(ret, 0, "uart_rx_disable failed");
141+
zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
142+
"RX_RDY not expected at this point");
143+
zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)), 0,
144+
"RX_BUF_RELEASED timeout");
145+
zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
146+
"RX_DISABLED timeout");
147+
148+
k_sem_reset(&rx_buf_released);
149+
k_sem_reset(&rx_disabled);
150+
151+
/* Check that RX can be reenabled after "manual" disabling. */
152+
ret = uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf),
153+
50 * USEC_PER_MSEC);
154+
zassert_equal(ret, 0, "uart_rx_enable failed");
155+
zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
156+
"RX_RDY not expected at this point");
157+
158+
/* Send enough data to completely fill RX buffer, so that RX ends. */
159+
ret = uart_tx(uart_dev, tx_buf, sizeof(tx_buf), 100 * USEC_PER_MSEC);
160+
zassert_equal(ret, 0, "uart_tx failed");
161+
zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
162+
zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
163+
zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
164+
"Extra RX_RDY received");
165+
zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)), 0,
166+
"RX_BUF_RELEASED timeout");
167+
zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
168+
"RX_DISABLED timeout");
169+
zassert_equal(tx_aborted_count, 0, "Unexpected TX abort");
170+
171+
zassert_equal(memcmp(tx_buf, rx_buf, sizeof(tx_buf)), 0,
172+
"Buffers not equal");
173+
174+
k_sem_reset(&rx_rdy);
175+
k_sem_reset(&rx_buf_released);
176+
k_sem_reset(&rx_disabled);
177+
k_sem_reset(&tx_done);
178+
memset(rx_buf, 0, sizeof(rx_buf));
179+
180+
/* Check that RX can be reenabled after automatic disabling. */
181+
ret = uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf),
182+
50 * USEC_PER_MSEC);
183+
zassert_equal(ret, 0, "uart_rx_enable failed");
184+
zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
185+
"RX_RDY not expected at this point");
186+
187+
/* Fill RX buffer again to confirm that RX still works properly. */
188+
ret = uart_tx(uart_dev, tx_buf, sizeof(tx_buf), 100 * USEC_PER_MSEC);
189+
zassert_equal(ret, 0, "uart_tx failed");
190+
zassert_equal(k_sem_take(&tx_done, K_MSEC(100)), 0, "TX_DONE timeout");
191+
zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), 0, "RX_RDY timeout");
192+
zassert_equal(k_sem_take(&rx_rdy, K_MSEC(100)), -EAGAIN,
193+
"Extra RX_RDY received");
194+
zassert_equal(k_sem_take(&rx_buf_released, K_MSEC(100)), 0,
195+
"RX_BUF_RELEASED timeout");
196+
zassert_equal(k_sem_take(&rx_disabled, K_MSEC(100)), 0,
197+
"RX_DISABLED timeout");
198+
zassert_equal(tx_aborted_count, 0, "Unexpected TX abort");
199+
200+
zassert_equal(memcmp(tx_buf, rx_buf, sizeof(tx_buf)), 0,
201+
"Buffers not equal");
202+
}
203+
106204
ZTEST_BMEM uint8_t chained_read_buf0[10];
107205
ZTEST_BMEM uint8_t chained_read_buf1[20];
108206
ZTEST_BMEM uint8_t chained_read_buf2[30];

0 commit comments

Comments
 (0)