blob: 2b108c49c3cd3593146b7ea34693f8a31ebba7a5 [file] [log] [blame]
Harald Weltedc023cf2022-11-29 23:15:18 +01001#pragma once
2
3/*! \file soft_uart.h
4 * Software UART implementation. */
5/*
6 * (C) 2022 by Harald Welte <laforge@gnumonks.org>
7 *
8 * All Rights Reserved
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 */
23
24#include <stdint.h>
25#include <osmocom/core/bits.h>
26#include <osmocom/core/msgb.h>
27
28enum osmo_soft_uart_parity_mode {
29 OSMO_SUART_PARITY_NONE,
30 OSMO_SUART_PARITY_EVEN,
31 OSMO_SUART_PARITY_ODD,
Vadim Yanitskiy0d78a002023-11-19 16:15:38 +070032 OSMO_SUART_PARITY_MARK, /* always 1 */
33 OSMO_SUART_PARITY_SPACE, /* always 0 */
Harald Weltedc023cf2022-11-29 23:15:18 +010034 _OSMO_SUART_PARITY_NUM
35};
36
37enum osmo_soft_uart_flags {
38 OSMO_SUART_F_FRAMING_ERROR = (1 << 0),
39 OSMO_SUART_F_PARITY_ERROR = (1 << 1),
40 OSMO_SUART_F_BREAK = (1 << 2),
41};
42
43#if 0
44enum osmo_soft_uart_status {
45 /* RTS, CTS, ... */
46 _fixme,
47};
48#endif
49
50/* configuration for a soft-uart */
51struct osmo_soft_uart_cfg {
52 /*! number of data bits (typically 5, 6, 7 or 8) */
53 uint8_t num_data_bits;
54 /*! number of stop bots (typically 1 or 2) */
55 uint8_t num_stop_bits;
56 /*! parity mode (none, even, odd) */
57 enum osmo_soft_uart_parity_mode parity_mode;
58 /*! size of receive buffer; UART will buffer up to that number of characters
59 * before calling the receive call-back */
60 unsigned int rx_buf_size;
61 /*! receive timeout; UART will flush receive buffer via the receive call-back
62 * after indicated number of milli-seconds even if it is not full yet */
63 unsigned int rx_timeout_ms;
64
65 /*! opaque application-private data; passed to call-backs */
66 void *priv;
67
68 /*! receive call-back. Either rx_buf_size characters were received or rx_timeout_ms
69 * expired, or an error flag was detected (related to last byte received).
70 * 'flags' is a bit-mask of osmo_soft_uart_flags, */
71 void (*rx_cb)(void *priv, struct msgb *rx_data, unsigned int flags);
72
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070073 /*! transmit call-back. The implementation is expected to provide at most
74 * tx_data->data_len characters (the actual amount is detetmined by the
75 * number of requested bits and the effective UART configuration). */
76 void (*tx_cb)(void *priv, struct msgb *tx_data);
77
Harald Weltedc023cf2022-11-29 23:15:18 +010078 /*! modem status line change call-back. gets bitmask of osmo_soft_uart_status */
79 void (*status_change_cb)(void *priv, unsigned int status);
80};
81
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070082extern const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg;
83
Harald Weltedc023cf2022-11-29 23:15:18 +010084struct osmo_soft_uart;
85
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070086struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
87 const struct osmo_soft_uart_cfg *cfg);
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +070088void osmo_soft_uart_free(struct osmo_soft_uart *suart);
Harald Weltedc023cf2022-11-29 23:15:18 +010089int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg);
Vadim Yanitskiycdde6712023-11-16 15:17:37 +070090int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable);
91int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable);
Harald Weltedc023cf2022-11-29 23:15:18 +010092
93int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits);
94int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits);
95
Harald Weltedc023cf2022-11-29 23:15:18 +010096int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status);
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +070097void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart);