uart.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /** \defgroup uart_apis, uart configuration and communication related apis
  21. * @brief uart apis
  22. */
  23. /** @addtogroup uart_apis
  24. * @{
  25. */
  26. #define RX_BUFF_SIZE 0x100
  27. #define TX_BUFF_SIZE 100
  28. //uart int enalbe register ctrl bits
  29. #define UART_RCV_INTEN BIT0
  30. #define UART_TRX_INTEN BIT1
  31. #define UART_LINE_STATUS_INTEN BIT2
  32. //uart int identification ctrl bits
  33. #define UART_INT_FLAG_MASK 0x0E
  34. //uart fifo ctrl bits
  35. #define UART_CLR_RCV_FIFO BIT1
  36. #define UART_CLR_TRX_FIFO BIT2
  37. #define UART_RCVFIFO_TRG_LVL_BITS BIT6
  38. //uart line control bits
  39. #define UART_DIV_LATCH_ACCESS_BIT BIT7
  40. //uart line status bits
  41. #define UART_RCV_DATA_RDY_FLAG BIT0
  42. #define UART_RCV_OVER_FLOW_FLAG BIT1
  43. #define UART_RCV_PARITY_ERR_FLAG BIT2
  44. #define UART_RCV_FRAME_ERR_FLAG BIT3
  45. #define UART_BRK_INT_FLAG BIT4
  46. #define UART_TRX_FIFO_EMPTY_FLAG BIT5
  47. #define UART_TRX_ALL_EMPTY_FLAG BIT6 // include fifo and shift reg
  48. #define UART_RCV_ERR_FLAG BIT7
  49. //send and receive message frame head
  50. #define FRAME_FLAG 0x7E
  51. typedef enum {
  52. UART_LINE_STATUS_INT_FLAG = 0x06,
  53. UART_RCV_FIFO_INT_FLAG = 0x04,
  54. UART_RCV_TMOUT_INT_FLAG = 0x0C,
  55. UART_TXBUFF_EMPTY_INT_FLAG = 0x02
  56. } UartIntType; //consider bit0 for int_flag
  57. typedef enum {
  58. RCV_ONE_BYTE = 0x0,
  59. RCV_FOUR_BYTE = 0x1,
  60. RCV_EIGHT_BYTE = 0x2,
  61. RCV_FOURTEEN_BYTE = 0x3
  62. } UartRcvFifoTrgLvl;
  63. typedef enum {
  64. FIVE_BITS = 0x0,
  65. SIX_BITS = 0x1,
  66. SEVEN_BITS = 0x2,
  67. EIGHT_BITS = 0x3
  68. } UartBitsNum4Char;
  69. typedef enum {
  70. ONE_STOP_BIT = 1,
  71. ONE_HALF_STOP_BIT = 2,
  72. TWO_STOP_BIT = 3
  73. } UartStopBitsNum;
  74. typedef enum {
  75. NONE_BITS = 0,
  76. ODD_BITS = 2,
  77. EVEN_BITS = 3
  78. } UartParityMode;
  79. typedef enum {
  80. STICK_PARITY_DIS = 0,
  81. STICK_PARITY_EN = 2
  82. } UartExistParity;
  83. typedef enum {
  84. BIT_RATE_9600 = 9600,
  85. BIT_RATE_19200 = 19200,
  86. BIT_RATE_38400 = 38400,
  87. BIT_RATE_57600 = 57600,
  88. BIT_RATE_115200 = 115200,
  89. BIT_RATE_230400 = 230400,
  90. BIT_RATE_460800 = 460800,
  91. BIT_RATE_921600 = 921600
  92. } UartBautRate;
  93. typedef enum {
  94. NONE_CTRL,
  95. HARDWARE_CTRL,
  96. XON_XOFF_CTRL
  97. } UartFlowCtrl;
  98. typedef enum {
  99. EMPTY,
  100. UNDER_WRITE,
  101. WRITE_OVER
  102. } RcvMsgBuffState;
  103. typedef struct {
  104. // uint32_t RcvBuffSize;
  105. uint8_t *pRcvMsgBuff;
  106. uint8_t *pWritePos;
  107. uint8_t *pReadPos;
  108. uint8_t TrigLvl;
  109. RcvMsgBuffState BuffState;
  110. } RcvMsgBuff;
  111. typedef struct {
  112. uint32_t TrxBuffSize;
  113. uint8_t *pTrxBuff;
  114. } TrxMsgBuff;
  115. typedef enum {
  116. BAUD_RATE_DET,
  117. WAIT_SYNC_FRM,
  118. SRCH_MSG_HEAD,
  119. RCV_MSG_BODY,
  120. RCV_ESC_CHAR,
  121. } RcvMsgState;
  122. typedef struct {
  123. UartBautRate baut_rate;
  124. UartBitsNum4Char data_bits;
  125. UartExistParity exist_parity;
  126. UartParityMode parity; // chip size in byte
  127. UartStopBitsNum stop_bits;
  128. UartFlowCtrl flow_ctrl;
  129. uint8_t buff_uart_no; //indicate which uart use tx/rx buffer
  130. uint8_t tx_uart_no;
  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 None
  141. *
  142. * @return None
  143. */
  144. void uartAttach(void);
  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);//for send message
  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 uint8_t uart_no : 0 for UART0, 1 for UART1.
  226. *
  227. * @return None.
  228. */
  229. void uart_tx_wait_idle(uint8_t uart_no);
  230. /**
  231. * @brief Get an input char from message channel.
  232. * Please do not call this function in SDK.
  233. *
  234. * @param uint8_t *pRxChar : the pointer to store the char.
  235. *
  236. * @return OK for successful.
  237. * FAIL for failed.
  238. */
  239. STATUS uart_rx_one_char(uint8_t *pRxChar);
  240. /**
  241. * @brief Get an input char to message channel, wait until successful.
  242. * Please do not call this function in SDK.
  243. *
  244. * @param None
  245. *
  246. * @return char : input char value.
  247. */
  248. char uart_rx_one_char_block(void);
  249. /**
  250. * @brief Get an input string line from message channel.
  251. * Please do not call this function in SDK.
  252. *
  253. * @param uint8_t *pString : the pointer to store the string.
  254. *
  255. * @param uint8_t MaxStrlen : the max string length, incude '\0'.
  256. *
  257. * @return OK.
  258. */
  259. STATUS UartRxString(uint8_t *pString, uint8_t MaxStrlen);
  260. /**
  261. * @brief Process uart recevied information in the interrupt handler.
  262. * Please do not call this function in SDK.
  263. *
  264. * @param void *para : the message receive buffer.
  265. *
  266. * @return None
  267. */
  268. void uart_rx_intr_handler(void *para);
  269. /**
  270. * @brief Get an char from receive buffer.
  271. * Please do not call this function in SDK.
  272. *
  273. * @param RcvMsgBuff *pRxBuff : the pointer to the struct that include receive buffer.
  274. *
  275. * @param uint8_t *pRxByte : the pointer to store the char.
  276. *
  277. * @return OK for successful.
  278. * FAIL for failed.
  279. */
  280. STATUS uart_rx_readbuff( RcvMsgBuff *pRxBuff, uint8_t *pRxByte);
  281. /**
  282. * @brief Get all chars from receive buffer.
  283. * Please do not call this function in SDK.
  284. *
  285. * @param uint8_t *pCmdLn : the pointer to store the string.
  286. *
  287. * @return OK for successful.
  288. * FAIL for failed.
  289. */
  290. STATUS UartGetCmdLn(uint8_t *pCmdLn);
  291. /**
  292. * @brief Get uart configuration struct.
  293. * Please do not call this function in SDK.
  294. *
  295. * @param None
  296. *
  297. * @return UartDevice * : uart configuration struct pointer.
  298. */
  299. UartDevice *GetUartDevice(void);
  300. /**
  301. * @brief Send an packet to download tool, with SLIP escaping.
  302. * Please do not call this function in SDK.
  303. *
  304. * @param uint8_t *p : the pointer to output string.
  305. *
  306. * @param int len : the string length.
  307. *
  308. * @return None.
  309. */
  310. void send_packet(uint8_t *p, int len);
  311. /**
  312. * @brief Receive an packet from download tool, with SLIP escaping.
  313. * Please do not call this function in SDK.
  314. *
  315. * @param uint8_t *p : the pointer to input string.
  316. *
  317. * @param int len : If string length > len, the string will be truncated.
  318. *
  319. * @param uint8_t is_sync : 0, only one UART module;
  320. * 1, two UART modules.
  321. *
  322. * @return int : the length of the string.
  323. */
  324. int recv_packet(uint8_t *p, int len, uint8_t is_sync);
  325. /**
  326. * @brief Send an packet to download tool, with SLIP escaping.
  327. * Please do not call this function in SDK.
  328. *
  329. * @param uint8_t *pData : the pointer to input string.
  330. *
  331. * @param uint16_t DataLen : the string length.
  332. *
  333. * @return OK for successful.
  334. * FAIL for failed.
  335. */
  336. STATUS SendMsg(uint8_t *pData, uint16_t DataLen);
  337. /**
  338. * @brief Receive an packet from download tool, with SLIP escaping.
  339. * Please do not call this function in SDK.
  340. *
  341. * @param uint8_t *pData : the pointer to input string.
  342. *
  343. * @param uint16_t MaxDataLen : If string length > MaxDataLen, the string will be truncated.
  344. *
  345. * @param uint8_t is_sync : 0, only one UART module;
  346. * 1, two UART modules.
  347. *
  348. * @return OK for successful.
  349. * FAIL for failed.
  350. */
  351. STATUS RcvMsg(uint8_t *pData, uint16_t MaxDataLen, uint8_t is_sync);
  352. extern UartDevice UartDev;
  353. /**
  354. * @}
  355. */
  356. #ifdef __cplusplus
  357. }
  358. #endif
  359. #endif /* _ROM_UART_H_ */