I am setting up an UART protocol that allows the communication between 2 boards; A Master board and a Slave board. The communication between them is going to be like this:
Example 1 :
Master [Tx] — GET_VALUE–> [Rx] Slave
Master [Rx] <– 0x05 ——– [Tx] Slave
Example 2:
Master [Tx] — GET_STATUS ———–> [Rx] Slave
Master [Rx] <– I_AM_BUSY ———— [Tx] Slave
Example 3:
Master [Tx] — START_OPERATION –> [Rx] Slave
Master [Rx] <———-ACK————[Tx] Slave
Data size is 1 Byte for most of data flow between the two card. That's why I am going to use macros to define these commands:
Example:
#define GET_VALUE 0x05
#define START_OPERATION 0x06
etc ...
In addition to these 1-Byte-data, There is only one element that should be transmitted between the two boards that it's size is not 1 Byte. In fact, I am willing to use serialization to transmit a data structure over UART using a circular FIFO buffer
Example:
Master [Tx] — GET_STRUCTURE ————> [Rx] Slave
Master [Rx] <———-STRUCTURE————[Tx] Slave
typedef struct{
uint8_t var1;
uint8_t var2;
uint8_t var3;
}MyStructTypeDef;
Shall be transmitted over UART in this format "variable_name:valueXXX":
var1:123XXXvar2:456XXXvar3:789XXX
XXX is a separator
My question is: If 99% of data flow is 1 Byte sized and only 1% of the data flow is not 1 Byte sized, Should I use circular buffer only to get the structure? Is there a better way to standardize the length of data passed over uart?
Best Answer
Can the master busy-wait on the UART waiting for responses? Can it collect and process the received characters fast enough to avoid overruns? If not then you need a buffer.
If the longest message will fit in the buffer and you reset it when sending each command then it doesn't need to be circular.
I presume the 'XXX's pad the string out to a fixed length. It could be more efficient to create a variable length string with single character separators, or even send the numbers in raw binary, but if bandwidth isn't an issue then what you are doing is fine.
You might also want to think about what to do if the master and slave get out of sync. For example if the slave is sending a structure but the master is expecting a reply to a different command, some of the characters in the structure message might be misinterpreted as valid responses. Also this,
is troubling. Can the 'value' be any binary number? If so then how will you know it is not a response to some other command? Critical responses such as
I_AM_BUSY
andACK
should have unique values that cannot appear in other data.