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 | |
| 28 | enum osmo_soft_uart_parity_mode { |
| 29 | OSMO_SUART_PARITY_NONE, |
| 30 | OSMO_SUART_PARITY_EVEN, |
| 31 | OSMO_SUART_PARITY_ODD, |
Vadim Yanitskiy | 0d78a00 | 2023-11-19 16:15:38 +0700 | [diff] [blame] | 32 | OSMO_SUART_PARITY_MARK, /* always 1 */ |
| 33 | OSMO_SUART_PARITY_SPACE, /* always 0 */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 34 | _OSMO_SUART_PARITY_NUM |
| 35 | }; |
| 36 | |
| 37 | enum 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 |
| 44 | enum osmo_soft_uart_status { |
| 45 | /* RTS, CTS, ... */ |
| 46 | _fixme, |
| 47 | }; |
| 48 | #endif |
| 49 | |
| 50 | /* configuration for a soft-uart */ |
| 51 | struct osmo_soft_uart_cfg { |
| 52 | /*! number of data bits (typically 5, 6, 7 or 8) */ |
| 53 | uint8_t num_data_bits; |
Vadim Yanitskiy | d4b94ce | 2023-11-24 03:00:02 +0700 | [diff] [blame] | 54 | /*! number of stop bits (typically 1 or 2) */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 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 |
Vadim Yanitskiy | d4b94ce | 2023-11-24 03:00:02 +0700 | [diff] [blame] | 62 | * after indicated number of milliseconds even if it is not full yet */ |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 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 Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 73 | /*! transmit call-back. The implementation is expected to provide at most |
Vadim Yanitskiy | d4b94ce | 2023-11-24 03:00:02 +0700 | [diff] [blame] | 74 | * tx_data->data_len characters (the actual amount is determined by the |
Vadim Yanitskiy | c9fc77f | 2023-11-16 00:55:35 +0700 | [diff] [blame] | 75 | * number of requested bits and the effective UART configuration). */ |
| 76 | void (*tx_cb)(void *priv, struct msgb *tx_data); |
| 77 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 78 | /*! 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 Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 82 | extern const struct osmo_soft_uart_cfg osmo_soft_uart_default_cfg; |
| 83 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 84 | struct osmo_soft_uart; |
| 85 | |
Vadim Yanitskiy | 82a1ae7 | 2023-11-16 15:34:50 +0700 | [diff] [blame] | 86 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name, |
| 87 | const struct osmo_soft_uart_cfg *cfg); |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame] | 88 | void osmo_soft_uart_free(struct osmo_soft_uart *suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 89 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg); |
Vadim Yanitskiy | cdde671 | 2023-11-16 15:17:37 +0700 | [diff] [blame] | 90 | int osmo_soft_uart_set_rx(struct osmo_soft_uart *suart, bool enable); |
| 91 | 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] | 92 | |
| 93 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits); |
| 94 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits); |
| 95 | |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 96 | 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] | 97 | void osmo_soft_uart_flush_rx(struct osmo_soft_uart *suart); |