uart.h 11 KB

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