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, |
| 32 | _OSMO_SUART_PARITY_NUM |
| 33 | }; |
| 34 | |
| 35 | enum 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 |
| 42 | enum osmo_soft_uart_status { |
| 43 | /* RTS, CTS, ... */ |
| 44 | _fixme, |
| 45 | }; |
| 46 | #endif |
| 47 | |
| 48 | /* configuration for a soft-uart */ |
| 49 | struct 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 | |
| 71 | /*! modem status line change call-back. gets bitmask of osmo_soft_uart_status */ |
| 72 | void (*status_change_cb)(void *priv, unsigned int status); |
| 73 | }; |
| 74 | |
| 75 | struct osmo_soft_uart; |
| 76 | |
| 77 | struct osmo_soft_uart *osmo_soft_uart_alloc(void *ctx, const char *name); |
Vadim Yanitskiy | 877cfed | 2023-11-12 17:21:31 +0700 | [diff] [blame^] | 78 | void osmo_soft_uart_free(struct osmo_soft_uart *suart); |
Harald Welte | dc023cf | 2022-11-29 23:15:18 +0100 | [diff] [blame] | 79 | int osmo_soft_uart_configure(struct osmo_soft_uart *suart, const struct osmo_soft_uart_cfg *cfg); |
| 80 | int osmo_soft_uart_enable(struct osmo_soft_uart *suart, bool rx, bool tx); |
| 81 | |
| 82 | int osmo_soft_uart_rx_ubits(struct osmo_soft_uart *suart, const ubit_t *ubits, size_t n_ubits); |
| 83 | int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits); |
| 84 | |
| 85 | void osmo_soft_uart_tx(struct osmo_soft_uart *suart, struct msgb *tx_data); |
| 86 | int osmo_soft_uart_set_status(struct osmo_soft_uart *suart, unsigned int status); |