uart.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include "ets_sys.h"
  9. #include "soc/soc.h"
  10. #include "soc/uart_reg.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /** \defgroup uart_apis, uart configuration and communication related apis
  15. * @brief uart apis
  16. */
  17. /** @addtogroup uart_apis
  18. * @{
  19. */
  20. #define RX_BUFF_SIZE 0x400
  21. #define TX_BUFF_SIZE 100
  22. //uart int enalbe register ctrl bits
  23. #define UART_RCV_INTEN BIT0
  24. #define UART_TRX_INTEN BIT1
  25. #define UART_LINE_STATUS_INTEN BIT2
  26. //uart int identification ctrl bits
  27. #define UART_INT_FLAG_MASK 0x0E
  28. //uart fifo ctrl bits
  29. #define UART_CLR_RCV_FIFO BIT1
  30. #define UART_CLR_TRX_FIFO BIT2
  31. #define UART_RCVFIFO_TRG_LVL_BITS BIT6
  32. //uart line control bits
  33. #define UART_DIV_LATCH_ACCESS_BIT BIT7
  34. //uart line status bits
  35. #define UART_RCV_DATA_RDY_FLAG BIT0
  36. #define UART_RCV_OVER_FLOW_FLAG BIT1
  37. #define UART_RCV_PARITY_ERR_FLAG BIT2
  38. #define UART_RCV_FRAME_ERR_FLAG BIT3
  39. #define UART_BRK_INT_FLAG BIT4
  40. #define UART_TRX_FIFO_EMPTY_FLAG BIT5
  41. #define UART_TRX_ALL_EMPTY_FLAG BIT6 // include fifo and shift reg
  42. #define UART_RCV_ERR_FLAG BIT7
  43. //send and receive message frame head
  44. #define FRAME_FLAG 0x7E
  45. typedef enum {
  46. UART_LINE_STATUS_INT_FLAG = 0x06,
  47. UART_RCV_FIFO_INT_FLAG = 0x04,
  48. UART_RCV_TMOUT_INT_FLAG = 0x0C,
  49. UART_TXBUFF_EMPTY_INT_FLAG = 0x02
  50. } UartIntType; //consider bit0 for int_flag
  51. typedef enum {
  52. RCV_ONE_BYTE = 0x0,
  53. RCV_FOUR_BYTE = 0x1,
  54. RCV_EIGHT_BYTE = 0x2,
  55. RCV_FOURTEEN_BYTE = 0x3
  56. } UartRcvFifoTrgLvl;
  57. typedef enum {
  58. FIVE_BITS = 0x0,
  59. SIX_BITS = 0x1,
  60. SEVEN_BITS = 0x2,
  61. EIGHT_BITS = 0x3
  62. } UartBitsNum4Char;
  63. typedef enum {
  64. ONE_STOP_BIT = 1,
  65. ONE_HALF_STOP_BIT = 2,
  66. TWO_STOP_BIT = 3
  67. } UartStopBitsNum;
  68. typedef enum {
  69. NONE_BITS = 0,
  70. ODD_BITS = 2,
  71. EVEN_BITS = 3
  72. } UartParityMode;
  73. typedef enum {
  74. STICK_PARITY_DIS = 0,
  75. STICK_PARITY_EN = 2
  76. } UartExistParity;
  77. typedef enum {
  78. BIT_RATE_9600 = 9600,
  79. BIT_RATE_19200 = 19200,
  80. BIT_RATE_38400 = 38400,
  81. BIT_RATE_57600 = 57600,
  82. BIT_RATE_115200 = 115200,
  83. BIT_RATE_230400 = 230400,
  84. BIT_RATE_460800 = 460800,
  85. BIT_RATE_921600 = 921600
  86. } UartBautRate;
  87. typedef enum {
  88. NONE_CTRL,
  89. HARDWARE_CTRL,
  90. XON_XOFF_CTRL
  91. } UartFlowCtrl;
  92. typedef enum {
  93. EMPTY,
  94. UNDER_WRITE,
  95. WRITE_OVER
  96. } RcvMsgBuffState;
  97. typedef struct {
  98. uint8_t *pRcvMsgBuff;
  99. uint8_t *pWritePos;
  100. uint8_t *pReadPos;
  101. uint8_t TrigLvl;
  102. RcvMsgBuffState BuffState;
  103. } RcvMsgBuff;
  104. typedef struct {
  105. uint32_t TrxBuffSize;
  106. uint8_t *pTrxBuff;
  107. } TrxMsgBuff;
  108. typedef enum {
  109. BAUD_RATE_DET,
  110. WAIT_SYNC_FRM,
  111. SRCH_MSG_HEAD,
  112. RCV_MSG_BODY,
  113. RCV_ESC_CHAR,
  114. } RcvMsgState;
  115. typedef struct {
  116. UartBautRate baut_rate;
  117. UartBitsNum4Char data_bits;
  118. UartExistParity exist_parity;
  119. UartParityMode parity; // chip size in byte
  120. UartStopBitsNum stop_bits;
  121. UartFlowCtrl flow_ctrl;
  122. uint8_t buff_uart_no; //indicate which uart use tx/rx buffer
  123. RcvMsgBuff rcv_buff;
  124. // TrxMsgBuff trx_buff;
  125. RcvMsgState rcv_state;
  126. int received;
  127. } UartDevice;
  128. /**
  129. * @brief Init uart device struct value and reset uart0/uart1 rx.
  130. * Please do not call this function in SDK.
  131. *
  132. * @param rxBuffer, must be a pointer to RX_BUFF_SIZE bytes or NULL
  133. *
  134. * @return None
  135. */
  136. void uartAttach(void *rxBuffer);
  137. /**
  138. * @brief Init uart0 or uart1 for UART download booting mode.
  139. * Please do not call this function in SDK.
  140. *
  141. * @param uint8_t uart_no : 0 for UART0, else for UART1.
  142. *
  143. * @param uint32_t clock : clock used by uart module, to adjust baudrate.
  144. *
  145. * @return None
  146. */
  147. void Uart_Init(uint8_t uart_no, uint32_t clock);
  148. /**
  149. * @brief Modify uart baudrate.
  150. * This function will reset RX/TX fifo for uart.
  151. *
  152. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  153. *
  154. * @param uint32_t DivLatchValue : (clock << 4)/baudrate.
  155. *
  156. * @return None
  157. */
  158. void uart_div_modify(uint8_t uart_no, uint32_t DivLatchValue);
  159. /**
  160. * @brief Switch printf channel of uart_tx_one_char.
  161. * Please do not call this function when printf.
  162. *
  163. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  164. *
  165. * @return None
  166. */
  167. void uart_tx_switch(uint8_t uart_no);
  168. /**
  169. * @brief Output a char to printf channel, wait until fifo not full.
  170. *
  171. * @param None
  172. *
  173. * @return OK.
  174. */
  175. ETS_STATUS uart_tx_one_char(uint8_t TxChar);
  176. /**
  177. * @brief Output a char to message exchange channel, wait until fifo not full.
  178. * Please do not call this function in SDK.
  179. *
  180. * @param None
  181. *
  182. * @return OK.
  183. */
  184. ETS_STATUS uart_tx_one_char2(uint8_t TxChar);
  185. /**
  186. * @brief Wait until uart tx full empty.
  187. *
  188. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  189. *
  190. * @return None.
  191. */
  192. void uart_tx_flush(uint8_t uart_no);
  193. /**
  194. * @brief Wait until uart tx full empty and the last char send ok.
  195. *
  196. * @param uart_no : 0 for UART0, 1 for UART1, 2 for UART2
  197. *
  198. * The function defined in ROM code has a bug, so we define the correct version
  199. * here for compatibility.
  200. */
  201. void uart_tx_wait_idle(uint8_t uart_no);
  202. /**
  203. * @brief Get an input char from message channel.
  204. * Please do not call this function in SDK.
  205. *
  206. * @param uint8_t *pRxChar : the pointer to store the char.
  207. *
  208. * @return OK for successful.
  209. * FAIL for failed.
  210. */
  211. ETS_STATUS uart_rx_one_char(uint8_t *pRxChar);
  212. /**
  213. * @brief Get an input char from message channel, wait until successful.
  214. * Please do not call this function in SDK.
  215. *
  216. * @param None
  217. *
  218. * @return char : input char value.
  219. */
  220. char uart_rx_one_char_block(void);
  221. /**
  222. * @brief Get an input string line from message channel.
  223. * Please do not call this function in SDK.
  224. *
  225. * @param uint8_t *pString : the pointer to store the string.
  226. *
  227. * @param uint8_t MaxStrlen : the max string length, incude '\0'.
  228. *
  229. * @return OK.
  230. */
  231. ETS_STATUS UartRxString(uint8_t *pString, uint8_t MaxStrlen);
  232. /**
  233. * @brief Get an char from receive buffer.
  234. * Please do not call this function in SDK.
  235. *
  236. * @param RcvMsgBuff *pRxBuff : the pointer to the struct that include receive buffer.
  237. *
  238. * @param uint8_t *pRxByte : the pointer to store the char.
  239. *
  240. * @return OK for successful.
  241. * FAIL for failed.
  242. */
  243. ETS_STATUS uart_rx_readbuff( RcvMsgBuff *pRxBuff, uint8_t *pRxByte);
  244. /**
  245. * @brief Get uart configuration struct.
  246. * Please do not call this function in SDK.
  247. *
  248. * @param None
  249. *
  250. * @return UartDevice * : uart configuration struct pointer.
  251. */
  252. UartDevice *GetUartDevice(void);
  253. /**
  254. * @brief Send an packet to download tool, with SLIP escaping.
  255. * Please do not call this function in SDK.
  256. *
  257. * @param uint8_t *p : the pointer to output string.
  258. *
  259. * @param int len : the string length.
  260. *
  261. * @return None.
  262. */
  263. void send_packet(uint8_t *p, int len);
  264. /**
  265. * @brief Receive an packet from download tool, with SLIP escaping.
  266. * Please do not call this function in SDK.
  267. *
  268. * @param uint8_t *p : the pointer to input string.
  269. *
  270. * @param int len : If string length > len, the string will be truncated.
  271. *
  272. * @param uint8_t is_sync : 0, only one UART module;
  273. * 1, two UART modules.
  274. *
  275. * @return int : the length of the string.
  276. */
  277. int recv_packet(uint8_t *p, int len, uint8_t is_sync);
  278. /**
  279. * @brief Initialize the USB ACM UART
  280. * Needs to be fed a buffer of at least 128 bytes, plus any rx buffer you may want to have.
  281. *
  282. * @param cdc_acm_work_mem Pointer to work mem for CDC-ACM code
  283. * @param cdc_acm_work_mem_len Length of work mem
  284. */
  285. void Uart_Init_USB(void *cdc_acm_work_mem, int cdc_acm_work_mem_len);
  286. extern UartDevice UartDev;
  287. /**
  288. * @}
  289. */
  290. #ifdef __cplusplus
  291. }
  292. #endif