uart.h 10.0 KB

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