uart.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ROM_UART_H_
  7. #define _ROM_UART_H_
  8. #include "esp_types.h"
  9. #include "esp_attr.h"
  10. #include "ets_sys.h"
  11. #include "soc/soc.h"
  12. #include "soc/uart_reg.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /** \defgroup uart_apis, uart configuration and communication related apis
  17. * @brief uart apis
  18. */
  19. /** @addtogroup uart_apis
  20. * @{
  21. */
  22. #define RX_BUFF_SIZE 0x400
  23. #define TX_BUFF_SIZE 100
  24. //uart int enalbe register ctrl bits
  25. #define UART_RCV_INTEN BIT0
  26. #define UART_TRX_INTEN BIT1
  27. #define UART_LINE_STATUS_INTEN BIT2
  28. //uart int identification ctrl bits
  29. #define UART_INT_FLAG_MASK 0x0E
  30. //uart fifo ctrl bits
  31. #define UART_CLR_RCV_FIFO BIT1
  32. #define UART_CLR_TRX_FIFO BIT2
  33. #define UART_RCVFIFO_TRG_LVL_BITS BIT6
  34. //uart line control bits
  35. #define UART_DIV_LATCH_ACCESS_BIT BIT7
  36. //uart line status bits
  37. #define UART_RCV_DATA_RDY_FLAG BIT0
  38. #define UART_RCV_OVER_FLOW_FLAG BIT1
  39. #define UART_RCV_PARITY_ERR_FLAG BIT2
  40. #define UART_RCV_FRAME_ERR_FLAG BIT3
  41. #define UART_BRK_INT_FLAG BIT4
  42. #define UART_TRX_FIFO_EMPTY_FLAG BIT5
  43. #define UART_TRX_ALL_EMPTY_FLAG BIT6 // include fifo and shift reg
  44. #define UART_RCV_ERR_FLAG BIT7
  45. //send and receive message frame head
  46. #define FRAME_FLAG 0x7E
  47. typedef enum {
  48. UART_LINE_STATUS_INT_FLAG = 0x06,
  49. UART_RCV_FIFO_INT_FLAG = 0x04,
  50. UART_RCV_TMOUT_INT_FLAG = 0x0C,
  51. UART_TXBUFF_EMPTY_INT_FLAG = 0x02
  52. } UartIntType; //consider bit0 for int_flag
  53. typedef enum {
  54. RCV_ONE_BYTE = 0x0,
  55. RCV_FOUR_BYTE = 0x1,
  56. RCV_EIGHT_BYTE = 0x2,
  57. RCV_FOURTEEN_BYTE = 0x3
  58. } UartRcvFifoTrgLvl;
  59. typedef enum {
  60. FIVE_BITS = 0x0,
  61. SIX_BITS = 0x1,
  62. SEVEN_BITS = 0x2,
  63. EIGHT_BITS = 0x3
  64. } UartBitsNum4Char;
  65. typedef enum {
  66. ONE_STOP_BIT = 1,
  67. ONE_HALF_STOP_BIT = 2,
  68. TWO_STOP_BIT = 3
  69. } UartStopBitsNum;
  70. typedef enum {
  71. NONE_BITS = 0,
  72. ODD_BITS = 2,
  73. EVEN_BITS = 3
  74. } UartParityMode;
  75. typedef enum {
  76. STICK_PARITY_DIS = 0,
  77. STICK_PARITY_EN = 2
  78. } UartExistParity;
  79. typedef enum {
  80. BIT_RATE_9600 = 9600,
  81. BIT_RATE_19200 = 19200,
  82. BIT_RATE_38400 = 38400,
  83. BIT_RATE_57600 = 57600,
  84. BIT_RATE_115200 = 115200,
  85. BIT_RATE_230400 = 230400,
  86. BIT_RATE_460800 = 460800,
  87. BIT_RATE_921600 = 921600
  88. } UartBautRate;
  89. typedef enum {
  90. NONE_CTRL,
  91. HARDWARE_CTRL,
  92. XON_XOFF_CTRL
  93. } UartFlowCtrl;
  94. typedef enum {
  95. EMPTY,
  96. UNDER_WRITE,
  97. WRITE_OVER
  98. } RcvMsgBuffState;
  99. typedef struct {
  100. uint8_t *pRcvMsgBuff;
  101. uint8_t *pWritePos;
  102. uint8_t *pReadPos;
  103. uint8_t TrigLvl;
  104. RcvMsgBuffState BuffState;
  105. } RcvMsgBuff;
  106. typedef struct {
  107. uint32_t TrxBuffSize;
  108. uint8_t *pTrxBuff;
  109. } TrxMsgBuff;
  110. typedef enum {
  111. BAUD_RATE_DET,
  112. WAIT_SYNC_FRM,
  113. SRCH_MSG_HEAD,
  114. RCV_MSG_BODY,
  115. RCV_ESC_CHAR,
  116. } RcvMsgState;
  117. typedef struct {
  118. UartBautRate baut_rate;
  119. UartBitsNum4Char data_bits;
  120. UartExistParity exist_parity;
  121. UartParityMode parity; // chip size in byte
  122. UartStopBitsNum stop_bits;
  123. UartFlowCtrl flow_ctrl;
  124. uint8_t buff_uart_no; //indicate which uart use tx/rx buffer
  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 rxBuffer, must be a pointer to RX_BUFF_SIZE bytes or NULL
  135. *
  136. * @return None
  137. */
  138. void uartAttach(void *rxBuffer);
  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
  220. *
  221. * The function defined in ROM code has a bug, so we define the correct version
  222. * here for compatibility.
  223. */
  224. void uart_tx_wait_idle(uint8_t uart_no);
  225. /**
  226. * @brief Get an input char from message channel.
  227. * Please do not call this function in SDK.
  228. *
  229. * @param uint8_t *pRxChar : the pointer to store the char.
  230. *
  231. * @return OK for successful.
  232. * FAIL for failed.
  233. */
  234. STATUS uart_rx_one_char(uint8_t *pRxChar);
  235. /**
  236. * @brief Get an input char from message channel, wait until successful.
  237. * Please do not call this function in SDK.
  238. *
  239. * @param None
  240. *
  241. * @return char : input char value.
  242. */
  243. char uart_rx_one_char_block(void);
  244. /**
  245. * @brief Get an input string line from message channel.
  246. * Please do not call this function in SDK.
  247. *
  248. * @param uint8_t *pString : the pointer to store the string.
  249. *
  250. * @param uint8_t MaxStrlen : the max string length, incude '\0'.
  251. *
  252. * @return OK.
  253. */
  254. STATUS UartRxString(uint8_t *pString, uint8_t MaxStrlen);
  255. /**
  256. * @brief Process uart recevied information in the interrupt handler.
  257. * Please do not call this function in SDK.
  258. *
  259. * @param void *para : the message receive buffer.
  260. *
  261. * @return None
  262. */
  263. void uart_rx_intr_handler(void *para);
  264. /**
  265. * @brief Get an char from receive buffer.
  266. * Please do not call this function in SDK.
  267. *
  268. * @param RcvMsgBuff *pRxBuff : the pointer to the struct that include receive buffer.
  269. *
  270. * @param uint8_t *pRxByte : the pointer to store the char.
  271. *
  272. * @return OK for successful.
  273. * FAIL for failed.
  274. */
  275. STATUS uart_rx_readbuff( RcvMsgBuff *pRxBuff, uint8_t *pRxByte);
  276. /**
  277. * @brief Get all chars from receive buffer.
  278. * Please do not call this function in SDK.
  279. *
  280. * @param uint8_t *pCmdLn : the pointer to store the string.
  281. *
  282. * @return OK for successful.
  283. * FAIL for failed.
  284. */
  285. STATUS UartGetCmdLn(uint8_t *pCmdLn);
  286. /**
  287. * @brief Get uart configuration struct.
  288. * Please do not call this function in SDK.
  289. *
  290. * @param None
  291. *
  292. * @return UartDevice * : uart configuration struct pointer.
  293. */
  294. UartDevice *GetUartDevice(void);
  295. /**
  296. * @brief Send an packet to download tool, with SLIP escaping.
  297. * Please do not call this function in SDK.
  298. *
  299. * @param uint8_t *p : the pointer to output string.
  300. *
  301. * @param int len : the string length.
  302. *
  303. * @return None.
  304. */
  305. void send_packet(uint8_t *p, int len);
  306. /**
  307. * @brief Receive an packet from download tool, with SLIP escaping.
  308. * Please do not call this function in SDK.
  309. *
  310. * @param uint8_t *p : the pointer to input string.
  311. *
  312. * @param int len : If string length > len, the string will be truncated.
  313. *
  314. * @param uint8_t is_sync : 0, only one UART module;
  315. * 1, two UART modules.
  316. *
  317. * @return int : the length of the string.
  318. */
  319. int recv_packet(uint8_t *p, int len, uint8_t is_sync);
  320. /**
  321. * @brief Send an packet to download tool, with SLIP escaping.
  322. * Please do not call this function in SDK.
  323. *
  324. * @param uint8_t *pData : the pointer to input string.
  325. *
  326. * @param uint16_t DataLen : the string length.
  327. *
  328. * @return OK for successful.
  329. * FAIL for failed.
  330. */
  331. STATUS SendMsg(uint8_t *pData, uint16_t DataLen);
  332. /**
  333. * @brief Receive an packet from download tool, with SLIP escaping.
  334. * Please do not call this function in SDK.
  335. *
  336. * @param uint8_t *pData : the pointer to input string.
  337. *
  338. * @param uint16_t MaxDataLen : If string length > MaxDataLen, the string will be truncated.
  339. *
  340. * @param uint8_t is_sync : 0, only one UART module;
  341. * 1, two UART modules.
  342. *
  343. * @return OK for successful.
  344. * FAIL for failed.
  345. */
  346. STATUS RcvMsg(uint8_t *pData, uint16_t MaxDataLen, uint8_t is_sync);
  347. /**
  348. * @brief Check if this UART is in download connection.
  349. * Please do not call this function in SDK.
  350. *
  351. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  352. *
  353. * @return ETS_NO_BOOT = 0 for no.
  354. * SEL_UART_BOOT = BIT(1) for yes.
  355. */
  356. uint8_t UartConnCheck(uint8_t uart_no);
  357. /**
  358. * @brief Initialize the USB ACM UART
  359. * Needs to be fed a buffer of at least 128 bytes, plus any rx buffer you may want to have.
  360. *
  361. * @param cdc_acm_work_mem Pointer to work mem for CDC-ACM code
  362. * @param cdc_acm_work_mem_len Length of work mem
  363. */
  364. void Uart_Init_USB(void *cdc_acm_work_mem, int cdc_acm_work_mem_len);
  365. /**
  366. * @brief Install handler to reset the chip when a RTS change has been detected on the CDC-ACM 'UART'.
  367. */
  368. void uart_usb_enable_reset_on_rts(void);
  369. extern UartDevice UartDev;
  370. /**
  371. * @}
  372. */
  373. #ifdef __cplusplus
  374. }
  375. #endif
  376. #endif /* _ROM_UART_H_ */