uart.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * ESPRSSIF MIT License
  3. *
  4. * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef UART_APP_H
  25. #define UART_APP_H
  26. #include "uart_register.h"
  27. #include "eagle_soc.h"
  28. #include "c_types.h"
  29. #define UART_TX_BUFFER_SIZE 256 //Ring buffer length of tx buffer
  30. #define UART_RX_BUFFER_SIZE 256 //Ring buffer length of rx buffer
  31. #define UART_BUFF_EN 0 //use uart buffer , FOR UART0
  32. #define UART_SELFTEST 0 //set 1:enable the loop test demo for uart buffer, FOR UART0
  33. #define UART_HW_RTS 0 //set 1: enable uart hw flow control RTS, PIN MTDO, FOR UART0
  34. #define UART_HW_CTS 0 //set1: enable uart hw flow contrl CTS , PIN MTCK, FOR UART0
  35. #define UART0 0
  36. #define UART1 1
  37. typedef enum {
  38. FIVE_BITS = 0x0,
  39. SIX_BITS = 0x1,
  40. SEVEN_BITS = 0x2,
  41. EIGHT_BITS = 0x3
  42. } UartBitsNum4Char;
  43. typedef enum {
  44. ONE_STOP_BIT = 0x1,
  45. ONE_HALF_STOP_BIT = 0x2,
  46. TWO_STOP_BIT = 0x3
  47. } UartStopBitsNum;
  48. typedef enum {
  49. NONE_BITS = 0x2,
  50. ODD_BITS = 1,
  51. EVEN_BITS = 0
  52. } UartParityMode;
  53. typedef enum {
  54. STICK_PARITY_DIS = 0,
  55. STICK_PARITY_EN = 1
  56. } UartExistParity;
  57. typedef enum {
  58. UART_None_Inverse = 0x0,
  59. UART_Rxd_Inverse = UART_RXD_INV,
  60. UART_CTS_Inverse = UART_CTS_INV,
  61. UART_Txd_Inverse = UART_TXD_INV,
  62. UART_RTS_Inverse = UART_RTS_INV,
  63. } UART_LineLevelInverse;
  64. typedef enum {
  65. BIT_RATE_300 = 300,
  66. BIT_RATE_600 = 600,
  67. BIT_RATE_1200 = 1200,
  68. BIT_RATE_2400 = 2400,
  69. BIT_RATE_4800 = 4800,
  70. BIT_RATE_9600 = 9600,
  71. BIT_RATE_19200 = 19200,
  72. BIT_RATE_38400 = 38400,
  73. BIT_RATE_57600 = 57600,
  74. BIT_RATE_74880 = 74880,
  75. BIT_RATE_115200 = 115200,
  76. BIT_RATE_230400 = 230400,
  77. BIT_RATE_460800 = 460800,
  78. BIT_RATE_921600 = 921600,
  79. BIT_RATE_1843200 = 1843200,
  80. BIT_RATE_3686400 = 3686400,
  81. } UartBautRate;
  82. typedef enum {
  83. NONE_CTRL,
  84. HARDWARE_CTRL,
  85. XON_XOFF_CTRL
  86. } UartFlowCtrl;
  87. typedef enum {
  88. USART_HardwareFlowControl_None = 0x0,
  89. USART_HardwareFlowControl_RTS = 0x1,
  90. USART_HardwareFlowControl_CTS = 0x2,
  91. USART_HardwareFlowControl_CTS_RTS = 0x3
  92. } UART_HwFlowCtrl;
  93. typedef enum {
  94. EMPTY,
  95. UNDER_WRITE,
  96. WRITE_OVER
  97. } RcvMsgBuffState;
  98. typedef struct {
  99. uint32 RcvBuffSize;
  100. uint8 *pRcvMsgBuff;
  101. uint8 *pWritePos;
  102. uint8 *pReadPos;
  103. uint8 TrigLvl; //JLU: may need to pad
  104. RcvMsgBuffState BuffState;
  105. } RcvMsgBuff;
  106. typedef struct {
  107. uint32 TrxBuffSize;
  108. uint8 *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;
  122. UartStopBitsNum stop_bits;
  123. UartFlowCtrl flow_ctrl;
  124. RcvMsgBuff rcv_buff;
  125. TrxMsgBuff trx_buff;
  126. RcvMsgState rcv_state;
  127. int received;
  128. int buff_uart_no; //indicate which uart use tx/rx buffer
  129. } UartDevice;
  130. void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
  131. void uart0_sendStr(const char *str);
  132. ///////////////////////////////////////
  133. #define UART_FIFO_LEN 128 //define the tx fifo length
  134. #define UART_TX_EMPTY_THRESH_VAL 0x10
  135. struct UartBuffer{
  136. uint32 UartBuffSize;
  137. uint8 *pUartBuff;
  138. uint8 *pInPos;
  139. uint8 *pOutPos;
  140. STATUS BuffState;
  141. uint16 Space; //remanent space of the buffer
  142. uint8 TcpControl;
  143. struct UartBuffer * nextBuff;
  144. };
  145. struct UartRxBuff{
  146. uint32 UartRxBuffSize;
  147. uint8 *pUartRxBuff;
  148. uint8 *pWritePos;
  149. uint8 *pReadPos;
  150. STATUS RxBuffState;
  151. uint32 Space; //remanent space of the buffer
  152. } ;
  153. typedef enum {
  154. RUN = 0,
  155. BLOCK = 1,
  156. } TCPState;
  157. //void ICACHE_FLASH_ATTR uart_test_rx();
  158. STATUS uart_tx_one_char(uint8 uart, uint8 TxChar);
  159. STATUS uart_tx_one_char_no_wait(uint8 uart, uint8 TxChar);
  160. void uart1_sendStr_no_wait(const char *str);
  161. struct UartBuffer* Uart_Buf_Init();
  162. #if UART_BUFF_EN
  163. LOCAL void Uart_Buf_Cpy(struct UartBuffer* pCur, char* pdata , uint16 data_len);
  164. void uart_buf_free(struct UartBuffer* pBuff);
  165. void tx_buff_enq(char* pdata, uint16 data_len );
  166. LOCAL void tx_fifo_insert(struct UartBuffer* pTxBuff, uint8 data_len, uint8 uart_no);
  167. void tx_start_uart_buffer(uint8 uart_no);
  168. uint16 rx_buff_deq(char* pdata, uint16 data_len );
  169. void Uart_rx_buff_enq();
  170. #endif
  171. void uart_rx_intr_enable(uint8 uart_no);
  172. void uart_rx_intr_disable(uint8 uart_no);
  173. void uart0_tx_buffer(uint8 *buf, uint16 len);
  174. //==============================================
  175. #define FUNC_UART0_CTS 4
  176. #define FUNC_U0CTS 4
  177. #define FUNC_U1TXD_BK 2
  178. #define UART_LINE_INV_MASK (0x3f<<19)
  179. void UART_SetWordLength(uint8 uart_no, UartBitsNum4Char len);
  180. void UART_SetStopBits(uint8 uart_no, UartStopBitsNum bit_num);
  181. void UART_SetLineInverse(uint8 uart_no, UART_LineLevelInverse inverse_mask);
  182. void UART_SetParity(uint8 uart_no, UartParityMode Parity_mode);
  183. void UART_SetBaudrate(uint8 uart_no,uint32 baud_rate);
  184. void UART_SetFlowCtrl(uint8 uart_no,UART_HwFlowCtrl flow_ctrl,uint8 rx_thresh);
  185. void UART_WaitTxFifoEmpty(uint8 uart_no , uint32 time_out_us); //do not use if tx flow control enabled
  186. void UART_ResetFifo(uint8 uart_no);
  187. void UART_ClearIntrStatus(uint8 uart_no,uint32 clr_mask);
  188. void UART_SetIntrEna(uint8 uart_no,uint32 ena_mask);
  189. void UART_SetPrintPort(uint8 uart_no);
  190. bool UART_CheckOutputFinished(uint8 uart_no, uint32 time_out_us);
  191. //==============================================
  192. #endif