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> |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 5 | * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdbool.h> |
| 24 | #include <stdint.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #include <osmocom/core/timer.h> |
| 28 | #include <osmocom/core/soft_uart.h> |
| 29 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 30 | /*! Rx/Tx flow state of a soft-UART */ |
| 31 | enum suart_flow_state { |
| 32 | SUART_FLOW_ST_IDLE, /*!< waiting for a start bit or Tx data */ |
| 33 | SUART_FLOW_ST_DATA, /*!< receiving/transmitting data bits */ |
| 34 | SUART_FLOW_ST_PARITY, /*!< receiving/transmitting parity bits */ |
| 35 | SUART_FLOW_ST_STOP, /*!< receiving/transmitting stop bits */ |
| 36 | }; |
| 37 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 38 | /*! Internal state of a soft-UART */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 39 | struct osmo_soft_uart { |
| 40 | struct osmo_soft_uart_cfg cfg; |
| 41 | const char *name; |
| 42 | struct { |
| 43 | bool running; |
| 44 | uint8_t bit_count; |
| 45 | uint8_t shift_reg; |
| 46 | struct msgb *msg; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 47 | ubit_t parity_bit; /* 0 (even) / 1 (odd) */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 48 | unsigned int flags; |
| 49 | unsigned int status; |
| 50 | struct osmo_timer_list timer; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 51 | enum suart_flow_state flow_state; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 52 | } rx; |
| 53 | struct { |
| 54 | bool running; |
| 55 | uint8_t bit_count; |
| 56 | uint8_t shift_reg; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 57 | ubit_t parity_bit; /* 0 (even) / 1 (odd) */ |
| 58 | enum suart_flow_state flow_state; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 59 | } tx; |
| 60 | }; |
| 61 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 62 | /*! Default soft-UART configuration (8-N-1) */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 63 | const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 64 | .num_data_bits = 8, |
| 65 | .num_stop_bits = 1, |
| 66 | .parity_mode = OSMO_SUART_PARITY_NONE, |
| 67 | .rx_buf_size = 1024, |
| 68 | .rx_timeout_ms = 100, |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /************************************************************************* |
| 72 | * Receiver |
| 73 | *************************************************************************/ |
| 74 | |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 75 | /*! Flush the receive buffer, passing ownership of the msgb to the .rx_cb(). |
| 76 | * \param[in] suart soft-UART instance holding the receive buffer. */ |
| 77 | void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 78 | { |
| 79 | if ((suart->rx.msg && msgb_length(suart->rx.msg)) || suart->rx.flags) { |
| 80 | osmo_timer_del(&suart->rx.timer); |
| 81 | if (suart->cfg.rx_cb) { |
| 82 | suart->cfg.rx_cb(suart->cfg.priv, suart->rx.msg, suart->rx.flags); |
| 83 | /* call-back has taken ownership of msgb, no need to free() here */ |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 84 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx"); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 85 | } else { |
| 86 | msgb_reset(suart->rx.msg); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* one character was received; add to receive buffer and notify user, if needed */ |
| 92 | static void suart_rx_ch(struct osmo_soft_uart *suart, uint8_t ch) |
| 93 | { |
| 94 | unsigned int msg_len; |
| 95 | |
| 96 | OSMO_ASSERT(suart->rx.msg); |
| 97 | msgb_put_u8(suart->rx.msg, ch); |
| 98 | msg_len = msgb_length(suart->rx.msg); |
| 99 | |
Vadim Yanitskiy | bf95f82 | 2023-11-18 01:46:16 +0700 | [diff] [blame] | 100 | if (msg_len >= suart->cfg.rx_buf_size || suart->rx.flags) { |
| 101 | /* either the buffer is full, or we hit a parity and/or a framing error */ |
| 102 | osmo_soft_uart_flush_rx(suart); |
| 103 | } else if (msg_len == 1) { |
| 104 | /* first character in new message: start timer */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 105 | osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000, |
| 106 | (suart->cfg.rx_timeout_ms % 1000) * 1000); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | /* receive a single bit */ |
| 111 | static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit) |
| 112 | { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 113 | if (!suart->rx.running) |
| 114 | return; |
| 115 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 116 | switch (suart->rx.flow_state) { |
| 117 | case SUART_FLOW_ST_IDLE: |
| 118 | if (bit == 0) { /* start bit condition */ |
| 119 | suart->rx.flow_state = SUART_FLOW_ST_DATA; |
| 120 | suart->rx.flags = 0x00; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 121 | suart->rx.shift_reg = 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 122 | suart->rx.bit_count = 0; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 123 | suart->rx.parity_bit = 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 124 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 125 | break; |
| 126 | case SUART_FLOW_ST_DATA: |
| 127 | suart->rx.bit_count++; |
| 128 | suart->rx.shift_reg >>= 1; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 129 | if (bit != 0) { |
| 130 | suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */ |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 131 | suart->rx.shift_reg |= 0x80; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 132 | } |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 133 | if (suart->rx.bit_count >= suart->cfg.num_data_bits) { |
| 134 | /* we have accumulated enough data bits */ |
| 135 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 136 | suart->rx.flow_state = SUART_FLOW_ST_PARITY; |
| 137 | else |
| 138 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
Vadim Yanitskiy | 2d2ce49 | 2023-11-19 15:37:10 +0700 | [diff] [blame] | 139 | /* align the register if needed */ |
| 140 | if (suart->cfg.num_data_bits < 8) |
| 141 | suart->rx.shift_reg >>= (8 - suart->cfg.num_data_bits); |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 142 | } |
| 143 | break; |
| 144 | case SUART_FLOW_ST_PARITY: |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 145 | switch (suart->cfg.parity_mode) { |
| 146 | case OSMO_SUART_PARITY_EVEN: |
| 147 | /* number of 1-bits (in both data and parity) shall be even */ |
| 148 | if (suart->rx.parity_bit != bit) |
| 149 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 150 | break; |
| 151 | case OSMO_SUART_PARITY_ODD: |
| 152 | /* number of 1-bits (in both data and parity) shall be odd */ |
| 153 | if (suart->rx.parity_bit == bit) |
| 154 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 155 | break; |
Vadim Yanitskiy | 0d78a00 | 2023-11-19 16:15:38 +0700 | [diff] [blame^] | 156 | case OSMO_SUART_PARITY_MARK: |
| 157 | /* parity bit must always be 1 */ |
| 158 | if (bit != 1) |
| 159 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 160 | break; |
| 161 | case OSMO_SUART_PARITY_SPACE: |
| 162 | /* parity bit must always be 0 */ |
| 163 | if (bit != 0) |
| 164 | suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 165 | break; |
Vadim Yanitskiy | 1c2b8c1 | 2023-11-16 00:44:42 +0700 | [diff] [blame] | 166 | case OSMO_SUART_PARITY_NONE: /* shall not happen */ |
| 167 | default: |
| 168 | OSMO_ASSERT(0); |
| 169 | } |
| 170 | |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 171 | suart->rx.flow_state = SUART_FLOW_ST_STOP; |
| 172 | break; |
| 173 | case SUART_FLOW_ST_STOP: |
| 174 | suart->rx.bit_count++; |
| 175 | if (bit != 1) |
| 176 | suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR; |
| 177 | |
| 178 | if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) { |
| 179 | /* we have accumulated enough stop bits */ |
| 180 | suart_rx_ch(suart, suart->rx.shift_reg); |
| 181 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
| 182 | } |
| 183 | break; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | /* receive timer expiration: flush rx-buffer to user call-back */ |
| 188 | static void suart_rx_timer_cb(void *data) |
| 189 | { |
| 190 | struct osmo_soft_uart *suart = data; |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 191 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 194 | /*! Feed a number of unpacked bits into the soft-UART receiver. |
| 195 | * \param[in] suart soft-UART instance to feed bits into. |
| 196 | * \param[in] ubits pointer to the unpacked bits. |
| 197 | * \param[in] n_ubits number of unpacked bits to be fed. |
| 198 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 199 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits) |
| 200 | { |
| 201 | for (size_t i = 0; i < n_ubits; i++) |
| 202 | osmo_uart_rx_bit(suart, ubits[i]); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /************************************************************************* |
| 207 | * Transmitter |
| 208 | *************************************************************************/ |
| 209 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 210 | /* pull a single bit out of the UART transmitter */ |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 211 | static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart, struct msgb *msg) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 212 | { |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 213 | ubit_t tx_bit = 1; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 214 | |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 215 | if (!suart->tx.running) |
| 216 | return tx_bit; |
| 217 | |
| 218 | switch (suart->tx.flow_state) { |
| 219 | case SUART_FLOW_ST_IDLE: |
| 220 | if (msgb_length(msg) > 0) { /* if we have pending data */ |
| 221 | suart->tx.shift_reg = msgb_pull_u8(msg); |
| 222 | suart->tx.flow_state = SUART_FLOW_ST_DATA; |
| 223 | suart->tx.bit_count = 0; |
| 224 | suart->tx.parity_bit = 0; |
| 225 | tx_bit = 0; |
| 226 | } |
| 227 | break; |
| 228 | case SUART_FLOW_ST_DATA: |
| 229 | tx_bit = suart->tx.shift_reg & 1; |
| 230 | suart->tx.parity_bit ^= tx_bit; |
| 231 | suart->tx.shift_reg >>= 1; |
| 232 | suart->tx.bit_count++; |
| 233 | if (suart->tx.bit_count >= suart->cfg.num_data_bits) { |
| 234 | /* we have transmitted all data bits */ |
| 235 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 236 | suart->tx.flow_state = SUART_FLOW_ST_PARITY; |
| 237 | else |
| 238 | suart->tx.flow_state = SUART_FLOW_ST_STOP; |
| 239 | } |
| 240 | break; |
| 241 | case SUART_FLOW_ST_PARITY: |
| 242 | switch (suart->cfg.parity_mode) { |
| 243 | case OSMO_SUART_PARITY_EVEN: |
| 244 | /* number of 1-bits (in both data and parity) shall be even */ |
| 245 | tx_bit = suart->tx.parity_bit; |
| 246 | break; |
| 247 | case OSMO_SUART_PARITY_ODD: |
| 248 | /* number of 1-bits (in both data and parity) shall be odd */ |
| 249 | tx_bit = !suart->tx.parity_bit; |
| 250 | break; |
Vadim Yanitskiy | 0d78a00 | 2023-11-19 16:15:38 +0700 | [diff] [blame^] | 251 | case OSMO_SUART_PARITY_MARK: |
| 252 | /* parity bit must always be 1 */ |
| 253 | tx_bit = 1; |
| 254 | break; |
| 255 | case OSMO_SUART_PARITY_SPACE: |
| 256 | /* parity bit must always be 0 */ |
| 257 | tx_bit = 0; |
| 258 | break; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 259 | case OSMO_SUART_PARITY_NONE: |
| 260 | default: /* shall not happen */ |
| 261 | OSMO_ASSERT(0); |
| 262 | } |
| 263 | |
| 264 | suart->tx.flow_state = SUART_FLOW_ST_STOP; |
| 265 | break; |
| 266 | case SUART_FLOW_ST_STOP: |
| 267 | suart->tx.bit_count++; |
| 268 | if (suart->tx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) { |
| 269 | /* we have transmitted all stop bits, we're done */ |
| 270 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
| 271 | } |
| 272 | break; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 273 | } |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 274 | |
| 275 | return tx_bit; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 278 | /*! Pull a number of unpacked bits out of the soft-UART transmitter. |
| 279 | * \param[in] suart soft-UART instance to pull the bits from. |
| 280 | * \param[out] ubits pointer to a buffer where to store pulled bits. |
| 281 | * \param[in] n_ubits number of unpacked bits to be pulled. |
| 282 | * \returns number of unpacked bits pulled; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 283 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits) |
| 284 | { |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 285 | const struct osmo_soft_uart_cfg *cfg = &suart->cfg; |
| 286 | size_t n_frame_bits; |
| 287 | struct msgb *msg; |
| 288 | |
| 289 | /* calculate UART frame size for the effective config */ |
| 290 | n_frame_bits = 1 + cfg->num_data_bits + cfg->num_stop_bits; |
| 291 | if (cfg->parity_mode != OSMO_SUART_PARITY_NONE) |
| 292 | n_frame_bits += 1; |
| 293 | |
| 294 | /* allocate a Tx buffer msgb */ |
| 295 | msg = msgb_alloc_c(suart, n_ubits / n_frame_bits, "soft_uart_tx"); |
| 296 | OSMO_ASSERT(msg != NULL); |
| 297 | |
| 298 | /* call the .tx_cb() to populate the Tx buffer */ |
| 299 | OSMO_ASSERT(cfg->tx_cb != NULL); |
| 300 | suart->cfg.tx_cb(cfg->priv, msg); |
| 301 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 302 | for (size_t i = 0; i < n_ubits; i++) |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 303 | ubits[i] = osmo_uart_tx_bit(suart, msg); |
| 304 | msgb_free(msg); |
| 305 | |
| 306 | return 0; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 309 | /*! Set the modem status lines of the given soft-UART. |
| 310 | * \param[in] suart soft-UART instance to update the modem status. |
| 311 | * \param[in] status mask of osmo_soft_uart_status. |
| 312 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 313 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status) |
| 314 | { |
| 315 | /* FIXME: Tx */ |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /************************************************************************* |
| 321 | * Management / Initialization |
| 322 | *************************************************************************/ |
| 323 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 324 | /*! Allocate a soft-UART instance. |
| 325 | * \param[in] ctx parent talloc context. |
| 326 | * \param[in] name name of the soft-UART instance. |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 327 | * \param[in] cfg initial configuration of the soft-UART instance. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 328 | * \returns pointer to allocated soft-UART instance; NULL on error. */ |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 329 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name, |
| 330 | const struct osmo_soft_uart_cfg *cfg) |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 331 | { |
| 332 | struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart); |
| 333 | if (!suart) |
| 334 | return NULL; |
| 335 | suart->name = talloc_strdup(suart, name); |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 336 | |
| 337 | OSMO_ASSERT(cfg != NULL); |
| 338 | suart->cfg = *cfg; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 339 | |
| 340 | return suart; |
| 341 | } |
| 342 | |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 343 | /*! Release memory taken by the given soft-UART. |
| 344 | * \param[in] suart soft-UART instance to be free()d. */ |
| 345 | void osmo_soft_uart_free(struct osmo_soft_uart *suart) |
| 346 | { |
| 347 | if (suart == NULL) |
| 348 | return; |
| 349 | |
| 350 | osmo_timer_del(&suart->rx.timer); |
| 351 | msgb_free(suart->rx.msg); |
| 352 | |
| 353 | talloc_free((void *)suart->name); |
| 354 | talloc_free(suart); |
| 355 | } |
| 356 | |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 357 | /*! Change soft-UART configuration to the user-provided config. |
| 358 | * \param[in] suart soft-UART instance to be re-configured. |
| 359 | * \param[in] cfg the user-provided config to be applied. |
| 360 | * \returns 0 on success; negative on error. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 361 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg) |
| 362 | { |
| 363 | /* consistency checks on the configuration */ |
| 364 | if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0) |
| 365 | return -EINVAL; |
| 366 | if (cfg->num_stop_bits == 0) |
| 367 | return -EINVAL; |
| 368 | if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM) |
| 369 | return -EINVAL; |
| 370 | if (cfg->rx_buf_size == 0) |
| 371 | return -EINVAL; |
| 372 | |
| 373 | if (suart->cfg.rx_buf_size > cfg->rx_buf_size || |
| 374 | suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) { |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 375 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | suart->cfg = *cfg; |
| 379 | |
| 380 | osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 385 | /*! Enable/disable receiver of the given soft-UART. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 386 | * \param[in] suart soft-UART instance to be re-configured. |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 387 | * \param[in] enable enable/disable state of the receiver. |
Vadim Yanitskiy | b72625d | 2023-11-16 00:32:33 +0700 | [diff] [blame] | 388 | * \returns 0 on success; negative on error. */ |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 389 | 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] | 390 | { |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 391 | if (!enable && suart->rx.running) { |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 392 | osmo_soft_uart_flush_rx(suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 393 | suart->rx.running = false; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 394 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 395 | } else if (enable && !suart->rx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 396 | if (!suart->rx.msg) |
| 397 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 398 | suart->rx.running = true; |
Vadim Yanitskiy | 1a3b511 | 2023-11-16 00:41:03 +0700 | [diff] [blame] | 399 | suart->rx.flow_state = SUART_FLOW_ST_IDLE; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | /*! Enable/disable transmitter of the given soft-UART. |
| 406 | * \param[in] suart soft-UART instance to be re-configured. |
| 407 | * \param[in] enable enable/disable state of the transmitter. |
| 408 | * \returns 0 on success; negative on error. */ |
| 409 | int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable) |
| 410 | { |
| 411 | if (!enable && suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 412 | suart->tx.running = false; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 413 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 414 | } else if (enable && !suart->tx.running) { |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 415 | suart->tx.running = true; |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 416 | suart->tx.flow_state = SUART_FLOW_ST_IDLE; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |