Colway Solutions
Modbus Member Logo

Optimise MSPL-C

 

Design constrains change from one platform to another. While someone is constrained for Data Memory (RAM) space, someone else is short of Code (Program) Memory (ROM/Flash) while yet another is short of both. In order to accommodate MSPL within the design constraints of most users, we have provided mechanisms to save RAM, ROM or both. The following sub sections describe the steps involved in using each of these techniques.

2.7.1       Set optimal buffer sizes

The library uses memory buffers to store incoming Modbus packets before decoding them and to store response packets before transmitting them. The sizes of these two buffers can be controlled by limiting the maximum number of Modbus data items (i.e. coils, registers etc.) that a master can request in one Modbus transaction. For instance if a Modbus Master sends a read request for 100 registers in one packet, the resulting response packet size will be greater than 200 bytes in comparison to a read request for just 10 registers. You can configure the library to entertain requests that can fit into a specific buffer size by defining the following macros:

Macro Name

Location

Remarks

RX_BUFFER_SIZE

MSPL_UserIf.h

Limits the size of incoming packets. If the incoming request packet size cannot be accommodated in this buffer size, the library outputs an "Error" debugger message, discards the received packet and sends no response to the master.

TX_BUFFER_SIZE

MSPL_UserIf.h

Limits the size of outgoing packets.

Note: No check is made by the library to verify if a Modbus request results in a response packet whose size is larger than this size.

 

2.7.1.1       Modbus Block Size Macros

Modbus block size is the number of data items that a master zor operate upon in one Modbus transaction. The size of a Modbus packet is limited to 256 bytes for Modbus RTU and 260 bytes for Modbus TCP. This in effect itself limits the number of items that can be operated upon in one transaction as below:

Transaction

Max permissible block size

Read Coils, Read Discrete Inputs

2000 coils and Discrete Inputs respectively

Read Holding Registers, Read Input Registers

125 registers

Write Multiple Coils

1968 coils

Write Multiple Registers

123 registers

 

However, in order to receive and service Modbus transactions that stretch up to the above max permissible limits, a device needs a transmit and a receive buffer of 256 bytes (260 in case of Modbus TCP). This may not be available or necessary in small devices employing low end microcontrollers. MSPL provides a way of using a lower buffer sizes and a set of macros which can be used to filter out Modbus transactions that exceed a set limit for block size. They are:

·    RD_BLK_SIZE_BITINFO

·    WR_BLK_SIZE_BITINFO

·    RD_BLK_SIZE_REGINFO

·    WR_BLK_SIZE_REGINFO

These macros must be set along with RX_BUFFER_SIZE and TX_BUFFER_SIZE to optimize the use of memory.

 

2.7.2       Enable only the function codes you require

The code size occupied by the library can be minimized by including only the Modbus functions required in your application and excluding others. For instance, if your device has only digital inputs, there is no use of including support for Modbus function Read Holding Register. The library provides a set of macros using which you can selectively include or exclude Modbus functions.

 

2.7.2.1       How to use the Modbus function support macros

To include support for a Modbus function, set the macro to ‘1’, else set it to ‘0’.

Example:

#define INCLUDE_READ_COILS 1 // Adds code to support Read Coils function

#define INCLUDE_READ_COILS 0 // Excludes code that implements Read Coils

                     // function

 

2.7.2.2       How does the library respond to an unsupported function request

When the library receives a request for an unsupported Modbus function it responds with Modbus Exception code 0x01 (ILLEGAL FUNCTION).

 

2.7.2.3       List of supported macros

Macro Name

Modbus Function Affected

INCLUDE_READ_COILS

Read Coils (Function Code 0x01)

INCLUDE_READ_DISCRETE_IP

Read Discrete Inputs (Function Code 0x02)

INCLUDE_READ_INPUT_REGS

Read Input Registers (Function Code 0x04)

INCLUDE_READ_HOLDING_REGS

Read Holding Registers (Function Code 0x03)

INCLUDE_WRITE_COILS

Write Multiple Coils (Function Code 0x0F)

INCLUDE_WRITE_REGISTERS

Write Multiple registers (Function Code 0x10)

 

2.7.3       Reduce Code Memory size by excluding support for message counters

The library maintains counters for total Modbus messages received by it, total messages responded by it, total errors encountered and so on. This functionality is optional, not mandatory, as per the Modbus standard. So you may exclude this functionality by setting macro INCLUDE_MSG_CTRS to zero.

 

#define INCLUDE_MSG_CTRS 0 // Exclude message counters related code

 

2.7.4       Reduce Code Memory size by configuring CRC macros (Modbus RTU only)

The amount of Code Memory (sometimes called Program Memory) used by the library can be reduced using two technics.

Method 1: Move CRC tables into Data Memory (RAM)

Steps

a)      Open file MSPL_UserIf.h

b)      Locate the definition of macro CRC_TABLE_LOCATION

c)       Change its value to IN_RAM. The modified line should look like this:

#define CRC_TABLE_LOCATION      IN_RAM

d)      Rebuild your project. You should see a reduction in code size by approximately 512 bytes and a corresponding increase in RAM usage.

 

Description

Two tables of 256 constant values are used in computing CRC bytes. The location of these tables is configurable. The above steps cause the tables to be stored in data memory. This saves code memory at the expense of data memory by moving the tables into RAM. Since RAM is faster than ROM access, this method may also improve the efficiency of code execution.

Method 2: Eliminate CRC table storage by computing table contents dynamically

Steps

a)      Open file MSPL_UserIf.h

b)      Locate the definition of macro CRC_TABLE_LOCATION

c)       Change its value to CREATE_DYNAMIC. The modified line should look like this:

#define CRC_TABLE_LOCATION     CREATE_DYNAMIC

d)      Rebuild your project. You should see a reduction in code size by approximately 512 bytes without a proportional increase in RAM usage.

 

Description

This setting eliminates the two CRC tables altogether by computing the values of this table dynamically as and when required. This saves both code memory as well as data memory. However, since the CRC table contents are computed twice (once for verifying the CRC of received request ADU and again for computing the CRC for the response ADU) for every Modbus transaction, this method considerable increases the load on the CPU. This may lead to slower response times from the library.

 

2.7.5       Reduce Data Memory (RAM) size by configuring CRC macros

The amount of Data Memory (sometimes called as RAM) used by the library can be reduced using two techniques.

Method 1: Move CRC tables into Code Memory (ROM)

Steps

a)      Open file MSPL_UserIf.h

b)      Locate the definition of macro CRC_TABLE_LOCATION

c)       Change its value to IN_ROM. The modified line should look like this:

#define CRC_TABLE_LOCATION     IN_ROM

d)      Rebuild your project. You should see a reduction in RAM usage but an increase in the code size.

 

Description

Two tables of 256 constant values are used in computing CRC bytes. The location of these tables is configurable. The above steps cause the tables to be stored in code memory. This saves data memory (RAM) at the expense of code memory (ROM) by moving the tables into ROM.

 

Method 2: Eliminate CRC table storage by computing table contents dynamically

Steps

a)      Open file MSPL_UserIf.h

b)      Locate the definition of macro CRC_TABLE_LOCATION

c)       Change its value to CREATE_DYNAMIC. The modified line should look like this:

#define CRC_TABLE_LOCATION     CREATE_DYNAMIC

d)      Rebuild your project. You should see a reduction in code size by approximately 512 bytes without a proportional increase in RAM usage.

 

 

Back to Index

Privacy Policy Site Map FAQ Contact Us