Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | /*! \file soft_uart.h |
| 4 | * Software UART implementation. */ |
| 5 | /* |
| 6 | * (C) 2022 by Harald Welte <laforge@gnumonks.org> |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 7 | * (C) 2023 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * SPDX-License-Identifier: GPL-2.0+ |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <stdint.h> |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 26 | #include <stdbool.h> |
| 27 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 28 | #include <osmocom/core/bits.h> |
| 29 | #include <osmocom/core/msgb.h> |
| 30 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 31 | /*! Parity mode. |
| 32 | * https://en.wikipedia.org/wiki/Parity_bit */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 33 | enum osmo_soft_uart_parity_mode { |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 34 | OSMO_SUART_PARITY_NONE, /*!< No parity bit */ |
| 35 | OSMO_SUART_PARITY_EVEN, /*!< Even parity */ |
| 36 | OSMO_SUART_PARITY_ODD, /*!< Odd parity */ |
| 37 | OSMO_SUART_PARITY_MARK, /*!< Always 1 */ |
| 38 | OSMO_SUART_PARITY_SPACE, /*!< Always 0 */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 39 | _OSMO_SUART_PARITY_NUM |
| 40 | }; |
| 41 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 42 | /*! Flags passed to the application. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 43 | enum osmo_soft_uart_flags { |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 44 | OSMO_SUART_F_FRAMING_ERROR = (1 << 0), /*!< Framing error occurred */ |
| 45 | OSMO_SUART_F_PARITY_ERROR = (1 << 1), /*!< Parity error occurred */ |
| 46 | OSMO_SUART_F_BREAK = (1 << 2), /*!< Break condition (not implemented) */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 49 | /*! Modem status "line" flags. |
| 50 | * https://en.wikipedia.org/wiki/RS-232#Data_and_control_signals */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 51 | enum osmo_soft_uart_status { |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 52 | OSMO_SUART_STATUS_F_DTR = (1 << 0), /*!< Data Terminal Ready */ |
| 53 | OSMO_SUART_STATUS_F_DCD = (1 << 1), /*!< Data Carrier Detect */ |
| 54 | OSMO_SUART_STATUS_F_DSR = (1 << 2), /*!< Data Set Ready */ |
| 55 | OSMO_SUART_STATUS_F_RI = (1 << 3), /*!< Ring Indicator */ |
| 56 | OSMO_SUART_STATUS_F_RTS_RTR = (1 << 4), /*!< Request To Send or Ready To Receive */ |
| 57 | OSMO_SUART_STATUS_F_CTS = (1 << 5), /*!< Clear To Send */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 58 | }; |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 59 | |
| 60 | /*! Flow control mode. |
| 61 | * https://en.wikipedia.org/wiki/Flow_control_(data)#Hardware_flow_control */ |
| 62 | enum osmo_soft_uart_flow_ctrl_mode { |
| 63 | /*! No flow control */ |
| 64 | OSMO_SUART_FLOW_CTRL_NONE, |
| 65 | /*! DTR/DSR flow control: Tx if DSR is active and drop DTR if cannot Rx anymore. */ |
| 66 | OSMO_SUART_FLOW_CTRL_DTR_DSR, |
| 67 | /*! RTS/CTS flow control: Tx if CTS is active and drop RTS if cannot Rx anymore. |
| 68 | * The technically correct name would be RTR/CTS, because the RTS signal actually |
| 69 | * indicates readiness to *receive* data (Ready To Receive), and not really used |
| 70 | * to request a transmission (Request To Send) nowadays. Alternatively, the RTS |
| 71 | * signal can be interpreted as "Request To Send to me". */ |
| 72 | OSMO_SUART_FLOW_CTRL_RTS_CTS, |
| 73 | }; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 74 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 75 | /*! Configuration for a soft-UART. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 76 | struct osmo_soft_uart_cfg { |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 77 | /*! Number of data bits (typically 5, 6, 7 or 8). */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 78 | uint8_t num_data_bits; |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 79 | /*! Number of stop bits (typically 1 or 2). */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 80 | uint8_t num_stop_bits; |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 81 | /*! Parity mode (none, even, odd, space, mark). */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 82 | enum osmo_soft_uart_parity_mode parity_mode; |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 83 | /*! Size of the receive buffer; UART will buffer up to that number |
| 84 | * of characters before calling the receive call-back. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 85 | unsigned int rx_buf_size; |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 86 | /*! Receive timeout; UART will flush the receive buffer via the receive call-back |
| 87 | * after indicated number of milliseconds, even if it is not full yet. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 88 | unsigned int rx_timeout_ms; |
| 89 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 90 | /*! Opaque application-private data; passed to call-backs. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 91 | void *priv; |
| 92 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 93 | /*! Receive call-back of the application. |
| 94 | * |
| 95 | * Called if at least one of the following conditions is met: |
| 96 | * a) rx_buf_size characters were received (Rx buffer is full); |
| 97 | * b) rx_timeout_ms expired and Rx buffer is not empty; |
| 98 | * c) a parity or framing error is occurred. |
| 99 | * |
| 100 | * \param[in] priv opaque application-private data. |
| 101 | * \param[in] rx_data msgb holding the received data. |
| 102 | * Must be free()ed by the application. |
| 103 | * \param[in] flags bit-mask of OSMO_SUART_F_*. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 104 | void (*rx_cb)(void *priv, struct msgb *rx_data, unsigned int flags); |
| 105 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 106 | /*! Transmit call-back of the application. |
| 107 | * |
| 108 | * The implementation is expected to provide at most tx_data->data_len |
| 109 | * characters (the actual amount is determined by the number of requested |
| 110 | * bits and the effective UART configuration). |
| 111 | * |
| 112 | * \param[in] priv opaque application-private data. |
| 113 | * \param[inout] tx_data msgb for writing to be transmitted data. */ |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 114 | void (*tx_cb)(void *priv, struct msgb *tx_data); |
| 115 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 116 | /*! Modem status line change call-back. |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame] | 117 | * \param[in] priv opaque application-private data. |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 118 | * \param[in] status updated status; bit-mask of OSMO_SUART_STATUS_F_*. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 119 | void (*status_change_cb)(void *priv, unsigned int status); |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 120 | |
| 121 | /*! "Hardware" flow control mode. */ |
| 122 | enum osmo_soft_uart_flow_ctrl_mode flow_ctrl_mode; |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 123 | }; |
| 124 | |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 125 | extern const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg; |
| 126 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 127 | struct osmo_soft_uart; |
| 128 | |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 129 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name, |
| 130 | const struct osmo_soft_uart_cfg *cfg); |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 131 | void osmo_soft_uart_free(struct osmo_soft_uart *suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 132 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg); |
Vadim Yanitskiy | 59afb8f | 2023-11-29 23:58:10 +0700 | [diff] [blame] | 133 | |
| 134 | const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart); |
| 135 | void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name); |
| 136 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 137 | int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable); |
| 138 | int osmo_soft_uart_set_tx(struct osmo_soft_uart *suart, bool enable); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 139 | |
| 140 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits); |
| 141 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits); |
| 142 | |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 143 | unsigned int osmo_soft_uart_get_status(const struct osmo_soft_uart *suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 144 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status); |
Vadim Yanitskiy | 6587dd0 | 2023-11-30 03:04:32 +0700 | [diff] [blame] | 145 | void osmo_soft_uart_set_status_line(struct osmo_soft_uart *suart, |
| 146 | enum osmo_soft_uart_status line, |
| 147 | bool active); |
| 148 | |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 149 | void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart); |