|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 OR MIT |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <stdint.h> |
| 10 | +#include <stdbool.h> |
| 11 | +#include <soc/soc_caps.h> |
| 12 | + |
| 13 | +#ifdef __cplusplus |
| 14 | +extern "C" { |
| 15 | +#endif // __cplusplus |
| 16 | + |
| 17 | +/** |
| 18 | + * @brief UART port number type |
| 19 | + * |
| 20 | + * Available UART ports depend on the chip's SOC_UART_HP_NUM capability. |
| 21 | + * Use the UART_NUM_* enum values (e.g., UART_NUM_0, UART_NUM_1, etc.) |
| 22 | + */ |
| 23 | + |
| 24 | +typedef enum { |
| 25 | + UART_NUM_0, /*!< UART port 0 */ |
| 26 | + UART_NUM_1, /*!< UART port 1 */ |
| 27 | +#if SOC_UART_HP_NUM > 2 |
| 28 | + UART_NUM_2, /*!< UART port 2 */ |
| 29 | +#endif |
| 30 | +#if SOC_UART_HP_NUM > 3 |
| 31 | + UART_NUM_3, /*!< UART port 3 */ |
| 32 | +#endif |
| 33 | +#if SOC_UART_HP_NUM > 4 |
| 34 | + UART_NUM_4, /*!< UART port 4 */ |
| 35 | +#endif |
| 36 | + UART_NUM_MAX, /*!< UART port max */ |
| 37 | +} uart_port_t; |
| 38 | + |
| 39 | +#define UART_INTR_RXFIFO_FULL (0x1 << 0) |
| 40 | +#define UART_INTR_RXFIFO_TOUT (0x1 << 8) |
| 41 | + |
| 42 | +/** |
| 43 | + * @brief Wait until UART is idle |
| 44 | + * |
| 45 | + * @param uart_num UART port number |
| 46 | + */ |
| 47 | +void stub_lib_uart_wait_idle(uart_port_t uart_num); |
| 48 | + |
| 49 | +/** |
| 50 | + * @brief Initialize UART |
| 51 | + * |
| 52 | + * @param uart_num UART port number |
| 53 | + */ |
| 54 | +void stub_lib_uart_init(uart_port_t uart_num); |
| 55 | + |
| 56 | +/** |
| 57 | + * @brief Set UART baud rate |
| 58 | + * |
| 59 | + * @param uart_num UART port number |
| 60 | + * @param baudrate Baud rate |
| 61 | + */ |
| 62 | +void stub_lib_uart_rominit_set_baudrate(uart_port_t uart_num, uint32_t baudrate); |
| 63 | + |
| 64 | +/** |
| 65 | + * @brief Attach interrupt handler to UART and configure interrupts |
| 66 | + * |
| 67 | + * @note This function requires ROM preinit (download mode initialization) to work correctly. |
| 68 | + * It uses ROM functions (ets_isr_attach, ets_isr_unmask) that are only available |
| 69 | + * after the ROM has entered download mode and completed its initialization. |
| 70 | + * Functions that contain "rominit" have this requirement. |
| 71 | + * |
| 72 | + * @param uart_num UART port number |
| 73 | + * @param intr_num CPU interrupt source |
| 74 | + * @param handler Interrupt handler function pointer |
| 75 | + * @param flags Interrupt enable flags (bitwise OR of UART_INTR_* values) |
| 76 | + */ |
| 77 | +void stub_lib_uart_rominit_intr_attach(uart_port_t uart_num, int intr_num, void *handler, uint32_t flags); |
| 78 | + |
| 79 | +/** |
| 80 | + * @brief Get and clear UART interrupt status |
| 81 | + * |
| 82 | + * This function returns the interrupt status and automatically clears it. |
| 83 | + * Call this at the beginning of your interrupt handler. |
| 84 | + * |
| 85 | + * @param uart_num UART port number |
| 86 | + * @return Bitmask of active interrupts that were cleared |
| 87 | + */ |
| 88 | +uint32_t stub_lib_uart_get_intr_flags(uart_port_t uart_num); |
| 89 | + |
| 90 | +/** |
| 91 | + * @brief Get number of bytes available in RX FIFO |
| 92 | + * |
| 93 | + * @param uart_num UART port number |
| 94 | + * @return Number of bytes currently in the RX FIFO |
| 95 | + */ |
| 96 | +uint32_t stub_lib_uart_get_rxfifo_count(uart_port_t uart_num); |
| 97 | + |
| 98 | +/** |
| 99 | + * @brief Read a single byte from UART RX FIFO |
| 100 | + * |
| 101 | + * This function reads one byte from the RX FIFO without checking |
| 102 | + * if data is available. Call stub_lib_uart_get_rxfifo_count() |
| 103 | + * first to ensure data is available. |
| 104 | + * |
| 105 | + * @param uart_num UART port number |
| 106 | + * @return The byte read from the FIFO |
| 107 | + */ |
| 108 | +uint8_t stub_lib_uart_read_rxfifo_byte(uart_port_t uart_num); |
| 109 | + |
| 110 | +/** |
| 111 | + * @brief Configure RX timeout threshold |
| 112 | + * |
| 113 | + * @param uart_num UART port number |
| 114 | + * @param timeout Timeout value in bit times (1-126, 0 to disable) |
| 115 | + * timeout mean the multiple of UART packets (~11 bytes) that can be received before timeout |
| 116 | + */ |
| 117 | +void stub_lib_uart_set_rx_timeout(uart_port_t uart_num, uint8_t timeout); |
| 118 | + |
| 119 | +/** |
| 120 | + * @brief Transmit a single byte over UART. |
| 121 | + * |
| 122 | + * @note This function operates on the console UART (set via stub_lib_uart_set_as_console). |
| 123 | + * It does not take a uart_num parameter. |
| 124 | + * |
| 125 | + * @param c Byte to transmit. |
| 126 | + * @return 0 if successful, non-zero if error occurred. |
| 127 | + */ |
| 128 | +uint8_t stub_lib_uart_tx_one_char(uint8_t c); |
| 129 | + |
| 130 | +/** |
| 131 | + * @brief Receive a single byte (blocking) from UART. |
| 132 | + * |
| 133 | + * @note This function operates on the console UART (set via stub_lib_uart_set_as_console). |
| 134 | + * It does not take a uart_num parameter. |
| 135 | + * |
| 136 | + * @return Received byte. |
| 137 | + */ |
| 138 | +uint8_t stub_lib_uart_rx_one_char(void); |
| 139 | + |
| 140 | +/** |
| 141 | + * @brief Flush any buffered transmit data. |
| 142 | + */ |
| 143 | +void stub_lib_uart_tx_flush(void); |
| 144 | + |
| 145 | +#ifdef __cplusplus |
| 146 | +} |
| 147 | +#endif // __cplusplus |
0 commit comments