blob: 15cea81660e08f6c0daa962b907dcef85d10c89f [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,
32 _OSMO_SUART_PARITY_NUM
33};
34
35enum osmo_soft_uart_flags {
36 OSMO_SUART_F_FRAMING_ERROR = (1 << 0),
37 OSMO_SUART_F_PARITY_ERROR = (1 << 1),
38 OSMO_SUART_F_BREAK = (1 << 2),
39};
40
41#if 0
42enum osmo_soft_uart_status {
43 /* RTS, CTS, ... */
44 _fixme,
45};
46#endif
47
48/* configuration for a soft-uart */
49struct osmo_soft_uart_cfg {
50 /*! number of data bits (typically 5, 6, 7 or 8) */
51 uint8_t num_data_bits;
52 /*! number of stop bots (typically 1 or 2) */
53 uint8_t num_stop_bits;
54 /*! parity mode (none, even, odd) */
55 enum osmo_soft_uart_parity_mode parity_mode;
56 /*! size of receive buffer; UART will buffer up to that number of characters
57 * before calling the receive call-back */
58 unsigned int rx_buf_size;
59 /*! receive timeout; UART will flush receive buffer via the receive call-back
60 * after indicated number of milli-seconds even if it is not full yet */
61 unsigned int rx_timeout_ms;
62
63 /*! opaque application-private data; passed to call-backs */
64 void *priv;
65
66 /*! receive call-back. Either rx_buf_size characters were received or rx_timeout_ms
67 * expired, or an error flag was detected (related to last byte received).
68 * 'flags' is a bit-mask of osmo_soft_uart_flags, */
69 void (*rx_cb)(void *priv, struct msgb *rx_data, unsigned int flags);
70
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070071 /*! transmit call-back. The implementation is expected to provide at most
72 * tx_data->data_len characters (the actual amount is detetmined by the
73 * number of requested bits and the effective UART configuration). */
74 void (*tx_cb)(void *priv, struct msgb *tx_data);
75
Harald Weltedc023cf2022-11-29 23:15:18 +010076 /*! modem status line change call-back. gets bitmask of osmo_soft_uart_status */
77 void (*status_change_cb)(void *priv, unsigned int status);
78};
79
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070080extern const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg;
81
Harald Weltedc023cf2022-11-29 23:15:18 +010082struct osmo_soft_uart;
83
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +070084struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
85 const struct osmo_soft_uart_cfg *cfg);
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +070086void osmo_soft_uart_free(struct osmo_soft_uart *suart);
Harald Weltedc023cf2022-11-29 23:15:18 +010087int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg);
Vadim Yanitskiycdde6712023-11-16 15:17:37 +070088int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable);
89int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable);
Harald Weltedc023cf2022-11-29 23:15:18 +010090
91int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits);
92int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits);
93
Harald Weltedc023cf2022-11-29 23:15:18 +010094int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status);
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +070095void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart);