uart.h 7.5 KB

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