Skip to main content
Firmware Stable

UART

This guide covers the physical, transport, and link layers for UART interfaces used by the YOS communication system (UMSG protocol). The physical layer defines baud rate and serial port mode. The transport layer determines the datagram byte ordering, redundancy checks and start/end of frame recognition mechanisms. The link layer defines the device addressing and service identification. The UMSG link layer omits the sender address due to the implicit 1:n topology. The construct can be used for the utility driver and common I/O API communication.

Physical layer configuration

The physical layer for UART interfaces is configured through cfg0-cfg3 of the msgconf command (interface number 1 for UMSG/UARTCOM, 2 for UMSG2, 5 for UMSG3/UARTEXT, 6 for UMSG4).

Baud rate (cfg0)

cfg0 sets the baud rate of the selected UART interface:

cfg0 valueBaud rate
81200 bps
92400 bps
104800 bps
119600 bps
1219200 bps
1338400 bps
1457600 bps
15115200 bps
16230400 bps
17460800 bps
18921600 bps

Mode and timing (cfg1, cfg2, cfg3)

cfg1, cfg2, and cfg3 configure the UART mode and timings. The default values are 0 which denotes 8 data bits, no parity, and 1 stop bit. Other values are reserved for further definition.

UART Frame Structure

ByteDescription
0session | 0x80
1(session | 0x80)^UMSG_VERSION
2Message length - 1
3Paylaod byte 0 (SID - link layer)
4Payload byte 1 (Receiver address - link layer)
5Payload byte 0
6Payload byte 1
7Payload byte 2
5 + nPayload byte n
Last byteCRC

CRC Calculation

CRC is calculated by XORing all bytes in the UART message, excluding the first two bytes.

crc = Byte2 ^ Byte3 ^ Byte4 ^ ... ^ Last_byte

Session increment

The sender must increment the session number each time a new datagram is composed. If the receiver gets multiple times a message with the same session, it is discarded.

UART Frame Creation Example (C Code)

/*!
\brief Create the UART frame (umsg)

\param[in] payload_len - Number of bytes to send
\param[in] payload - payload to send
0 byte of data = SID
1 byte of data = Receiver addr
2-end bytes = Payload
\param[in] output_buf - Output buffer
\param[in] output_buf_len - Number of bytes in output buffer

\return int32_t
0 - success
-1 - output (tx) buffer too small
*/

#define UMSG_VERSION 0x55

int32_t
umsg_compose_datagram(uint8_t payload_len, const uint8_t* payload, uint8_t* output_buf, uint8_t output_buf_len)
{
static uint8_t session = 0;

if (payload_len + 4 > output_buf_len)
return -1;

session |= 0x80;
*output_buf++ = session;
*output_buf++ = session ^ UMSG_VERSION;
*output_buf++ = payload_len - 1;

uint8_t crc = 0;
crc ^= payload_len - 1;

for (int i = 0; i < payload_len; i++) {
crc ^= *payload;
*output_buf++ = *payload++;
}

*output_buf++ = crc;
session++;

return 0;
}