I'm writing a driver in C for a WiFi module attached to a PIC18 microcontroller. I want to implement some functions that I'm familiar with in computer application level programming like Window's API recv function for WinSock. The module itself already implements the WiFi communication protocol and the TCP/IP stack, and communicate with the PIC18 microcontroller through UART.
Is this the correct way to achieve this? Efficiency is very important here (It's an embedded system too).
#define RECEIVE_TIMEOUT 200
#define RECEIVE_RETRY_COUNT 4
UINT16 ReceiveData(void* _data, UINT16 length)
{
UINT16 received = 0;
UINT8 retry_count = 0;
while(received < length)
{
if(UART1_Data_Ready())
{
((BYTE*)_data)[received] = ReceiveByte();
received++;
continue;
}
if(retry_count == RECEIVE_RETRY_COUNT)
break;
retry_count++;
Delay_ms(RECEIVE_TIMEOUT/RECEIVE_RETRY_COUNT);
}
return received;
}
ReceiveByte()
? \$\endgroup\$UART1_Read()
. I don't know the specific of this function because it's from a library. \$\endgroup\$Delay_ms
implemented? nop loop, or asynchronous? \$\endgroup\$Delay_ms()
Is implemented as loop. \$\endgroup\$