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; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame^] | 46 | ubit_t parity_bit; /* 0 (even) / 1 (odd) */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 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; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame^] | 120 | suart->rx.parity_bit = 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 121 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 122 | break; |
| 123 | case SUART_FLOW_ST_DATA: |
| 124 | suart->rx.bit_count++; |
| 125 | suart->rx.shift_reg >>= 1; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame^] | 126 | if (bit != 0) { |
| 127 | suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */ |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 128 | suart->rx.shift_reg |= 0x80; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame^] | 129 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 130 | if (suart->rx.bit_count >= suart->cfg.num_data_bits) { |
| 131 | /* we have accumulated enough data bits */ |
| 132 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 133 | suart->rx.flow_state = SUART_FLOW_ST_PARITY; |
| 134 | else |
| 135 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
| 136 | } |
| 137 | break; |
| 138 | case SUART_FLOW_ST_PARITY: |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame^] | 139 | switch (suart->cfg.parity_mode) { |
| 140 | case OSMO_SUART_PARITY_EVEN: |
| 141 | /* number of 1-bits (in both data and parity) shall be even */ |
| 142 | if (suart->rx.parity_bit != bit) |
| 143 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 144 | break; |
| 145 | case OSMO_SUART_PARITY_ODD: |
| 146 | /* number of 1-bits (in both data and parity) shall be odd */ |
| 147 | if (suart->rx.parity_bit == bit) |
| 148 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 149 | break; |
| 150 | case OSMO_SUART_PARITY_NONE: /* shall not happen */ |
| 151 | default: |
| 152 | OSMO_ASSERT(0); |
| 153 | } |
| 154 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 155 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
| 156 | break; |
| 157 | case SUART_FLOW_ST_STOP: |
| 158 | suart->rx.bit_count++; |
| 159 | if (bit != 1) |
| 160 | suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR; |
| 161 | |
| 162 | if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) { |
| 163 | /* we have accumulated enough stop bits */ |
| 164 | suart_rx_ch(suart, suart->rx.shift_reg); |
| 165 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
| 166 | } |
| 167 | break; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | /* receive timer expiration: flush rx-buffer to user call-back */ |
| 172 | static void suart_rx_timer_cb(void *data) |
| 173 | { |
| 174 | struct osmo_soft_uart *suart = data; |
| 175 | suart_flush_rx(suart); |
| 176 | } |
| 177 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 178 | /*! Feed a number of unpacked bits into the soft-UART receiver. |
| 179 | * \param[in] suart soft-UART instance to feed bits into. |
| 180 | * \param[in] ubits pointer to the unpacked bits. |
| 181 | * \param[in] n_ubits number of unpacked bits to be fed. |
| 182 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 183 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits) |
| 184 | { |
| 185 | for (size_t i = 0; i < n_ubits; i++) |
| 186 | osmo_uart_rx_bit(suart, ubits[i]); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /************************************************************************* |
| 191 | * Transmitter |
| 192 | *************************************************************************/ |
| 193 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 194 | /*! Enqueue the given message buffer into the transmit queue of the soft-UART. |
| 195 | * \param[in] suart soft-UART instance for transmitting data. |
| 196 | * \param[in] tx_data message buffer containing to be transmitted data. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 197 | void osmo_soft_uart_tx(struct osmo_soft_uart *suart, struct msgb *tx_data) |
| 198 | { |
| 199 | if (!suart->tx.msg) |
| 200 | suart->tx.msg = tx_data; |
| 201 | else |
| 202 | msgb_enqueue(&suart->tx.queue, tx_data); |
| 203 | } |
| 204 | |
| 205 | /* pull a single bit out of the UART transmitter */ |
| 206 | static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart) |
| 207 | { |
| 208 | if (!suart->tx.running) |
| 209 | return 1; |
| 210 | |
| 211 | if (suart->tx.bit_count == 0) { |
| 212 | /* do we have anything to transmit? */ |
| 213 | /* FIXME */ |
| 214 | } |
| 215 | /* FIXME */ |
| 216 | return 1; |
| 217 | } |
| 218 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 219 | /*! Pull a number of unpacked bits out of the soft-UART transmitter. |
| 220 | * \param[in] suart soft-UART instance to pull the bits from. |
| 221 | * \param[out] ubits pointer to a buffer where to store pulled bits. |
| 222 | * \param[in] n_ubits number of unpacked bits to be pulled. |
| 223 | * \returns number of unpacked bits pulled; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 224 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits) |
| 225 | { |
| 226 | for (size_t i = 0; i < n_ubits; i++) |
| 227 | ubits[i] = osmo_uart_tx_bit(suart); |
| 228 | return n_ubits; |
| 229 | } |
| 230 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 231 | /*! Set the modem status lines of the given soft-UART. |
| 232 | * \param[in] suart soft-UART instance to update the modem status. |
| 233 | * \param[in] status mask of osmo_soft_uart_status. |
| 234 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 235 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status) |
| 236 | { |
| 237 | /* FIXME: Tx */ |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /************************************************************************* |
| 243 | * Management / Initialization |
| 244 | *************************************************************************/ |
| 245 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 246 | /*! Allocate a soft-UART instance. |
| 247 | * \param[in] ctx parent talloc context. |
| 248 | * \param[in] name name of the soft-UART instance. |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 249 | * \param[in] cfg initial configuration of the soft-UART instance. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 250 | * \returns pointer to allocated soft-UART instance; NULL on error. */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 251 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name, |
| 252 | const struct osmo_soft_uart_cfg *cfg) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 253 | { |
| 254 | struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart); |
| 255 | if (!suart) |
| 256 | return NULL; |
| 257 | suart->name = talloc_strdup(suart, name); |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 258 | |
| 259 | OSMO_ASSERT(cfg != NULL); |
| 260 | suart->cfg = *cfg; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 261 | |
| 262 | return suart; |
| 263 | } |
| 264 | |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 265 | /*! Release memory taken by the given soft-UART. |
| 266 | * \param[in] suart soft-UART instance to be free()d. */ |
| 267 | void osmo_soft_uart_free(struct osmo_soft_uart *suart) |
| 268 | { |
| 269 | if (suart == NULL) |
| 270 | return; |
| 271 | |
| 272 | osmo_timer_del(&suart->rx.timer); |
| 273 | msgb_free(suart->rx.msg); |
| 274 | |
| 275 | talloc_free((void *)suart->name); |
| 276 | talloc_free(suart); |
| 277 | } |
| 278 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 279 | /*! Change soft-UART configuration to the user-provided config. |
| 280 | * \param[in] suart soft-UART instance to be re-configured. |
| 281 | * \param[in] cfg the user-provided config to be applied. |
| 282 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 283 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg) |
| 284 | { |
| 285 | /* consistency checks on the configuration */ |
| 286 | if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0) |
| 287 | return -EINVAL; |
| 288 | if (cfg->num_stop_bits == 0) |
| 289 | return -EINVAL; |
| 290 | if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM) |
| 291 | return -EINVAL; |
| 292 | if (cfg->rx_buf_size == 0) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | if (suart->cfg.rx_buf_size > cfg->rx_buf_size || |
| 296 | suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) { |
| 297 | suart_flush_rx(suart); |
| 298 | } |
| 299 | |
| 300 | suart->cfg = *cfg; |
| 301 | |
| 302 | osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart); |
| 303 | INIT_LLIST_HEAD(&suart->tx.queue); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 308 | /*! Enable/disable receiver of the given soft-UART. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 309 | * \param[in] suart soft-UART instance to be re-configured. |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 310 | * \param[in] enable enable/disable state of the receiver. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 311 | * \returns 0 on success; negative on error. */ |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 312 | 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] | 313 | { |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 314 | if (!enable && suart->rx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 315 | suart_flush_rx(suart); |
| 316 | suart->rx.running = false; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 317 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 318 | } else if (enable && !suart->rx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 319 | if (!suart->rx.msg) |
| 320 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 321 | suart->rx.running = true; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 322 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /*! Enable/disable transmitter of the given soft-UART. |
| 329 | * \param[in] suart soft-UART instance to be re-configured. |
| 330 | * \param[in] enable enable/disable state of the transmitter. |
| 331 | * \returns 0 on success; negative on error. */ |
| 332 | int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable) |
| 333 | { |
| 334 | if (!enable && suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 335 | /* FIXME: Tx */ |
| 336 | suart->tx.running = false; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 337 | } else if (enable && !suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 338 | suart->tx.running = true; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |