uart.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * SPDX-FileCopyrightText: 2010-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 Re-calculate UART baudrate divisor for a given (changed)
  163. * clock speed.
  164. * This function will not reset RX/TX fifo for uart.
  165. *
  166. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  167. *
  168. * @param uint32_t clock : clock used by uart module, to adjust baudrate.
  169. *
  170. * @return None
  171. */
  172. void uart_div_reinit(uint8_t uart_no, uint32_t clock);
  173. /**
  174. * @brief Init uart0 or uart1 for UART download booting mode.
  175. * Please do not call this function in SDK.
  176. *
  177. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  178. *
  179. * @param uint8_t is_sync : 0, only one UART module, easy to detect, wait until detected;
  180. * 1, two UART modules, hard to detect, detect and return.
  181. *
  182. * @return None
  183. */
  184. int uart_baudrate_detect(uint8_t uart_no, uint8_t is_sync);
  185. /**
  186. * @brief Switch printf channel of uart_tx_one_char.
  187. * Please do not call this function when printf.
  188. *
  189. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  190. *
  191. * @return None
  192. */
  193. void uart_tx_switch(uint8_t uart_no);
  194. /**
  195. * @brief Switch message exchange channel for UART download booting.
  196. * Please do not call this function in SDK.
  197. *
  198. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  199. *
  200. * @return None
  201. */
  202. void uart_buff_switch(uint8_t uart_no);
  203. /**
  204. * @brief Output a char to printf channel, wait until fifo not full.
  205. *
  206. * @param None
  207. *
  208. * @return OK.
  209. */
  210. ETS_STATUS uart_tx_one_char(uint8_t TxChar);
  211. /**
  212. * @brief Output a char to message exchange channel, wait until fifo not full.
  213. * Please do not call this function in SDK.
  214. *
  215. * @param None
  216. *
  217. * @return OK.
  218. */
  219. ETS_STATUS uart_tx_one_char2(uint8_t TxChar);
  220. /**
  221. * @brief Wait until uart tx full empty.
  222. *
  223. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  224. *
  225. * @return None.
  226. */
  227. void uart_tx_flush(uint8_t uart_no);
  228. /**
  229. * @brief Wait until uart tx full empty and the last char send ok.
  230. *
  231. * @param uart_no : 0 for UART0, 1 for UART1, 2 for UART2
  232. *
  233. * The function defined in ROM code has a bug, so we define the correct version
  234. * here for compatibility.
  235. */
  236. void uart_tx_wait_idle(uint8_t uart_no);
  237. /**
  238. * @brief Get an input char from message channel.
  239. * Please do not call this function in SDK.
  240. *
  241. * @param uint8_t *pRxChar : the pointer to store the char.
  242. *
  243. * @return OK for successful.
  244. * FAIL for failed.
  245. */
  246. ETS_STATUS uart_rx_one_char(uint8_t *pRxChar);
  247. /**
  248. * @brief Get an input char from message channel, wait until successful.
  249. * Please do not call this function in SDK.
  250. *
  251. * @param None
  252. *
  253. * @return char : input char value.
  254. */
  255. char uart_rx_one_char_block(void);
  256. /**
  257. * @brief Get an input string line from message channel.
  258. * Please do not call this function in SDK.
  259. *
  260. * @param uint8_t *pString : the pointer to store the string.
  261. *
  262. * @param uint8_t MaxStrlen : the max string length, incude '\0'.
  263. *
  264. * @return OK.
  265. */
  266. ETS_STATUS UartRxString(uint8_t *pString, uint8_t MaxStrlen);
  267. /**
  268. * @brief Process uart recevied information in the interrupt handler.
  269. * Please do not call this function in SDK.
  270. *
  271. * @param void *para : the message receive buffer.
  272. *
  273. * @return None
  274. */
  275. void uart_rx_intr_handler(void *para);
  276. /**
  277. * @brief Get an char from receive buffer.
  278. * Please do not call this function in SDK.
  279. *
  280. * @param RcvMsgBuff *pRxBuff : the pointer to the struct that include receive buffer.
  281. *
  282. * @param uint8_t *pRxByte : the pointer to store the char.
  283. *
  284. * @return OK for successful.
  285. * FAIL for failed.
  286. */
  287. ETS_STATUS uart_rx_readbuff( RcvMsgBuff *pRxBuff, uint8_t *pRxByte);
  288. /**
  289. * @brief Get all chars from receive buffer.
  290. * Please do not call this function in SDK.
  291. *
  292. * @param uint8_t *pCmdLn : the pointer to store the string.
  293. *
  294. * @return OK for successful.
  295. * FAIL for failed.
  296. */
  297. ETS_STATUS UartGetCmdLn(uint8_t *pCmdLn);
  298. /**
  299. * @brief Get uart configuration struct.
  300. * Please do not call this function in SDK.
  301. *
  302. * @param None
  303. *
  304. * @return UartDevice * : uart configuration struct pointer.
  305. */
  306. UartDevice *GetUartDevice(void);
  307. /**
  308. * @brief Send an packet to download tool, with SLIP escaping.
  309. * Please do not call this function in SDK.
  310. *
  311. * @param uint8_t *p : the pointer to output string.
  312. *
  313. * @param int len : the string length.
  314. *
  315. * @return None.
  316. */
  317. void send_packet(uint8_t *p, int len);
  318. /**
  319. * @brief Receive an packet from download tool, with SLIP escaping.
  320. * Please do not call this function in SDK.
  321. *
  322. * @param uint8_t *p : the pointer to input string.
  323. *
  324. * @param int len : If string length > len, the string will be truncated.
  325. *
  326. * @param uint8_t is_sync : 0, only one UART module;
  327. * 1, two UART modules.
  328. *
  329. * @return int : the length of the string.
  330. */
  331. int recv_packet(uint8_t *p, int len, uint8_t is_sync);
  332. /**
  333. * @brief Send an packet to 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 DataLen : the string length.
  339. *
  340. * @return OK for successful.
  341. * FAIL for failed.
  342. */
  343. ETS_STATUS SendMsg(uint8_t *pData, uint16_t DataLen);
  344. /**
  345. * @brief Receive an packet from download tool, with SLIP escaping.
  346. * Please do not call this function in SDK.
  347. *
  348. * @param uint8_t *pData : the pointer to input string.
  349. *
  350. * @param uint16_t MaxDataLen : If string length > MaxDataLen, the string will be truncated.
  351. *
  352. * @param uint8_t is_sync : 0, only one UART module;
  353. * 1, two UART modules.
  354. *
  355. * @return OK for successful.
  356. * FAIL for failed.
  357. */
  358. ETS_STATUS RcvMsg(uint8_t *pData, uint16_t MaxDataLen, uint8_t is_sync);
  359. /**
  360. * @brief Check if this UART is in download connection.
  361. * Please do not call this function in SDK.
  362. *
  363. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  364. *
  365. * @return ETS_NO_BOOT = 0 for no.
  366. * SEL_UART_BOOT = BIT(1) for yes.
  367. */
  368. uint8_t UartConnCheck(uint8_t uart_no);
  369. /**
  370. * @brief Initialize the USB ACM UART
  371. * Needs to be fed a buffer of at least 128 bytes (ESP_ROM_CDC_ACM_WORK_BUF_MIN),
  372. * plus any rx buffer you may want to have.
  373. *
  374. * @param cdc_acm_work_mem Pointer to work mem for CDC-ACM code
  375. * @param cdc_acm_work_mem_len Length of work mem
  376. */
  377. void Uart_Init_USB(void *cdc_acm_work_mem, int cdc_acm_work_mem_len);
  378. /**
  379. * @brief Install handler to reset the chip when a RTS change has been detected on the CDC-ACM 'UART'.
  380. */
  381. void uart_usb_enable_reset_on_rts(void);
  382. extern UartDevice UartDev;
  383. /**
  384. * @}
  385. */
  386. #ifdef __cplusplus
  387. }
  388. #endif
  389. #endif /* _ROM_UART_H_ */