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 | |
| 29 | /*! one instance of a soft-uart */ |
| 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 | |
| 52 | static struct osmo_soft_uart_cfg suart_default_cfg = { |
| 53 | .num_data_bits = 8, |
| 54 | .num_stop_bits = 1, |
| 55 | .parity_mode = OSMO_SUART_PARITY_NONE, |
| 56 | .rx_buf_size = 1024, |
| 57 | .rx_timeout_ms = 100, |
| 58 | .priv = NULL, |
| 59 | .rx_cb = NULL, |
| 60 | .status_change_cb = NULL, |
| 61 | }; |
| 62 | |
| 63 | /************************************************************************* |
| 64 | * Receiver |
| 65 | *************************************************************************/ |
| 66 | |
| 67 | /* flush the receive buffer + allocate new one, as needed */ |
| 68 | static void suart_flush_rx(struct osmo_soft_uart *suart) |
| 69 | { |
| 70 | if ((suart->rx.msg && msgb_length(suart->rx.msg)) || suart->rx.flags) { |
| 71 | osmo_timer_del(&suart->rx.timer); |
| 72 | if (suart->cfg.rx_cb) { |
| 73 | suart->cfg.rx_cb(suart->cfg.priv, suart->rx.msg, suart->rx.flags); |
| 74 | /* call-back has taken ownership of msgb, no need to free() here */ |
| 75 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 76 | } else { |
| 77 | msgb_reset(suart->rx.msg); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* one character was received; add to receive buffer and notify user, if needed */ |
| 83 | static void suart_rx_ch(struct osmo_soft_uart *suart, uint8_t ch) |
| 84 | { |
| 85 | unsigned int msg_len; |
| 86 | |
| 87 | OSMO_ASSERT(suart->rx.msg); |
| 88 | msgb_put_u8(suart->rx.msg, ch); |
| 89 | msg_len = msgb_length(suart->rx.msg); |
| 90 | |
| 91 | /* first character in new message: start timer */ |
| 92 | if (msg_len == 1) { |
| 93 | osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000, |
| 94 | (suart->cfg.rx_timeout_ms % 1000) * 1000); |
| 95 | } else if (msg_len >= suart->cfg.rx_buf_size || suart->rx.flags) { |
| 96 | suart_flush_rx(suart); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* receive a single bit */ |
| 101 | static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit) |
| 102 | { |
| 103 | unsigned int num_parity_bits = 0; |
| 104 | |
| 105 | if (!suart->rx.running) |
| 106 | return; |
| 107 | |
| 108 | if (suart->rx.bit_count == 0) { |
| 109 | /* start bit is 0. Wait if there is none */ |
| 110 | if (bit == 0) { |
| 111 | /* START bit */ |
| 112 | suart->rx.flags = 0; |
| 113 | suart->rx.shift_reg = 0; |
| 114 | suart->rx.bit_count++; |
| 115 | } |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE) |
| 120 | num_parity_bits = 1; |
| 121 | |
| 122 | suart->rx.bit_count++; |
| 123 | if (suart->rx.bit_count <= 1 + suart->cfg.num_data_bits) { |
| 124 | /* DATA bit */ |
| 125 | suart->rx.shift_reg = suart->rx.shift_reg >> 1; |
| 126 | if (bit) |
| 127 | suart->rx.shift_reg |= 0x80; |
| 128 | } else if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE && |
| 129 | suart->rx.bit_count == 1 + suart->cfg.num_data_bits + 1) { |
| 130 | /* PARITY bit */ |
| 131 | suart->rx.parity_bit = bit; |
| 132 | /* TODO: verify parity */ |
| 133 | //suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR; |
| 134 | } else if (suart->rx.bit_count <= |
| 135 | 1 + suart->cfg.num_data_bits + num_parity_bits + suart->cfg.num_stop_bits) { |
| 136 | /* STOP bit */ |
| 137 | if (bit != 1) { |
| 138 | fprintf(stderr, "framing error: stop bit %u != 1\n", suart->rx.bit_count); |
| 139 | suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR; |
| 140 | } |
| 141 | |
| 142 | if (suart->rx.bit_count == 1 + suart->cfg.num_data_bits + num_parity_bits + suart->cfg.num_stop_bits) { |
| 143 | //printf("Rx: 0x%02x %c\n", suart->rx.shift_reg, suart->rx.shift_reg); |
| 144 | suart_rx_ch(suart, suart->rx.shift_reg); |
| 145 | suart->rx.bit_count = 0; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* receive timer expiration: flush rx-buffer to user call-back */ |
| 151 | static void suart_rx_timer_cb(void *data) |
| 152 | { |
| 153 | struct osmo_soft_uart *suart = data; |
| 154 | suart_flush_rx(suart); |
| 155 | } |
| 156 | |
| 157 | /*! feed a number of unpacked bits into the soft-uart receiver */ |
| 158 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits) |
| 159 | { |
| 160 | for (size_t i = 0; i < n_ubits; i++) |
| 161 | osmo_uart_rx_bit(suart, ubits[i]); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /************************************************************************* |
| 166 | * Transmitter |
| 167 | *************************************************************************/ |
| 168 | |
| 169 | /*! enqueue the given message buffer into the transmit queue of the UART. */ |
| 170 | void osmo_soft_uart_tx(struct osmo_soft_uart *suart, struct msgb *tx_data) |
| 171 | { |
| 172 | if (!suart->tx.msg) |
| 173 | suart->tx.msg = tx_data; |
| 174 | else |
| 175 | msgb_enqueue(&suart->tx.queue, tx_data); |
| 176 | } |
| 177 | |
| 178 | /* pull a single bit out of the UART transmitter */ |
| 179 | static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart) |
| 180 | { |
| 181 | if (!suart->tx.running) |
| 182 | return 1; |
| 183 | |
| 184 | if (suart->tx.bit_count == 0) { |
| 185 | /* do we have anything to transmit? */ |
| 186 | /* FIXME */ |
| 187 | } |
| 188 | /* FIXME */ |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | /*! pull then number of specified unpacked bits out of the UART Transmitter */ |
| 193 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits) |
| 194 | { |
| 195 | for (size_t i = 0; i < n_ubits; i++) |
| 196 | ubits[i] = osmo_uart_tx_bit(suart); |
| 197 | return n_ubits; |
| 198 | } |
| 199 | |
| 200 | /*! Set the modem status lines of the UART */ |
| 201 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status) |
| 202 | { |
| 203 | /* FIXME: Tx */ |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /************************************************************************* |
| 209 | * Management / Initialization |
| 210 | *************************************************************************/ |
| 211 | |
| 212 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name) |
| 213 | { |
| 214 | struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart); |
| 215 | if (!suart) |
| 216 | return NULL; |
| 217 | suart->name = talloc_strdup(suart, name); |
| 218 | suart->cfg = suart_default_cfg; |
| 219 | |
| 220 | return suart; |
| 221 | } |
| 222 | |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame^] | 223 | /*! Release memory taken by the given soft-UART. |
| 224 | * \param[in] suart soft-UART instance to be free()d. */ |
| 225 | void osmo_soft_uart_free(struct osmo_soft_uart *suart) |
| 226 | { |
| 227 | if (suart == NULL) |
| 228 | return; |
| 229 | |
| 230 | osmo_timer_del(&suart->rx.timer); |
| 231 | msgb_free(suart->rx.msg); |
| 232 | |
| 233 | talloc_free((void *)suart->name); |
| 234 | talloc_free(suart); |
| 235 | } |
| 236 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 237 | /*! change soft-UART configuration to user-provided config */ |
| 238 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg) |
| 239 | { |
| 240 | /* consistency checks on the configuration */ |
| 241 | if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0) |
| 242 | return -EINVAL; |
| 243 | if (cfg->num_stop_bits == 0) |
| 244 | return -EINVAL; |
| 245 | if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM) |
| 246 | return -EINVAL; |
| 247 | if (cfg->rx_buf_size == 0) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | if (suart->cfg.rx_buf_size > cfg->rx_buf_size || |
| 251 | suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) { |
| 252 | suart_flush_rx(suart); |
| 253 | } |
| 254 | |
| 255 | suart->cfg = *cfg; |
| 256 | |
| 257 | osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart); |
| 258 | INIT_LLIST_HEAD(&suart->tx.queue); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | int osmo_soft_uart_enable(struct osmo_soft_uart *suart, bool rx, bool tx) |
| 264 | { |
| 265 | if (!rx && suart->rx.running) { |
| 266 | suart_flush_rx(suart); |
| 267 | suart->rx.running = false; |
| 268 | } else if (rx && !suart->rx.running) { |
| 269 | if (!suart->rx.msg) |
| 270 | suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx"); |
| 271 | suart->rx.running = true; |
| 272 | } |
| 273 | |
| 274 | if (!tx && suart->tx.running) { |
| 275 | /* FIXME: Tx */ |
| 276 | suart->tx.running = false; |
| 277 | } else if (tx && !suart->tx.running) { |
| 278 | suart->tx.running = true; |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |