uart.h 11 KB

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