由于 esp8266 uart 速度可以设置为 115200*40(即大约 4.5Mbps),因此我们可以设置这样的 uart 环回测试。
在 uart.c 中
本地空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);
}
uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));
WRITE_PERI_REG(UART_CONF0(uart_no), UartDev.exist_parity
| UartDev.parity
| (UartDev.stop_bits << UART_STOP_BIT_NUM_S)
| (UartDev.data_bits << 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);//|UART_LOOPBACK);/*if set UART_LOOPBACK BIT,the tx rx will be connected inside the chip*/
CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
//set rx fifo trigger and rx tout en , rx tout thresh
//WRITE_PERI_REG(UART_CONF1(uart_no), (UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S);
WRITE_PERI_REG(UART_CONF1(uart_no), UART_RX_TOUT_EN |
(0x10 &UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S );
//clear all interrupt
WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
//enable rx_interrupt : rx fifo full and rx tout
CLEAR_PERI_REG_MASK(UART_INT_ENA(uart_no), 0x1ff);//DISABLE FIFO_FULL INT
SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA);// UART_RXFIFO_FULL_INT_ENA |
}
状态 ICACHE_FLASH_ATTR
uart0_tx_one_char(uint8 TxChar) //add defini
tion in uart.h
{
while (true)
{
uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(UART0)) & (UART_TXFIFO_CNT<> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
break;
}
}
WRITE_PERI_REG(UART_FIFO(UART0) , TxChar);
return OK;
}
本地空ICACHE_FLASH_ATTR
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
*/
RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para;
uint8 RcvChar;
static uint32 t1=0,t2=0;
uint32 fifo_cnt=0;
if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST)) {
os_printf("full intrnr");
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
}else if(UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST)) {
//os_printf("uart rx time out nr");
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
RcvChar = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
if(RcvChar=='U'){
uart0_tx_one_char(RcvChar);
}else{
uart0_tx_one_char('T');
uart0_tx_one_char(RcvChar);
uart0_tx_one_char('T');
while(1){}
}
}
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
}
else{
//clear all interrupt
os_printf("other intr occurednr");
WRITE_PERI_REG(UART_INT_CLR(UART0), 0xffff);
}
}
无效ICACHE_FLASH_ATTR
uart_init(UartBautRate uart0_br, UartBautRate uart1_br)
{
// rom use 74880 baut_rate, here reinitialize
UartDev.baut_rate = uart0_br;
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);
}
在 user_main.c 中:
#include "osapi.h"
//#include "driver/uart.h"
os_timer_t uart_init_timer;
无效 loop_test_init()
{
uart_init(115200*40, 74880);
int i = 0;
while( (READ_PERI_REG(UART_STATUS(0)) & (UART_TXFIFO_CNT<