blob: 5ddddb45a42dc8a1271ef9625c26d834baa6abfe [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
Vadim Yanitskiy459cb062023-11-21 20:12:36 +070027#include <osmocom/core/utils.h>
Harald Weltedc023cf2022-11-29 23:15:18 +010028#include <osmocom/core/timer.h>
29#include <osmocom/core/soft_uart.h>
30
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +070031/*! Rx/Tx flow state of a soft-UART */
32enum suart_flow_state {
33 SUART_FLOW_ST_IDLE, /*!< waiting for a start bit or Tx data */
34 SUART_FLOW_ST_DATA, /*!< receiving/transmitting data bits */
35 SUART_FLOW_ST_PARITY, /*!< receiving/transmitting parity bits */
36 SUART_FLOW_ST_STOP, /*!< receiving/transmitting stop bits */
37};
38
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +070039/*! Internal state of a soft-UART */
Harald Weltedc023cf2022-11-29 23:15:18 +010040struct osmo_soft_uart {
41 struct osmo_soft_uart_cfg cfg;
42 const char *name;
43 struct {
44 bool running;
45 uint8_t bit_count;
46 uint8_t shift_reg;
47 struct msgb *msg;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +070048 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
Harald Weltedc023cf2022-11-29 23:15:18 +010049 unsigned int flags;
50 unsigned int status;
51 struct osmo_timer_list timer;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +070052 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010053 } rx;
54 struct {
55 bool running;
56 uint8_t bit_count;
57 uint8_t shift_reg;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070058 ubit_t parity_bit; /* 0 (even) / 1 (odd) */
59 enum suart_flow_state flow_state;
Harald Weltedc023cf2022-11-29 23:15:18 +010060 } tx;
61};
62
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +070063/*! Default soft-UART configuration (8-N-1) */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070064const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg = {
Harald Weltedc023cf2022-11-29 23:15:18 +010065 .num_data_bits = 8,
66 .num_stop_bits = 1,
67 .parity_mode = OSMO_SUART_PARITY_NONE,
68 .rx_buf_size = 1024,
69 .rx_timeout_ms = 100,
Harald Weltedc023cf2022-11-29 23:15:18 +010070};
71
72/*************************************************************************
73 * Receiver
74 *************************************************************************/
75
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +070076/*! Flush the receive buffer, passing ownership of the msgb to the .rx_cb().
77 * \param[in] suart soft-UART instance holding the receive buffer. */
78void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart)
Harald Weltedc023cf2022-11-29 23:15:18 +010079{
80 if ((suart->rx.msg && msgb_length(suart->rx.msg)) || suart->rx.flags) {
81 osmo_timer_del(&suart->rx.timer);
82 if (suart->cfg.rx_cb) {
83 suart->cfg.rx_cb(suart->cfg.priv, suart->rx.msg, suart->rx.flags);
84 /* call-back has taken ownership of msgb, no need to free() here */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070085 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart_rx");
Harald Weltedc023cf2022-11-29 23:15:18 +010086 } else {
87 msgb_reset(suart->rx.msg);
88 }
89 }
90}
91
92/* one character was received; add to receive buffer and notify user, if needed */
93static void suart_rx_ch(struct osmo_soft_uart *suart, uint8_t ch)
94{
95 unsigned int msg_len;
96
97 OSMO_ASSERT(suart->rx.msg);
98 msgb_put_u8(suart->rx.msg, ch);
99 msg_len = msgb_length(suart->rx.msg);
100
Vadim Yanitskiybf95f822023-11-18 01:46:16 +0700101 if (msg_len >= suart->cfg.rx_buf_size || suart->rx.flags) {
102 /* either the buffer is full, or we hit a parity and/or a framing error */
103 osmo_soft_uart_flush_rx(suart);
104 } else if (msg_len == 1) {
105 /* first character in new message: start timer */
Harald Weltedc023cf2022-11-29 23:15:18 +0100106 osmo_timer_schedule(&suart->rx.timer, suart->cfg.rx_timeout_ms / 1000,
107 (suart->cfg.rx_timeout_ms % 1000) * 1000);
Harald Weltedc023cf2022-11-29 23:15:18 +0100108 }
109}
110
111/* receive a single bit */
112static inline void osmo_uart_rx_bit(struct osmo_soft_uart *suart, const ubit_t bit)
113{
Harald Weltedc023cf2022-11-29 23:15:18 +0100114 if (!suart->rx.running)
115 return;
116
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700117 switch (suart->rx.flow_state) {
118 case SUART_FLOW_ST_IDLE:
119 if (bit == 0) { /* start bit condition */
120 suart->rx.flow_state = SUART_FLOW_ST_DATA;
121 suart->rx.flags = 0x00;
Harald Weltedc023cf2022-11-29 23:15:18 +0100122 suart->rx.shift_reg = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100123 suart->rx.bit_count = 0;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700124 suart->rx.parity_bit = 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100125 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700126 break;
127 case SUART_FLOW_ST_DATA:
128 suart->rx.bit_count++;
129 suart->rx.shift_reg >>= 1;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700130 if (bit != 0) {
131 suart->rx.parity_bit = !suart->rx.parity_bit; /* flip */
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700132 suart->rx.shift_reg |= 0x80;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700133 }
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700134 if (suart->rx.bit_count >= suart->cfg.num_data_bits) {
135 /* we have accumulated enough data bits */
136 if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE)
137 suart->rx.flow_state = SUART_FLOW_ST_PARITY;
138 else
139 suart->rx.flow_state = SUART_FLOW_ST_STOP;
Vadim Yanitskiy2d2ce492023-11-19 15:37:10 +0700140 /* align the register if needed */
141 if (suart->cfg.num_data_bits < 8)
142 suart->rx.shift_reg >>= (8 - suart->cfg.num_data_bits);
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700143 }
144 break;
145 case SUART_FLOW_ST_PARITY:
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700146 switch (suart->cfg.parity_mode) {
147 case OSMO_SUART_PARITY_EVEN:
148 /* number of 1-bits (in both data and parity) shall be even */
149 if (suart->rx.parity_bit != bit)
150 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
151 break;
152 case OSMO_SUART_PARITY_ODD:
153 /* number of 1-bits (in both data and parity) shall be odd */
154 if (suart->rx.parity_bit == bit)
155 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
156 break;
Vadim Yanitskiy0d78a002023-11-19 16:15:38 +0700157 case OSMO_SUART_PARITY_MARK:
158 /* parity bit must always be 1 */
159 if (bit != 1)
160 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
161 break;
162 case OSMO_SUART_PARITY_SPACE:
163 /* parity bit must always be 0 */
164 if (bit != 0)
165 suart->rx.flags |= OSMO_SUART_F_PARITY_ERROR;
166 break;
Vadim Yanitskiy1c2b8c12023-11-16 00:44:42 +0700167 case OSMO_SUART_PARITY_NONE: /* shall not happen */
168 default:
169 OSMO_ASSERT(0);
170 }
171
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700172 suart->rx.flow_state = SUART_FLOW_ST_STOP;
173 break;
174 case SUART_FLOW_ST_STOP:
175 suart->rx.bit_count++;
176 if (bit != 1)
177 suart->rx.flags |= OSMO_SUART_F_FRAMING_ERROR;
178
179 if (suart->rx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
180 /* we have accumulated enough stop bits */
181 suart_rx_ch(suart, suart->rx.shift_reg);
182 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
183 }
184 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100185 }
186}
187
188/* receive timer expiration: flush rx-buffer to user call-back */
189static void suart_rx_timer_cb(void *data)
190{
191 struct osmo_soft_uart *suart = data;
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700192 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100193}
194
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700195/*! Feed a number of unpacked bits into the soft-UART receiver.
196 * \param[in] suart soft-UART instance to feed bits into.
197 * \param[in] ubits pointer to the unpacked bits.
198 * \param[in] n_ubits number of unpacked bits to be fed.
199 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100200int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits)
201{
202 for (size_t i = 0; i < n_ubits; i++)
203 osmo_uart_rx_bit(suart, ubits[i]);
204 return 0;
205}
206
207/*************************************************************************
208 * Transmitter
209 *************************************************************************/
210
Harald Weltedc023cf2022-11-29 23:15:18 +0100211/* pull a single bit out of the UART transmitter */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700212static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart, struct msgb *msg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100213{
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700214 ubit_t tx_bit = 1;
Harald Weltedc023cf2022-11-29 23:15:18 +0100215
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700216 if (!suart->tx.running)
217 return tx_bit;
218
219 switch (suart->tx.flow_state) {
220 case SUART_FLOW_ST_IDLE:
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700221 if (msg && msgb_length(msg) > 0) { /* if we have pending data */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700222 suart->tx.shift_reg = msgb_pull_u8(msg);
223 suart->tx.flow_state = SUART_FLOW_ST_DATA;
224 suart->tx.bit_count = 0;
225 suart->tx.parity_bit = 0;
226 tx_bit = 0;
227 }
228 break;
229 case SUART_FLOW_ST_DATA:
230 tx_bit = suart->tx.shift_reg & 1;
231 suart->tx.parity_bit ^= tx_bit;
232 suart->tx.shift_reg >>= 1;
233 suart->tx.bit_count++;
234 if (suart->tx.bit_count >= suart->cfg.num_data_bits) {
235 /* we have transmitted all data bits */
236 if (suart->cfg.parity_mode != OSMO_SUART_PARITY_NONE)
237 suart->tx.flow_state = SUART_FLOW_ST_PARITY;
238 else
239 suart->tx.flow_state = SUART_FLOW_ST_STOP;
240 }
241 break;
242 case SUART_FLOW_ST_PARITY:
243 switch (suart->cfg.parity_mode) {
244 case OSMO_SUART_PARITY_EVEN:
245 /* number of 1-bits (in both data and parity) shall be even */
246 tx_bit = suart->tx.parity_bit;
247 break;
248 case OSMO_SUART_PARITY_ODD:
249 /* number of 1-bits (in both data and parity) shall be odd */
250 tx_bit = !suart->tx.parity_bit;
251 break;
Vadim Yanitskiy0d78a002023-11-19 16:15:38 +0700252 case OSMO_SUART_PARITY_MARK:
253 /* parity bit must always be 1 */
254 tx_bit = 1;
255 break;
256 case OSMO_SUART_PARITY_SPACE:
257 /* parity bit must always be 0 */
258 tx_bit = 0;
259 break;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700260 case OSMO_SUART_PARITY_NONE:
261 default: /* shall not happen */
262 OSMO_ASSERT(0);
263 }
264
265 suart->tx.flow_state = SUART_FLOW_ST_STOP;
266 break;
267 case SUART_FLOW_ST_STOP:
268 suart->tx.bit_count++;
269 if (suart->tx.bit_count >= (suart->cfg.num_data_bits + suart->cfg.num_stop_bits)) {
270 /* we have transmitted all stop bits, we're done */
271 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
272 }
273 break;
Harald Weltedc023cf2022-11-29 23:15:18 +0100274 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700275
276 return tx_bit;
Harald Weltedc023cf2022-11-29 23:15:18 +0100277}
278
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700279/*! Pull a number of unpacked bits out of the soft-UART transmitter.
280 * \param[in] suart soft-UART instance to pull the bits from.
281 * \param[out] ubits pointer to a buffer where to store pulled bits.
282 * \param[in] n_ubits number of unpacked bits to be pulled.
283 * \returns number of unpacked bits pulled; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100284int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits)
285{
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700286 const struct osmo_soft_uart_cfg *cfg = &suart->cfg;
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700287 size_t n_frame_bits, n_chars;
288 struct msgb *msg = NULL;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700289
Vadim Yanitskiy459cb062023-11-21 20:12:36 +0700290 if (OSMO_UNLIKELY(n_ubits == 0))
291 return -EINVAL;
292
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700293 /* calculate UART frame size for the effective config */
294 n_frame_bits = 1 + cfg->num_data_bits + cfg->num_stop_bits;
295 if (cfg->parity_mode != OSMO_SUART_PARITY_NONE)
296 n_frame_bits += 1;
297
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700298 /* calculate the number of characters we can fit into n_ubits */
299 n_chars = n_ubits / n_frame_bits;
300 if (n_chars == 0) {
301 /* we can transmit at least one character */
302 if (suart->tx.flow_state == SUART_FLOW_ST_IDLE)
303 n_chars = 1;
304 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700305
Vadim Yanitskiyffdd9972023-11-21 02:25:32 +0700306 if (n_chars > 0) {
307 /* allocate a Tx buffer msgb */
308 msg = msgb_alloc_c(suart, n_chars, "soft_uart_tx");
309 OSMO_ASSERT(msg != NULL);
310
311 /* call the .tx_cb() to populate the Tx buffer */
312 OSMO_ASSERT(cfg->tx_cb != NULL);
313 suart->cfg.tx_cb(cfg->priv, msg);
314 }
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700315
Harald Weltedc023cf2022-11-29 23:15:18 +0100316 for (size_t i = 0; i < n_ubits; i++)
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700317 ubits[i] = osmo_uart_tx_bit(suart, msg);
318 msgb_free(msg);
319
320 return 0;
Harald Weltedc023cf2022-11-29 23:15:18 +0100321}
322
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700323/*! Set the modem status lines of the given soft-UART.
324 * \param[in] suart soft-UART instance to update the modem status.
325 * \param[in] status mask of osmo_soft_uart_status.
326 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100327int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status)
328{
329 /* FIXME: Tx */
330 return 0;
331}
332
333
334/*************************************************************************
335 * Management / Initialization
336 *************************************************************************/
337
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700338/*! Allocate a soft-UART instance.
339 * \param[in] ctx parent talloc context.
340 * \param[in] name name of the soft-UART instance.
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700341 * \param[in] cfg initial configuration of the soft-UART instance.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700342 * \returns pointer to allocated soft-UART instance; NULL on error. */
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700343struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
344 const struct osmo_soft_uart_cfg *cfg)
Harald Weltedc023cf2022-11-29 23:15:18 +0100345{
346 struct osmo_soft_uart *suart = talloc_zero(ctx, struct osmo_soft_uart);
347 if (!suart)
348 return NULL;
349 suart->name = talloc_strdup(suart, name);
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700350
351 OSMO_ASSERT(cfg != NULL);
352 suart->cfg = *cfg;
Harald Weltedc023cf2022-11-29 23:15:18 +0100353
354 return suart;
355}
356
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +0700357/*! Release memory taken by the given soft-UART.
358 * \param[in] suart soft-UART instance to be free()d. */
359void osmo_soft_uart_free(struct osmo_soft_uart *suart)
360{
361 if (suart == NULL)
362 return;
363
364 osmo_timer_del(&suart->rx.timer);
365 msgb_free(suart->rx.msg);
366
367 talloc_free((void *)suart->name);
368 talloc_free(suart);
369}
370
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700371/*! Change soft-UART configuration to the user-provided config.
372 * \param[in] suart soft-UART instance to be re-configured.
373 * \param[in] cfg the user-provided config to be applied.
374 * \returns 0 on success; negative on error. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100375int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg)
376{
377 /* consistency checks on the configuration */
378 if (cfg->num_data_bits > 8 || cfg->num_data_bits == 0)
379 return -EINVAL;
380 if (cfg->num_stop_bits == 0)
381 return -EINVAL;
382 if (cfg->parity_mode < 0 || cfg->parity_mode >= _OSMO_SUART_PARITY_NUM)
383 return -EINVAL;
384 if (cfg->rx_buf_size == 0)
385 return -EINVAL;
386
387 if (suart->cfg.rx_buf_size > cfg->rx_buf_size ||
388 suart->cfg.rx_timeout_ms > cfg->rx_timeout_ms) {
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700389 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100390 }
391
392 suart->cfg = *cfg;
393
394 osmo_timer_setup(&suart->rx.timer, suart_rx_timer_cb, suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100395
396 return 0;
397}
398
Vadim Yanitskiy59afb8f2023-11-29 23:58:10 +0700399/*! Get a name for the given soft-UART instance.
400 * \param[in] suart soft-UART instance to get the name from.
401 * \returns name of the given soft-UART instance. */
402const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart)
403{
404 return suart->name;
405}
406
407/*! Set a new name for the given soft-UART instance.
408 * \param[in] suart soft-UART instance to set the name for.
409 * \param[in] name the new name. */
410void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name)
411{
412 osmo_talloc_replace_string(suart, (char **)&suart->name, name);
413}
414
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700415/*! Enable/disable receiver of the given soft-UART.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700416 * \param[in] suart soft-UART instance to be re-configured.
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700417 * \param[in] enable enable/disable state of the receiver.
Vadim Yanitskiyb72625d2023-11-16 00:32:33 +0700418 * \returns 0 on success; negative on error. */
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700419int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable)
Harald Weltedc023cf2022-11-29 23:15:18 +0100420{
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700421 if (!enable && suart->rx.running) {
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700422 osmo_soft_uart_flush_rx(suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100423 suart->rx.running = false;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700424 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700425 } else if (enable && !suart->rx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100426 if (!suart->rx.msg)
427 suart->rx.msg = msgb_alloc_c(suart, suart->cfg.rx_buf_size, "soft_uart rx");
428 suart->rx.running = true;
Vadim Yanitskiy1a3b5112023-11-16 00:41:03 +0700429 suart->rx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100430 }
431
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700432 return 0;
433}
434
435/*! Enable/disable transmitter of the given soft-UART.
436 * \param[in] suart soft-UART instance to be re-configured.
437 * \param[in] enable enable/disable state of the transmitter.
438 * \returns 0 on success; negative on error. */
439int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable)
440{
441 if (!enable && suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100442 suart->tx.running = false;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700443 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700444 } else if (enable && !suart->tx.running) {
Harald Weltedc023cf2022-11-29 23:15:18 +0100445 suart->tx.running = true;
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +0700446 suart->tx.flow_state = SUART_FLOW_ST_IDLE;
Harald Weltedc023cf2022-11-29 23:15:18 +0100447 }
448
449 return 0;
450}