Modbus Vault v1.0
Loading...
Searching...
No Matches
modbus_slicer.c File Reference

Implementation of the modbus slicer. More...

#include "modbus_slicer.h"
#include <string.h>
Include dependency graph for modbus_slicer.c:

Macros

#define MODBUS_SLICER_USECONDS_PER_SECOND   (1000000UL)
 
#define MODBUS_SLICER_INT_MULTIPLY_BY_3_5(num)   ((7 * num) / 2)
 
#define MODBUS_SLICER_INT_CALCULATE_20_PERCENT(num)   ((num) / 5)
 

Functions

void modbus_slicer_init (modbus_slicer_t *slicer_ptr, modbus_slicer_config_t *config_ptr)
 Initialize slicer.
 
void modbus_slicer_feed (modbus_slicer_t *slicer_ptr, const uint8_t *data_ptr, size_t length, int64_t timestamp_us)
 Feed data to slicer state-machine.
 
void modbus_slicer_timeout (modbus_slicer_t *slicer_ptr)
 Signal frame timeout.
 
void modbus_slicer_check_timeout (modbus_slicer_t *slicer_ptr)
 Check for frame timeout.
 

Detailed Description

Implementation of the modbus slicer.

  • Pre-calculates 3.5 characters timeout
  • Uses "slab pool" for buffering frames and passes it to consumers
  • Copies arriving bytes to buffer
  • Calls registered callback function on data/error

Macro Definition Documentation

◆ MODBUS_SLICER_INT_CALCULATE_20_PERCENT

#define MODBUS_SLICER_INT_CALCULATE_20_PERCENT (   num)    ((num) / 5)

Multiply a num by 20% or (1/5)

◆ MODBUS_SLICER_INT_MULTIPLY_BY_3_5

#define MODBUS_SLICER_INT_MULTIPLY_BY_3_5 (   num)    ((7 * num) / 2)

Multiply a num by 3.5

◆ MODBUS_SLICER_USECONDS_PER_SECOND

#define MODBUS_SLICER_USECONDS_PER_SECOND   (1000000UL)

Micro-seconds in one second

Function Documentation

◆ modbus_slicer_check_timeout()

void modbus_slicer_check_timeout ( modbus_slicer_t slicer_ptr)

Check for frame timeout.

Check for frame timeout by comparing last received byte to current time and timeout if it exceeds 3.5 characters time

Parameters
slicer_ptrPointer to slicer structure
Note
Frame boundary primary determined using timing from rs485 driver layer as it's timeout threshold is set approximately 3.5 characters time
Here is the call graph for this function:
Here is the caller graph for this function:

◆ modbus_slicer_feed()

void modbus_slicer_feed ( modbus_slicer_t slicer_ptr,
const uint8_t *  data_ptr,
size_t  length,
int64_t  timestamp_us 
)

Feed data to slicer state-machine.

Parameters
slicer_ptrPointer to slicer structure
data_ptrPointer to data buffer
lengthData buffer length
timestamp_usData timestamp
Note
A slab gets allocated here and if passed to callback function successfully it's the consumer responsibility to free it otherwise it gets freed later in the same component
Emits overflow when data is coming but buffer is exhausted
Emits no memory when there is no available buffers
Here is the call graph for this function:
Here is the caller graph for this function:

◆ modbus_slicer_init()

void modbus_slicer_init ( modbus_slicer_t slicer_ptr,
modbus_slicer_config_t config_ptr 
)

Initialize slicer.

Initialize slicer by calculating 3.5 characters time using standard frame length 11-bits and baud rate used by rs485 driver

Parameters
slicer_ptrPointer to slicer structure
config_ptrPointer to slicer configuration structure
Note
Assumes standard frame length of 11-bits
Added 20% to 3.5 characters duration for timing practicality
Here is the caller graph for this function:

◆ modbus_slicer_timeout()

void modbus_slicer_timeout ( modbus_slicer_t slicer_ptr)

Signal frame timeout.

Slice the frame since 3.5 characters time has passed and call registered callback function

Parameters
slicer_ptrPointer to slicer structure
Note
Intercharacter timing is not implemented here since current setup doesn't allow for 1.5 characters timeout
Frames are dropped if there is no callback registered
Callback is triggered if there is data present otherwise it is called when errors happen and slab is reused if present
Here is the caller graph for this function: