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> |
| 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 Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 28 | /*! Parity mode. |
| 29 | * https://en.wikipedia.org/wiki/Parity_bit */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 30 | enum osmo_soft_uart_parity_mode { |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 31 | 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 Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 36 | _OSMO_SUART_PARITY_NUM |
| 37 | }; |
| 38 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 39 | /*! Flags passed to the application. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 40 | enum osmo_soft_uart_flags { |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 41 | 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 Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | #if 0 |
| 47 | enum osmo_soft_uart_status { |
| 48 | /* RTS, CTS, ... */ |
| 49 | _fixme, |
| 50 | }; |
| 51 | #endif |
| 52 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 53 | /*! Configuration for a soft-UART. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 54 | struct osmo_soft_uart_cfg { |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 55 | /*! Number of data bits (typically 5, 6, 7 or 8). */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 56 | uint8_t num_data_bits; |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 57 | /*! Number of stop bits (typically 1 or 2). */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 58 | uint8_t num_stop_bits; |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 59 | /*! Parity mode (none, even, odd, space, mark). */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 60 | enum osmo_soft_uart_parity_mode parity_mode; |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 61 | /*! Size of the receive buffer; UART will buffer up to that number |
| 62 | * of characters before calling the receive call-back. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 63 | unsigned int rx_buf_size; |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 64 | /*! 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 Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 66 | unsigned int rx_timeout_ms; |
| 67 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 68 | /*! Opaque application-private data; passed to call-backs. */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 69 | void *priv; |
| 70 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 71 | /*! 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 Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 82 | void (*rx_cb)(void *priv, struct msgb *rx_data, unsigned int flags); |
| 83 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 84 | /*! 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 Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 92 | void (*tx_cb)(void *priv, struct msgb *tx_data); |
| 93 | |
Vadim Yanitskiy | 275c86e | 2023-12-04 01:44:09 +0700 | [diff] [blame^] | 94 | /*! 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 Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 100 | void (*status_change_cb)(void *priv, unsigned int status); |
| 101 | }; |
| 102 | |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 103 | extern const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg; |
| 104 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 105 | struct osmo_soft_uart; |
| 106 | |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 107 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name, |
| 108 | const struct osmo_soft_uart_cfg *cfg); |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 109 | void osmo_soft_uart_free(struct osmo_soft_uart *suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 110 | 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] | 111 | |
| 112 | const char *osmo_soft_uart_get_name(const struct osmo_soft_uart *suart); |
| 113 | void osmo_soft_uart_set_name(struct osmo_soft_uart *suart, const char *name); |
| 114 | |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 115 | int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable); |
| 116 | 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] | 117 | |
| 118 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits); |
| 119 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits); |
| 120 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 121 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status); |
Vadim Yanitskiy | 03f0ed7 | 2023-11-18 00:29:08 +0700 | [diff] [blame] | 122 | void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart); |