uart.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * File : uart.h
  3. * Copyright (C) 2013 - 2016, Espressif Systems
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of version 3 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef UART_APP_H
  18. #define UART_APP_H
  19. #include "uart_register.h"
  20. #include "eagle_soc.h"
  21. #include "c_types.h"
  22. #define UART_TX_BUFFER_SIZE 256 //Ring buffer length of tx buffer
  23. #define UART_RX_BUFFER_SIZE 256 //Ring buffer length of rx buffer
  24. #define UART_BUFF_EN 0 //use uart buffer , FOR UART0
  25. #define UART_SELFTEST 0 //set 1:enable the loop test demo for uart buffer, FOR UART0
  26. #define UART_HW_RTS 0 //set 1: enable uart hw flow control RTS, PIN MTDO, FOR UART0
  27. #define UART_HW_CTS 0 //set1: enable uart hw flow contrl CTS , PIN MTCK, FOR UART0
  28. #define UART0 0
  29. #define UART1 1
  30. typedef enum {
  31. FIVE_BITS = 0x0,
  32. SIX_BITS = 0x1,
  33. SEVEN_BITS = 0x2,
  34. EIGHT_BITS = 0x3
  35. } UartBitsNum4Char;
  36. typedef enum {
  37. ONE_STOP_BIT = 0x1,
  38. ONE_HALF_STOP_BIT = 0x2,
  39. TWO_STOP_BIT = 0x3
  40. } UartStopBitsNum;
  41. typedef enum {
  42. NONE_BITS = 0x2,
  43. ODD_BITS = 1,
  44. EVEN_BITS = 0
  45. } UartParityMode;
  46. typedef enum {
  47. STICK_PARITY_DIS = 0,
  48. STICK_PARITY_EN = 1
  49. } UartExistParity;
  50. typedef enum {
  51. UART_None_Inverse = 0x0,
  52. UART_Rxd_Inverse = UART_RXD_INV,
  53. UART_CTS_Inverse = UART_CTS_INV,
  54. UART_Txd_Inverse = UART_TXD_INV,
  55. UART_RTS_Inverse = UART_RTS_INV,
  56. } UART_LineLevelInverse;
  57. typedef enum {
  58. BIT_RATE_300 = 300,
  59. BIT_RATE_600 = 600,
  60. BIT_RATE_1200 = 1200,
  61. BIT_RATE_2400 = 2400,
  62. BIT_RATE_4800 = 4800,
  63. BIT_RATE_9600 = 9600,
  64. BIT_RATE_19200 = 19200,
  65. BIT_RATE_38400 = 38400,
  66. BIT_RATE_57600 = 57600,
  67. BIT_RATE_74880 = 74880,
  68. BIT_RATE_115200 = 115200,
  69. BIT_RATE_230400 = 230400,
  70. BIT_RATE_460800 = 460800,
  71. BIT_RATE_921600 = 921600,
  72. BIT_RATE_1843200 = 1843200,
  73. BIT_RATE_3686400 = 3686400,
  74. } UartBautRate;
  75. typedef enum {
  76. NONE_CTRL,
  77. HARDWARE_CTRL,
  78. XON_XOFF_CTRL
  79. } UartFlowCtrl;
  80. typedef enum {
  81. USART_HardwareFlowControl_None = 0x0,
  82. USART_HardwareFlowControl_RTS = 0x1,
  83. USART_HardwareFlowControl_CTS = 0x2,
  84. USART_HardwareFlowControl_CTS_RTS = 0x3
  85. } UART_HwFlowCtrl;
  86. typedef enum {
  87. EMPTY,
  88. UNDER_WRITE,
  89. WRITE_OVER
  90. } RcvMsgBuffState;
  91. typedef struct {
  92. uint32 RcvBuffSize;
  93. uint8 *pRcvMsgBuff;
  94. uint8 *pWritePos;
  95. uint8 *pReadPos;
  96. uint8 TrigLvl; //JLU: may need to pad
  97. RcvMsgBuffState BuffState;
  98. } RcvMsgBuff;
  99. typedef struct {
  100. uint32 TrxBuffSize;
  101. uint8 *pTrxBuff;
  102. } TrxMsgBuff;
  103. typedef enum {
  104. BAUD_RATE_DET,
  105. WAIT_SYNC_FRM,
  106. SRCH_MSG_HEAD,
  107. RCV_MSG_BODY,
  108. RCV_ESC_CHAR,
  109. } RcvMsgState;
  110. typedef struct {
  111. UartBautRate baut_rate;
  112. UartBitsNum4Char data_bits;
  113. UartExistParity exist_parity;
  114. UartParityMode parity;
  115. UartStopBitsNum stop_bits;
  116. UartFlowCtrl flow_ctrl;
  117. RcvMsgBuff rcv_buff;
  118. TrxMsgBuff trx_buff;
  119. RcvMsgState rcv_state;
  120. int received;
  121. int buff_uart_no; //indicate which uart use tx/rx buffer
  122. } UartDevice;
  123. void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
  124. void uart0_sendStr(const char *str);
  125. ///////////////////////////////////////
  126. #define UART_FIFO_LEN 128 //define the tx fifo length
  127. #define UART_TX_EMPTY_THRESH_VAL 0x10
  128. struct UartBuffer{
  129. uint32 UartBuffSize;
  130. uint8 *pUartBuff;
  131. uint8 *pInPos;
  132. uint8 *pOutPos;
  133. STATUS BuffState;
  134. uint16 Space; //remanent space of the buffer
  135. uint8 TcpControl;
  136. struct UartBuffer * nextBuff;
  137. };
  138. struct UartRxBuff{
  139. uint32 UartRxBuffSize;
  140. uint8 *pUartRxBuff;
  141. uint8 *pWritePos;
  142. uint8 *pReadPos;
  143. STATUS RxBuffState;
  144. uint32 Space; //remanent space of the buffer
  145. } ;
  146. typedef enum {
  147. RUN = 0,
  148. BLOCK = 1,
  149. } TCPState;
  150. //void ICACHE_FLASH_ATTR uart_test_rx();
  151. STATUS uart_tx_one_char(uint8 uart, uint8 TxChar);
  152. STATUS uart_tx_one_char_no_wait(uint8 uart, uint8 TxChar);
  153. void uart1_sendStr_no_wait(const char *str);
  154. struct UartBuffer* Uart_Buf_Init();
  155. #if UART_BUFF_EN
  156. LOCAL void Uart_Buf_Cpy(struct UartBuffer* pCur, char* pdata , uint16 data_len);
  157. void uart_buf_free(struct UartBuffer* pBuff);
  158. void tx_buff_enq(char* pdata, uint16 data_len );
  159. LOCAL void tx_fifo_insert(struct UartBuffer* pTxBuff, uint8 data_len, uint8 uart_no);
  160. void tx_start_uart_buffer(uint8 uart_no);
  161. uint16 rx_buff_deq(char* pdata, uint16 data_len );
  162. void Uart_rx_buff_enq();
  163. #endif
  164. void uart_rx_intr_enable(uint8 uart_no);
  165. void uart_rx_intr_disable(uint8 uart_no);
  166. void uart0_tx_buffer(uint8 *buf, uint16 len);
  167. //==============================================
  168. #define FUNC_UART0_CTS 4
  169. #define FUNC_U0CTS 4
  170. #define FUNC_U1TXD_BK 2
  171. #define UART_LINE_INV_MASK (0x3f<<19)
  172. void UART_SetWordLength(uint8 uart_no, UartBitsNum4Char len);
  173. void UART_SetStopBits(uint8 uart_no, UartStopBitsNum bit_num);
  174. void UART_SetLineInverse(uint8 uart_no, UART_LineLevelInverse inverse_mask);
  175. void UART_SetParity(uint8 uart_no, UartParityMode Parity_mode);
  176. void UART_SetBaudrate(uint8 uart_no,uint32 baud_rate);
  177. void UART_SetFlowCtrl(uint8 uart_no,UART_HwFlowCtrl flow_ctrl,uint8 rx_thresh);
  178. void UART_WaitTxFifoEmpty(uint8 uart_no , uint32 time_out_us); //do not use if tx flow control enabled
  179. void UART_ResetFifo(uint8 uart_no);
  180. void UART_ClearIntrStatus(uint8 uart_no,uint32 clr_mask);
  181. void UART_SetIntrEna(uint8 uart_no,uint32 ena_mask);
  182. void UART_SetPrintPort(uint8 uart_no);
  183. bool UART_CheckOutputFinished(uint8 uart_no, uint32 time_out_us);
  184. //==============================================
  185. #endif