I'm working with the Tiva Launchpad EK-TM4C123GXL and the ESP8266 WIFI module.
When this module gets a wifi packet, it sends it to the microcontroller through the UART port.
The format used by ESP8266 to send a packet (to the uC via UART) is:
+IPD,n:xxxxx\r\nOK\r\n
where:
n
is the length (in bytes) of the data packet:
indicates that the next byte will be the first data bytexxxxx
is the data packet\r\nOK\r\n
are 6 bytes and they are useless for me.
For example:
+IPD,5:hello\r\nOK\r\n
Here is my situation:
I'm working on an existing project, where I can't change these two things:
1- The UART module is already configured to generate an interrupt when its Receive FIFO (of 16 bytes) is half-full.
2- The ISR (Interrupt Service Routine) which handles this interrupt:
reads only one byte from the UARTDR (UART Data Register)
saves it into a variable
calls a function (called rx_data()) which will handle that byte.
Now, I have to write this function, called rx_data(), in C language.
So, the message coming form the ESP8266 module is passed to this function, rx_data(), one byte at a time, and this function must be able to:
identify the header
+IPD,
read the length
n
of the data packetsave the data packet
xxxxx
(which is located after the:
character and before the first\r
character) into a bufferdiscard the final bytes
\r\nOK\r\n
(these 6 bytes are useless for me, but, anyway, I must read them to remove them from the Receive FIFO)
I think to work step by step, so now I' m reasoning on:
how to identify the +IPD,
, considering that only one byte at a time is passed to this function?
rx_data
is called the byte is added to the buffer and the index (also a global var) is incremented. Do this until you have a full message or until you see/have the data you want, and clear the buffer and reset the index to start again. – DigitalNinja