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