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 | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame^] | 29 | /*! Rx/Tx flow state of a soft-UART */ |
| 30 | enum suart_flow_state { |
| 31 | SUART_FLOW_ST_IDLE, /*!< waiting for a start bit or Tx data */ |
| 32 | SUART_FLOW_ST_DATA, /*!< receiving/transmitting data bits */ |
| 33 | SUART_FLOW_ST_PARITY, /*!< receiving/transmitting parity bits */ |
| 34 | SUART_FLOW_ST_STOP, /*!< receiving/transmitting stop bits */ |
| 35 | }; |
| 36 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 37 | /*! Internal state of a soft-UART */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 38 | struct osmo_soft_uart { |
| 39 | struct osmo_soft_uart_cfg cfg; |
| 40 | const char *name; |
| 41 | struct { |
| 42 | bool running; |
| 43 | uint8_t bit_count; |
| 44 | uint8_t shift_reg; |
| 45 | struct msgb *msg; |
| 46 | ubit_t parity_bit; |
| 47 | unsigned int flags; |
| 48 | unsigned int status; |
| 49 | struct osmo_timer_list timer; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame^] | 50 | enum suart_flow_state flow_state; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 51 | } rx; |
| 52 | struct { |
| 53 | bool running; |
| 54 | uint8_t bit_count; |
| 55 | uint8_t shift_reg; |
| 56 | struct msgb *msg; |
| 57 | struct llist_head queue; |
| 58 | } tx; |
| 59 | }; |
| 60 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 61 | /*! Default soft-UART configuration (8-N-1) */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 62 | const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 63 | .num_data_bits = 8, |
| 64 | .num_stop_bits = 1, |
| 65 | .parity_mode = OSMO_SUART_PARITY_NONE, |
| 66 | .rx_buf_size = 1024, |
| 67 | .rx_timeout_ms = 100, |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /************************************************************************* |
| 71 | * Receiver |
| 72 | *************************************************************************/ |
| 73 | |
| 74 | /* flush the receive buffer + allocate new one, as needed */ |
| 75 | static void suart_flush_rx(struct osmo_soft_uart *suart) |
| 76 | { |
| 77 | if ((suart->rx.msg && msgb_length(suart->rx.msg)) || suart->rx.flags) { |
| 78 | osmo_timer_del(&suart->rx.timer); |
| 79 | if (suart->cfg.rx_cb) { |
| 80 | suart->cfg.rx_cb(suart->cfg.priv, suart->rx.msg, suart->rx.flags); |
| 81 | /* call-back has taken ownership of msgb, no need to free() here */ |
| 82 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 83 | } else { |
| 84 | msgb_reset(suart->rx.msg); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /* one character was received; add to receive buffer and notify user, if needed */ |
| 90 | static void suart_rx_ch(struct osmo_soft_uart *suart, uint8_t ch) |
| 91 | { |
| 92 | unsigned int msg_len; |
| 93 | |
| 94 | OSMO_ASSERT(suart->rx.msg); |
| 95 | msgb_put_u8(suart->rx.msg, ch); |
| 96 | msg_len = msgb_length(suart->rx.msg); |
| 97 | |
| 98 | /* first character in new message: start timer */ |
| 99 | if (msg_len == 1) { |
| 100 | osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000, |
| 101 | (suart->cfg.rx_timeout_ms % 1000) * 1000); |
| 102 | } else if (msg_len >= suart->cfg.rx_buf_size || suart->rx.flags) { |
| 103 | suart_flush_rx(suart); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* receive a single bit */ |
| 108 | static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit) |
| 109 | { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 110 | if (!suart->rx.running) |
| 111 | return; |
| 112 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame^] | 113 | switch (suart->rx.flow_state) { |
| 114 | case SUART_FLOW_ST_IDLE: |
| 115 | if (bit == 0) { /* start bit condition */ |
| 116 | suart->rx.flow_state = SUART_FLOW_ST_DATA; |
| 117 | suart->rx.flags = 0x00; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 118 | suart->rx.shift_reg = 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 119 | suart->rx.bit_count = 0; |
| 120 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame^] | 121 | break; |
| 122 | case SUART_FLOW_ST_DATA: |
| 123 | suart->rx.bit_count++; |
| 124 | suart->rx.shift_reg >>= 1; |
| 125 | if (bit != 0) |
| 126 | suart->rx.shift_reg |= 0x80; |
| 127 | if (suart->rx.bit_count >= suart->cfg.num_data_bits) { |
| 128 | /* we have accumulated enough data bits */ |
| 129 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 130 | suart->rx.flow_state = SUART_FLOW_ST_PARITY; |
| 131 | else |
| 132 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
| 133 | } |
| 134 | break; |
| 135 | case SUART_FLOW_ST_PARITY: |
| 136 | /* TODO: actually verify parity */ |
| 137 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
| 138 | break; |
| 139 | case SUART_FLOW_ST_STOP: |
| 140 | suart->rx.bit_count++; |
| 141 | if (bit != 1) |
| 142 | suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR; |
| 143 | |
| 144 | if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) { |
| 145 | /* we have accumulated enough stop bits */ |
| 146 | suart_rx_ch(suart, suart->rx.shift_reg); |
| 147 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
| 148 | } |
| 149 | break; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | /* receive timer expiration: flush rx-buffer to user call-back */ |
| 154 | static void suart_rx_timer_cb(void *data) |
| 155 | { |
| 156 | struct osmo_soft_uart *suart = data; |
| 157 | suart_flush_rx(suart); |
| 158 | } |
| 159 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 160 | /*! Feed a number of unpacked bits into the soft-UART receiver. |
| 161 | * \param[in] suart soft-UART instance to feed bits into. |
| 162 | * \param[in] ubits pointer to the unpacked bits. |
| 163 | * \param[in] n_ubits number of unpacked bits to be fed. |
| 164 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 165 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits) |
| 166 | { |
| 167 | for (size_t i = 0; i < n_ubits; i++) |
| 168 | osmo_uart_rx_bit(suart, ubits[i]); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /************************************************************************* |
| 173 | * Transmitter |
| 174 | *************************************************************************/ |
| 175 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 176 | /*! Enqueue the given message buffer into the transmit queue of the soft-UART. |
| 177 | * \param[in] suart soft-UART instance for transmitting data. |
| 178 | * \param[in] tx_data message buffer containing to be transmitted data. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 179 | void osmo_soft_uart_tx(struct osmo_soft_uart *suart, struct msgb *tx_data) |
| 180 | { |
| 181 | if (!suart->tx.msg) |
| 182 | suart->tx.msg = tx_data; |
| 183 | else |
| 184 | msgb_enqueue(&suart->tx.queue, tx_data); |
| 185 | } |
| 186 | |
| 187 | /* pull a single bit out of the UART transmitter */ |
| 188 | static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart) |
| 189 | { |
| 190 | if (!suart->tx.running) |
| 191 | return 1; |
| 192 | |
| 193 | if (suart->tx.bit_count == 0) { |
| 194 | /* do we have anything to transmit? */ |
| 195 | /* FIXME */ |
| 196 | } |
| 197 | /* FIXME */ |
| 198 | return 1; |
| 199 | } |
| 200 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 201 | /*! Pull a number of unpacked bits out of the soft-UART transmitter. |
| 202 | * \param[in] suart soft-UART instance to pull the bits from. |
| 203 | * \param[out] ubits pointer to a buffer where to store pulled bits. |
| 204 | * \param[in] n_ubits number of unpacked bits to be pulled. |
| 205 | * \returns number of unpacked bits pulled; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 206 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits) |
| 207 | { |
| 208 | for (size_t i = 0; i < n_ubits; i++) |
| 209 | ubits[i] = osmo_uart_tx_bit(suart); |
| 210 | return n_ubits; |
| 211 | } |
| 212 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 213 | /*! Set the modem status lines of the given soft-UART. |
| 214 | * \param[in] suart soft-UART instance to update the modem status. |
| 215 | * \param[in] status mask of osmo_soft_uart_status. |
| 216 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 217 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status) |
| 218 | { |
| 219 | /* FIXME: Tx */ |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /************************************************************************* |
| 225 | * Management / Initialization |
| 226 | *************************************************************************/ |
| 227 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 228 | /*! Allocate a soft-UART instance. |
| 229 | * \param[in] ctx parent talloc context. |
| 230 | * \param[in] name name of the soft-UART instance. |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 231 | * \param[in] cfg initial configuration of the soft-UART instance. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 232 | * \returns pointer to allocated soft-UART instance; NULL on error. */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 233 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name, |
| 234 | const struct osmo_soft_uart_cfg *cfg) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 235 | { |
| 236 | struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart); |
| 237 | if (!suart) |
| 238 | return NULL; |
| 239 | suart->name = talloc_strdup(suart, name); |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 240 | |
| 241 | OSMO_ASSERT(cfg != NULL); |
| 242 | suart->cfg = *cfg; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 243 | |
| 244 | return suart; |
| 245 | } |
| 246 | |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 247 | /*! Release memory taken by the given soft-UART. |
| 248 | * \param[in] suart soft-UART instance to be free()d. */ |
| 249 | void osmo_soft_uart_free(struct osmo_soft_uart *suart) |
| 250 | { |
| 251 | if (suart == NULL) |
| 252 | return; |
| 253 | |
| 254 | osmo_timer_del(&suart->rx.timer); |
| 255 | msgb_free(suart->rx.msg); |
| 256 | |
| 257 | talloc_free((void *)suart->name); |
| 258 | talloc_free(suart); |
| 259 | } |
| 260 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 261 | /*! Change soft-UART configuration to the user-provided config. |
| 262 | * \param[in] suart soft-UART instance to be re-configured. |
| 263 | * \param[in] cfg the user-provided config to be applied. |
| 264 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 265 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg) |
| 266 | { |
| 267 | /* consistency checks on the configuration */ |
| 268 | if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0) |
| 269 | return -EINVAL; |
| 270 | if (cfg->num_stop_bits == 0) |
| 271 | return -EINVAL; |
| 272 | if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM) |
| 273 | return -EINVAL; |
| 274 | if (cfg->rx_buf_size == 0) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | if (suart->cfg.rx_buf_size > cfg->rx_buf_size || |
| 278 | suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) { |
| 279 | suart_flush_rx(suart); |
| 280 | } |
| 281 | |
| 282 | suart->cfg = *cfg; |
| 283 | |
| 284 | osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart); |
| 285 | INIT_LLIST_HEAD(&suart->tx.queue); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 290 | /*! Enable/disable receiver of the given soft-UART. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 291 | * \param[in] suart soft-UART instance to be re-configured. |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 292 | * \param[in] enable enable/disable state of the receiver. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 293 | * \returns 0 on success; negative on error. */ |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 294 | int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 295 | { |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 296 | if (!enable && suart->rx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 297 | suart_flush_rx(suart); |
| 298 | suart->rx.running = false; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame^] | 299 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 300 | } else if (enable && !suart->rx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 301 | if (!suart->rx.msg) |
| 302 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 303 | suart->rx.running = true; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame^] | 304 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /*! Enable/disable transmitter of the given soft-UART. |
| 311 | * \param[in] suart soft-UART instance to be re-configured. |
| 312 | * \param[in] enable enable/disable state of the transmitter. |
| 313 | * \returns 0 on success; negative on error. */ |
| 314 | int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable) |
| 315 | { |
| 316 | if (!enable && suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 317 | /* FIXME: Tx */ |
| 318 | suart->tx.running = false; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 319 | } else if (enable && !suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 320 | suart->tx.running = true; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |