blob: d8ce9d9a37b5e62e7b7a71d76e804ce97ce880f2 [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>
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 Yanitskiy1a3b5112023-11-16 00:41:03 +070029/*! Rx/Tx flow state of a soft-UART */
30enum 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 Yanitskiyb72625d2023-11-16 00:32:33 +070037/*! Internal state of a soft-UART */
Harald Weltedc023cf2022-11-29 23:15:18 +010038struct 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 Yanitskiy1c2b8c12023-11-16 00:44:42 +070046 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
Harald Weltedc023cf2022-11-29 23:15:18 +010047 unsigned int flags;
48 unsigned int status;
49 struct osmo_timer_list timer;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +070050 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010051 } 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 Yanitskiyb72625d2023-11-16 00:32:33 +070061/*! Default soft-UART configuration (8-N-1) */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070062const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = {
Harald Weltedc023cf2022-11-29 23:15:18 +010063 .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 Weltedc023cf2022-11-29 23:15:18 +010068};
69
70/*************************************************************************
71 * Receiver
72 *************************************************************************/
73
74/* flush the receive buffer + allocate new one, as needed */
75static 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 */
90static 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 */
108static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit)
109{
Harald Weltedc023cf2022-11-29 23:15:18 +0100110 if (!suart->rx.running)
111 return;
112
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700113 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 Weltedc023cf2022-11-29 23:15:18 +0100118 suart->rx.shift_reg = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100119 suart->rx.bit_count = 0;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700120 suart->rx.parity_bit = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100121 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700122 break;
123 case SUART_FLOW_ST_DATA:
124 suart->rx.bit_count++;
125 suart->rx.shift_reg >>= 1;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700126 if (bit != 0) {
127 suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700128 suart->rx.shift_reg |= 0x80;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700129 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700130 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 Yanitskiy1c2b8c12023-11-16 00:44:42 +0700139 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 Yanitskiy1a3b5112023-11-16 00:41:03 +0700155 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 Weltedc023cf2022-11-29 23:15:18 +0100168 }
169}
170
171/* receive timer expiration: flush rx-buffer to user call-back */
172static void suart_rx_timer_cb(void *data)
173{
174 struct osmo_soft_uart *suart = data;
175 suart_flush_rx(suart);
176}
177
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700178/*! 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 Weltedc023cf2022-11-29 23:15:18 +0100183int 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 Yanitskiyb72625d2023-11-16 00:32:33 +0700194/*! 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 Weltedc023cf2022-11-29 23:15:18 +0100197void 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 */
206static 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 Yanitskiyb72625d2023-11-16 00:32:33 +0700219/*! 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 Weltedc023cf2022-11-29 23:15:18 +0100224int 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 Yanitskiyb72625d2023-11-16 00:32:33 +0700231/*! 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 Weltedc023cf2022-11-29 23:15:18 +0100235int 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 Yanitskiyb72625d2023-11-16 00:32:33 +0700246/*! Allocate a soft-UART instance.
247 * \param[in] ctx parent talloc context.
248 * \param[in] name name of the soft-UART instance.
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700249 * \param[in] cfg initial configuration of the soft-UART instance.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700250 * \returns pointer to allocated soft-UART instance; NULL on error. */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700251struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
252 const struct osmo_soft_uart_cfg *cfg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100253{
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 Yanitskiy82a1ae72023-11-16 15:34:50 +0700258
259 OSMO_ASSERT(cfg != NULL);
260 suart->cfg = *cfg;
Harald Weltedc023cf2022-11-29 23:15:18 +0100261
262 return suart;
263}
264
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +0700265/*! Release memory taken by the given soft-UART.
266 * \param[in] suart soft-UART instance to be free()d. */
267void 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 Yanitskiyb72625d2023-11-16 00:32:33 +0700279/*! 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 Weltedc023cf2022-11-29 23:15:18 +0100283int 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 Yanitskiycdde6712023-11-16 15:17:37 +0700308/*! Enable/disable receiver of the given soft-UART.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700309 * \param[in] suart soft-UART instance to be re-configured.
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700310 * \param[in] enable enable/disable state of the receiver.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700311 * \returns 0 on success; negative on error. */
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700312int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable)
Harald Weltedc023cf2022-11-29 23:15:18 +0100313{
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700314 if (!enable && suart->rx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100315 suart_flush_rx(suart);
316 suart->rx.running = false;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700317 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700318 } else if (enable && !suart->rx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100319 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 Yanitskiy1a3b5112023-11-16 00:41:03 +0700322 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100323 }
324
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700325 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. */
332int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable)
333{
334 if (!enable && suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100335 /* FIXME: Tx */
336 suart->tx.running = false;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700337 } else if (enable && !suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100338 suart->tx.running = true;
339 }
340
341 return 0;
342}