Skip to content

Commit f669de3

Browse files
[nrf fromtree] tests: modem: backends: uart: add hw flow control option
Extend test modem uart backend test suite to support testing hw flow control, which is performed by using a small receive buffer for the modem uart backend, and slowing down the read of received data, ensuring the buffer will be overrun if hw flow control is not working. Signed-off-by: Bjarki Arge Andreasen <[email protected]> (cherry picked from commit 24dd379)
1 parent 1348b3f commit f669de3

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
source "Kconfig.zephyr"
5+
6+
config TEST_HW_FLOW_CONTROL
7+
bool "Test hardware flow control"
8+
help
9+
By default, the test uses backend transmit and receive
10+
buffers of adequate size, and waits until transmitted
11+
data has been fully received before next transmit.
12+
13+
This option limits the backend receive buffer size and
14+
imposes a delay on reading received data, ensuring
15+
transmitted data will be dropped if hw flow control is
16+
not enabled.

tests/subsys/modem/backends/uart/src/main.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#include <stdlib.h>
2626
#include <string.h>
2727

28+
#ifdef CONFIG_TEST_HW_FLOW_CONTROL
29+
#define TEST_BACKEND_RECEIVE_BUFFER_SIZE 256
30+
#else
31+
#define TEST_BACKEND_RECEIVE_BUFFER_SIZE 4096
32+
#endif
33+
2834
/*************************************************************************************************/
2935
/* Mock pipe */
3036
/*************************************************************************************************/
@@ -36,7 +42,7 @@ K_SEM_DEFINE(receive_ready_sem, 0, 1);
3642
/*************************************************************************************************/
3743
/* Buffers */
3844
/*************************************************************************************************/
39-
static uint8_t backend_receive_buffer[4096];
45+
static uint8_t backend_receive_buffer[TEST_BACKEND_RECEIVE_BUFFER_SIZE];
4046
static uint8_t backend_transmit_buffer[4096];
4147
RING_BUF_DECLARE(transmit_ring_buf, 4096);
4248
static uint8_t receive_buffer[4096];
@@ -131,21 +137,32 @@ static int transmit_prng(uint32_t remaining)
131137

132138
static int receive_prng(void)
133139
{
134-
int ret = 0;
140+
int ret;
141+
142+
if (k_sem_take(&receive_ready_sem, K_SECONDS(1))) {
143+
return -ETIMEDOUT;
144+
}
145+
146+
if (IS_ENABLED(CONFIG_TEST_HW_FLOW_CONTROL)) {
147+
k_msleep(100);
148+
}
135149

136-
if (k_sem_take(&receive_ready_sem, K_NO_WAIT) == 0) {
137-
ret = modem_pipe_receive(pipe, receive_buffer, sizeof(receive_buffer));
138-
if (ret < 0) {
150+
ret = modem_pipe_receive(pipe, receive_buffer, sizeof(receive_buffer));
151+
if (ret == 0) {
152+
return -ENODATA;
153+
}
154+
155+
if (ret < 0) {
156+
return -EFAULT;
157+
}
158+
159+
for (uint32_t i = 0; i < (uint32_t)ret; i++) {
160+
if (receive_prng_random() != receive_buffer[i]) {
139161
return -EFAULT;
140162
}
141-
for (uint32_t i = 0; i < (uint32_t)ret; i++) {
142-
if (receive_prng_random() != receive_buffer[i]) {
143-
return -EFAULT;
144-
}
145-
}
146-
printk("RX: %u\n", (uint32_t)ret);
147163
}
148164

165+
printk("RX: %u\n", (uint32_t)ret);
149166
return ret;
150167
}
151168

@@ -157,9 +174,9 @@ static void *test_modem_backend_uart_setup(void)
157174
const struct modem_backend_uart_config config = {
158175
.uart = uart,
159176
.receive_buf = backend_receive_buffer,
160-
.receive_buf_size = 1024,
177+
.receive_buf_size = sizeof(backend_receive_buffer),
161178
.transmit_buf = backend_transmit_buffer,
162-
.transmit_buf_size = 1024,
179+
.transmit_buf_size = sizeof(backend_transmit_buffer),
163180
};
164181

165182
pipe = modem_backend_uart_init(&uart_backend, &config);
@@ -172,12 +189,12 @@ static void test_modem_backend_uart_before(void *f)
172189
prng_reset();
173190
ring_buf_reset(&transmit_ring_buf);
174191
k_sem_reset(&receive_ready_sem);
175-
__ASSERT_NO_MSG(modem_pipe_open(pipe, K_SECONDS(10)) == 0);
192+
__ASSERT_NO_MSG(modem_pipe_open(pipe, K_SECONDS(1)) == 0);
176193
}
177194

178195
static void test_modem_backend_uart_after(void *f)
179196
{
180-
__ASSERT_NO_MSG(modem_pipe_close(pipe, K_SECONDS(10)) == 0);
197+
__ASSERT_NO_MSG(modem_pipe_close(pipe, K_SECONDS(1)) == 0);
181198
}
182199

183200
/*************************************************************************************************/
@@ -201,7 +218,6 @@ ZTEST(modem_backend_uart_suite, test_transmit_receive)
201218
ret = receive_prng();
202219
zassert(ret > -1, "Received data is corrupted");
203220
received += (uint32_t)ret;
204-
k_yield();
205221
}
206222
}
207223
}

0 commit comments

Comments
 (0)