ESP8266(果云科技的开发板源码)

@ESP8266(果云科技的开发板源码)TOC
最近调试果云科技的老板子ESP8266开发板,比较老了,调试串口输入很麻烦,因为API函数很麻烦,源码已上传,请下载。下面是代码:

/*

  • File : uart.c
  • Copyright © 2013 - 2016, Espressif Systems
  • This program is free software: you can redistribute it and/or modify
  • it under the terms of version 3 of the GNU General Public License as
  • published by the Free Software Foundation.
  • This program is distributed in the hope that it will be useful,
  • but WITHOUT ANY WARRANTY; without even the implied warranty of
  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  • GNU General Public License for more details.
  • You should have received a copy of the GNU General Public License along
  • with this program. If not, see http://www.gnu.org/licenses/.
    */
    #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”
    #include “user_interface.h”
    #include “c_types.h”
    #include “espconn.h”
    // UartDev is defined and initialized in rom code.
    extern UartDevice UartDev;

uint8 uartRxBuffer[256] = {0};

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

LOCAL void uart0_rx_intr_handler(void *para);

/******************************************************************************

  • FunctionName : uart_config

  • Description : Internal used function

  •            UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled
    
  •            UART1 just used for debug output
    
  • Parameters : uart_no, use UART0 or UART1 defined ahead

  • Returns : NONE
    ******************************************************************************/
    LOCAL 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
    #endif
    #if UART_HW_CTS
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_U0CTS); //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) < | ((UartDev.stop_bits & UART_STOP_BIT_NUM) << UART_STOP_BIT_NUM_S)
    | ((UartDev.data_bits & UART_BIT_NUM) << UART_BIT_NUM_S));

    //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);

    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) |
    #if UART_HW_RTS
    ((110 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) |
    UART_RX_FLOW_EN | //enbale rx flow control
    #endif
    (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S |
    UART_RX_TOUT_EN|
    ((0x10 & UART_TXFIFO_EMPTY_THRHD)< #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 )
    #endif
    SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA |UART_FRM_ERR_INT_ENA);
    }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);
    }

/******************************************************************************

  • FunctionName : uart1_tx_one_char
  • Description : Internal used function
  •            Use uart1 interface to transfer one char
    
  • Parameters : uint8 TxChar - character to tx
  • Returns : OK
    *******************************************************************************/
    STATUS uart_tx_one_char(uint8 uart, uint8 TxChar)
    {
    while (true){
    uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT< if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
    break;
    }
    }
    WRITE_PERI_REG(UART_FIFO(uart) , TxChar);
    return OK;
    }

/******************************************************************************

  • FunctionName : uart1_write_char

  • Description : Internal used function

  •            Do some special deal while tx char is '\r' or '\n'
    
  • Parameters : char c - character to tx

  • Returns : NONE
    *******************************************************************************/
    LOCAL 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]);
    }
    }

void ICACHE_FLASH_ATTR
uart0_tx_SendNum(uint32 num)
{
uint8 buf[10];
uint32 numTmp = num;
int8 i=0;
while(numTmp)
{
numTmp=numTmp/10;
i++;
}
buf[i–]=’\0’;
for(;i>=0;i–)
{
buf[i]=num%10 + ‘0’;
num/=10;
}
uart0_sendStr(buf);
}
/******************************************************************************

  • 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

  •            UART0 interrupt handler, add self handle code inside
    
  • Parameters : void *para - point to ETS_UART_INTR_ATTACH’s arg

  • Returns : NONE
    *******************************************************************************/
    LOCAL void
    uart0_rx_intr_handler(void *para)
    {
    int fifo_len;
    uart_rx_intr_disable(UART0);//关闭中断
    WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
    CLEAR_PERI_REG_MASK(UART_INT_ENA(UART0),UART_TXFIFO_EMPTY_INT_ENA);
    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 {
    d_tmp=READ_PERI_REG(UART_FIFO(UART0))&0xFF;
    uartRxBuffer[idx]=d_tmp;
    if(idx23||idx24)
    uart_tx_one_char(UART0,d_tmp);
    }
    /if((uartRxBuffer[23]==37)&&(uartRxBuffer[24]==30))
    pwm_duty+=2500;
    if(pwm_duty>=10000)
    pwm_duty=10000;
    if((uartRxBuffer[23]==37)&&(uartRxBuffer[24]==31))
    pwm_duty-=2500;
    if(pwm_duty<0)
    pwm_duty=0;
    if((uartRxBuffer[23]==37)&&(uartRxBuffer[24]==32))
    pwm_duty=5000;
    if((uartRxBuffer[23]==37)&&(uartRxBuffer[24]==33))
    pwm_duty=0;
    /
    WRITE_PERI_REG(UART_INT_CLR(UART0),UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
    uart_rx_intr_enable(UART0);
    }

/******************************************************************************

  • 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 esp_udp_remote_port4 = {192,168,4,2}; //本地IP
    uint8 uart_buf[128]={0};
    uint16 len = 0;
    len = rx_buff_deq(uart_buf, 128 );

    //---------任务创建实验添加的代码 判断收到的字节 给任务发消息
    if(uart_buf[0]==0x12)
    {
    system_os_post(1,1+0x30,2+0x30);
    //user_UDP_SendData(esp_udp_remote_port,8000,uart_buf,1);
    gpio16_output_set(0);//默认低电平
    tx_buff_enq(uart_buf,len);
    }
    if(uart_buf[0]==0x34)
    {
    system_os_post(2,8+0x30,8+0x30);
    //user_UDP_SendData(esp_udp_remote_port,8000,uart_buf,1);
    gpio16_output_set(1);//默认低电平
    tx_buff_enq(uart_buf,len);
    }
    // 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 d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
uart_tx_one_char(UART0, d_tmp);
}
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


/*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(115200, 115200);
}

/******************************************************************************

  • 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
    ***************************************************************************/
    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 = 0;
    return pBuff;
    }
    }

//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);
}

}

/******************************************************************************

  • 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)<

}

//--------------------------------
LOCAL void tx_fifo_insert(struct UartBuffer* pTxBuff, uint8 data_len, uint8 uart_no)
{
uint8 i;
for(i = 0; 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

  • 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==0x2){
}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&0x1){
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)
{
// 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

}

欢迎使用Markdown编辑器

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

新的改变

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 全新的界面设计 ,将会带来全新的写作体验;
  2. 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
  3. 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
  4. 全新的 KaTeX数学公式 语法;
  5. 增加了支持甘特图的mermaid语法1 功能;
  6. 增加了 多屏幕编辑 Markdown文章功能;
  7. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
  8. 增加了 检查列表 功能。

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G

合理的创建标题,有助于目录的生成

直接输入1次#,并按下space后,将生成1级标题。
输入2次#,并按下space后,将生成2级标题。
以此类推,我们支持6级标题。有助于使用TOC语法后生成一个完美的目录。

如何改变文本的样式

强调文本 强调文本

加粗文本 加粗文本

标记文本

删除文本

引用文本

H2O is是液体。

210 运算结果是 1024.

插入链接与图片

链接: link.

图片: Alt

带尺寸的图片: Alt

居中的图片: Alt

居中并且带尺寸的图片: Alt

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// An highlighted block
var foo = 'bar';

生成一个适合你的列表

  • 项目
    • 项目
      • 项目
  1. 项目1
  2. 项目2
  3. 项目3
  • 计划任务
  • 完成任务

创建一个表格

一个简单的表格是这么创建的:

项目 Value
电脑 $1600
手机 $12
导管 $1

设定内容居中、居左、居右

使用:---------:居中
使用:----------居左
使用----------:居右

第一列 第二列 第三列
第一列文本居中 第二列文本居右 第三列文本居左

SmartyPants

SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:

TYPE ASCII HTML
Single backticks 'Isn't this fun?' ‘Isn’t this fun?’
Quotes "Isn't this fun?" “Isn’t this fun?”
Dashes -- is en-dash, --- is em-dash – is en-dash, — is em-dash

创建一个自定义列表

Markdown
Text-to- HTML conversion tool
Authors
John
Luke

如何创建一个注脚

一个具有注脚的文本。2

注释也是必不可少的

Markdown将文本转换为 HTML

KaTeX数学公式

您可以使用渲染LaTeX数学表达式 KaTeX:

Gamma公式展示 Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ(n)=(n1)!nN 是通过欧拉积分

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=0tz1etdt.

你可以找到更多关于的信息 LaTeX 数学表达式here.

新的甘特图功能,丰富你的文章

Mon 06 Mon 13 Mon 20 已完成 进行中 计划一 计划二 现有任务 Adding GANTT diagram functionality to mermaid
  • 关于 甘特图 语法,参考 这儿,

UML 图表

可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图:

张三 李四 王五 你好!李四, 最近怎么样? 你最近怎么样,王五? 我很好,谢谢! 我很好,谢谢! 李四想了很长时间, 文字太长了 不适合放在一行. 打量着王五... 很好... 王五, 你怎么样? 张三 李四 王五

这将产生一个流程图。:

链接
长方形
圆角长方形
菱形
  • 关于 Mermaid 语法,参考 这儿,

FLowchart流程图

我们依旧会支持flowchart的流程图:

Created with Raphaël 2.2.0 开始 我的操作 确认? 结束 yes no
  • 关于 Flowchart流程图 语法,参考 这儿.

导出与导入

导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。


  1. mermaid语法说明 ↩︎

  2. 注脚的解释 ↩︎

你可能感兴趣的:(ESP8266(果云科技的开发板源码))