Macros | Enumerations | Functions | Variables
serial_protocol.cpp File Reference
#include <async_comm/serial.h>
#include <cstdint>
#include <cstdio>
#include <chrono>
#include <condition_variable>
#include <mutex>
Include dependency graph for serial_protocol.cpp:

Go to the source code of this file.

Macros

#define BAUD_RATE   921600
 
#define CRC_LEN   1
 
#define NUM_MSGS   40000
 
#define NUM_START_BITS   1
 
#define NUM_STOP_BITS   1
 
#define PACKET_LEN   (START_BYTE_LEN + PAYLOAD_LEN + CRC_LEN)
 
#define PAYLOAD_LEN   12
 
#define START_BYTE   0xA5
 
#define START_BYTE_LEN   1
 

Enumerations

enum  ParseState { PARSE_STATE_IDLE, PARSE_STATE_GOT_START_BYTE, PARSE_STATE_GOT_PAYLOAD }
 States for the parser state machine. More...
 

Functions

void callback (const uint8_t *buf, size_t len)
 Callback function for the async_comm library. More...
 
int main (int argc, char **argv)
 
void pack_message (uint8_t *dst, uint32_t id, uint32_t v1, uint32_t v2)
 Pack message contents into a buffer. More...
 
void parse_byte (uint8_t byte)
 Passes a received byte through the parser state machine. More...
 
void unpack_payload (uint8_t *src, uint32_t *id, uint32_t *v1, uint32_t *v2)
 Unpack the contents of a message payload buffer. More...
 
uint8_t update_crc (uint8_t inCrc, uint8_t inData)
 Recursively update the cyclic redundancy check (CRC) More...
 

Variables

volatile bool all_messages_received = false
 flag for whether all messages have been received back More...
 
std::condition_variable condition_variable
 condition variable used to suspend main thread until all messages have been received back More...
 
std::mutex mutex
 mutex for synchronization between the main thread and callback thread More...
 
ParseState parse_state = PARSE_STATE_IDLE
 Current state of the parser state machine. More...
 
uint8_t receive_buffer [PAYLOAD_LEN]
 Buffer for accumulating received payload. More...
 
volatile int receive_count = 0
 Keeps track of how many valid messages have been received. More...
 
bool received [NUM_MSGS]
 Keeps track of which messages we've received back. More...
 

Detailed Description

Author
Daniel Koch danie.nosp@m.lpko.nosp@m.ch@gm.nosp@m.ail..nosp@m.com

This example implements a simple serial protocol, and tests the async_comm library using that protocol on a serial loopback (USB-to-UART converter with the RX and TX pins connected together).

The message defined by the serial protocol has the following format:

Field Type Size (bytes) Description
Start Byte 1 Identifies the beginning of a message, value is 0xA5
id uint32_t 4 Sequential message ID
v1 uint32_t 4 The first data field
v2 uint32_t 4 The second data field
CRC uint8_t 1 Cyclic redundancy check (CRC) byte

The "payload" of the message is the part that contains the actual data, and consists of the id, v1, and v2 fields.

The parser is implemented as a finite state machine.

Definition in file serial_protocol.cpp.

Macro Definition Documentation

#define BAUD_RATE   921600

Definition at line 77 of file serial_protocol.cpp.

#define CRC_LEN   1

Definition at line 73 of file serial_protocol.cpp.

#define NUM_MSGS   40000

Definition at line 66 of file serial_protocol.cpp.

#define NUM_START_BITS   1

Definition at line 78 of file serial_protocol.cpp.

#define NUM_STOP_BITS   1

Definition at line 79 of file serial_protocol.cpp.

#define PACKET_LEN   (START_BYTE_LEN + PAYLOAD_LEN + CRC_LEN)

Definition at line 74 of file serial_protocol.cpp.

#define PAYLOAD_LEN   12

Definition at line 72 of file serial_protocol.cpp.

#define START_BYTE   0xA5

Definition at line 69 of file serial_protocol.cpp.

#define START_BYTE_LEN   1

Definition at line 71 of file serial_protocol.cpp.

Enumeration Type Documentation

enum ParseState

States for the parser state machine.

Enumerator
PARSE_STATE_IDLE 
PARSE_STATE_GOT_START_BYTE 
PARSE_STATE_GOT_PAYLOAD 

Definition at line 162 of file serial_protocol.cpp.

Function Documentation

void callback ( const uint8_t *  buf,
size_t  len 
)

Callback function for the async_comm library.

Passes the received bytes through the parser state machine.

Parameters
bufReceived bytes buffer
lenNumber of bytes received

Definition at line 241 of file serial_protocol.cpp.

int main ( int  argc,
char **  argv 
)

Definition at line 250 of file serial_protocol.cpp.

void pack_message ( uint8_t *  dst,
uint32_t  id,
uint32_t  v1,
uint32_t  v2 
)

Pack message contents into a buffer.

Parameters
[out]dstBuffer in which to store the message
[in]idID field of the message
[in]v1First data field of the message
[in]v2Second data field of the message
Postcondition
The specified buffer contains the complete message packet, including start byte and CRC byte

Definition at line 125 of file serial_protocol.cpp.

void parse_byte ( uint8_t  byte)

Passes a received byte through the parser state machine.

Parameters
byteThe byte to process

Definition at line 184 of file serial_protocol.cpp.

void unpack_payload ( uint8_t *  src,
uint32_t *  id,
uint32_t *  v1,
uint32_t *  v2 
)

Unpack the contents of a message payload buffer.

Parameters
[in]srcThe buffer to unpack
[out]idID field of the message
[out]v1First data field of the message
[out]v2Second data field of the message
Precondition
Buffer contains a valid message payload
Postcondition
Payload contents have been placed into the specified variables

Definition at line 151 of file serial_protocol.cpp.

uint8_t update_crc ( uint8_t  inCrc,
uint8_t  inData 
)

Recursively update the cyclic redundancy check (CRC)

This uses the CRC-8-CCITT polynomial.

Source: http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html#gab27eaaef6d7fd096bd7d57bf3f9ba083

Parameters
inCrcThe current CRC value. This should be initialized to 0 before processing first byte.
inDataThe byte being processed
Returns
The new CRC value

Definition at line 93 of file serial_protocol.cpp.

Variable Documentation

volatile bool all_messages_received = false

flag for whether all messages have been received back

Definition at line 177 of file serial_protocol.cpp.

std::condition_variable condition_variable

condition variable used to suspend main thread until all messages have been received back

Definition at line 176 of file serial_protocol.cpp.

std::mutex mutex

mutex for synchronization between the main thread and callback thread

Definition at line 175 of file serial_protocol.cpp.

Current state of the parser state machine.

Definition at line 169 of file serial_protocol.cpp.

uint8_t receive_buffer[PAYLOAD_LEN]

Buffer for accumulating received payload.

Definition at line 170 of file serial_protocol.cpp.

volatile int receive_count = 0

Keeps track of how many valid messages have been received.

Definition at line 172 of file serial_protocol.cpp.

bool received[NUM_MSGS]

Keeps track of which messages we've received back.

Definition at line 173 of file serial_protocol.cpp.



async_comm
Author(s):
autogenerated on Fri May 14 2021 02:35:38