uart.h 11 KB

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