-->
Page 1 of 1

Esp8266 does not clear rx buffer

PostPosted: Fri Jul 13, 2018 11:58 am
by morcillo
Hello,

I am using esp-open-rtos and I am communicating my esp8266 with an ST microcontroller board. I recently noticed that my esp8266 gets a CRC error (kepps receiving the same CRC for my very simple protocol) for about 2 to 10 minutes every few hours.

I have checked and I can see that the ST microcontroller is sending the correct data (it's not even sending the same CRC being received by the esp8266), and I have even checked it with a logic analyzer and I can clearly see that the protocol is being sent correctly by the ST but the esp8266 receives a CRC error, but it all fixes itself when I insert a breakpoint in the ST microcontroller and resume it again.

My protocol is startByte-CommandByte-SizeByte-Data(up to 150bytes)-CRCb1-CRCb2, and here is my code

static inline uart_numOfReceivedBytes(int uart_num) {
return FIELD2VAL(UART_STATUS_RXFIFO_COUNT, UART(uart_num).status);
}

bool receiveData(void) {
uint32_t timeStart = timer_get_count(FRC2) / 5000;
uint32_t timeNow = 0;
int numOfBytes = uart_numOfReceivedBytes(0);

int i;
bool received = false;

if(numOfBytes == 0) {
return false;
}

do{
if(numOfBytes > 2) {
for(i = 0; i < numOfBytes; i++)
g_rcvBuffer[i] = uart_getc(0);
received = true;
break;
} else {
numOfBytes = uart_numOfReceivedBytes(0);
}

timeNow = timer_get_count(FRC2) / 5000;
} while ((timeNow - timeStart) < 50);
do{
int leftToRead = g_rcvBuffer[2] - numOfBytes;
if(leftToRead == 0) {
received = true;
break;
}

if(leftToRead + CRC_LEN <= uart_numOfReceivedBytes(0)) {
for(i = 0; i < uart_numOfReceivedBytes(0); i++) {
g_rcvBuffer[numOfBytes+i] = uart_getc(0);
}
received = true;
}

timeNow = timer_get_count(FRC2) / 5000;
} while ((timeNow - timeStart) < 50 && received == false);

if(received == true) {
for(i = 0; i < 3; i++){
uint32_t timeStart = timer_get_count(FRC2)/5000;
while(timer_get_count(FRC2)/5000 - timeStart < 3);
//uart_putc(0, g_rcvBuffer[i]);
}
return isProtocolOk(&g_rcvBuffer[0]);
} else {
for(i = 0; i < numOfBytes; i++)
uart_getc(0);
}

return false;
}

This is called by a freeRTOS task, every second it entes the task in a non-blocking manner (the uart_numOfReceivedBytes function was written by me just to get the received byte count). Every time it does not receive a byte it exits the function but everytime it does get something it has 50ms to receive the first 3 bytes (length is the third byte) and then it checks how many bytes are left to read by comparing it to the length byte. If it was incapable of receiving the entire frame but has some bytes left to read, it reads them but just discards them.

Any ideas what could be wrong? g_rcvBuffer is cleared after the data received is used. Also when I decide to print the received protocol when a CRC error occurs I can see that it it keeps receiving the same packet, with the difference of the first few bytes.