blob: c7c8020607f9b8fd37d1be1c25ac7c4dfdd68cd3 [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;
46 ubit_t parity_bit;
47 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;
120 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700121 break;
122 case SUART_FLOW_ST_DATA:
123 suart->rx.bit_count++;
124 suart->rx.shift_reg >>= 1;
125 if (bit != 0)
126 suart->rx.shift_reg |= 0x80;
127 if (suart->rx.bit_count >= suart->cfg.num_data_bits) {
128 /* we have accumulated enough data bits */
129 if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE)
130 suart->rx.flow_state = SUART_FLOW_ST_PARITY;
131 else
132 suart->rx.flow_state = SUART_FLOW_ST_STOP;
133 }
134 break;
135 case SUART_FLOW_ST_PARITY:
136 /* TODO: actually verify parity */
137 suart->rx.flow_state = SUART_FLOW_ST_STOP;
138 break;
139 case SUART_FLOW_ST_STOP:
140 suart->rx.bit_count++;
141 if (bit != 1)
142 suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR;
143
144 if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
145 /* we have accumulated enough stop bits */
146 suart_rx_ch(suart, suart->rx.shift_reg);
147 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
148 }
149 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100150 }
151}
152
153/* receive timer expiration: flush rx-buffer to user call-back */
154static void suart_rx_timer_cb(void *data)
155{
156 struct osmo_soft_uart *suart = data;
157 suart_flush_rx(suart);
158}
159
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700160/*! Feed a number of unpacked bits into the soft-UART receiver.
161 * \param[in] suart soft-UART instance to feed bits into.
162 * \param[in] ubits pointer to the unpacked bits.
163 * \param[in] n_ubits number of unpacked bits to be fed.
164 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100165int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits)
166{
167 for (size_t i = 0; i < n_ubits; i++)
168 osmo_uart_rx_bit(suart, ubits[i]);
169 return 0;
170}
171
172/*************************************************************************
173 * Transmitter
174 *************************************************************************/
175
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700176/*! Enqueue the given message buffer into the transmit queue of the soft-UART.
177 * \param[in] suart soft-UART instance for transmitting data.
178 * \param[in] tx_data message buffer containing to be transmitted data. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100179void osmo_soft_uart_tx(struct osmo_soft_uart *suart, struct msgb *tx_data)
180{
181 if (!suart->tx.msg)
182 suart->tx.msg = tx_data;
183 else
184 msgb_enqueue(&suart->tx.queue, tx_data);
185}
186
187/* pull a single bit out of the UART transmitter */
188static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart)
189{
190 if (!suart->tx.running)
191 return 1;
192
193 if (suart->tx.bit_count == 0) {
194 /* do we have anything to transmit? */
195 /* FIXME */
196 }
197 /* FIXME */
198 return 1;
199}
200
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700201/*! Pull a number of unpacked bits out of the soft-UART transmitter.
202 * \param[in] suart soft-UART instance to pull the bits from.
203 * \param[out] ubits pointer to a buffer where to store pulled bits.
204 * \param[in] n_ubits number of unpacked bits to be pulled.
205 * \returns number of unpacked bits pulled; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100206int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits)
207{
208 for (size_t i = 0; i < n_ubits; i++)
209 ubits[i] = osmo_uart_tx_bit(suart);
210 return n_ubits;
211}
212
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700213/*! Set the modem status lines of the given soft-UART.
214 * \param[in] suart soft-UART instance to update the modem status.
215 * \param[in] status mask of osmo_soft_uart_status.
216 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100217int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status)
218{
219 /* FIXME: Tx */
220 return 0;
221}
222
223
224/*************************************************************************
225 * Management / Initialization
226 *************************************************************************/
227
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700228/*! Allocate a soft-UART instance.
229 * \param[in] ctx parent talloc context.
230 * \param[in] name name of the soft-UART instance.
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700231 * \param[in] cfg initial configuration of the soft-UART instance.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700232 * \returns pointer to allocated soft-UART instance; NULL on error. */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700233struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
234 const struct osmo_soft_uart_cfg *cfg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100235{
236 struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart);
237 if (!suart)
238 return NULL;
239 suart->name = talloc_strdup(suart, name);
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700240
241 OSMO_ASSERT(cfg != NULL);
242 suart->cfg = *cfg;
Harald Weltedc023cf2022-11-29 23:15:18 +0100243
244 return suart;
245}
246
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +0700247/*! Release memory taken by the given soft-UART.
248 * \param[in] suart soft-UART instance to be free()d. */
249void osmo_soft_uart_free(struct osmo_soft_uart *suart)
250{
251 if (suart == NULL)
252 return;
253
254 osmo_timer_del(&suart->rx.timer);
255 msgb_free(suart->rx.msg);
256
257 talloc_free((void *)suart->name);
258 talloc_free(suart);
259}
260
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700261/*! Change soft-UART configuration to the user-provided config.
262 * \param[in] suart soft-UART instance to be re-configured.
263 * \param[in] cfg the user-provided config to be applied.
264 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100265int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg)
266{
267 /* consistency checks on the configuration */
268 if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0)
269 return -EINVAL;
270 if (cfg->num_stop_bits == 0)
271 return -EINVAL;
272 if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM)
273 return -EINVAL;
274 if (cfg->rx_buf_size == 0)
275 return -EINVAL;
276
277 if (suart->cfg.rx_buf_size > cfg->rx_buf_size ||
278 suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) {
279 suart_flush_rx(suart);
280 }
281
282 suart->cfg = *cfg;
283
284 osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart);
285 INIT_LLIST_HEAD(&suart->tx.queue);
286
287 return 0;
288}
289
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700290/*! Enable/disable receiver of the given soft-UART.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700291 * \param[in] suart soft-UART instance to be re-configured.
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700292 * \param[in] enable enable/disable state of the receiver.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700293 * \returns 0 on success; negative on error. */
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700294int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable)
Harald Weltedc023cf2022-11-29 23:15:18 +0100295{
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700296 if (!enable && suart->rx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100297 suart_flush_rx(suart);
298 suart->rx.running = false;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700299 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700300 } else if (enable && !suart->rx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100301 if (!suart->rx.msg)
302 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx");
303 suart->rx.running = true;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700304 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100305 }
306
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700307 return 0;
308}
309
310/*! Enable/disable transmitter of the given soft-UART.
311 * \param[in] suart soft-UART instance to be re-configured.
312 * \param[in] enable enable/disable state of the transmitter.
313 * \returns 0 on success; negative on error. */
314int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable)
315{
316 if (!enable && suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100317 /* FIXME: Tx */
318 suart->tx.running = false;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700319 } else if (enable && !suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100320 suart->tx.running = true;
321 }
322
323 return 0;
324}