Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 1 | /*! \file soft_uart.c |
| 2 | * Software UART implementation. */ |
| 3 | /* |
| 4 | * (C) 2022 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdbool.h> |
| 23 | #include <stdint.h> |
| 24 | #include <errno.h> |
| 25 | |
| 26 | #include <osmocom/core/timer.h> |
| 27 | #include <osmocom/core/soft_uart.h> |
| 28 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 29 | /*! Internal state of a soft-UART */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 30 | struct osmo_soft_uart { |
| 31 | struct osmo_soft_uart_cfg cfg; |
| 32 | const char *name; |
| 33 | struct { |
| 34 | bool running; |
| 35 | uint8_t bit_count; |
| 36 | uint8_t shift_reg; |
| 37 | struct msgb *msg; |
| 38 | ubit_t parity_bit; |
| 39 | unsigned int flags; |
| 40 | unsigned int status; |
| 41 | struct osmo_timer_list timer; |
| 42 | } rx; |
| 43 | struct { |
| 44 | bool running; |
| 45 | uint8_t bit_count; |
| 46 | uint8_t shift_reg; |
| 47 | struct msgb *msg; |
| 48 | struct llist_head queue; |
| 49 | } tx; |
| 50 | }; |
| 51 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 52 | /*! Default soft-UART configuration (8-N-1) */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 53 | static struct osmo_soft_uart_cfg suart_default_cfg = { |
| 54 | .num_data_bits = 8, |
| 55 | .num_stop_bits = 1, |
| 56 | .parity_mode = OSMO_SUART_PARITY_NONE, |
| 57 | .rx_buf_size = 1024, |
| 58 | .rx_timeout_ms = 100, |
| 59 | .priv = NULL, |
| 60 | .rx_cb = NULL, |
| 61 | .status_change_cb = NULL, |
| 62 | }; |
| 63 | |
| 64 | /************************************************************************* |
| 65 | * Receiver |
| 66 | *************************************************************************/ |
| 67 | |
| 68 | /* flush the receive buffer + allocate new one, as needed */ |
| 69 | static void suart_flush_rx(struct osmo_soft_uart *suart) |
| 70 | { |
| 71 | if ((suart->rx.msg && msgb_length(suart->rx.msg)) || suart->rx.flags) { |
| 72 | osmo_timer_del(&suart->rx.timer); |
| 73 | if (suart->cfg.rx_cb) { |
| 74 | suart->cfg.rx_cb(suart->cfg.priv, suart->rx.msg, suart->rx.flags); |
| 75 | /* call-back has taken ownership of msgb, no need to free() here */ |
| 76 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 77 | } else { |
| 78 | msgb_reset(suart->rx.msg); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* one character was received; add to receive buffer and notify user, if needed */ |
| 84 | static void suart_rx_ch(struct osmo_soft_uart *suart, uint8_t ch) |
| 85 | { |
| 86 | unsigned int msg_len; |
| 87 | |
| 88 | OSMO_ASSERT(suart->rx.msg); |
| 89 | msgb_put_u8(suart->rx.msg, ch); |
| 90 | msg_len = msgb_length(suart->rx.msg); |
| 91 | |
| 92 | /* first character in new message: start timer */ |
| 93 | if (msg_len == 1) { |
| 94 | osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000, |
| 95 | (suart->cfg.rx_timeout_ms % 1000) * 1000); |
| 96 | } else if (msg_len >= suart->cfg.rx_buf_size || suart->rx.flags) { |
| 97 | suart_flush_rx(suart); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* receive a single bit */ |
| 102 | static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit) |
| 103 | { |
| 104 | unsigned int num_parity_bits = 0; |
| 105 | |
| 106 | if (!suart->rx.running) |
| 107 | return; |
| 108 | |
| 109 | if (suart->rx.bit_count == 0) { |
| 110 | /* start bit is 0. Wait if there is none */ |
| 111 | if (bit == 0) { |
| 112 | /* START bit */ |
| 113 | suart->rx.flags = 0; |
| 114 | suart->rx.shift_reg = 0; |
| 115 | suart->rx.bit_count++; |
| 116 | } |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 121 | num_parity_bits = 1; |
| 122 | |
| 123 | suart->rx.bit_count++; |
| 124 | if (suart->rx.bit_count <= 1 + suart->cfg.num_data_bits) { |
| 125 | /* DATA bit */ |
| 126 | suart->rx.shift_reg = suart->rx.shift_reg >> 1; |
| 127 | if (bit) |
| 128 | suart->rx.shift_reg |= 0x80; |
| 129 | } else if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE && |
| 130 | suart->rx.bit_count == 1 + suart->cfg.num_data_bits + 1) { |
| 131 | /* PARITY bit */ |
| 132 | suart->rx.parity_bit = bit; |
| 133 | /* TODO: verify parity */ |
| 134 | //suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 135 | } else if (suart->rx.bit_count <= |
| 136 | 1 + suart->cfg.num_data_bits + num_parity_bits + suart->cfg.num_stop_bits) { |
| 137 | /* STOP bit */ |
| 138 | if (bit != 1) { |
| 139 | fprintf(stderr, "framing error: stop bit %u != 1\n", suart->rx.bit_count); |
| 140 | suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR; |
| 141 | } |
| 142 | |
| 143 | if (suart->rx.bit_count == 1 + suart->cfg.num_data_bits + num_parity_bits + suart->cfg.num_stop_bits) { |
| 144 | //printf("Rx: 0x%02x %c\n", suart->rx.shift_reg, suart->rx.shift_reg); |
| 145 | suart_rx_ch(suart, suart->rx.shift_reg); |
| 146 | suart->rx.bit_count = 0; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* receive timer expiration: flush rx-buffer to user call-back */ |
| 152 | static void suart_rx_timer_cb(void *data) |
| 153 | { |
| 154 | struct osmo_soft_uart *suart = data; |
| 155 | suart_flush_rx(suart); |
| 156 | } |
| 157 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 158 | /*! Feed a number of unpacked bits into the soft-UART receiver. |
| 159 | * \param[in] suart soft-UART instance to feed bits into. |
| 160 | * \param[in] ubits pointer to the unpacked bits. |
| 161 | * \param[in] n_ubits number of unpacked bits to be fed. |
| 162 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 163 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits) |
| 164 | { |
| 165 | for (size_t i = 0; i < n_ubits; i++) |
| 166 | osmo_uart_rx_bit(suart, ubits[i]); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /************************************************************************* |
| 171 | * Transmitter |
| 172 | *************************************************************************/ |
| 173 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 174 | /*! Enqueue the given message buffer into the transmit queue of the soft-UART. |
| 175 | * \param[in] suart soft-UART instance for transmitting data. |
| 176 | * \param[in] tx_data message buffer containing to be transmitted data. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 177 | void osmo_soft_uart_tx(struct osmo_soft_uart *suart, struct msgb *tx_data) |
| 178 | { |
| 179 | if (!suart->tx.msg) |
| 180 | suart->tx.msg = tx_data; |
| 181 | else |
| 182 | msgb_enqueue(&suart->tx.queue, tx_data); |
| 183 | } |
| 184 | |
| 185 | /* pull a single bit out of the UART transmitter */ |
| 186 | static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart) |
| 187 | { |
| 188 | if (!suart->tx.running) |
| 189 | return 1; |
| 190 | |
| 191 | if (suart->tx.bit_count == 0) { |
| 192 | /* do we have anything to transmit? */ |
| 193 | /* FIXME */ |
| 194 | } |
| 195 | /* FIXME */ |
| 196 | return 1; |
| 197 | } |
| 198 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 199 | /*! Pull a number of unpacked bits out of the soft-UART transmitter. |
| 200 | * \param[in] suart soft-UART instance to pull the bits from. |
| 201 | * \param[out] ubits pointer to a buffer where to store pulled bits. |
| 202 | * \param[in] n_ubits number of unpacked bits to be pulled. |
| 203 | * \returns number of unpacked bits pulled; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 204 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits) |
| 205 | { |
| 206 | for (size_t i = 0; i < n_ubits; i++) |
| 207 | ubits[i] = osmo_uart_tx_bit(suart); |
| 208 | return n_ubits; |
| 209 | } |
| 210 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 211 | /*! Set the modem status lines of the given soft-UART. |
| 212 | * \param[in] suart soft-UART instance to update the modem status. |
| 213 | * \param[in] status mask of osmo_soft_uart_status. |
| 214 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 215 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status) |
| 216 | { |
| 217 | /* FIXME: Tx */ |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /************************************************************************* |
| 223 | * Management / Initialization |
| 224 | *************************************************************************/ |
| 225 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 226 | /*! Allocate a soft-UART instance. |
| 227 | * \param[in] ctx parent talloc context. |
| 228 | * \param[in] name name of the soft-UART instance. |
| 229 | * \returns pointer to allocated soft-UART instance; NULL on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 230 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name) |
| 231 | { |
| 232 | struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart); |
| 233 | if (!suart) |
| 234 | return NULL; |
| 235 | suart->name = talloc_strdup(suart, name); |
| 236 | suart->cfg = suart_default_cfg; |
| 237 | |
| 238 | return suart; |
| 239 | } |
| 240 | |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 241 | /*! Release memory taken by the given soft-UART. |
| 242 | * \param[in] suart soft-UART instance to be free()d. */ |
| 243 | void osmo_soft_uart_free(struct osmo_soft_uart *suart) |
| 244 | { |
| 245 | if (suart == NULL) |
| 246 | return; |
| 247 | |
| 248 | osmo_timer_del(&suart->rx.timer); |
| 249 | msgb_free(suart->rx.msg); |
| 250 | |
| 251 | talloc_free((void *)suart->name); |
| 252 | talloc_free(suart); |
| 253 | } |
| 254 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 255 | /*! Change soft-UART configuration to the user-provided config. |
| 256 | * \param[in] suart soft-UART instance to be re-configured. |
| 257 | * \param[in] cfg the user-provided config to be applied. |
| 258 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 259 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg) |
| 260 | { |
| 261 | /* consistency checks on the configuration */ |
| 262 | if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0) |
| 263 | return -EINVAL; |
| 264 | if (cfg->num_stop_bits == 0) |
| 265 | return -EINVAL; |
| 266 | if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM) |
| 267 | return -EINVAL; |
| 268 | if (cfg->rx_buf_size == 0) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | if (suart->cfg.rx_buf_size > cfg->rx_buf_size || |
| 272 | suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) { |
| 273 | suart_flush_rx(suart); |
| 274 | } |
| 275 | |
| 276 | suart->cfg = *cfg; |
| 277 | |
| 278 | osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart); |
| 279 | INIT_LLIST_HEAD(&suart->tx.queue); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame^] | 284 | /*! Enable/disable receiver and/or transmitter of the given soft-UART. |
| 285 | * \param[in] suart soft-UART instance to be re-configured. |
| 286 | * \param[in] rx enable/disable state of the receiver. |
| 287 | * \param[in] tx enable/disable state of the transmitter. |
| 288 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 289 | int osmo_soft_uart_enable(struct osmo_soft_uart *suart, bool rx, bool tx) |
| 290 | { |
| 291 | if (!rx && suart->rx.running) { |
| 292 | suart_flush_rx(suart); |
| 293 | suart->rx.running = false; |
| 294 | } else if (rx && !suart->rx.running) { |
| 295 | if (!suart->rx.msg) |
| 296 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 297 | suart->rx.running = true; |
| 298 | } |
| 299 | |
| 300 | if (!tx && suart->tx.running) { |
| 301 | /* FIXME: Tx */ |
| 302 | suart->tx.running = false; |
| 303 | } else if (tx && !suart->tx.running) { |
| 304 | suart->tx.running = true; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |