1 /*
2  * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include "soc/soc_caps.h"
16 #include "soc/clk_tree_defs.h"
17 #include "esp_assert.h"
18 
19 /**
20  * @brief UART port number, can be UART_NUM_0 ~ (UART_NUM_MAX -1).
21  */
22 typedef enum {
23     UART_NUM_0,                         /*!< UART port 0 */
24     UART_NUM_1,                         /*!< UART port 1 */
25 #if SOC_UART_HP_NUM > 2
26     UART_NUM_2,                         /*!< UART port 2 */
27 #endif
28 #if SOC_UART_HP_NUM > 3
29     UART_NUM_3,                         /*!< UART port 3 */
30 #endif
31 #if SOC_UART_HP_NUM > 4
32     UART_NUM_4,                         /*!< UART port 4 */
33 #endif
34 #if (SOC_UART_LP_NUM >= 1)
35     LP_UART_NUM_0,                      /*!< LP UART port 0 */
36 #endif
37     UART_NUM_MAX,                       /*!< UART port max */
38 } uart_port_t;
39 
40 ESP_STATIC_ASSERT(UART_NUM_MAX == SOC_UART_NUM, "UART_NUM_MAX does not match SOC_UART_NUM");
41 
42 /**
43  * @brief UART mode selection
44  */
45 typedef enum {
46     UART_MODE_UART = 0x00,                      /*!< mode: regular UART mode*/
47     UART_MODE_RS485_HALF_DUPLEX = 0x01,         /*!< mode: half duplex RS485 UART mode control by RTS pin */
48     UART_MODE_IRDA = 0x02,                      /*!< mode: IRDA  UART mode*/
49     UART_MODE_RS485_COLLISION_DETECT = 0x03,    /*!< mode: RS485 collision detection UART mode (used for test purposes)*/
50     UART_MODE_RS485_APP_CTRL = 0x04,            /*!< mode: application control RS485 UART mode (used for test purposes)*/
51 } uart_mode_t;
52 
53 /**
54  * @brief UART word length constants
55  */
56 typedef enum {
57     UART_DATA_5_BITS   = 0x0,    /*!< word length: 5bits*/
58     UART_DATA_6_BITS   = 0x1,    /*!< word length: 6bits*/
59     UART_DATA_7_BITS   = 0x2,    /*!< word length: 7bits*/
60     UART_DATA_8_BITS   = 0x3,    /*!< word length: 8bits*/
61     UART_DATA_BITS_MAX = 0x4,
62 } uart_word_length_t;
63 
64 /**
65  * @brief UART stop bits number
66  */
67 typedef enum {
68     UART_STOP_BITS_1   = 0x1,  /*!< stop bit: 1bit*/
69     UART_STOP_BITS_1_5 = 0x2,  /*!< stop bit: 1.5bits*/
70     UART_STOP_BITS_2   = 0x3,  /*!< stop bit: 2bits*/
71     UART_STOP_BITS_MAX = 0x4,
72 } uart_stop_bits_t;
73 
74 /**
75  * @brief UART parity constants
76  */
77 typedef enum {
78     UART_PARITY_DISABLE  = 0x0,  /*!< Disable UART parity*/
79     UART_PARITY_EVEN     = 0x2,  /*!< Enable UART even parity*/
80     UART_PARITY_ODD      = 0x3   /*!< Enable UART odd parity*/
81 } uart_parity_t;
82 
83 /**
84  * @brief UART hardware flow control modes
85  */
86 typedef enum {
87     UART_HW_FLOWCTRL_DISABLE = 0x0,   /*!< disable hardware flow control*/
88     UART_HW_FLOWCTRL_RTS     = 0x1,   /*!< enable RX hardware flow control (rts)*/
89     UART_HW_FLOWCTRL_CTS     = 0x2,   /*!< enable TX hardware flow control (cts)*/
90     UART_HW_FLOWCTRL_CTS_RTS = 0x3,   /*!< enable hardware flow control*/
91     UART_HW_FLOWCTRL_MAX     = 0x4,
92 } uart_hw_flowcontrol_t;
93 
94 /**
95  * @brief UART signal bit map
96  */
97 typedef enum {
98     UART_SIGNAL_INV_DISABLE  =  0,            /*!< Disable UART signal inverse*/
99     UART_SIGNAL_IRDA_TX_INV  = (0x1 << 0),    /*!< inverse the UART irda_tx signal*/
100     UART_SIGNAL_IRDA_RX_INV  = (0x1 << 1),    /*!< inverse the UART irda_rx signal*/
101     UART_SIGNAL_RXD_INV      = (0x1 << 2),    /*!< inverse the UART rxd signal*/
102     UART_SIGNAL_CTS_INV      = (0x1 << 3),    /*!< inverse the UART cts signal*/
103     UART_SIGNAL_DSR_INV      = (0x1 << 4),    /*!< inverse the UART dsr signal*/
104     UART_SIGNAL_TXD_INV      = (0x1 << 5),    /*!< inverse the UART txd signal*/
105     UART_SIGNAL_RTS_INV      = (0x1 << 6),    /*!< inverse the UART rts signal*/
106     UART_SIGNAL_DTR_INV      = (0x1 << 7),    /*!< inverse the UART dtr signal*/
107 } uart_signal_inv_t;
108 
109 /**
110  * @brief UART source clock
111  */
112 typedef soc_periph_uart_clk_src_legacy_t uart_sclk_t;
113 
114 #if (SOC_UART_LP_NUM >= 1)
115 /**
116  * @brief LP_UART source clock
117  */
118 typedef soc_periph_lp_uart_clk_src_t lp_uart_sclk_t;
119 #endif
120 
121 /**
122  * @brief UART AT cmd char configuration parameters
123  *        Note that this function may different on different chip. Please refer to the TRM at confirguration.
124  */
125 typedef struct {
126     uint8_t  cmd_char;             /*!< UART AT cmd char*/
127     uint8_t  char_num;             /*!< AT cmd char repeat number*/
128     uint32_t gap_tout;             /*!< gap time(in baud-rate) between AT cmd char*/
129     uint32_t pre_idle;             /*!< the idle time(in baud-rate) between the non AT char and first AT char*/
130     uint32_t post_idle;            /*!< the idle time(in baud-rate) between the last AT char and the none AT char*/
131 } uart_at_cmd_t;
132 
133 /**
134  * @brief UART software flow control configuration parameters
135  */
136 typedef struct {
137     uint8_t  xon_char;      /*!< Xon flow control char*/
138     uint8_t  xoff_char;     /*!< Xoff flow control char*/
139     uint8_t xon_thrd;      /*!< If the software flow control is enabled and the data amount in rxfifo is less than xon_thrd, an xon_char will be sent*/
140     uint8_t xoff_thrd;       /*!< If the software flow control is enabled and the data amount in rxfifo is more than xoff_thrd, an xoff_char will be sent*/
141 } uart_sw_flowctrl_t;
142 
143 #ifdef __cplusplus
144 }
145 #endif
146