blob: bc563ca09c8b00e2733382158a5b8895926e8ecd [file] [log] [blame]
Harald Weltedc023cf2022-11-29 23:15:18 +01001/*! \file soft_uart.c
2 * Software UART implementation. */
3/*
4 * (C) 2022 by Harald Welte <laforge@gnumonks.org>
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +07005 * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Harald Weltedc023cf2022-11-29 23:15:18 +01006 *
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 Yanitskiy1a3b5112023-11-16 00:41:03 +070030/*! Rx/Tx flow state of a soft-UART */
31enum 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 Yanitskiyb72625d2023-11-16 00:32:33 +070038/*! Internal state of a soft-UART */
Harald Weltedc023cf2022-11-29 23:15:18 +010039struct 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 Yanitskiy1c2b8c12023-11-16 00:44:42 +070047 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
Harald Weltedc023cf2022-11-29 23:15:18 +010048 unsigned int flags;
49 unsigned int status;
50 struct osmo_timer_list timer;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +070051 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010052 } rx;
53 struct {
54 bool running;
55 uint8_t bit_count;
56 uint8_t shift_reg;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070057 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
58 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010059 } tx;
60};
61
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +070062/*! Default soft-UART configuration (8-N-1) */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070063const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = {
Harald Weltedc023cf2022-11-29 23:15:18 +010064 .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 Weltedc023cf2022-11-29 23:15:18 +010069};
70
71/*************************************************************************
72 * Receiver
73 *************************************************************************/
74
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +070075/*! Flush the receive buffer, passing ownership of the msgb to the .rx_cb().
76 * \param[in] suart soft-UART instance holding the receive buffer. */
77void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart)
Harald Weltedc023cf2022-11-29 23:15:18 +010078{
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 Yanitskiyc9fc77f2023-11-16 00:55:35 +070084 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx");
Harald Weltedc023cf2022-11-29 23:15:18 +010085 } 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 */
92static 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 Yanitskiybf95f822023-11-18 01:46:16 +0700100 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 Weltedc023cf2022-11-29 23:15:18 +0100105 osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000,
106 (suart->cfg.rx_timeout_ms % 1000) * 1000);
Harald Weltedc023cf2022-11-29 23:15:18 +0100107 }
108}
109
110/* receive a single bit */
111static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit)
112{
Harald Weltedc023cf2022-11-29 23:15:18 +0100113 if (!suart->rx.running)
114 return;
115
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700116 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 Weltedc023cf2022-11-29 23:15:18 +0100121 suart->rx.shift_reg = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100122 suart->rx.bit_count = 0;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700123 suart->rx.parity_bit = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100124 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700125 break;
126 case SUART_FLOW_ST_DATA:
127 suart->rx.bit_count++;
128 suart->rx.shift_reg >>= 1;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700129 if (bit != 0) {
130 suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700131 suart->rx.shift_reg |= 0x80;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700132 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700133 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 Yanitskiy2d2ce492023-11-19 15:37:10 +0700139 /* 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 Yanitskiy1a3b5112023-11-16 00:41:03 +0700142 }
143 break;
144 case SUART_FLOW_ST_PARITY:
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700145 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;
156 case OSMO_SUART_PARITY_NONE: /* shall not happen */
157 default:
158 OSMO_ASSERT(0);
159 }
160
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700161 suart->rx.flow_state = SUART_FLOW_ST_STOP;
162 break;
163 case SUART_FLOW_ST_STOP:
164 suart->rx.bit_count++;
165 if (bit != 1)
166 suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR;
167
168 if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
169 /* we have accumulated enough stop bits */
170 suart_rx_ch(suart, suart->rx.shift_reg);
171 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
172 }
173 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100174 }
175}
176
177/* receive timer expiration: flush rx-buffer to user call-back */
178static void suart_rx_timer_cb(void *data)
179{
180 struct osmo_soft_uart *suart = data;
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700181 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100182}
183
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700184/*! Feed a number of unpacked bits into the soft-UART receiver.
185 * \param[in] suart soft-UART instance to feed bits into.
186 * \param[in] ubits pointer to the unpacked bits.
187 * \param[in] n_ubits number of unpacked bits to be fed.
188 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100189int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits)
190{
191 for (size_t i = 0; i < n_ubits; i++)
192 osmo_uart_rx_bit(suart, ubits[i]);
193 return 0;
194}
195
196/*************************************************************************
197 * Transmitter
198 *************************************************************************/
199
Harald Weltedc023cf2022-11-29 23:15:18 +0100200/* pull a single bit out of the UART transmitter */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700201static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart, struct msgb *msg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100202{
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700203 ubit_t tx_bit = 1;
Harald Weltedc023cf2022-11-29 23:15:18 +0100204
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700205 if (!suart->tx.running)
206 return tx_bit;
207
208 switch (suart->tx.flow_state) {
209 case SUART_FLOW_ST_IDLE:
210 if (msgb_length(msg) > 0) { /* if we have pending data */
211 suart->tx.shift_reg = msgb_pull_u8(msg);
212 suart->tx.flow_state = SUART_FLOW_ST_DATA;
213 suart->tx.bit_count = 0;
214 suart->tx.parity_bit = 0;
215 tx_bit = 0;
216 }
217 break;
218 case SUART_FLOW_ST_DATA:
219 tx_bit = suart->tx.shift_reg & 1;
220 suart->tx.parity_bit ^= tx_bit;
221 suart->tx.shift_reg >>= 1;
222 suart->tx.bit_count++;
223 if (suart->tx.bit_count >= suart->cfg.num_data_bits) {
224 /* we have transmitted all data bits */
225 if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE)
226 suart->tx.flow_state = SUART_FLOW_ST_PARITY;
227 else
228 suart->tx.flow_state = SUART_FLOW_ST_STOP;
229 }
230 break;
231 case SUART_FLOW_ST_PARITY:
232 switch (suart->cfg.parity_mode) {
233 case OSMO_SUART_PARITY_EVEN:
234 /* number of 1-bits (in both data and parity) shall be even */
235 tx_bit = suart->tx.parity_bit;
236 break;
237 case OSMO_SUART_PARITY_ODD:
238 /* number of 1-bits (in both data and parity) shall be odd */
239 tx_bit = !suart->tx.parity_bit;
240 break;
241 case OSMO_SUART_PARITY_NONE:
242 default: /* shall not happen */
243 OSMO_ASSERT(0);
244 }
245
246 suart->tx.flow_state = SUART_FLOW_ST_STOP;
247 break;
248 case SUART_FLOW_ST_STOP:
249 suart->tx.bit_count++;
250 if (suart->tx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
251 /* we have transmitted all stop bits, we're done */
252 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
253 }
254 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100255 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700256
257 return tx_bit;
Harald Weltedc023cf2022-11-29 23:15:18 +0100258}
259
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700260/*! Pull a number of unpacked bits out of the soft-UART transmitter.
261 * \param[in] suart soft-UART instance to pull the bits from.
262 * \param[out] ubits pointer to a buffer where to store pulled bits.
263 * \param[in] n_ubits number of unpacked bits to be pulled.
264 * \returns number of unpacked bits pulled; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100265int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits)
266{
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700267 const struct osmo_soft_uart_cfg *cfg = &suart->cfg;
268 size_t n_frame_bits;
269 struct msgb *msg;
270
271 /* calculate UART frame size for the effective config */
272 n_frame_bits = 1 + cfg->num_data_bits + cfg->num_stop_bits;
273 if (cfg->parity_mode != OSMO_SUART_PARITY_NONE)
274 n_frame_bits += 1;
275
276 /* allocate a Tx buffer msgb */
277 msg = msgb_alloc_c(suart, n_ubits / n_frame_bits, "soft_uart_tx");
278 OSMO_ASSERT(msg != NULL);
279
280 /* call the .tx_cb() to populate the Tx buffer */
281 OSMO_ASSERT(cfg->tx_cb != NULL);
282 suart->cfg.tx_cb(cfg->priv, msg);
283
Harald Weltedc023cf2022-11-29 23:15:18 +0100284 for (size_t i = 0; i < n_ubits; i++)
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700285 ubits[i] = osmo_uart_tx_bit(suart, msg);
286 msgb_free(msg);
287
288 return 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100289}
290
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700291/*! Set the modem status lines of the given soft-UART.
292 * \param[in] suart soft-UART instance to update the modem status.
293 * \param[in] status mask of osmo_soft_uart_status.
294 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100295int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status)
296{
297 /* FIXME: Tx */
298 return 0;
299}
300
301
302/*************************************************************************
303 * Management / Initialization
304 *************************************************************************/
305
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700306/*! Allocate a soft-UART instance.
307 * \param[in] ctx parent talloc context.
308 * \param[in] name name of the soft-UART instance.
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700309 * \param[in] cfg initial configuration of the soft-UART instance.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700310 * \returns pointer to allocated soft-UART instance; NULL on error. */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700311struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
312 const struct osmo_soft_uart_cfg *cfg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100313{
314 struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart);
315 if (!suart)
316 return NULL;
317 suart->name = talloc_strdup(suart, name);
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700318
319 OSMO_ASSERT(cfg != NULL);
320 suart->cfg = *cfg;
Harald Weltedc023cf2022-11-29 23:15:18 +0100321
322 return suart;
323}
324
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +0700325/*! Release memory taken by the given soft-UART.
326 * \param[in] suart soft-UART instance to be free()d. */
327void osmo_soft_uart_free(struct osmo_soft_uart *suart)
328{
329 if (suart == NULL)
330 return;
331
332 osmo_timer_del(&suart->rx.timer);
333 msgb_free(suart->rx.msg);
334
335 talloc_free((void *)suart->name);
336 talloc_free(suart);
337}
338
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700339/*! Change soft-UART configuration to the user-provided config.
340 * \param[in] suart soft-UART instance to be re-configured.
341 * \param[in] cfg the user-provided config to be applied.
342 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100343int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg)
344{
345 /* consistency checks on the configuration */
346 if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0)
347 return -EINVAL;
348 if (cfg->num_stop_bits == 0)
349 return -EINVAL;
350 if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM)
351 return -EINVAL;
352 if (cfg->rx_buf_size == 0)
353 return -EINVAL;
354
355 if (suart->cfg.rx_buf_size > cfg->rx_buf_size ||
356 suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) {
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700357 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100358 }
359
360 suart->cfg = *cfg;
361
362 osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100363
364 return 0;
365}
366
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700367/*! Enable/disable receiver of the given soft-UART.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700368 * \param[in] suart soft-UART instance to be re-configured.
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700369 * \param[in] enable enable/disable state of the receiver.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700370 * \returns 0 on success; negative on error. */
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700371int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable)
Harald Weltedc023cf2022-11-29 23:15:18 +0100372{
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700373 if (!enable && suart->rx.running) {
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700374 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100375 suart->rx.running = false;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700376 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700377 } else if (enable && !suart->rx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100378 if (!suart->rx.msg)
379 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx");
380 suart->rx.running = true;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700381 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100382 }
383
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700384 return 0;
385}
386
387/*! Enable/disable transmitter of the given soft-UART.
388 * \param[in] suart soft-UART instance to be re-configured.
389 * \param[in] enable enable/disable state of the transmitter.
390 * \returns 0 on success; negative on error. */
391int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable)
392{
393 if (!enable && suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100394 suart->tx.running = false;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700395 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700396 } else if (enable && !suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100397 suart->tx.running = true;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700398 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100399 }
400
401 return 0;
402}