Sitemap

Implement a CANopen Slave with the ES32

10 min readApr 25, 2025

CANopen is a communication protocol based on CAN that has been widely adopted in the industrial field. CAN provides reliable lower-layer data transmission, while CANopen offers a standardized application layer protocol. In the CANopen protocol, each CANopen device has an object dictionary. The CANopen device model is depicted in the image below. The object dictionary describes all the behaviors and parameters of the CANopen device.

CanFestival is an open-source and free CANopen protocol stack that is powerful and provides a graphical object dictionary editing tool. RT-Thread has packaged CanFestival, so you only need to complete the corresponding driver to implement CANopen. This article will introduce the method of implementing a CANopen slave on the ES32 platform, based on the RT-Thread BSP.

Before starting this experiment, readers should first understand the following points:

1. This experiment is based on RT-Thread version 4.0.4. Users can download it from either GitHub or Gitee:

● GitHub download for RT-Thread 4.0.4: https://github.com/RT-Thread/rt-thread/tree/v4.0.4

● Gitee download for RT-Thread 4.0.4: https://gitee.com/rtthread/rt-thread/tree/v4.0.4

2. A basic understanding of hardware and software environment configuration, as well as how to use ES-CodeMaker. For details, please refer to the article: Engineer’s Note | ES-CodeMaker for RT-Thread (Part One) Quick Start.(http://mp.weixin.qq.com/s?__biz=MzAwNDQ5MjM5OA==&mid=2247486048&idx=1&sn=a4189ae197c6215e74ced59242f17a47&chksm=9b2a5761ac5dde77790b9f73d38dd572c41ec0ca26ebd5f7a0993fe91af6c7dd97166855ef72&scene=21#wechat_redirect

Information on how to obtain the ES-CodeMaker for RT-Thread software will be provided at the end of the article.

Hardware Configuration

In this experiment, we will use the ZLG USBCAN-E-P as the master station, the ES-PDS-ES32F3696LX development board, and the ES-PDS-CAN module as the slave device.

CANopen Master Card

Insert the ES-PDS-CAN module into the CAN interface of the development board. The connections between the CAN module and the development board are as follows:

Connection between the USBCAN-E-P CANopen Master Card and the ES-PDS-CAN Module:

Jumper Settings on the ES-PDS-ES32F3696LX Development Board:Close JP4: This will supply power to the chip on the development board.

Close JP2 and JP3: This will convert UART2 to serial port through the onboard debugger, making it easier to debug the program.

Driver Configuration

  1. The driver configuration can be accomplished through ES-CodeMaker, which allows for visual pin function configuration. To create a new project, select the chip ES32F3696LX, enter the project name and path, and choose the template pkg-example-canopen.

2. Enable the UART2 function and assign the corresponding pins for use as the console functionality of RT-Thread.

3. Activate the CAN0 pins: PB8 and PB9, and select the appropriate CAN functionality. You can also set a name for the CAN device. The device configuration is as follows: baud rate 1,000,000, enable automatic retransmission on failure, and set the synchronization jump width to 4 time slots.

4. Enable AD16C4T0 as HWTIMER, and select the corresponding functionality, allowing you to set a name for the HWTIMER device. The device configuration is as follows: incremental counting mode.

RT-Thread Configuration

1. Use the RT-Thread env tool to configure the RT-Thread kernel, components, packages, and drivers.

2. Open the ENV tool in the path rt-thread-v4.0.4\bsp\essemi\es32f369x.

● Enter menuconfig to configure the project.

Navigate to RT-Thread Kernel -> Kernel Device Object to set up the kernel device objects. Change the console device name to “uart2”.

● RT-Thread package CanFestival configuration:

RT-Thread online packages → miscellaneous packages Enable the CanFestival package and select version v1.0.0. CanFestival is an open-source, free CANopen protocol stack.

RT-Thread online packages → miscellaneous packages → CanFestival: A free software CANopen framework Open the configuration interface. Select the device names for CAN and hwtimer, as well as the thread priority.

● UART Configuration

Hardware Drivers Config -> On-chip Peripheral Drivers -> UART Drivers Enable UART2 for communication with the RT-Thread console.

● CAN Configuration

Hardware Drivers Config -> On-chip Peripheral Drivers -> CAN Drivers Enable CAN0 to provide CAN driver support for CanFestival.

● TIMER Configuration

Hardware Drivers Config -> On-chip Peripheral Drivers -> HWTIMER Drivers Enable TIMER0 to provide timer driver support for CanFestival.

● Enable CANopen Example

Hardware Drivers Config -> Pkgs Support Example -> Canopen -> USBCAN_E_P_EXAMPLE_SLAVE Enable the CANopen slave example.

After configuring everything, save and exit.

3. Enter the command pkgs — update to update the software packages. Then, optimize some interfaces of the package according to the “Software Package Modification Instructions”.

4. Enter the command scons — target=mdk5 to generate a Keil5 project. (For other platforms like IAR, specify — target=xxx accordingly.)

5. Open the project in Keil5, compile, and download.

Experiment Instructions

1. Install the CANopen Object Dictionary Editing Tool

The core of the CANopen protocol is the object dictionary. Due to the complexity of the CANopen object dictionary, it is not recommended to write the object dictionary by hand. The installation method for the CANopen object dictionary editing tool is as follows:

☞ First, obtain the resources:

Get Python 2.7 from the official source:

[Python 2.7.15 Download](https://www.python.org/downloads/release/python-2715/)

Get wxPython 2.8 from the official source:

[wxPython 2.8.12.1 Download](https://sourceforge.net/projects/wxpython/files/wxPython/2.8.12.1/)

There are two ways to obtain the CANopen source code:

a. Get the CANopen source code from the official source:

[CANopen Source on Bitbucket](https://bitbucket.org/Mongo/canfestival-3-asc)

b. Get the CANopen source code from Baidu Pan:

[Baidu Pan Link](https://pan.baidu.com/s/1b9K0vIzu_EVWK1BLURJEPQ) Extraction code: j2w2

After successfully obtaining the resources:

a. Install Python 2.7.

b. Install wxPython 2.8.

c. Extract the CANopen source code to obtain the folder named `Mongo-canfestival-3-asc-1a25f5151a8d`.

d.Extract `Mongo-canfestival-3-asc-1a25f5151a8d\objdictgen\Gnosis_Utils-current.tar.gz` to get the file `Gnosis_Utils-1.2.2.tar`.

After extracting `Gnosis_Utils-1.2.2.tar`, you will get the folder named `Gnosis_Utils-1.2.2`.

e. Copy the `Gnosis_Utils-1.2.2\gnosis` folder to the path `Mongo-canfestival-3-asc-1a25f5151a8d\objdictgen`, as shown in the image below.

f. Double-click `Mongo-canfestival-…\objdictgen\objdictedit.py`.

If the installation is successful, the following interface will appear:

2. Usage of the CANopen Object Dictionary Editing Tool:

a. Click the “File” button in the top left corner to create a new object dictionary or open an existing one.

Open the object dictionary in the BSP.

After successfully opening the object dictionary, the following interface will be displayed:

b. Click on the options to edit the dictionary, as shown in the following image:

The description of the index ranges in the CANopen object dictionary is as follows:

The object dictionary index range from 0x2000 to 0x5FFF is reserved for manufacturer-specific protocol areas and is commonly used to store application data.

As shown in the following image, you can directly modify the data at object dictionary index 0x2100 using the editing tool.

c. The methods for outputting the CANopen object dictionary are as follows:

Clicking on 【建立字典】 will generate the .c and .h object dictionary files. If the currently open .od file is within the project, it will update the object dictionary in the project.

Clicking on 【导出成EDS文件】 will generate an .eds file. The CANopen master uses the .eds file to parse the slave.

4. CANopen Slave Program Flow.

In CANopen, PDO (Process Data Object) is used for exchanging real-time data between multiple nodes. PDOs are divided into RPDO (Receive PDO) and TPDO (Transmit PDO). RPDO is for receiving data by the slave, while TPDO is for sending data from the slave.

In the case of the slave object dictionary, there are 4 RPDOs and 4 TPDOs. In the figure below, the value of N ranges from 1 to 4.

As shown in the figure above, the application layer flow of the CANopen slave program is as follows:

a. Initialize the underlying drivers. Initialize CANopen and import the object dictionary.

b. After the slave enters the Pre-Operational state, continuously read and write the object dictionary.

Note: Due to the independent clocks of both parties, there may be discrepancies. Thus, the hardware timer currently in use is accelerated to ensure that the slave can promptly respond to the master.

5. Installation of the CANopen Master Card Environment.

In this experiment, the USBCAN-E-P CANopen master card is used as the CANopen master, and the development board serves as the CANopen slave.

☞ Obtain USBCAN-E-P information from the official website:

☆ Download the PC-side upper computer software installation package for USBCAN-E-P:

[https://www.zlg.cn/data/upload/software/Can/CANopen_Setup.rar](https://www.zlg.cn/data/upload/software/Can/CANopen_Setup.rar)

☆ Download the user manual for USBCAN-E-P:

[https://www.zlg.cn/data/upload/software/Can/USBCAN_E_P_CANOpen_um.pdf](https://www.zlg.cn/data/upload/software/Can/USBCAN_E_P_CANOpen_um.pdf)

☆ Download the drivers for USBCAN-E-P:

[https://www.zlg.cn/data/upload/software/Can/USBCAN_E_P_drive.zip](https://www.zlg.cn/data/upload/software/Can/USBCAN_E_P_drive.zip)

After completing the installation of the PC-side upper computer software, you will obtain the software CANManager for CANopen:

Then, according to the user manual, install the driver for USBCAN-E-P.

Experimental Phenomena

  1. After downloading the CANopen slave program, the RT-Thread console outputs the following information:

2. Then, open the CANManager for CANopen.

Import the EDS file of the CANopen slave.

Select the EDS file for the slave example. The file path is: `drivers\pkg_support_example\canopen\USBCAN-E-P-example-slave\app_od.eds`

Click the search button to start searching for the slave, as shown in the figure below:

Once the slave is found, click the close button in the lower right corner, as shown in the figure below:

3. Click the start button in the upper left corner and configure the settings as shown in the picture:

After starting, the following phenomenon can be observed: the master receives the data in the TPDO from the slave, with the data in the red box continuously cycling. The data in the blue box remains unchanged, representing the initial value in the dictionary. This is consistent with the program flowchart logic.

After clicking the manual send button for RPDO1, the last 4 bytes of data in TPDO1 match the last 4 bytes of data in RPDO1.

After modifying the last 4 bytes of data in RPDO2, clicking the manual send button for RPDO2 shows that the last 4 bytes of data in TPDO2 are consistent with those in RPDO2.

4. After completing the experiment, the RT-Thread console outputs the following information:

If the master and slave send data to the CAN bus almost simultaneously, there will be urgent messages indicating CAN communication overload.

received EMCY message. Node: 08  ErrorCode: 8110  ErrorRegister: 11

The error code descriptions for CANopen urgent messages are as follows:

Software Package Modification Instructions

1. CanFestival Package Modification (Version: v1.0.0)

This package requires 3 modifications:

Modification 1

Reason for Modification: The CAN filter control block configuration is incorrect.

a. The macro `RT_CAN_FILTER_ITEM_INIT` sets the filter table number to -1. If the filter table number is -1, it indicates that no filter table number is specified, and this filter control block will not be initialized. This will result in an invalid callback function. Therefore, the filter table number needs to be modified: change the filter table number to 0.

b. Adapt the RT-Thread driver framework. Change the filter table mode to 0.

Specific modifications are as follows: (Code Before Modification + Code After Modification)

Modify `packages\CanFestival-v1.0.0\src\can_rtthread.c`. The content of `can_rtthread.c` is as follows:

Before Modification:

struct rt_can_filter_item filter1item[1] =
{
RT_CAN_FILTER_ITEM_INIT(0x180, 0, 0, 1, 0, can1ind, &can_data.event)
};

After Modification:

struct rt_can_filter_item filter1item[1] =
{
{
.id = 0x180,
.ide = 0,
.rtr = 0,
.mode = 0,/*过滤表模式*/
.mask = 0,
.hdr = 0, /*过滤表号*/
.ind = can1ind,
.args = &can_data.event
}
};

Modification 2

Reason for Modification: There is a conflict in some symbol definitions. In the ES32 low-level library, `TRUE` and `FALSE` are defined as enumerations. In the package, `TRUE` and `FALSE` are defined as macros.

Specific Modification:

Add the following header file below `#define __def_h__` in `packages\CanFestival-v1.0.0\inc\def.h`:

#include "type.h"

Modification 3

Reason for Modification: Remove unnecessary files.

Specific Modification:

Remove the file `packages\CanFestival-v1.0.0\src\timer_rtthread.c`.

#include <stm32f10x.h>

--

--

RT-Thread IoT OS
RT-Thread IoT OS

Written by RT-Thread IoT OS

An Open-Source Community-Powered Real-Time Operating System (RTOS) Project! Let’s develop, DIY, create, share, and explore this new IoT World together!

No responses yet