Skip to content

Commit 95d8deb

Browse files
Jordan Yatescarlescufi
authored andcommitted
modem: modem_iface_uart_async: added
Adds a communications backend based on the asynchronous UART API, instead of the interrupt-driven UART API. The primary advantage of this backend is an improved robustness to dropping bytes under high interrupt or critical section loads. Under all loads system efficiency is improved by: * Reducing the time spent writing out individual bytes. * Reducing the number of UART interrupts fired. * Waking up the RX thread much less often. When utilising this backend over `nordic,nrf-uarte` on a nRF52840, the baudrate of an esp-at modem could be pushed to at least 921600 without dropping bytes, compared to a maximum of 230400 with the interrupt API. Signed-off-by: Jordan Yates <[email protected]>
1 parent 5228de3 commit 95d8deb

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed

drivers/modem/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zephyr_library_sources_ifdef(CONFIG_MODEM_CONTEXT
1111
)
1212

1313
zephyr_library_sources_ifdef(CONFIG_MODEM_IFACE_UART_INTERRUPT modem_iface_uart_interrupt.c)
14+
zephyr_library_sources_ifdef(CONFIG_MODEM_IFACE_UART_ASYNC modem_iface_uart_async.c)
1415
zephyr_library_sources_ifdef(CONFIG_MODEM_CMD_HANDLER modem_cmd_handler.c)
1516
zephyr_library_sources_ifdef(CONFIG_MODEM_SOCKET modem_socket.c)
1617

drivers/modem/Kconfig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,40 @@ config MODEM_IFACE_UART_INTERRUPT
8282
depends on SERIAL_SUPPORT_INTERRUPT
8383
select UART_INTERRUPT_DRIVEN
8484

85+
config MODEM_IFACE_UART_ASYNC
86+
bool "UART-based modem interface using async API"
87+
depends on SERIAL_SUPPORT_ASYNC
88+
select UART_ASYNC_API
89+
8590
endchoice
8691

92+
if MODEM_IFACE_UART_ASYNC
93+
94+
config MODEM_IFACE_UART_ASYNC_RX_BUFFER_SIZE
95+
int "Size in bytes of the RX buffers provided to UART driver"
96+
default 64
97+
help
98+
Increasing this value decreases the number of UART interrupts needed
99+
to receive large packets.
100+
101+
config MODEM_IFACE_UART_ASYNC_RX_NUM_BUFFERS
102+
int "Number of RX buffers available to the UART driver"
103+
default 2
104+
help
105+
This value needs to be twice the number of UART modems using the
106+
driver to avoid buffer starvation.
107+
108+
config MODEM_IFACE_UART_ASYNC_RX_TIMEOUT_US
109+
int "Timeout for flushing RX buffers after receiving no additional data"
110+
default 278
111+
help
112+
Decreasing this value can help increase data throughput when high
113+
baudrates are used. 278us is 4 bytes at 115200 baud. Decreasing this
114+
value too much can result in spurious interrupts. Leaving it too
115+
high can reduce data throughput.
116+
117+
endif # MODEM_IFACE_UART_ASYNC
118+
87119
endif # MODEM_IFACE_UART
88120

89121
config MODEM_CMD_HANDLER

drivers/modem/modem_iface_uart.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ struct modem_iface_uart_data {
3232

3333
/* rx semaphore */
3434
struct k_sem rx_sem;
35+
36+
#ifdef CONFIG_MODEM_IFACE_UART_ASYNC
37+
38+
/* tx semaphore */
39+
struct k_sem tx_sem;
40+
41+
#endif /* CONFIG_MODEM_IFACE_UART_ASYNC */
3542
};
3643

3744
/**
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Copyright (c) 2022, Commonwealth Scientific and Industrial Research
3+
* Organisation (CSIRO) ABN 41 687 119 230.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <zephyr/logging/log.h>
9+
#include <zephyr/kernel.h>
10+
#include <zephyr/drivers/uart.h>
11+
12+
#include "modem_context.h"
13+
#include "modem_iface_uart.h"
14+
15+
LOG_MODULE_REGISTER(modem_iface_uart_async, CONFIG_MODEM_LOG_LEVEL);
16+
17+
#define RX_BUFFER_SIZE CONFIG_MODEM_IFACE_UART_ASYNC_RX_BUFFER_SIZE
18+
#define RX_BUFFER_NUM CONFIG_MODEM_IFACE_UART_ASYNC_RX_NUM_BUFFERS
19+
20+
K_MEM_SLAB_DEFINE(uart_modem_async_rx_slab, RX_BUFFER_SIZE, RX_BUFFER_NUM, 1);
21+
22+
static void iface_uart_async_callback(const struct device *dev,
23+
struct uart_event *evt,
24+
void *user_data)
25+
{
26+
struct modem_iface *iface = user_data;
27+
struct modem_iface_uart_data *data = iface->iface_data;
28+
uint32_t written;
29+
void *buf;
30+
int rc;
31+
32+
switch (evt->type) {
33+
case UART_TX_DONE:
34+
k_sem_give(&data->tx_sem);
35+
break;
36+
case UART_RX_BUF_REQUEST:
37+
/* Allocate next RX buffer for UART driver */
38+
rc = k_mem_slab_alloc(&uart_modem_async_rx_slab, (void **)&buf, K_NO_WAIT);
39+
if (rc < 0) {
40+
/* Major problems, UART_RX_BUF_RELEASED event is not being generated, or
41+
* CONFIG_MODEM_IFACE_UART_ASYNC_RX_NUM_BUFFERS is not large enough.
42+
*/
43+
LOG_ERR("RX buffer starvation");
44+
break;
45+
}
46+
/* Provide the buffer to the UART driver */
47+
uart_rx_buf_rsp(dev, buf, RX_BUFFER_SIZE);
48+
break;
49+
case UART_RX_BUF_RELEASED:
50+
/* UART driver is done with memory, free it */
51+
k_mem_slab_free(&uart_modem_async_rx_slab, (void **)&evt->data.rx_buf.buf);
52+
break;
53+
case UART_RX_RDY:
54+
/* Place received data on the ring buffer */
55+
written = ring_buf_put(&data->rx_rb,
56+
evt->data.rx.buf + evt->data.rx.offset,
57+
evt->data.rx.len);
58+
if (written != evt->data.rx.len) {
59+
LOG_WRN("Received bytes dropped from ring buf");
60+
}
61+
/* Notify upper layer that new data has arrived */
62+
k_sem_give(&data->rx_sem);
63+
break;
64+
default:
65+
break;
66+
}
67+
}
68+
69+
static int modem_iface_uart_async_read(struct modem_iface *iface,
70+
uint8_t *buf, size_t size, size_t *bytes_read)
71+
{
72+
struct modem_iface_uart_data *data;
73+
74+
if (!iface || !iface->iface_data) {
75+
return -EINVAL;
76+
}
77+
78+
if (size == 0) {
79+
*bytes_read = 0;
80+
return 0;
81+
}
82+
83+
/* Pull data off the ring buffer */
84+
data = iface->iface_data;
85+
*bytes_read = ring_buf_get(&data->rx_rb, buf, size);
86+
return 0;
87+
}
88+
89+
static int modem_iface_uart_async_write(struct modem_iface *iface,
90+
const uint8_t *buf, size_t size)
91+
{
92+
struct modem_iface_uart_data *data;
93+
int rc;
94+
95+
if (!iface || !iface->iface_data) {
96+
return -EINVAL;
97+
}
98+
99+
if (size == 0) {
100+
return 0;
101+
}
102+
103+
/* Start the transmission */
104+
rc = uart_tx(iface->dev, buf, size, SYS_FOREVER_MS);
105+
if (rc >= 0) {
106+
/* Wait until the transmission completes */
107+
data = iface->iface_data;
108+
k_sem_take(&data->tx_sem, K_FOREVER);
109+
}
110+
return rc;
111+
}
112+
113+
int modem_iface_uart_init_dev(struct modem_iface *iface,
114+
const struct device *dev)
115+
{
116+
struct modem_iface_uart_data *data;
117+
void *buf;
118+
int rc;
119+
120+
if (!device_is_ready(dev)) {
121+
return -ENODEV;
122+
}
123+
124+
/* Check if there's already a device inited to this iface. If so,
125+
* interrupts needs to be disabled on that too before switching to avoid
126+
* race conditions with modem_iface_uart_isr.
127+
*/
128+
if (iface->dev) {
129+
LOG_WRN("Device %s already inited", iface->dev->name);
130+
uart_rx_disable(iface->dev);
131+
}
132+
133+
iface->dev = dev;
134+
data = iface->iface_data;
135+
136+
/* Configure async UART callback */
137+
rc = uart_callback_set(dev, iface_uart_async_callback, iface);
138+
if (rc < 0) {
139+
LOG_ERR("Failed to set UART callback");
140+
return rc;
141+
}
142+
/* Enable reception permanently on the interface */
143+
k_mem_slab_alloc(&uart_modem_async_rx_slab, (void **)&buf, K_FOREVER);
144+
rc = uart_rx_enable(dev, buf, RX_BUFFER_SIZE, CONFIG_MODEM_IFACE_UART_ASYNC_RX_TIMEOUT_US);
145+
if (rc < 0) {
146+
LOG_ERR("Failed to enable UART RX");
147+
}
148+
return rc;
149+
}
150+
151+
int modem_iface_uart_init(struct modem_iface *iface,
152+
struct modem_iface_uart_data *data,
153+
const struct device *dev)
154+
{
155+
int ret;
156+
157+
if (!iface || !data) {
158+
return -EINVAL;
159+
}
160+
161+
iface->iface_data = data;
162+
iface->read = modem_iface_uart_async_read;
163+
iface->write = modem_iface_uart_async_write;
164+
165+
ring_buf_init(&data->rx_rb, data->rx_rb_buf_len, data->rx_rb_buf);
166+
k_sem_init(&data->rx_sem, 0, 1);
167+
k_sem_init(&data->tx_sem, 0, 1);
168+
169+
/* get UART device */
170+
ret = modem_iface_uart_init_dev(iface, dev);
171+
if (ret < 0) {
172+
iface->iface_data = NULL;
173+
iface->read = NULL;
174+
iface->write = NULL;
175+
176+
return ret;
177+
}
178+
179+
return 0;
180+
}

0 commit comments

Comments
 (0)