uart.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 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 Switch printf channel of uart_tx_one_char.
  163. * Please do not call this function when printf.
  164. *
  165. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  166. *
  167. * @return None
  168. */
  169. void uart_tx_switch(uint8_t uart_no);
  170. /**
  171. * @brief Output a char to printf channel, wait until fifo not full.
  172. *
  173. * @param None
  174. *
  175. * @return OK.
  176. */
  177. ETS_STATUS uart_tx_one_char(uint8_t TxChar);
  178. /**
  179. * @brief Output a char to message exchange channel, wait until fifo not full.
  180. * Please do not call this function in SDK.
  181. *
  182. * @param None
  183. *
  184. * @return OK.
  185. */
  186. ETS_STATUS uart_tx_one_char2(uint8_t TxChar);
  187. /**
  188. * @brief Wait until uart tx full empty.
  189. *
  190. * @param uint8_t uart_no : 0 for UART0, 1 for UART1.
  191. *
  192. * @return None.
  193. */
  194. void uart_tx_flush(uint8_t uart_no);
  195. /**
  196. * @brief Wait until uart tx full empty and the last char send ok.
  197. *
  198. * @param uart_no : 0 for UART0, 1 for UART1
  199. *
  200. * The function defined in ROM code has a bug, so we define the correct version
  201. * here for compatibility.
  202. */
  203. void uart_tx_wait_idle(uint8_t uart_no);
  204. /**
  205. * @brief Get an input char from message channel.
  206. * Please do not call this function in SDK.
  207. *
  208. * @param uint8_t *pRxChar : the pointer to store the char.
  209. *
  210. * @return OK for successful.
  211. * FAIL for failed.
  212. */
  213. ETS_STATUS uart_rx_one_char(uint8_t *pRxChar);
  214. /**
  215. * @brief Get an input char from message channel, wait until successful.
  216. * Please do not call this function in SDK.
  217. *
  218. * @param None
  219. *
  220. * @return char : input char value.
  221. */
  222. char uart_rx_one_char_block(void);
  223. /**
  224. * @brief Get an input string line from message channel.
  225. * Please do not call this function in SDK.
  226. *
  227. * @param uint8_t *pString : the pointer to store the string.
  228. *
  229. * @param uint8_t MaxStrlen : the max string length, incude '\0'.
  230. *
  231. * @return OK.
  232. */
  233. ETS_STATUS UartRxString(uint8_t *pString, uint8_t MaxStrlen);
  234. /**
  235. * @brief Get an char from receive buffer.
  236. * Please do not call this function in SDK.
  237. *
  238. * @param RcvMsgBuff *pRxBuff : the pointer to the struct that include receive buffer.
  239. *
  240. * @param uint8_t *pRxByte : the pointer to store the char.
  241. *
  242. * @return OK for successful.
  243. * FAIL for failed.
  244. */
  245. ETS_STATUS uart_rx_readbuff( RcvMsgBuff *pRxBuff, uint8_t *pRxByte);
  246. /**
  247. * @brief Get all chars from receive buffer.
  248. * Please do not call this function in SDK.
  249. *
  250. * @param uint8_t *pCmdLn : the pointer to store the string.
  251. *
  252. * @return OK for successful.
  253. * FAIL for failed.
  254. */
  255. ETS_STATUS UartGetCmdLn(uint8_t *pCmdLn);
  256. /**
  257. * @brief Get uart configuration struct.
  258. * Please do not call this function in SDK.
  259. *
  260. * @param None
  261. *
  262. * @return UartDevice * : uart configuration struct pointer.
  263. */
  264. UartDevice *GetUartDevice(void);
  265. /**
  266. * @brief Send an packet to download tool, with SLIP escaping.
  267. * Please do not call this function in SDK.
  268. *
  269. * @param uint8_t *p : the pointer to output string.
  270. *
  271. * @param int len : the string length.
  272. *
  273. * @return None.
  274. */
  275. void send_packet(uint8_t *p, int len);
  276. /**
  277. * @brief Receive an packet from download tool, with SLIP escaping.
  278. * Please do not call this function in SDK.
  279. *
  280. * @param uint8_t *p : the pointer to input string.
  281. *
  282. * @param int len : If string length > len, the string will be truncated.
  283. *
  284. * @param uint8_t is_sync : 0, only one UART module;
  285. * 1, two UART modules.
  286. *
  287. * @return int : the length of the string.
  288. */
  289. int recv_packet(uint8_t *p, int len, uint8_t is_sync);
  290. extern UartDevice UartDev;
  291. /**
  292. * @}
  293. */
  294. #ifdef __cplusplus
  295. }
  296. #endif
  297. #endif /* _ROM_UART_H_ */