Browse Source

Added UART interface.

curiousmuch 6 years ago
parent
commit
d1a7fdde4c
7 changed files with 333 additions and 904 deletions
  1. 5 0
      .gitignore
  2. 4 3
      Makefile
  3. 156 708
      driver/uart.c
  4. 42 158
      include/driver/uart.h
  5. 4 1
      include/interface/uart_interface.h
  6. 113 30
      interface/uart_interface.c
  7. 9 4
      user/user_main.c

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+*.o
+*.a
+*.bin
+*.ld
+*.out

+ 4 - 3
Makefile

@@ -36,7 +36,7 @@ TARGET		?= ideasX_
 TARGET_LIB ?= ideasX.a
 
 # which modules (subdirectories) of the project to include in compiling
-USER_MODULES		= user driver gdbstub log hal
+USER_MODULES		= user driver gdbstub log hal interface
 USER_INC				= include
 USER_LIB				=
 
@@ -44,7 +44,8 @@ USER_LIB				=
 LIB_MODULES			= mqtt
 
 SDK_LIBDIR = lib
-SDK_LIBS = c gcc phy pp net80211 wpa main lwip crypto ssl json driver
+SDK_LIBS = c gcc phy pp net80211 wpa main lwip crypto ssl json
+#SDK_LIBS = c gcc phy pp net80211 wpa main lwip crypto ssl json driver
 SDK_INC = include include/json
 
 
@@ -113,7 +114,7 @@ CFLAGS		= -g			\
 						-fdata-sections	\
 						-fno-builtin-printf\
 						-DICACHE_FLASH \
-						-DBUID_TIME=\"$(DATETIME)\"
+						-DBUILD_TIME=\"$(DATETIME)\"
 
 # linker flags used to generate the main object file
 LDFLAGS		= -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static

+ 156 - 708
driver/uart.c

@@ -1,54 +1,22 @@
-/*
- * ESPRSSIF 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:
+/******************************************************************************
+ * 2016 ideasX (Tyler Berezowsky)
  *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
+ * FileName: uart.c
  *
- * 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.
+ * Description: Two UART mode configration and interrupt handler.
+ *              Check your hardware connection while use this mode.
+ *              Future Work: Modify ISR to handle UART timeout and Errors
  *
- */
+ * Modification history:
+ *     2014/3/12, v1.0 create this file.
+*******************************************************************************/
 
-#include "ets_sys.h"
-#include "osapi.h"
 #include "driver/uart.h"
-#include "osapi.h"
-#include "driver/uart_register.h"
-#include "mem.h"
-#include "os_type.h"
-
 // UartDev is defined and initialized in rom code.
-extern UartDevice    UartDev;
-
-LOCAL struct UartBuffer* pTxBuffer = NULL;
-LOCAL struct UartBuffer* pRxBuffer = NULL;
-
-/*uart demo with a system task, to output what uart receives*/
-/*this is a example to process uart data from task,please change the priority to fit your application task if exists*/
-/*it might conflict with your task, if so,please arrange the priority of different task,  or combine it to a different event in the same task. */
-#define uart_recvTaskPrio        0
-#define uart_recvTaskQueueLen    10
-os_event_t    uart_recvTaskQueue[uart_recvTaskQueueLen];
-
-#define DBG  
-#define DBG1 uart1_sendStr_no_wait
-#define DBG2 os_printf
+extern UartDevice UartDev;
 
 
-LOCAL void uart0_rx_intr_handler(void *para);
+static void uart0_rx_intr_handler(void *para);
 
 /******************************************************************************
  * FunctionName : uart_config
@@ -58,56 +26,60 @@ LOCAL void uart0_rx_intr_handler(void *para);
  * Parameters   : uart_no, use UART0 or UART1 defined ahead
  * Returns      : NONE
 *******************************************************************************/
-LOCAL void ICACHE_FLASH_ATTR
+static void ICACHE_FLASH_ATTR
 uart_config(uint8 uart_no)
 {
-    if (uart_no == UART1){
-        PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
-    }else{
-        /* rcv_buff size if 0x100 */
-        ETS_UART_INTR_ATTACH(uart0_rx_intr_handler,  &(UartDev.rcv_buff));
-        PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
-        PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
-	#if UART_HW_RTS
-        PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);   //HW FLOW CONTROL RTS PIN
+    if (uart_no == UART1) {
+        PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);                 // setup gpio2 for UART1
+    }
+    else {
+        ETS_UART_INTR_ATTACH(uart0_rx_intr_handler,  &(UartDev.rcv_buff));      // attach uart0_rx_intr_handler ISR
+        PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);                                 // disable pullup on TX pin
+        PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);                    // setup gpio TX pin
+    /* setup hardware flow control */
+    #if UART_HW_RTS
+        PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);   //SETUP HW FLOW CONTROL RTS PIN
         #endif
-	#if UART_HW_CTS
-        PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_U0CTS);   //HW FLOW CONTROL CTS PIN
+    #if UART_HW_CTS
+        PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_U0CTS);   //SETUP HW FLOW CONTROL CTS PIN
         #endif
     }
-    uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));//SET BAUDRATE
-    
-    WRITE_PERI_REG(UART_CONF0(uart_no), ((UartDev.exist_parity & UART_PARITY_EN_M)  <<  UART_PARITY_EN_S) //SET BIT AND PARITY MODE
-                                                                        | ((UartDev.parity & UART_PARITY_M)  <<UART_PARITY_S )
-                                                                        | ((UartDev.stop_bits & UART_STOP_BIT_NUM) << UART_STOP_BIT_NUM_S)
-                                                                        | ((UartDev.data_bits & UART_BIT_NUM) << UART_BIT_NUM_S));
-    
+
+    uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));  // setup baudrate
+
+    WRITE_PERI_REG(UART_CONF0(uart_no), ((UartDev.exist_parity & UART_PARITY_EN_M)  <<  UART_PARITY_EN_S)   // setup parity
+                    | ((UartDev.parity & UART_PARITY_M)  <<UART_PARITY_S )
+                    | ((UartDev.stop_bits & UART_STOP_BIT_NUM) << UART_STOP_BIT_NUM_S)                      // setup stop bits
+                    | ((UartDev.data_bits & UART_BIT_NUM) << UART_BIT_NUM_S));                              // setup data bits
+
+
     //clear rx and tx fifo,not ready
-    SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);    //RESET FIFO
-    CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
-    
+    SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);      // set clear RX FIFO bits
+    CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);    // clear clear RX FIFO bits
+
+
     if (uart_no == UART0){
-        //set rx fifo trigger
         WRITE_PERI_REG(UART_CONF1(uart_no),
-        ((100 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
+        ((1 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |        // set UART0 full interrupt to 1 bytes
         #if UART_HW_RTS
-        ((110 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |
-        UART_RX_FLOW_EN |   //enbale rx flow control
+            ((110 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |          // set hardware flow control threshold
+            UART_RX_FLOW_EN |                                               // set enable hardware flow control flag
         #endif
-        (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |
-        UART_RX_TOUT_EN|
-        ((0x10 & UART_TXFIFO_EMPTY_THRHD)<<UART_TXFIFO_EMPTY_THRHD_S));//wjl 
+        (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |               // setup timeout interrupt
+        UART_RX_TOUT_EN|                                                    // setup enable timeout flag
+        ((0x10 & UART_TXFIFO_EMPTY_THRHD)<<UART_TXFIFO_EMPTY_THRHD_S));     // setup UART0 empty interrupt threshold to 16 bytes
+
         #if UART_HW_CTS
-        SET_PERI_REG_MASK( UART_CONF0(uart_no),UART_TX_FLOW_EN);  //add this sentense to add a tx flow control via MTCK( CTS )
+            SET_PERI_REG_MASK( UART_CONF0(uart_no),UART_TX_FLOW_EN);        //add this sentense to add a tx flow control via MTCK( CTS )
         #endif
-        SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA |UART_FRM_ERR_INT_ENA);
-    }else{
+        //SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UARben affleck in batman suitT_RXFIFO_TOUT_INT_ENA |UART_FRM_ERR_INT_ENA);     // enable interrupts for timeout and byte error
+    }
+    else{
         WRITE_PERI_REG(UART_CONF1(uart_no),((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S));//TrigLvl default val == 1
     }
-    //clear all interrupt
-    WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
-    //enable rx_interrupt
-    SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_OVF_INT_ENA);
+
+    WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);                          //clear all interrupts
+    SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);     // enable full RX FIFO interrupt
 }
 
 /******************************************************************************
@@ -117,16 +89,18 @@ uart_config(uint8 uart_no)
  * Parameters   : uint8 TxChar - character to tx
  * Returns      : OK
 *******************************************************************************/
- STATUS uart_tx_one_char(uint8 uart, uint8 TxChar)
+static STATUS ICACHE_FLASH_ATTR
+uart1_tx_one_char(uint8 TxChar)
 {
-    while (true){
-        uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
-        if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
-            break;
-        }
-    }
-    WRITE_PERI_REG(UART_FIFO(uart) , TxChar);
-    return OK;
+    while (true)    // dump FIFO until empty
+	{
+		uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(UART1)) & (UART_TXFIFO_CNT<<UART_TXFIFO_CNT_S);
+		if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
+			break;
+		}
+	}
+	WRITE_PERI_REG(UART_FIFO(UART1) , TxChar); // add character to FIFO
+	return OK;
 }
 
 /******************************************************************************
@@ -136,80 +110,18 @@ uart_config(uint8 uart_no)
  * Parameters   : char c - character to tx
  * Returns      : NONE
 *******************************************************************************/
-LOCAL void ICACHE_FLASH_ATTR
+static void ICACHE_FLASH_ATTR
 uart1_write_char(char c)
 {
-    if (c == '\n'){
-        uart_tx_one_char(UART1, '\r');
-        uart_tx_one_char(UART1, '\n');
-    }else if (c == '\r'){
-    
-    }else{
-        uart_tx_one_char(UART1, c);
-    }
-}
-
-//os_printf output to fifo or to the tx buffer
-LOCAL void ICACHE_FLASH_ATTR
-uart0_write_char_no_wait(char c)
-{
-#if UART_BUFF_EN    //send to uart0 fifo but do not wait 
-    uint8 chr;
-    if (c == '\n'){
-        chr = '\r';
-        tx_buff_enq(&chr, 1);
-        chr = '\n';
-        tx_buff_enq(&chr, 1);
-    }else if (c == '\r'){
-    
-    }else{
-        tx_buff_enq(&c,1);
-    }
-#else //send to uart tx buffer
-    if (c == '\n'){
-        uart_tx_one_char_no_wait(UART0, '\r');
-        uart_tx_one_char_no_wait(UART0, '\n');
-    }else if (c == '\r'){
-    
-    }
-    else{
-        uart_tx_one_char_no_wait(UART0, c);
-    }
-#endif
-}
-
-/******************************************************************************
- * FunctionName : uart0_tx_buffer
- * Description  : use uart0 to transfer buffer
- * Parameters   : uint8 *buf - point to send buffer
- *                uint16 len - buffer len
- * Returns      :
-*******************************************************************************/
-void ICACHE_FLASH_ATTR
-uart0_tx_buffer(uint8 *buf, uint16 len)
-{
-    uint16 i;
-    for (i = 0; i < len; i++)
-    {
-        uart_tx_one_char(UART0, buf[i]);
+    if (c == '\n') {
+        uart1_tx_one_char('\r');
+        uart1_tx_one_char('\n');
+    } else if (c == '\r') {
+    } else {
+        uart1_tx_one_char(c);
     }
 }
 
-/******************************************************************************
- * FunctionName : uart0_sendStr
- * Description  : use uart0 to transfer buffer
- * Parameters   : uint8 *buf - point to send buffer
- *                uint16 len - buffer len
- * Returns      :
-*******************************************************************************/
-void ICACHE_FLASH_ATTR
-uart0_sendStr(const char *str)
-{
-    while(*str){
-        uart_tx_one_char(UART0, *str++);
-    }
-}
-void at_port_print(const char *str) __attribute__((alias("uart0_sendStr")));
 /******************************************************************************
  * FunctionName : uart0_rx_intr_handler
  * Description  : Internal used function
@@ -217,580 +129,116 @@ void at_port_print(const char *str) __attribute__((alias("uart0_sendStr")));
  * Parameters   : void *para - point to ETS_UART_INTR_ATTACH's arg
  * Returns      : NONE
 *******************************************************************************/
-LOCAL void
-uart0_rx_intr_handler(void *para)
+extern void process_command(char* str);
+static void uart0_rx_intr_handler(void *para)
 {
     /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents
-    * uart1 and uart0 respectively
-    */
+     * uart1 and uart0 respectively
+     */
+    RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para;
     uint8 RcvChar;
-    uint8 uart_no = UART0;//UartDev.buff_uart_no;
-    uint8 fifo_len = 0;
-    uint8 buf_idx = 0;
-    uint8 temp,cnt;
-    //RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para;
-    
-    	/*ATTENTION:*/
-	/*IN NON-OS VERSION SDK, DO NOT USE "ICACHE_FLASH_ATTR" FUNCTIONS IN THE WHOLE HANDLER PROCESS*/
-	/*ALL THE FUNCTIONS CALLED IN INTERRUPT HANDLER MUST BE DECLARED IN RAM */
-	/*IF NOT , POST AN EVENT AND PROCESS IN SYSTEM TASK */
-    if(UART_FRM_ERR_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_FRM_ERR_INT_ST)){
-        DBG1("FRM_ERR\r\n");
-        WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_FRM_ERR_INT_CLR);
-    }else if(UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST)){
-        DBG("f");
-        uart_rx_intr_disable(UART0);
-        WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
-        system_os_post(uart_recvTaskPrio, 0, 0);
-    }else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)){
-        DBG("t");
-        uart_rx_intr_disable(UART0);
-        WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
-        system_os_post(uart_recvTaskPrio, 0, 0);
-    }else if(UART_TXFIFO_EMPTY_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_TXFIFO_EMPTY_INT_ST)){
-        DBG("e");
-	/* to output uart data from uart buffer directly in empty interrupt handler*/
-	/*instead of processing in system event, in order not to wait for current task/function to quit */
-	/*ATTENTION:*/
-	/*IN NON-OS VERSION SDK, DO NOT USE "ICACHE_FLASH_ATTR" FUNCTIONS IN THE WHOLE HANDLER PROCESS*/
-	/*ALL THE FUNCTIONS CALLED IN INTERRUPT HANDLER MUST BE DECLARED IN RAM */
-	CLEAR_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
-	#if UART_BUFF_EN
-		tx_start_uart_buffer(UART0);
-	#endif
-        //system_os_post(uart_recvTaskPrio, 1, 0);
-        WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_TXFIFO_EMPTY_INT_CLR);
-        
-    }else if(UART_RXFIFO_OVF_INT_ST  == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_OVF_INT_ST)){
-        WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_RXFIFO_OVF_INT_CLR);
-        DBG1("RX OVF!!\r\n");
-    }
 
-}
-
-/******************************************************************************
- * FunctionName : uart_init
- * Description  : user interface for init uart
- * Parameters   : UartBautRate uart0_br - uart0 bautrate
- *                UartBautRate uart1_br - uart1 bautrate
- * Returns      : NONE
-*******************************************************************************/
-#if UART_SELFTEST&UART_BUFF_EN
-os_timer_t buff_timer_t;
-void ICACHE_FLASH_ATTR
-uart_test_rx()
-{
-    uint8 uart_buf[128]={0};
-    uint16 len = 0;
-    len = rx_buff_deq(uart_buf, 128 );
-    tx_buff_enq(uart_buf,len);
-}
-#endif
-
-LOCAL void ICACHE_FLASH_ATTR ///////
-uart_recvTask(os_event_t *events)
-{
-    if(events->sig == 0){
-    #if  UART_BUFF_EN  
-        Uart_rx_buff_enq();
-    #else
-        uint8 fifo_len = (READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
-        uint8 d_tmp = 0;
-        uint8 idx=0;
-        for(idx=0;idx<fifo_len;idx++) {
-            d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
-            uart_tx_one_char(UART0, d_tmp);
+    // if uart0 fifo isn't full return
+    if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST)){
+
+    WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR); // clear full FIFO flag
+
+        while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
+            RcvChar = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
+
+            /* you can add your handle code below.*/
+
+    		uart_tx_one_char(RcvChar);    // echo recieved character
+            // NOTE: Lets modify the \r\n sequence to something more american.
+            // NOTE: Add hook for backspace character...I need to learn the ASCII code
+            // NOTE: Backspace character should also remove the last two stored characters
+            // in the buffer. This might be possible by moving the pWritePos back 2 characters?
+
+            // if "\r\n" recieved dump UART buffer into str
+            if (RcvChar == '\r') continue;
+    		if (RcvChar == '\n') {
+
+    			int len;
+    			char *str;
+    			len = pRxBuff->pWritePos - pRxBuff->pReadPos;
+    			if (len == 0) continue;
+    			if (len < 0) len += RX_BUFF_SIZE;
+    			str = (char*)os_zalloc(len+1);
+    			if (str) {
+    				uint8 loop;
+    				for (loop = 0; loop < len; loop++) str[loop] = uart0_rx_one_char();
+    				str[len] = '\0';
+    				process_command(str);
+    				os_free(str);
+    			}
+
+            }
+            // otherwise add character to buffer
+            else {
+    			*(pRxBuff->pWritePos) = RcvChar;
+    			pRxBuff->pWritePos++;
+    		}
+            // if we hit the end of the buffer, loop back to the beginning
+            if (pRxBuff->pWritePos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
+                // overflow ...we may need more error handle here.
+                pRxBuff->pWritePos = pRxBuff->pRcvMsgBuff ;
+            }
         }
-        WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
-        uart_rx_intr_enable(UART0);
-    #endif
-    }else if(events->sig == 1){
-    #if UART_BUFF_EN
-	 //already move uart buffer output to uart empty interrupt
-        //tx_start_uart_buffer(UART0);
-    #else 
-    
-    #endif
     }
 }
 
-void ICACHE_FLASH_ATTR
-uart_init(UartBautRate uart0_br, UartBautRate uart1_br)
-{
-    /*this is a example to process uart data from task,please change the priority to fit your application task if exists*/
-    system_os_task(uart_recvTask, uart_recvTaskPrio, uart_recvTaskQueue, uart_recvTaskQueueLen);  //demo with a task to process the uart data
-    
-    UartDev.baut_rate = uart0_br;
-    uart_config(UART0);
-    UartDev.baut_rate = uart1_br;
-    uart_config(UART1);
-    ETS_UART_INTR_ENABLE();
-    
-    #if UART_BUFF_EN
-    pTxBuffer = Uart_Buf_Init(UART_TX_BUFFER_SIZE);
-    pRxBuffer = Uart_Buf_Init(UART_RX_BUFFER_SIZE);
-    #endif
-
+ICACHE_FLASH_ATTR int uart0_rx_one_char() {
+  if(UartDev.rcv_buff.pReadPos == UartDev.rcv_buff.pWritePos) return -1;
+  int ret = *UartDev.rcv_buff.pReadPos;
+  UartDev.rcv_buff.pReadPos++;
+  if(UartDev.rcv_buff.pReadPos == (UartDev.rcv_buff.pRcvMsgBuff + RX_BUFF_SIZE)) {
+    UartDev.rcv_buff.pReadPos = UartDev.rcv_buff.pRcvMsgBuff;
+  }
 
-    /*option 1: use default print, output from uart0 , will wait some time if fifo is full */
-    //do nothing...
-
-    /*option 2: output from uart1,uart1 output will not wait , just for output debug info */
-    /*os_printf output uart data via uart1(GPIO2)*/
-    //os_install_putc1((void *)uart1_write_char);    //use this one to output debug information via uart1 //
-
-    /*option 3: output from uart0 will skip current byte if fifo is full now... */
-    /*see uart0_write_char_no_wait:you can output via a buffer or output directly */
-    /*os_printf output uart data via uart0 or uart buffer*/
-    //os_install_putc1((void *)uart0_write_char_no_wait);  //use this to print via uart0
-    
-    #if UART_SELFTEST&UART_BUFF_EN
-    os_timer_disarm(&buff_timer_t);
-    os_timer_setfn(&buff_timer_t, uart_test_rx , NULL);   //a demo to process the data in uart rx buffer
-    os_timer_arm(&buff_timer_t,10,1);
-    #endif
-}
-
-void ICACHE_FLASH_ATTR
-uart_reattach()
-{
-    uart_init(BIT_RATE_115200, BIT_RATE_115200);
+  return ret;
 }
 
 /******************************************************************************
- * FunctionName : uart_tx_one_char_no_wait
- * Description  : uart tx a single char without waiting for fifo 
- * Parameters   : uint8 uart - uart port
- *                uint8 TxChar - char to tx
- * Returns      : STATUS
-*******************************************************************************/
-STATUS uart_tx_one_char_no_wait(uint8 uart, uint8 TxChar)
-{
-    uint8 fifo_cnt = (( READ_PERI_REG(UART_STATUS(uart))>>UART_TXFIFO_CNT_S)& UART_TXFIFO_CNT);
-    if (fifo_cnt < 126) {
-        WRITE_PERI_REG(UART_FIFO(uart) , TxChar);
-    }
-    return OK;
-}
-
-STATUS uart0_tx_one_char_no_wait(uint8 TxChar)
-{
-    uint8 fifo_cnt = (( READ_PERI_REG(UART_STATUS(UART0))>>UART_TXFIFO_CNT_S)& UART_TXFIFO_CNT);
-    if (fifo_cnt < 126) {
-        WRITE_PERI_REG(UART_FIFO(UART0) , TxChar);
-    }
-    return OK;
-}
-
-
-/******************************************************************************
- * FunctionName : uart1_sendStr_no_wait
- * Description  : uart tx a string without waiting for every char, used for print debug info which can be lost
- * Parameters   : const char *str - string to be sent
- * Returns      : NONE
-*******************************************************************************/
-void uart1_sendStr_no_wait(const char *str)
-{
-    while(*str){
-        uart_tx_one_char_no_wait(UART1, *str++);
-    }
-}
-
-
-#if UART_BUFF_EN
-/******************************************************************************
- * FunctionName : Uart_Buf_Init
- * Description  : tx buffer enqueue: fill a first linked buffer 
- * Parameters   : char *pdata - data point  to be enqueue
- * Returns      : NONE
+ * FunctionName : uart0_tx_buffer
+ * Description  : use uart0 to transfer buffer
+ * Parameters   : uint8 *buf - point to send buffer
+ *                uint16 len - buffer len
+ * Returns      :
 *******************************************************************************/
-struct UartBuffer* ICACHE_FLASH_ATTR
-Uart_Buf_Init(uint32 buf_size)
-{
-    uint32 heap_size = system_get_free_heap_size();
-    if(heap_size <=buf_size){
-        DBG1("no buf for uart\n\r");
-        return NULL;
-    }else{
-        DBG("test heap size: %d\n\r",heap_size);
-        struct UartBuffer* pBuff = (struct UartBuffer* )os_malloc(sizeof(struct UartBuffer));
-        pBuff->UartBuffSize = buf_size;
-        pBuff->pUartBuff = (uint8*)os_malloc(pBuff->UartBuffSize);
-        pBuff->pInPos = pBuff->pUartBuff;
-        pBuff->pOutPos = pBuff->pUartBuff;
-        pBuff->Space = pBuff->UartBuffSize;
-        pBuff->BuffState = OK;
-        pBuff->nextBuff = NULL;
-        pBuff->TcpControl = RUN;
-        return pBuff;
-    }
-}
+// void ICACHE_FLASH_ATTR uart0_tx_buffer(uint8 *buf, uint16 len)
+// {
+//     uint16 i;
+//
+//     for (i = 0; i < len; i++) {
+//         uart_tx_one_char(buf[i]);
+//     }
+// }
 
-
-//copy uart buffer
-LOCAL void Uart_Buf_Cpy(struct UartBuffer* pCur, char* pdata , uint16 data_len)
-{
-    if(data_len == 0) return ;
-    
-    uint16 tail_len = pCur->pUartBuff + pCur->UartBuffSize - pCur->pInPos ;
-    if(tail_len >= data_len){  //do not need to loop back  the queue
-        os_memcpy(pCur->pInPos , pdata , data_len );
-        pCur->pInPos += ( data_len );
-        pCur->pInPos = (pCur->pUartBuff +  (pCur->pInPos - pCur->pUartBuff) % pCur->UartBuffSize );
-        pCur->Space -=data_len;
-    }else{
-        os_memcpy(pCur->pInPos, pdata, tail_len);
-        pCur->pInPos += ( tail_len );
-        pCur->pInPos = (pCur->pUartBuff +  (pCur->pInPos - pCur->pUartBuff) % pCur->UartBuffSize );
-        pCur->Space -=tail_len;
-        os_memcpy(pCur->pInPos, pdata+tail_len , data_len-tail_len);
-        pCur->pInPos += ( data_len-tail_len );
-        pCur->pInPos = (pCur->pUartBuff +  (pCur->pInPos - pCur->pUartBuff) % pCur->UartBuffSize );
-        pCur->Space -=( data_len-tail_len);
+void ICACHE_FLASH_ATTR uart0_send(const char *str) {
+	uint8 i = 0;
+	while (str[i]) {
+        uart_tx_one_char(str[i]);
+		i++;
     }
-    
 }
 
 /******************************************************************************
- * FunctionName : uart_buf_free
- * Description  : deinit of the tx buffer
- * Parameters   : struct UartBuffer* pTxBuff - tx buffer struct pointer
- * Returns      : NONE
-*******************************************************************************/
-void ICACHE_FLASH_ATTR
-uart_buf_free(struct UartBuffer* pBuff)
-{
-    os_free(pBuff->pUartBuff);
-    os_free(pBuff);
-}
-
-
-//rx buffer dequeue
-uint16 ICACHE_FLASH_ATTR
-rx_buff_deq(char* pdata, uint16 data_len )
-{
-    uint16 buf_len =  (pRxBuffer->UartBuffSize- pRxBuffer->Space);
-    uint16 tail_len = pRxBuffer->pUartBuff + pRxBuffer->UartBuffSize - pRxBuffer->pOutPos ;
-    uint16 len_tmp = 0;
-    len_tmp = ((data_len > buf_len)?buf_len:data_len);
-    if(pRxBuffer->pOutPos <= pRxBuffer->pInPos){
-        os_memcpy(pdata, pRxBuffer->pOutPos,len_tmp);
-        pRxBuffer->pOutPos+= len_tmp;
-        pRxBuffer->Space += len_tmp;
-    }else{
-        if(len_tmp>tail_len){
-            os_memcpy(pdata, pRxBuffer->pOutPos, tail_len);
-            pRxBuffer->pOutPos += tail_len;
-            pRxBuffer->pOutPos = (pRxBuffer->pUartBuff +  (pRxBuffer->pOutPos- pRxBuffer->pUartBuff) % pRxBuffer->UartBuffSize );
-            pRxBuffer->Space += tail_len;
-            
-            os_memcpy(pdata+tail_len , pRxBuffer->pOutPos, len_tmp-tail_len);
-            pRxBuffer->pOutPos+= ( len_tmp-tail_len );
-            pRxBuffer->pOutPos= (pRxBuffer->pUartBuff +  (pRxBuffer->pOutPos- pRxBuffer->pUartBuff) % pRxBuffer->UartBuffSize );
-            pRxBuffer->Space +=( len_tmp-tail_len);                
-        }else{
-            //os_printf("case 3 in rx deq\n\r");
-            os_memcpy(pdata, pRxBuffer->pOutPos, len_tmp);
-            pRxBuffer->pOutPos += len_tmp;
-            pRxBuffer->pOutPos = (pRxBuffer->pUartBuff +  (pRxBuffer->pOutPos- pRxBuffer->pUartBuff) % pRxBuffer->UartBuffSize );
-            pRxBuffer->Space += len_tmp;
-        }
-    }
-    if(pRxBuffer->Space >= UART_FIFO_LEN){
-        uart_rx_intr_enable(UART0);
-    }
-    return len_tmp; 
-}
-
-
-//move data from uart fifo to rx buffer
-void Uart_rx_buff_enq()
-{
-    uint8 fifo_len,buf_idx;
-    uint8 fifo_data;
-    #if 1
-    fifo_len = (READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT;
-    if(fifo_len >= pRxBuffer->Space){
-        os_printf("buf full!!!\n\r");            
-    }else{
-        buf_idx=0;
-        while(buf_idx < fifo_len){
-            buf_idx++;
-            fifo_data = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
-            *(pRxBuffer->pInPos++) = fifo_data;
-            if(pRxBuffer->pInPos == (pRxBuffer->pUartBuff + pRxBuffer->UartBuffSize)){
-                pRxBuffer->pInPos = pRxBuffer->pUartBuff;
-            }            
-        }
-        pRxBuffer->Space -= fifo_len ;
-        if(pRxBuffer->Space >= UART_FIFO_LEN){
-            //os_printf("after rx enq buf enough\n\r");
-            uart_rx_intr_enable(UART0);
-        }
-    }
-    #endif
-}
-
-
-//fill the uart tx buffer
-void ICACHE_FLASH_ATTR
-tx_buff_enq(char* pdata, uint16 data_len )
-{
-    CLEAR_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
-
-    if(pTxBuffer == NULL){
-        DBG1("\n\rnull, create buffer struct\n\r");
-        pTxBuffer = Uart_Buf_Init(UART_TX_BUFFER_SIZE);
-        if(pTxBuffer!= NULL){
-            Uart_Buf_Cpy(pTxBuffer ,  pdata,  data_len );
-        }else{
-            DBG1("uart tx MALLOC no buf \n\r");
-        }
-    }else{
-        if(data_len <= pTxBuffer->Space){
-        Uart_Buf_Cpy(pTxBuffer ,  pdata,  data_len);
-        }else{
-            DBG1("UART TX BUF FULL!!!!\n\r");
-        }
-    }
-    #if 0
-    if(pTxBuffer->Space <= URAT_TX_LOWER_SIZE){
-	    set_tcp_block();        
-    }
-    #endif
-    SET_PERI_REG_MASK(UART_CONF1(UART0), (UART_TX_EMPTY_THRESH_VAL & UART_TXFIFO_EMPTY_THRHD)<<UART_TXFIFO_EMPTY_THRHD_S);
-    SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
-}
-
-
-
-//--------------------------------
-LOCAL void tx_fifo_insert(struct UartBuffer* pTxBuff, uint8 data_len,  uint8 uart_no)
-{
-    uint8 i;
-    for(i = 0; i<data_len;i++){
-        WRITE_PERI_REG(UART_FIFO(uart_no) , *(pTxBuff->pOutPos++));
-        if(pTxBuff->pOutPos == (pTxBuff->pUartBuff + pTxBuff->UartBuffSize)){
-            pTxBuff->pOutPos = pTxBuff->pUartBuff;
-        }
-    }
-    pTxBuff->pOutPos = (pTxBuff->pUartBuff +  (pTxBuff->pOutPos - pTxBuff->pUartBuff) % pTxBuff->UartBuffSize );
-    pTxBuff->Space += data_len;
-}
-
-
-/******************************************************************************
- * FunctionName : tx_start_uart_buffer
- * Description  : get data from the tx buffer and fill the uart tx fifo, co-work with the uart fifo empty interrupt
- * Parameters   : uint8 uart_no - uart port num
+ * FunctionName : uart_init
+ * Description  : user interface for init uart
+ * Parameters   : UartBautRate uart0_br - uart0 bautrate
+ *                UartBautRate uart1_br - uart1 bautrate
  * Returns      : NONE
 *******************************************************************************/
-void tx_start_uart_buffer(uint8 uart_no)
-{
-    uint8 tx_fifo_len = (READ_PERI_REG(UART_STATUS(uart_no))>>UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT;
-    uint8 fifo_remain = UART_FIFO_LEN - tx_fifo_len ;
-    uint8 len_tmp;
-    uint16 tail_ptx_len,head_ptx_len,data_len;
-    //struct UartBuffer* pTxBuff = *get_buff_prt();
-    
-    if(pTxBuffer){      
-        data_len = (pTxBuffer->UartBuffSize - pTxBuffer->Space);
-        if(data_len > fifo_remain){
-            len_tmp = fifo_remain;
-            tx_fifo_insert( pTxBuffer,len_tmp,uart_no);
-            SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
-        }else{
-            len_tmp = data_len;
-            tx_fifo_insert( pTxBuffer,len_tmp,uart_no);
-        }
-    }else{
-        DBG1("pTxBuff null \n\r");
-    }
-}
-
-#endif
-
-
-void uart_rx_intr_disable(uint8 uart_no)
-{
-#if 1
-    CLEAR_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA);
-#else
-    ETS_UART_INTR_DISABLE();
-#endif
-}
-
-void uart_rx_intr_enable(uint8 uart_no)
-{
-#if 1
-    SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA);
-#else
-    ETS_UART_INTR_ENABLE();
-#endif
-}
-
-
-//========================================================
-LOCAL void
-uart0_write_char(char c)
-{
-    if (c == '\n') {
-        uart_tx_one_char(UART0, '\r');
-        uart_tx_one_char(UART0, '\n');
-    } else if (c == '\r') {
-    } else {
-        uart_tx_one_char(UART0, c);
-    }
-}
-
-void ICACHE_FLASH_ATTR
-UART_SetWordLength(uint8 uart_no, UartBitsNum4Char len) 
-{
-    SET_PERI_REG_BITS(UART_CONF0(uart_no),UART_BIT_NUM,len,UART_BIT_NUM_S);
-}
-
-void ICACHE_FLASH_ATTR
-UART_SetStopBits(uint8 uart_no, UartStopBitsNum bit_num) 
-{
-    SET_PERI_REG_BITS(UART_CONF0(uart_no),UART_STOP_BIT_NUM,bit_num,UART_STOP_BIT_NUM_S);
-}
-
-void ICACHE_FLASH_ATTR
-UART_SetLineInverse(uint8 uart_no, UART_LineLevelInverse inverse_mask) 
-{
-    CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_LINE_INV_MASK);
-    SET_PERI_REG_MASK(UART_CONF0(uart_no), inverse_mask);
-}
-
-void ICACHE_FLASH_ATTR
-UART_SetParity(uint8 uart_no, UartParityMode Parity_mode) 
-{
-    CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_PARITY |UART_PARITY_EN);
-    if(Parity_mode==NONE_BITS){
-    }else{
-        SET_PERI_REG_MASK(UART_CONF0(uart_no), Parity_mode|UART_PARITY_EN);
-    }
-}
-
 void ICACHE_FLASH_ATTR
-UART_SetBaudrate(uint8 uart_no,uint32 baud_rate)
-{
-    uart_div_modify(uart_no, UART_CLK_FREQ /baud_rate);
-}
-
-void ICACHE_FLASH_ATTR
-UART_SetFlowCtrl(uint8 uart_no,UART_HwFlowCtrl flow_ctrl,uint8 rx_thresh)
-{
-    if(flow_ctrl&USART_HardwareFlowControl_RTS){
-        PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);
-        SET_PERI_REG_BITS(UART_CONF1(uart_no),UART_RX_FLOW_THRHD,rx_thresh,UART_RX_FLOW_THRHD_S);
-        SET_PERI_REG_MASK(UART_CONF1(uart_no), UART_RX_FLOW_EN);
-    }else{
-        CLEAR_PERI_REG_MASK(UART_CONF1(uart_no), UART_RX_FLOW_EN);
-    }
-    if(flow_ctrl&USART_HardwareFlowControl_CTS){
-        PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_UART0_CTS);
-        SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_TX_FLOW_EN);
-    }else{
-        CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_TX_FLOW_EN);
-    }
-}
-
-void ICACHE_FLASH_ATTR
-UART_WaitTxFifoEmpty(uint8 uart_no , uint32 time_out_us) //do not use if tx flow control enabled
-{
-    uint32 t_s = system_get_time();
-    while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S)){
-		
-        if(( system_get_time() - t_s )> time_out_us){
-            break;
-        }
-	WRITE_PERI_REG(0X60000914, 0X73);//WTD
-
-    }
-}
-
-
-bool ICACHE_FLASH_ATTR
-UART_CheckOutputFinished(uint8 uart_no, uint32 time_out_us)
-{
-    uint32 t_start = system_get_time();
-    uint8 tx_fifo_len;
-    uint32 tx_buff_len;
-    while(1){
-        tx_fifo_len =( (READ_PERI_REG(UART_STATUS(uart_no))>>UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT);
-        if(pTxBuffer){
-            tx_buff_len = ((pTxBuffer->UartBuffSize)-(pTxBuffer->Space));
-        }else{
-            tx_buff_len = 0;
-        }
-		
-        if( tx_fifo_len==0 && tx_buff_len==0){
-            return TRUE;
-        }
-        if( system_get_time() - t_start > time_out_us){
-            return FALSE;
-        }
-	WRITE_PERI_REG(0X60000914, 0X73);//WTD
-    }    
-}
-
-
-void ICACHE_FLASH_ATTR
-UART_ResetFifo(uint8 uart_no)
-{
-    SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
-    CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
-}
-
-void ICACHE_FLASH_ATTR
-UART_ClearIntrStatus(uint8 uart_no,uint32 clr_mask)
-{
-    WRITE_PERI_REG(UART_INT_CLR(uart_no), clr_mask);
-}
-
-void ICACHE_FLASH_ATTR
-UART_SetIntrEna(uint8 uart_no,uint32 ena_mask)
-{
-    SET_PERI_REG_MASK(UART_INT_ENA(uart_no), ena_mask);
-}
-
-
-void ICACHE_FLASH_ATTR
-UART_SetPrintPort(uint8 uart_no)
-{
-    if(uart_no==1){
-        os_install_putc1(uart1_write_char);
-    }else{
-        /*option 1: do not wait if uart fifo is full,drop current character*/
-        os_install_putc1(uart0_write_char_no_wait);
-	/*option 2: wait for a while if uart fifo is full*/
-	os_install_putc1(uart0_write_char);
-    }
-}
-
-
-//========================================================
-
-
-/*test code*/
-void ICACHE_FLASH_ATTR
-uart_init_2(UartBautRate uart0_br, UartBautRate uart1_br)
-{
+uart_init(UartBautRate uart0_br, UartBautRate uart1_br, bool enable_uart1) {
     // rom use 74880 baut_rate, here reinitialize
     UartDev.baut_rate = uart0_br;
-    UartDev.exist_parity = STICK_PARITY_EN;
-    UartDev.parity = EVEN_BITS;
-    UartDev.stop_bits = ONE_STOP_BIT;
-    UartDev.data_bits = EIGHT_BITS;
-	
     uart_config(UART0);
-    UartDev.baut_rate = uart1_br;
-    uart_config(UART1);
-    ETS_UART_INTR_ENABLE();
-
-    // install uart1 putc callback
-    os_install_putc1((void *)uart1_write_char);//print output at UART1
-}
 
+    if (enable_uart1) {
+        UartDev.baut_rate = uart1_br;
+        uart_config(UART1);
+    }
 
+    ETS_UART_INTR_ENABLE();
+}

+ 42 - 158
include/driver/uart.h

@@ -1,49 +1,22 @@
-/*
- * ESPRSSIF 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 "driver/uart_register.h"
 #include "eagle_soc.h"
 #include "c_types.h"
-
-#define UART_TX_BUFFER_SIZE 256  //Ring buffer length of tx buffer
-#define UART_RX_BUFFER_SIZE 256 //Ring buffer length of rx buffer
-
-#define UART_BUFF_EN  0   //use uart buffer  , FOR UART0
-#define UART_SELFTEST  0  //set 1:enable the loop test demo for uart buffer, FOR UART0
-
-#define UART_HW_RTS   0   //set 1: enable uart hw flow control RTS, PIN MTDO, FOR UART0
-#define UART_HW_CTS  0    //set1: enable uart hw flow contrl CTS , PIN MTCK, FOR UART0
-
+#include "ets_sys.h"
+#include "osapi.h"
+#include <mem.h>
 
 
+#define RX_BUFF_SIZE        0x100
+#define TX_BUFF_SIZE        100
 
-#define UART0   0
-#define UART1   1
+#define UART_HW_RTS         0
+#define UART_HW_CTS         0
 
+#define UART0               0
+#define UART1               1
 
 typedef enum {
     FIVE_BITS = 0x0,
@@ -53,48 +26,32 @@ typedef enum {
 } UartBitsNum4Char;
 
 typedef enum {
-    ONE_STOP_BIT             = 0x1,
-    ONE_HALF_STOP_BIT        = 0x2,
-    TWO_STOP_BIT             = 0x3
+    ONE_STOP_BIT             = 0,
+    ONE_HALF_STOP_BIT        = BIT2,
+    TWO_STOP_BIT             = BIT2
 } UartStopBitsNum;
 
 typedef enum {
-    NONE_BITS = 0x2,
-    ODD_BITS   = 1,
-    EVEN_BITS = 0
+    NONE_BITS = 0,
+    ODD_BITS   = 0,
+    EVEN_BITS = BIT4
 } UartParityMode;
 
 typedef enum {
     STICK_PARITY_DIS   = 0,
-    STICK_PARITY_EN    = 1
+    STICK_PARITY_EN    = BIT3 | BIT5
 } UartExistParity;
 
 typedef enum {
-    UART_None_Inverse = 0x0,
-    UART_Rxd_Inverse = UART_RXD_INV,
-    UART_CTS_Inverse = UART_CTS_INV,
-    UART_Txd_Inverse = UART_TXD_INV,
-    UART_RTS_Inverse = UART_RTS_INV,
-} UART_LineLevelInverse;
-
-
-typedef enum {
-    BIT_RATE_300 = 300,
-    BIT_RATE_600 = 600,
-    BIT_RATE_1200 = 1200,
-    BIT_RATE_2400 = 2400,
-    BIT_RATE_4800 = 4800,
-    BIT_RATE_9600   = 9600,
-    BIT_RATE_19200  = 19200,
-    BIT_RATE_38400  = 38400,
-    BIT_RATE_57600  = 57600,
-    BIT_RATE_74880  = 74880,
+    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_460800 = 460800,
-    BIT_RATE_921600 = 921600,
-    BIT_RATE_1843200 = 1843200,
-    BIT_RATE_3686400 = 3686400,
+    BIT_RATE_921600 = 921600
 } UartBautRate;
 
 typedef enum {
@@ -103,13 +60,6 @@ typedef enum {
     XON_XOFF_CTRL
 } UartFlowCtrl;
 
-typedef enum {
-    USART_HardwareFlowControl_None = 0x0,
-    USART_HardwareFlowControl_RTS = 0x1,
-    USART_HardwareFlowControl_CTS = 0x2,
-    USART_HardwareFlowControl_CTS_RTS = 0x3
-} UART_HwFlowCtrl;
-
 typedef enum {
     EMPTY,
     UNDER_WRITE,
@@ -126,8 +76,8 @@ typedef struct {
 } RcvMsgBuff;
 
 typedef struct {
-    uint32   TrxBuffSize;
-    uint8   *pTrxBuff;
+    uint32      TrxBuffSize;
+    uint8      *pTrxBuff;
 } TrxMsgBuff;
 
 typedef enum {
@@ -139,91 +89,25 @@ typedef enum {
 } RcvMsgState;
 
 typedef struct {
-    UartBautRate 	     baut_rate;
-    UartBitsNum4Char  data_bits;
-    UartExistParity      exist_parity;
-    UartParityMode 	    parity;    
-    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
+    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);
+#define DISABLE_UART1   0
+#define ENABLE_UART1    1
 
+void uart_init(UartBautRate uart0_br, UartBautRate uart1_br, bool enable_uart1);
+int uart0_rx_one_char();
+void uart0_send(const char *buf);
+#define uart0_sendStr uart0_send
 
-///////////////////////////////////////
-#define UART_FIFO_LEN  128  //define the tx fifo length
-#define UART_TX_EMPTY_THRESH_VAL 0x10
-
-
- struct UartBuffer{
-    uint32     UartBuffSize;
-    uint8     *pUartBuff;
-    uint8     *pInPos;
-    uint8     *pOutPos;
-    STATUS  BuffState;
-    uint16    Space;  //remanent space of the buffer
-    uint8  TcpControl;
-    struct  UartBuffer     *  nextBuff;
-};
-
-struct UartRxBuff{
-    uint32     UartRxBuffSize;
-    uint8     *pUartRxBuff;
-    uint8     *pWritePos;
-    uint8     *pReadPos;
-    STATUS RxBuffState;
-    uint32    Space;  //remanent space of the buffer
-} ;
-
-typedef enum {
-    RUN = 0,
-    BLOCK = 1,
-} TCPState;
-
-//void ICACHE_FLASH_ATTR uart_test_rx();
-STATUS uart_tx_one_char(uint8 uart, uint8 TxChar);
-STATUS uart_tx_one_char_no_wait(uint8 uart, uint8 TxChar);
-void  uart1_sendStr_no_wait(const char *str);
-struct UartBuffer*  Uart_Buf_Init();
-
-
-#if UART_BUFF_EN
-LOCAL void  Uart_Buf_Cpy(struct UartBuffer* pCur, char* pdata , uint16 data_len);
-void  uart_buf_free(struct UartBuffer* pBuff);
-void  tx_buff_enq(char* pdata, uint16 data_len );
-LOCAL void  tx_fifo_insert(struct UartBuffer* pTxBuff, uint8 data_len,  uint8 uart_no);
-void  tx_start_uart_buffer(uint8 uart_no);
-uint16  rx_buff_deq(char* pdata, uint16 data_len );
-void  Uart_rx_buff_enq();
 #endif
-void  uart_rx_intr_enable(uint8 uart_no);
-void  uart_rx_intr_disable(uint8 uart_no);
-void uart0_tx_buffer(uint8 *buf, uint16 len);
-
-//==============================================
-#define FUNC_UART0_CTS 4
-#define FUNC_U0CTS                      4
-#define FUNC_U1TXD_BK                   2
-#define UART_LINE_INV_MASK  (0x3f<<19)
-void UART_SetWordLength(uint8 uart_no, UartBitsNum4Char len);
-void UART_SetStopBits(uint8 uart_no, UartStopBitsNum bit_num);
-void UART_SetLineInverse(uint8 uart_no, UART_LineLevelInverse inverse_mask);
-void UART_SetParity(uint8 uart_no, UartParityMode Parity_mode);
-void UART_SetBaudrate(uint8 uart_no,uint32 baud_rate);
-void UART_SetFlowCtrl(uint8 uart_no,UART_HwFlowCtrl flow_ctrl,uint8 rx_thresh);
-void UART_WaitTxFifoEmpty(uint8 uart_no , uint32 time_out_us); //do not use if tx flow control enabled
-void UART_ResetFifo(uint8 uart_no);
-void UART_ClearIntrStatus(uint8 uart_no,uint32 clr_mask);
-void UART_SetIntrEna(uint8 uart_no,uint32 ena_mask);
-void UART_SetPrintPort(uint8 uart_no);
-bool UART_CheckOutputFinished(uint8 uart_no, uint32 time_out_us);
-//==============================================
-
-#endif
-

+ 4 - 1
include/interface/uart_interface.h

@@ -2,9 +2,12 @@
 #define UART_INTERFACE_H
 
 #include "esp_common.h"
+#include "log/esp_log.h"
 
 void process_command(uint8_t *str);
 void uart0_rx_intr_handler_ideasX(void *para);
-
+#ifndef BUILD_TIME
+#define BUILD_TIME      "YYYYMMDD"
+#endif
 
 #endif

+ 113 - 30
interface/uart_interface.c

@@ -4,17 +4,32 @@
  * Date: 20170518
  */
 
-#include "uart_interface.h"
-//#include "uart.h"
+#include "interface/uart_interface.h"
 
-static void show_info(void)
+static const char* TAG = "uart_interface.c";
+
+static void show_WiFi_current(void)
+{
+    struct station_config stationConfig;
+    wifi_station_get_config(&stationConfig);
+    os_printf("SSID: %s\nBSSID: "MACSTR"\n", stationConfig.ssid, MAC2STR(stationConfig.bssid)); // fix MAX2STR error
+}
+
+static void show_WiFi(void)
 {
-     printf("SDK: v%s\n", system_get_sdk_version());
-     printf("Free Heap: %d\n", system_get_free_heap_size());
-     printf("CPU Frequency: %d MHz\n", system_get_cpu_freq());
-     printf("System Chip ID: 0x%x\n", system_get_chip_id());
-     printf("SPI Flash ID: 0x%x\n", spi_flash_get_id());
-     printf("SPI Flash Size: %d\n", (1 << ((spi_flash_get_id() >> 16) & 0xff)));
+    struct station_config stationConfig[5];
+    uint8_t num = wifi_station_get_ap_info(stationConfig);
+
+    os_printf("\n--------Stored Access Points---------\n");
+    uint32_t i;
+    for (i=0;i<num;i++)
+    {
+        os_printf("AP: %d/%d\n", i, (num-1));
+        os_printf("\tSSID: %s\n", stationConfig[i].ssid);
+        os_printf("\tPASSWORD: %s\n", stationConfig[i].password);
+        if (stationConfig[i].bssid_set)
+            os_printf("\tBSSID: "MACSTR"\n", MAC2STR(stationConfig[i].bssid));     // fix MAC2STR
+    }
 }
 
 static void show_IP(void)
@@ -22,40 +37,108 @@ static void show_IP(void)
  	struct ip_info ipconfig;
  	wifi_get_ip_info(STATION_IF, &ipconfig);
  	if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) {
- 		printf("IP: %d.%d.%d.%d, MASK: %d.%d.%d.%d, GW: %d.%d.%d.%d\n",
+ 		os_printf("IP: %d.%d.%d.%d, MASK: %d.%d.%d.%d, GW: %d.%d.%d.%d\n",
  			IP2STR(&ipconfig.ip), IP2STR(&ipconfig.netmask), IP2STR(&ipconfig.gw));
  	} else {
- 		printf("Network Status: %d\n", wifi_station_get_connect_status());
+ 		os_printf("Network Status: %d\n", wifi_station_get_connect_status());
  	}
 }
 
+static void select_ap(uint8_t* str)
+{
+    uint8_t* token;
+    uint8_t num;
+    token = strtok(str, "=");
+    token = strtok(NULL, ",");
+    num = atoi(token);
+    ESP_LOGD(TAG,"Setting to Station: %d\n", num);
+    if (wifi_station_ap_change(num))
+        os_printf("OK\n");
+    else
+        os_printf("FAIL\n");
+}
+
+static void add_ap(uint8_t* str)
+{
+    //strip command and seperate into SSID / password
+    uint8_t* token;
+    struct station_config stationConfig;
+    token = strtok(str, "=");  // disregard first field
+    token = strtok(NULL, ",");
+    if (token != NULL) // set ssid
+    {
+        ESP_LOGD(TAG, "\rSSID: %s\n", token);
+        os_sprintf(stationConfig.ssid, "%s", token);
+    }
+    token = strtok(NULL, ",");
+    if (token != NULL)  // set password field
+    {
+        ESP_LOGD(TAG, "Password: %s\n", token);
+        os_sprintf(stationConfig.password, "%s", token);
+
+    }
+    stationConfig.bssid_set = 0;
+    if (wifi_station_set_config(&stationConfig))
+        os_printf("OK\n");
+    else
+        os_printf("FAIL\n");
+}
+
+static void show_info(void)
+{
+    os_printf("\n--------System Information--------\n");
+    os_printf("SDK: v%s\n", system_get_sdk_version());
+    os_printf("Firmware Build Date:" BUILD_TIME"\n");
+    os_printf("Free Heap: %d\n", system_get_free_heap_size());
+    os_printf("CPU Frequency: %d MHz\n", system_get_cpu_freq());
+    os_printf("System Chip ID: 0x%x\n", system_get_chip_id());
+    os_printf("SPI Flash ID: 0x%x\n", spi_flash_get_id());
+    os_printf("SPI Flash Size: %d\n", (1 << ((spi_flash_get_id() >> 16) & 0xff)));
+    os_printf("--------Network Status------------\n");
+    show_IP();
+    show_WiFi_current();
+    // print broker information
+}
+
 void process_command(uint8_t *str)
 {
-	if (!strcmp(str, "help"))
-    {
-		printf("available commands\n");
-		printf("  help - display this message\n");
-		printf("  ip - show current ip\n");
-		printf("  wifi - show current and stored configurations\n");
-		printf("  setup - add new wifi AP\n");
-		printf("  restart - restart the esp8266\n");
-		printf("  switch - switch to the other rom and reboot\n");
-		printf("  info - show device info\n");
-		printf("  ota - force ota update, switch rom and reboot\n");
-		printf("  shutdown - force shutdown of module\n");
-		printf("\n");
-	}
-    else if (!strcmp(str, "restart"))
+    if (!strcmp(str, "AR+RST"))         // reset
     {
-		printf("Restarting...\n");
+        ESP_LOGI(TAG, "Manual Restarting...\n");
 		system_restart();
 	}
-    else if (!strcmp(str, "info"))
+    else if (!strcmp(str, "AT+SD"))     // shutdown
+    {
+        ESP_LOGI(TAG, "Manual Shutdown...\n");
+    }
+    else if (!strcmp(str, "AT+INFO"))   // print system information
     {
         show_info();
     }
-    else if (!strcmp(str, "ip"))
+    else if (!strcmp(str, "AT+WL"))     // list all stored APs
+    {
+        show_WiFi();
+    }
+    else if (!strncmp(str, "AT+WA", 5)) // add ap
+    {
+        add_ap(str);
+    }
+    else if (!strncmp(str, "AT+WS", 5)) // manually select ap
+    {
+        select_ap(str);
+    }
+    else if (!strcmp(str, "AT+WD"))     // delete stored AP
+    {
+        ESP_LOGI(TAG, "N/A\n");
+    }
+
+    else if (!strcmp(str, "AT+OTA"))    // manually force OTA update
+    {
+        ESP_LOGI(TAG, "N/A\n");
+
+    }
+    else if (!strcmp(str, "AT+BS"))     // store MQTT broker
     {
-		show_IP();
+        ESP_LOGI(TAG, "N/A\n");
     }
 }

+ 9 - 4
user/user_main.c

@@ -69,11 +69,11 @@ void wifi_handle_event_cb(System_Event_t *evt)
 
  void ICACHE_FLASH_ATTR app_init(void)
 {
-    uart_init(BIT_RATE_115200, BIT_RATE_115200);
+    uart_init(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1);
     ESP_LOGI(TAG, "Starting LSM6DS3 Demo\n");
+    ESP_LOGI(TAG, "WiFi Testing \n");
 
-
-    while(true)
+    //while(true)
     {
         LSM6DS3_Enable_I2C_Bridge(1);
         uint16_t x = max17043_getVoltage();
@@ -89,6 +89,9 @@ void wifi_handle_event_cb(System_Event_t *evt)
     }
 
 
+    {
+
+    }
 
   // wifi_set_opmode(STATION_MODE);
   // wifi_station_ap_number_set(MAX_APS);
@@ -108,5 +111,7 @@ void wifi_handle_event_cb(System_Event_t *evt)
 
 void user_init(void)
 {
-  system_init_done_cb(app_init);
+    wifi_set_opmode(STATION_MODE);
+    wifi_station_ap_number_set(5);
+    system_init_done_cb(app_init);
 }