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_reg.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 0x400 37 #define TX_BUFF_SIZE 100 38 39 //uart int enalbe 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 RcvMsgBuff rcv_buff; 160 // TrxMsgBuff trx_buff; 161 RcvMsgState rcv_state; 162 int received; 163 } UartDevice; 164 165 /** 166 * @brief Init uart device struct value and reset uart0/uart1 rx. 167 * Please do not call this function in SDK. 168 * 169 * @param rxBuffer, must be a pointer to RX_BUFF_SIZE bytes or NULL 170 * 171 * @return None 172 */ 173 void uartAttach(void *rxBuffer); 174 175 /** 176 * @brief Init uart0 or uart1 for UART download booting mode. 177 * Please do not call this function in SDK. 178 * 179 * @param uint8_t uart_no : 0 for UART0, else for UART1. 180 * 181 * @param uint32_t clock : clock used by uart module, to adjust baudrate. 182 * 183 * @return None 184 */ 185 void Uart_Init(uint8_t uart_no, uint32_t clock); 186 187 /** 188 * @brief Modify uart baudrate. 189 * This function will reset RX/TX fifo for uart. 190 * 191 * @param uint8_t uart_no : 0 for UART0, 1 for UART1. 192 * 193 * @param uint32_t DivLatchValue : (clock << 4)/baudrate. 194 * 195 * @return None 196 */ 197 void uart_div_modify(uint8_t uart_no, uint32_t DivLatchValue); 198 199 200 /** 201 * @brief Re-calculate UART baudrate divisor for a given (changed) 202 * clock speed. 203 * This function will not reset RX/TX fifo for uart. 204 * 205 * @param uint8_t uart_no : 0 for UART0, 1 for UART1. 206 * 207 * @param uint32_t clock : clock used by uart module, to adjust baudrate. 208 * 209 * @return None 210 */ 211 void uart_div_reinit(uint8_t uart_no, uint32_t clock); 212 213 214 /** 215 * @brief Init uart0 or uart1 for UART download booting mode. 216 * Please do not call this function in SDK. 217 * 218 * @param uint8_t uart_no : 0 for UART0, 1 for UART1. 219 * 220 * @param uint8_t is_sync : 0, only one UART module, easy to detect, wait until detected; 221 * 1, two UART modules, hard to detect, detect and return. 222 * 223 * @return None 224 */ 225 int uart_baudrate_detect(uint8_t uart_no, uint8_t is_sync); 226 227 /** 228 * @brief Switch printf channel of uart_tx_one_char. 229 * Please do not call this function when printf. 230 * 231 * @param uint8_t uart_no : 0 for UART0, 1 for UART1. 232 * 233 * @return None 234 */ 235 void uart_tx_switch(uint8_t uart_no); 236 237 /** 238 * @brief Switch message exchange channel for UART download booting. 239 * Please do not call this function in SDK. 240 * 241 * @param uint8_t uart_no : 0 for UART0, 1 for UART1. 242 * 243 * @return None 244 */ 245 void uart_buff_switch(uint8_t uart_no); 246 247 /** 248 * @brief Output a char to printf channel, wait until fifo not full. 249 * 250 * @param None 251 * 252 * @return OK. 253 */ 254 STATUS uart_tx_one_char(uint8_t TxChar); 255 256 /** 257 * @brief Output a char to message exchange channel, wait until fifo not full. 258 * Please do not call this function in SDK. 259 * 260 * @param None 261 * 262 * @return OK. 263 */ 264 STATUS uart_tx_one_char2(uint8_t TxChar); 265 266 /** 267 * @brief Wait until uart tx full empty. 268 * 269 * @param uint8_t uart_no : 0 for UART0, 1 for UART1. 270 * 271 * @return None. 272 */ 273 void uart_tx_flush(uint8_t uart_no); 274 275 /** 276 * @brief Wait until uart tx full empty and the last char send ok. 277 * 278 * @param uart_no : 0 for UART0, 1 for UART1, 2 for UART2 279 * 280 * The function defined in ROM code has a bug, so we define the correct version 281 * here for compatibility. 282 */ 283 void uart_tx_wait_idle(uint8_t uart_no); 284 285 /** 286 * @brief Get an input char from message channel. 287 * Please do not call this function in SDK. 288 * 289 * @param uint8_t *pRxChar : the pointer to store the char. 290 * 291 * @return OK for successful. 292 * FAIL for failed. 293 */ 294 STATUS uart_rx_one_char(uint8_t *pRxChar); 295 296 /** 297 * @brief Get an input char from message channel, wait until successful. 298 * Please do not call this function in SDK. 299 * 300 * @param None 301 * 302 * @return char : input char value. 303 */ 304 char uart_rx_one_char_block(void); 305 306 /** 307 * @brief Get an input string line from message channel. 308 * Please do not call this function in SDK. 309 * 310 * @param uint8_t *pString : the pointer to store the string. 311 * 312 * @param uint8_t MaxStrlen : the max string length, incude '\0'. 313 * 314 * @return OK. 315 */ 316 STATUS UartRxString(uint8_t *pString, uint8_t MaxStrlen); 317 318 /** 319 * @brief Process uart recevied information in the interrupt handler. 320 * Please do not call this function in SDK. 321 * 322 * @param void *para : the message receive buffer. 323 * 324 * @return None 325 */ 326 void uart_rx_intr_handler(void *para); 327 328 /** 329 * @brief Get an char from receive buffer. 330 * Please do not call this function in SDK. 331 * 332 * @param RcvMsgBuff *pRxBuff : the pointer to the struct that include receive buffer. 333 * 334 * @param uint8_t *pRxByte : the pointer to store the char. 335 * 336 * @return OK for successful. 337 * FAIL for failed. 338 */ 339 STATUS uart_rx_readbuff( RcvMsgBuff *pRxBuff, uint8_t *pRxByte); 340 341 /** 342 * @brief Get all chars from receive buffer. 343 * Please do not call this function in SDK. 344 * 345 * @param uint8_t *pCmdLn : the pointer to store the string. 346 * 347 * @return OK for successful. 348 * FAIL for failed. 349 */ 350 STATUS UartGetCmdLn(uint8_t *pCmdLn); 351 352 /** 353 * @brief Get uart configuration struct. 354 * Please do not call this function in SDK. 355 * 356 * @param None 357 * 358 * @return UartDevice * : uart configuration struct pointer. 359 */ 360 UartDevice *GetUartDevice(void); 361 362 /** 363 * @brief Send an packet to download tool, with SLIP escaping. 364 * Please do not call this function in SDK. 365 * 366 * @param uint8_t *p : the pointer to output string. 367 * 368 * @param int len : the string length. 369 * 370 * @return None. 371 */ 372 void send_packet(uint8_t *p, int len); 373 374 /** 375 * @brief Receive an packet from download tool, with SLIP escaping. 376 * Please do not call this function in SDK. 377 * 378 * @param uint8_t *p : the pointer to input string. 379 * 380 * @param int len : If string length > len, the string will be truncated. 381 * 382 * @param uint8_t is_sync : 0, only one UART module; 383 * 1, two UART modules. 384 * 385 * @return int : the length of the string. 386 */ 387 int recv_packet(uint8_t *p, int len, uint8_t is_sync); 388 389 /** 390 * @brief Send an packet to download tool, with SLIP escaping. 391 * Please do not call this function in SDK. 392 * 393 * @param uint8_t *pData : the pointer to input string. 394 * 395 * @param uint16_t DataLen : the string length. 396 * 397 * @return OK for successful. 398 * FAIL for failed. 399 */ 400 STATUS SendMsg(uint8_t *pData, uint16_t DataLen); 401 402 /** 403 * @brief Receive an packet from download tool, with SLIP escaping. 404 * Please do not call this function in SDK. 405 * 406 * @param uint8_t *pData : the pointer to input string. 407 * 408 * @param uint16_t MaxDataLen : If string length > MaxDataLen, the string will be truncated. 409 * 410 * @param uint8_t is_sync : 0, only one UART module; 411 * 1, two UART modules. 412 * 413 * @return OK for successful. 414 * FAIL for failed. 415 */ 416 STATUS RcvMsg(uint8_t *pData, uint16_t MaxDataLen, uint8_t is_sync); 417 418 /** 419 * @brief Check if this UART is in download connection. 420 * Please do not call this function in SDK. 421 * 422 * @param uint8_t uart_no : 0 for UART0, 1 for UART1. 423 * 424 * @return ETS_NO_BOOT = 0 for no. 425 * SEL_UART_BOOT = BIT(1) for yes. 426 */ 427 uint8_t UartConnCheck(uint8_t uart_no); 428 429 /** 430 * @brief Initialize the USB ACM UART 431 * Needs to be fed a buffer of at least 128 bytes (ESP_ROM_CDC_ACM_WORK_BUF_MIN), 432 * plus any rx buffer you may want to have. 433 * 434 * @param cdc_acm_work_mem Pointer to work mem for CDC-ACM code 435 * @param cdc_acm_work_mem_len Length of work mem 436 */ 437 void Uart_Init_USB(void *cdc_acm_work_mem, int cdc_acm_work_mem_len); 438 439 440 /** 441 * @brief Install handler to reset the chip when a RTS change has been detected on the CDC-ACM 'UART'. 442 */ 443 void uart_usb_enable_reset_on_rts(void); 444 445 446 extern UartDevice UartDev; 447 448 /** 449 * @} 450 */ 451 452 #ifdef __cplusplus 453 } 454 #endif 455 456 #endif /* _ROM_UART_H_ */ 457