Speeduino
Loading...
Searching...
No Matches
Modules | Typedefs | Functions | Variables
Serial comms implementation
Collaboration diagram for Serial comms implementation:

Modules

 Serial return codes sent to TS
 
 Hard coded response for some TS messages
 

Typedefs

using crc_t = uint32_t
 

Functions

void processSerialCommand (void)
 Processes a message once it has been fully received.
 
void sendToothLog (void)
 Should be called when serialStatusFlag == SERIAL_TRANSMIT_TOOTH_INPROGRESS,.
 
void sendCompositeLog (void)
 Should be called when serialStatusFlag == LOG_SEND_COMPOSITE.
 
bool isRxTimeout (void)
 Has the current receive operation timed out?
 
void flushRXbuffer (void)
 Flush all remaining bytes from the rx serial buffer.
 
static uint32_t reverse_bytes (uint32_t i)
 Reverse the byte order of a uint32_t.
 
void writeByteReliableBlocking (byte value)
 
static void readSerialTimeout (char *buffer, size_t length)
 Read from the serial port into the supplied buffer.
 
template<typename TIntegral >
static TIntegral readSerialIntegralTimeout (void)
 Reads an integral type, timing out if necessary.
 
static uint32_t serialWrite (uint32_t value)
 Write a uint32_t to Serial.
 
static void serialWrite (uint16_t value)
 Write a uint16_t to Serial.
 
static uint16_t writeNonBlocking (const byte *buffer, size_t length)
 Send as much data as possible without blocking the caller.
 
static size_t writeNonBlocking (size_t start, uint32_t value)
 Write a uint32_t to Serial without blocking the caller.
 
static uint16_t sendBufferAndCrcNonBlocking (const byte *buffer, size_t start, size_t length)
 Send the buffer, followed by it's CRC.
 
static void sendSerialPayloadNonBlocking (uint16_t payloadLength)
 Start sending the shared serialPayload buffer.
 
static void sendReturnCodeMsg (byte returnCode)
 Send a message to TS containing only a return code.
 
static bool updatePageValues (uint8_t pageNum, uint16_t offset, const byte *buffer, uint16_t length)
 Update a pages contents from a buffer.
 
static void loadPageValuesToBuffer (uint8_t pageNum, uint16_t offset, byte *buffer, uint16_t length)
 Loads a pages contents into a buffer.
 
static void generateLiveValues (uint16_t offset, uint16_t packetLength)
 Send a status record back to tuning/logging SW. This will "live" information from currentStatus struct.
 
static void loadO2CalibrationChunk (uint16_t offset, uint16_t chunkSize)
 Update the oxygen sensor table from serialPayload.
 
static uint16_t toTemperature (byte lo, byte hi)
 Convert 2 bytes into an offset temperature in degrees Celsius.
 
static void processTemperatureCalibrationTableUpdate (uint16_t calibrationLength, uint8_t calibrationPage, uint16_t *values, uint16_t *bins)
 Update a temperature calibration table from serialPayload.
 
void serialReceive (void)
 The serial receive pump. Should be called whenever the serial port has data available to read.
 
void serialTransmit (void)
 The serial transmit pump. Should be called when serialStatusFlag indicates a transmit operation is in progress.
 

Variables

static constexpr uint8_t SEND_OUTPUT_CHANNELS = 48U
 Code for the "send output channels command".
 
static uint16_t serialBytesRxTx = 0
 The number of bytes received or transmitted to date during nonblocking I/O.
 
static constexpr uint16_t SERIAL_TIMEOUT = 400
 Timeout threshold in milliseconds.
 
uint32_t serialReceiveStartTime = 0
 The time in milliseconds at which the serial receive started. Used for calculating whether a timeout has occurred.
 
static FastCRC32 CRC32_serial
 Support accumulation of a CRC during non-blocking operations.
 
static FastCRC32 CRC32_calibration
 Support accumulation of a CRC during calibration loads. Must be a separate instance to CRC32_serial due to calibration data being sent in multiple packets.
 
static uint8_t serialPayload [SERIAL_BUFFER_SIZE]
 Serial payload buffer.
 
static uint16_t serialPayloadLength = 0
 How many bytes in serialPayload were received or sent.
 
StreampPrimarySerial
 

Detailed Description

Typedef Documentation

◆ crc_t

Function Documentation

◆ flushRXbuffer()

void flushRXbuffer ( void  )

Flush all remaining bytes from the rx serial buffer.

Here is the caller graph for this function:

◆ generateLiveValues()

static void generateLiveValues ( uint16_t  offset,
uint16_t  packetLength 
)
static

Send a status record back to tuning/logging SW. This will "live" information from currentStatus struct.

Parameters
offset- Start field number
packetLength- Length of actual message (after possible ack/confirm headers) E.g. tuning sw command 'A' (Send all values) will send data from field number 0, LOG_ENTRY_SIZE fields.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isRxTimeout()

bool isRxTimeout ( void  )

Has the current receive operation timed out?

Checks whether the current serial command should be timed out.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadO2CalibrationChunk()

static void loadO2CalibrationChunk ( uint16_t  offset,
uint16_t  chunkSize 
)
static

Update the oxygen sensor table from serialPayload.

Parameters
offsetOffset into serialPayload and the table
chunkSizeNumber of bytes available in serialPayload
Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadPageValuesToBuffer()

static void loadPageValuesToBuffer ( uint8_t  pageNum,
uint16_t  offset,
byte buffer,
uint16_t  length 
)
static

Loads a pages contents into a buffer.

Parameters
pageNumThe index of the page to update
offsetOffset into the page
bufferThe buffer to read from
lengthThe buffer length
Here is the call graph for this function:
Here is the caller graph for this function:

◆ processSerialCommand()

void processSerialCommand ( void  )

Processes a message once it has been fully received.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processTemperatureCalibrationTableUpdate()

static void processTemperatureCalibrationTableUpdate ( uint16_t  calibrationLength,
uint8_t  calibrationPage,
uint16_t values,
uint16_t bins 
)
static

Update a temperature calibration table from serialPayload.

Parameters
calibrationLengthThe chunk size received from TS
calibrationPageIndex of the table
valuesThe table values
binsThe table bin values
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readSerialIntegralTimeout()

template<typename TIntegral >
static TIntegral readSerialIntegralTimeout ( void  )
static

Reads an integral type, timing out if necessary.

Template Parameters
TIntegralThe integral type. E.g. uint16_t
Here is the call graph for this function:

◆ readSerialTimeout()

static void readSerialTimeout ( char buffer,
size_t  length 
)
static

Read from the serial port into the supplied buffer.

Attention
The buffer is filled in reverse, since TS comms is little-endian.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reverse_bytes()

static uint32_t reverse_bytes ( uint32_t  i)
static

Reverse the byte order of a uint32_t.

Attention
noinline is needed to prevent enlarging callers stack frame, which in turn throws off free ram reporting.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendBufferAndCrcNonBlocking()

static uint16_t sendBufferAndCrcNonBlocking ( const byte buffer,
size_t  start,
size_t  length 
)
static

Send the buffer, followed by it's CRC.

This is supposed to be called multiple times for the same buffer until it's all sent.

Parameters
bufferThe buffer
startIndex into the buffer to start sending at. [0, length)
lengthTotal size of the buffer
Returns
Cumulative total number of bytes written . I.e. the next start value
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendCompositeLog()

void sendCompositeLog ( void  )

Should be called when serialStatusFlag == LOG_SEND_COMPOSITE.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendReturnCodeMsg()

static void sendReturnCodeMsg ( byte  returnCode)
static

Send a message to TS containing only a return code.

This is used when TS asks for an action to happen (E.g. start a logger) or to signal an error condition to TS

Attention
This is a blocking operation
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendSerialPayloadNonBlocking()

static void sendSerialPayloadNonBlocking ( uint16_t  payloadLength)
static

Start sending the shared serialPayload buffer.

serialStatusFlag will be signal the result of the send:
serialStatusFlag == SERIAL_INACTIVE: send is complete
serialStatusFlag == SERIAL_TRANSMIT_INPROGRESS: partial send, subsequent calls to continueSerialTransmission will finish sending serialPayload

Parameters
payloadLengthHow many bytes to send [0, sizeof(serialPayload))
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendToothLog()

void sendToothLog ( void  )

Should be called when serialStatusFlag == SERIAL_TRANSMIT_TOOTH_INPROGRESS,.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ serialReceive()

void serialReceive ( void  )

The serial receive pump. Should be called whenever the serial port has data available to read.

Processes the incoming data on the serial buffer based on the command sent. Can be either data for a new command or a continuation of data for command that is already in progress:

Commands are single byte (letter symbol) commands.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ serialTransmit()

void serialTransmit ( void  )

The serial transmit pump. Should be called when serialStatusFlag indicates a transmit operation is in progress.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ serialWrite() [1/2]

static void serialWrite ( uint16_t  value)
static

Write a uint16_t to Serial.

Here is the call graph for this function:

◆ serialWrite() [2/2]

static uint32_t serialWrite ( uint32_t  value)
static

Write a uint32_t to Serial.

Returns
The value as transmitted on the wire
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toTemperature()

static uint16_t toTemperature ( byte  lo,
byte  hi 
)
static

Convert 2 bytes into an offset temperature in degrees Celsius.

Attention
Returned value will be offset CALIBRATION_TEMPERATURE_OFFSET
Here is the call graph for this function:
Here is the caller graph for this function:

◆ updatePageValues()

static bool updatePageValues ( uint8_t  pageNum,
uint16_t  offset,
const byte buffer,
uint16_t  length 
)
static

Update a pages contents from a buffer.

Parameters
pageNumThe index of the page to update
offsetOffset into the page
bufferThe buffer to read from
lengthThe buffer length
Returns
true if page updated successfully
false if page cannot be updated
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeByteReliableBlocking()

void writeByteReliableBlocking ( byte  value)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeNonBlocking() [1/2]

static uint16_t writeNonBlocking ( const byte buffer,
size_t  length 
)
static

Send as much data as possible without blocking the caller.

Returns
Number of bytes sent
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeNonBlocking() [2/2]

static size_t writeNonBlocking ( size_t  start,
uint32_t  value 
)
static

Write a uint32_t to Serial without blocking the caller.

Returns
Number of bytes sent
Here is the call graph for this function:

Variable Documentation

◆ CRC32_calibration

FastCRC32 CRC32_calibration
static

Support accumulation of a CRC during calibration loads. Must be a separate instance to CRC32_serial due to calibration data being sent in multiple packets.

◆ CRC32_serial

FastCRC32 CRC32_serial
static

Support accumulation of a CRC during non-blocking operations.

◆ pPrimarySerial

Stream* pPrimarySerial

◆ SEND_OUTPUT_CHANNELS

constexpr uint8_t SEND_OUTPUT_CHANNELS = 48U
staticconstexpr

Code for the "send output channels command".

◆ SERIAL_TIMEOUT

constexpr uint16_t SERIAL_TIMEOUT = 400
staticconstexpr

Timeout threshold in milliseconds.

◆ serialBytesRxTx

uint16_t serialBytesRxTx = 0
static

The number of bytes received or transmitted to date during nonblocking I/O.

Note
We can share one variable between rx & tx because we only support simplex serial comms. I.e. we can only be receiving or transmitting at any one time.

◆ serialPayload

uint8_t serialPayload[SERIAL_BUFFER_SIZE]
static

Serial payload buffer.

◆ serialPayloadLength

uint16_t serialPayloadLength = 0
static

How many bytes in serialPayload were received or sent.

◆ serialReceiveStartTime

uint32_t serialReceiveStartTime = 0

The time in milliseconds at which the serial receive started. Used for calculating whether a timeout has occurred.