Modbus 1.0
Serial Modbus slave communication example on USART2 with CircleOS and FreeRTOS

Code size: Author:yrt
Source files included:yes Version:1.0
Use circleOS:yes (4.1) Creation date:2011-04-28 09:42:38
Hardware modification:no Modification date:2011-04-28 10:53:48
Based on the STM32 Primer:EvoPrimer
Downloads:2333 Views:31544
Average ratings:0/5 (0 rates)

Download: (501 KB)


 Modbus protocol on serial port with CircleOS and FreeRTOS 

Introduction :

The example uses USART2 on Primer extension port to communicate with a MODBUS host like a PC.
MODBUS is an application-layer messaging protocol, positioned at level 7 of the OSI model.
It provides client/server communication between devices connected on different  types of buses or networks.

The aim of this example is also :

  • to show how use the ST library within a CircleOS application (version 3.4.0),
  • to show how to build a CircleOS application with several sources files, thanks to a library application,
  • to show how to gather several applications within the same Ride project,
  • to show how to implement FreeRTOS on EvoPrimer (version 7.0.0),
  • to show how to implement SWV "printf like" traces.

An array of 10 halfwords periodically incremented can be read by the host.
Another array of 10 halfwords can be written and read by the host.
The application is waiting for host requests, and detects end of frame with a Time Inter Character equals to 3 characters (depending on the USART speed = 57600 bauds by default). Then it checks the request validity (CRC, length, type of request), and answers with the values requested. Only words read/write and echo are supported (functions 3, 4, 6, 8, 15 and 16).
CircleOS application (ModbusCircleLib and MobusCircle):  

The application is split into 2 Ride applications:

  • ModbusCircleLib : allows to build a library that can be load into the Primer with the CircleMgr tool or the "add_to_circle.bat" script.
  • ModbusCircle : allows to load and debug the application with the CircleOS.

 FreeRTOS application (ModebusFreeRTOS):  

3 FreeRTOS tasks are created :

  • 1 communication task for protocol management, called every 0.3 ms 
  • 1 acquisition task for incrementing some variables, called every second,
  • 1 Hmi task for displaying diagnostic counters, called every 100 ms.

A FreeRTOS mutex is used to protect the variables access between the communication and the acquisition tasks.
Some files are extracted from CircleOS in order to use LCD display API's.

  Notes :  

  • The application has been developped on Open4/EvoPrimer  STM32E but is source compatible with Primer 2  and other Open4/EvoPrimer STM32 platforms, with little changes. 
  • The "Evo_Circle_STM32E.elf" and "FAT_OP4.elf" files location depend on your Ride installation; delete them from the projet and add it back before building the project.
  • This example also uses the SWV functionality for debugging received requests.
    Check that SWO is enabled in "RLink Configuration/Advanced Options/SWO configuration"  and CPU clock equals 72000 KHz.
    Please refer to the Ride7 ARM Kit documentation for how to configure  the SWV debug functionnality at the Ride side.
  • For more information about the Modbus protocol see :
    For a free Modbus host program on PC see :
  • For more information about FreeRTOS see :

Version V1.0

  •  First release.