|
| 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