text: /*
   * ESPRESSIF MIT License
   *
   * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
   *
   * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
   * it is free of charge, to any person obtaining a copy of this software and associated
   * documentation files (the "Software"), to deal in the Software without restriction, including
   * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
   * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
   * to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in all copies or
   * substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   *
   */
  
  #ifndef UART_APP_H
  #define UART_APP_H
  
  #include "uart_register.h"
  #include "eagle_soc.h"
  #include "c_types.h"
  
  #define RX_BUFF_SIZE    256
  #define TX_BUFF_SIZE    100
  #define UART0   0
  #define UART1   1
  
  typedef enum {
      FIVE_BITS = 0x0,
      SIX_BITS = 0x1,
      SEVEN_BITS = 0x2,
      EIGHT_BITS = 0x3
  } UartBitsNum4Char;
  
  typedef enum {
      ONE_STOP_BIT             = 0,
      ONE_HALF_STOP_BIT        = BIT2,
      TWO_STOP_BIT             = BIT2
  } UartStopBitsNum;
  
  typedef enum {
      NONE_BITS = 0,
      ODD_BITS   = 0,
      EVEN_BITS = BIT4
  } UartParityMode;
  
  typedef enum {
      STICK_PARITY_DIS   = 0,
      STICK_PARITY_EN    = BIT3 | BIT5
  } UartExistParity;
  
  typedef enum {
      BIT_RATE_9600     = 9600,
      BIT_RATE_19200   = 19200,
      BIT_RATE_38400   = 38400,
      BIT_RATE_57600   = 57600,
      BIT_RATE_74880   = 74880,
      BIT_RATE_115200 = 115200,
      BIT_RATE_230400 = 230400,
      BIT_RATE_256000 = 256000,
      BIT_RATE_460800 = 460800,
      BIT_RATE_921600 = 921600
  } UartBautRate;
  
  typedef enum {
      NONE_CTRL,
      HARDWARE_CTRL,
      XON_XOFF_CTRL
  } UartFlowCtrl;
  
  typedef enum {
      EMPTY,
      UNDER_WRITE,
      WRITE_OVER
  } RcvMsgBuffState;
  
  typedef struct {
      uint32     RcvBuffSize;
      uint8     *pRcvMsgBuff;
      uint8     *pWritePos;
      uint8     *pReadPos;
      uint8      TrigLvl; //JLU: may need to pad
      RcvMsgBuffState  BuffState;
  } RcvMsgBuff;
  
  typedef struct {
      uint32   TrxBuffSize;
      uint8   *pTrxBuff;
  } TrxMsgBuff;
  
  typedef enum {
      BAUD_RATE_DET,
      WAIT_SYNC_FRM,
      SRCH_MSG_HEAD,
      RCV_MSG_BODY,
      RCV_ESC_CHAR,
  } RcvMsgState;
  
  typedef struct {
      UartBautRate 	     baut_rate;
      UartBitsNum4Char  data_bits;
      UartExistParity      exist_parity;
      UartParityMode 	    parity;    // chip size in byte
      UartStopBitsNum   stop_bits;
      UartFlowCtrl         flow_ctrl;
      RcvMsgBuff          rcv_buff;
      TrxMsgBuff           trx_buff;
      RcvMsgState        rcv_state;
      int                      received;
      int                      buff_uart_no;  //indicate which uart use tx/rx buffer
  } UartDevice;
  
  void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
  void uart0_sendStr(const char *str);
  #endif
  
  