blob: 17a7c6750234678c900652369b1bc359b0d60d39 [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
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070028/*! Parity mode.
29 * https://en.wikipedia.org/wiki/Parity_bit */
Harald Weltedc023cf2022-11-29 23:15:18 +010030enum osmo_soft_uart_parity_mode {
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070031 OSMO_SUART_PARITY_NONE, /*!< No parity bit */
32 OSMO_SUART_PARITY_EVEN, /*!< Even parity */
33 OSMO_SUART_PARITY_ODD, /*!< Odd parity */
34 OSMO_SUART_PARITY_MARK, /*!< Always 1 */
35 OSMO_SUART_PARITY_SPACE, /*!< Always 0 */
Harald Weltedc023cf2022-11-29 23:15:18 +010036 _OSMO_SUART_PARITY_NUM
37};
38
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070039/*! Flags passed to the application. */
Harald Weltedc023cf2022-11-29 23:15:18 +010040enum osmo_soft_uart_flags {
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070041 OSMO_SUART_F_FRAMING_ERROR = (1 << 0), /*!< Framing error occurred */
42 OSMO_SUART_F_PARITY_ERROR = (1 << 1), /*!< Parity error occurred */
43 OSMO_SUART_F_BREAK = (1 << 2), /*!< Break condition (not implemented) */
Harald Weltedc023cf2022-11-29 23:15:18 +010044};
45
46#if 0
47enum osmo_soft_uart_status {
48 /* RTS, CTS, ... */
49 _fixme,
50};
51#endif
52
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070053/*! Configuration for a soft-UART. */
Harald Weltedc023cf2022-11-29 23:15:18 +010054struct osmo_soft_uart_cfg {
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070055 /*! Number of data bits (typically 5, 6, 7 or 8). */
Harald Weltedc023cf2022-11-29 23:15:18 +010056 uint8_t num_data_bits;
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070057 /*! Number of stop bits (typically 1 or 2). */
Harald Weltedc023cf2022-11-29 23:15:18 +010058 uint8_t num_stop_bits;
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070059 /*! Parity mode (none, even, odd, space, mark). */
Harald Weltedc023cf2022-11-29 23:15:18 +010060 enum osmo_soft_uart_parity_mode parity_mode;
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070061 /*! Size of the receive buffer; UART will buffer up to that number
62 * of characters before calling the receive call-back. */
Harald Weltedc023cf2022-11-29 23:15:18 +010063 unsigned int rx_buf_size;
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070064 /*! Receive timeout; UART will flush the receive buffer via the receive call-back
65 * after indicated number of milliseconds, even if it is not full yet. */
Harald Weltedc023cf2022-11-29 23:15:18 +010066 unsigned int rx_timeout_ms;
67
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070068 /*! Opaque application-private data; passed to call-backs. */
Harald Weltedc023cf2022-11-29 23:15:18 +010069 void *priv;
70
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070071 /*! Receive call-back of the application.
72 *
73 * Called if at least one of the following conditions is met:
74 * a) rx_buf_size characters were received (Rx buffer is full);
75 * b) rx_timeout_ms expired and Rx buffer is not empty;
76 * c) a parity or framing error is occurred.
77 *
78 * \param[in] priv opaque application-private data.
79 * \param[in] rx_data msgb holding the received data.
80 * Must be free()ed by the application.
81 * \param[in] flags bit-mask of OSMO_SUART_F_*. */
Harald Weltedc023cf2022-11-29 23:15:18 +010082 void (*rx_cb)(void *priv, struct msgb *rx_data, unsigned int flags);
83
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070084 /*! Transmit call-back of the application.
85 *
86 * The implementation is expected to provide at most tx_data->data_len
87 * characters (the actual amount is determined by the number of requested
88 * bits and the effective UART configuration).
89 *
90 * \param[in] priv opaque application-private data.
91 * \param[inout] tx_data msgb for writing to be transmitted data. */
Vadim Yanitskiyc9fc77f2023-11-16 00:55:35 +070092 void (*tx_cb)(void *priv, struct msgb *tx_data);
93
Vadim Yanitskiy275c86e2023-12-04 01:44:09 +070094 /*! Modem status line change call-back.
95 *
96 * FIXME: flow control is not implemented, so it's never called.
97 *
98 * \param[in] priv opaque application-private data.
99 * \param[in] status bit-mask of osmo_soft_uart_status. */
Harald Weltedc023cf2022-11-29 23:15:18 +0100100 void (*status_change_cb)(void *priv, unsigned int status);
101};
102
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700103extern const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg;
104
Harald Weltedc023cf2022-11-29 23:15:18 +0100105struct osmo_soft_uart;
106
Vadim Yanitskiy82a1ae72023-11-16 15:34:50 +0700107struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name,
108 const struct osmo_soft_uart_cfg *cfg);
Vadim Yanitskiy877cfed2023-11-12 17:21:31 +0700109void osmo_soft_uart_free(struct osmo_soft_uart *suart);
Harald Weltedc023cf2022-11-29 23:15:18 +0100110int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg);
Vadim Yanitskiy59afb8f2023-11-29 23:58:10 +0700111
112const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart);
113void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name);
114
Vadim Yanitskiycdde6712023-11-16 15:17:37 +0700115int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable);
116int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable);
Harald Weltedc023cf2022-11-29 23:15:18 +0100117
118int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits);
119int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits);
120
Harald Weltedc023cf2022-11-29 23:15:18 +0100121int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status);
Vadim Yanitskiy03f0ed72023-11-18 00:29:08 +0700122void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart);