This code example demonstrates the implementation of an EtherCAT slave node using the Beckhoff Slave Stack Code (SSC) Tool to generate the slave stack code for XMC4300 Relax EtherCAT Kit. Alternatively, XMC4800 IoT Connectivity Kit is also supported for this code example.
Provide feedback on this code example.
- ModusToolbox™ v3.5 or later (tested with v3.5)
- Board support package (BSP) minimum required version: 1.0.0
- SEGGER J-Link software
- SSC Tool v5.13
- TwinCAT 3.1 (tested with v3.1.4024)
- Programming language: C
- Associated parts: XMC4300 MCU
- GNU Arm® Embedded Compiler v11.3.1 (
GCC_ARM) – Default value ofTOOLCHAIN
- XMC4300 Relax EtherCAT Kit (
KIT_XMC43_RLX_ECAT_V2.1) – Default value ofTARGET - XMC4800 IoT Connectivity Kit (
KIT_XMC48_IOT_AWS_WIFI)
Connect the Micro-USB cable to debug connector (X101) and a LAN cable from your master device to the IN port (X250) of the respective kit as shown in Figure 1. See the kit user guide to ensure that the board is configured correctly.
Figure 1. Kit setup
See the ModusToolbox™ tools package installation guide for information about installing and configuring the tools package. Tools package v3.5 is needed to run this code example.
This code example uses ModusToolbox™ Device Configurator's EtherCAT personality, which is pre-configured in the project. To check the details on how to use the EtherCAT personality for custom applications, see the "HW Configuration in Device Configurator" section of the EtherCAT API reference manual.
For TwinCAT 3.1, please download the extended Automation Engineering (XAE) tool first, then also download TwinCAT 3.1 Package Manager from Beckhoff's webpage.
The ModusToolbox™ tools package provides the Project Creator as both a GUI tool and a command line tool.
Use Project Creator GUI
-
Open the Project Creator GUI tool
There are several ways to do this, including launching it from the dashboard or from inside the Eclipse IDE. For more details, see the Project Creator user guide (locally available at {ModusToolbox™ install directory}/tools_{version}/project-creator/docs/project-creator.pdf)
-
On the Choose Board Support Package (BSP) page, select the BSP of "KIT_XMC43_RELAX_ECAT_V1". See Supported kits
Note: To use this code example for a kit not listed here, you may need to update the source files. If the kit does not have the required resources, the application may not work
-
On the Select Application page:
a. Select the Applications(s) Root Path and the Target IDE
Note: Depending on how you open the Project Creator tool, these fields may be pre-selected for you
b. Select this code example from the list by enabling its check box
Note: You can narrow the list of displayed examples by typing in the filter box
c. (Optional) Change the suggested New Application Name and New BSP Name
d. Click Create to complete the application creation process
Use Project Creator CLI
The 'project-creator-cli' tool can be used to create applications from a CLI terminal or from within batch files or shell scripts. This tool is available in the {ModusToolbox™ install directory}/tools_{version}/project-creator/ directory.
Use a CLI terminal to invoke the 'project-creator-cli' tool. On Windows, use the command-line 'modus-shell' program provided in the ModusToolbox™ installation instead of a standard Windows command-line application. This shell provides access to all ModusToolbox™ tools. You can access it by typing "modus-shell" in the search box in the Windows menu. In Linux and macOS, you can use any terminal application.
The following example clones the "XMC4300 EtherCAT SSC" application with the desired name "Xmc4300EtherCatSSC" configured for the KIT_XMC43_RLX_ECAT_V2.1 BSP into the specified working directory, C:/mtb_projects:
project-creator-cli --board-id KIT_XMC43_RLX_ECAT_V2.1 --app-id mtb-example-xmc43-ethcat-ssc --user-app-name Xmc4300EtherCatSSC --target-dir "C:/mtb_projects"
The 'project-creator-cli' tool has the following arguments:
| Argument | Description | Required/optional |
|---|---|---|
--board-id |
Defined in the field of the BSP manifest | Required |
--app-id |
Defined in the field of the CE manifest | Required |
--target-dir |
Specify the directory in which the application is to be created if you prefer not to use the default current working directory | Optional |
--user-app-name |
Specify the name of the application if you prefer to have a name other than the example's default name | Optional |
Note: The project-creator-cli tool uses the
git cloneandmake getlibscommands to fetch the repository and import the required libraries. For details, see the "Project creator tools" section of the ModusToolbox™ tools package user guide (locally available at {ModusToolbox™ install directory}/docs_{version}/mtb_user_guide.pdf).
After the project has been created, you can open it in your preferred development environment.
Eclipse IDE
If you opened the Project Creator tool from the included Eclipse IDE, the project will open in Eclipse automatically.
For more details, see the Eclipse IDE for ModusToolbox™ user guide (locally available at {ModusToolbox™ install directory}/docs_{version}/mt_ide_user_guide.pdf).
Visual Studio (VS) Code
Launch VS Code manually, and then open the generated {project-name}.code-workspace file located in the project directory.
For more details, see the Visual Studio Code for ModusToolbox™ user guide (locally available at {ModusToolbox™ install directory}/docs_{version}/mt_vscode_user_guide.pdf).
Command line
If you prefer to use the CLI, open the appropriate terminal, and navigate to the project directory. On Windows, use the command-line 'modus-shell' program; on Linux and macOS, you can use any terminal application. From there, you can run various make commands.
For more details, see the ModusToolbox™ tools package user guide (locally available at {ModusToolbox™ install directory}/docs_{version}/mtb_user_guide.pdf).
-
Use the spreadsheet (XMC_ESC.xlsx) provided in the example project to define your EtherCAT slave object dictionary (after creating the project in ModusToolbox™, you can find this spreadsheet under imports > mtb-xmc-ecat > ssc_config folder). The Beckhoff SSC Tool uses the spreadsheet as an input to generate output files. The generated EtherCAT Slave Stack Code applies for XMC4300 and XMC4800. The generated EtherCAT Slave Information (ESI) file applies for the EtherCAT host, where the relevant interface information about the slave is stored
Figure 2. EtherCAT Slave Information
-
Double-click the spreadsheet to open it. Check the content of the file. The data defined in both I/O directions is 4 x 16-bit integers and 8 x 1-bit Booleans. For details on how to define your own object dictionary, see the EtherCAT Slave Design Quick Guide.pdf inside the SSC Tool
Figure 3. SSC Tool
-
Start the SSC Tool and create a new project: File > New. Select Custom and click Import. Select the configuration xml file inside the example project (under imports > mtb-xmc-ecat > ssc_config folder) and import it. Select the Infineon device inside the dropdown list (Infineon XMC™ EtherCAT hardware) and click OK. Click yes if the question "External files need to be added...." appears. If a popped window appears and asks for "Add patch to project", select "No". Your project will be created
Figure 4. Project creation
-
Check the settings inside SlaveInformation: vendor ID, vendor name, product ID, and product code are customer-specific and are used by the host to identify the slave. Define the revision number, serial number, device name, and HW/SW version per the requirement. The vendor ID/name and product code assigned to Infineon can be used for the evaluation purpose only. For production, your own vendor ID/name assigned by the EtherCAT Technology Group is mandatory
Figure 5. Slave information
-
In order to use the CE properly with XMC4300/4800 generated EtherCAT stack, please click to Add File(s) button to include the patch file in the stack. Select the patch file (already included in the project), then open it. Select OK if a window appears
Figure 6. Adding patch file
-
Import the spreadsheet, which defines the interface of your EtherCAT node. In SSC Tool, go to Tool > Application > Import and select the spreadsheet file provided inside the example project
-
Click Project > Create new Slave Files to start file generation. Adapt the destination folder for the EtherCAT SSC and ESI files. For this example, the default settings are recommended, which directs the stack generation inside the Src folder under the ssc_config folder in the project
-
Check the availability of the generated SSC
Figure 7. Generated SSC
-
Inside the XMC_ESC.c generated file, implement the link to your application as shown in the following. Modify the source code accordingly, which copies the application data to/from ESC memory to the local application memory:
Before copying the data
After copying the data
Code
memcpy(pData,&(((UINT16 *)&IN_GENERIC0x6000)[1]),SIZEOF(IN_GENERIC0x6000)-2);Before copying the data
After copying the data
Code
memcpy(&(((UINT16 *)&OUT_GENERIC0x7000)[1]),pData,SIZEOF(OUT_GENERIC0x7000)-2);Before copying the data
After copying the data
Code
void process_app(TOBJ7000 *OUT_GENERIC, TOBJ6000 *IN_GENERIC); void APPL_Application(void) { process_app(&OUT_GENERIC0x7000, &IN_GENERIC0x6000); } -
Copy the XMC_ESC.xml generated ESI file into the project (by default, generated under the ssc_config folder) into the TwinCAT EtherCAT folder (default path is C:\TwinCAT\3.1\Config\Io\EtherCAT). If any TwinCAT program is running, restart it
Figure 8. Copy the ESI file
Note: After the stack generation, open the project workspace and delete the holder.c file, as it is no longer needed. After that, open the project Makefile and delete the lines starting with CY_IGNORE as shown in the following figure
-
Use SEGGER J-Flash Lite to clean the flash of XMC4300, which is located in Program Files > SEGGER > JLink > JFlashLite. Open the tool, select the target device as XMC4300-256, and then click Erase Chip as shown in the following figure
Figure 9. Cleaning XMC4300
-
If you use the XMC4800 IoT Connectivity Kit, please go to the "Library Manager" from the IDE's Quick Panel window, and select "APP_KIT_XMC4800_IOT_WIFI" as the active BSP, then click "Update"
Figure 10. Selecting new BSP for XMC4800 IoT Kit
-
Build the project. If there are any library-related issues, run the
make getlibscommand from the ModusToolbox™ IDE's terminal window, retry building, and then flash the code. If the previous steps are correct, the Ethernet IN-port should be flashingFigure 11. Build the project
-
Start the TwinCAT XAE Shell application from the Windows start menu, create a new TwinCAT project, right-click I/O-Devices, and select Add New Item. Choose the EtherCAT Master option with your target type and click Ok
Figure 12. EtherCAT Master
-
Select and search the network adapter you want to use
Note: If the device is not found, install the respective device driver and follow the instructions by TwinCAT, clicking the Compatible Devices button.
-
Right-click the EtherCAT Master and select Scan Boxes
-
Observe that the slave appears as a node on the EtherCAT master bus. The RUN-LED is flashing indicating the PREOP state
Figure 13. EtherCAT slave
-
EtherCAT master view: Inside the EtherCAT master online state, see the queued frames counting up, the connected slave and its PREOP state
EtherCAT slave view: The slave PREOP state is indicated within the TwinCAT system managerFigure 14. TwinCAT system manager
-
Set the master device to free run mode by clicking the Toggle Free Run icon on the upper side
Figure 15. Free run mode
-
EtherCAT slave view: Online status of slave shows the slave in the OP state
EtherCAT master view: The master online status shows the slave in the OP state; frames are not queued; cyclic counter is incrementing
XMC4300 Relax EtherCAT Kit: RUN-LED is static turned on indicating the OP state -
While pushing BUTTON1 on XMC4300 Relax EtherCAT Kit, the button state is updated on the host. The IN_GEN_Bit1 state changes in accordance with the state of BUTTON1
-
Right-click on the OUT_GEN_Bit1 of the slave node and select Online Write '1' inside the context menu. Change the value from '0' to '1' to switch on LED1 and '1' to '0' to switch off LED1. LED1 XMC4300 Relax EtherCAT Kit is turned on/off according to the OUT_GEN_Bit1 setting
Figure 16. Switching LEDs
You can debug the example to step through the code.
In Eclipse IDE
Use the <Application Name> Debug (JLink) configuration in the Quick Panel. For details, see the "Program and debug" section in the Eclipse IDE for ModusToolbox™ user guide.
In other IDEs
Follow the instructions in your preferred IDE.
The project uses a custom design.modus file for the EtherCAT configuration, which is located under templates folder.
| Resources | Links |
|---|---|
| Code examples | Using ModusToolbox™ on GitHub |
| Device documentation | XMC4300 datasheet - XMC4800 datasheet XMC4300 reference manual - XMC4800 reference manual |
| Development kits | XMC4300 Relax EtherCAT Kit XMC4800 IoT FreeRTOS Connectivity Kit |
| Libraries on GitHub | mtb-xmclib-cat3 - XMC™ Peripheral Driver Library (XMCLib) mtb-xmc-emeeprom - XMC™ emulated EEPROM middleware library mtb-xmc-ecat - XMC™ EtherCAT middleware library |
| Tools | ModusToolbox™ – ModusToolbox™ software is a collection of easy-to-use libraries and tools enabling rapid development with Infineon MCUs for applications ranging from wireless and cloud-connected systems, edge AI/ML, embedded sense and control, to wired USB connectivity using PSOC™ Industrial/IoT MCUs, AIROC™ Wi-Fi and Bluetooth® connectivity devices, XMC™ Industrial MCUs, and EZ-USB™/EZ-PD™ wired connectivity controllers. ModusToolbox™ incorporates a comprehensive set of BSPs, HAL, libraries, configuration tools, and provides support for industry-standard IDEs to fast-track your embedded application development |
Infineon provides a wealth of data at www.infineon.com to help you select the right device, and quickly and effectively integrate it into your design.
For XMC4000 MCU devices, see 32-bit XMC4000 industrial microcontroller based on Arm® Cortex®-M.
For more information about EtherCAT, see EtherCAT webpage.
Document title: CE241733 – XMC4300 EtherCAT SSC
| Version | Description of change |
|---|---|
| 1.0.0 | New code example |
| 1.1.0 | Patch file added |
All other trademarks or registered trademarks referenced herein are the property of their respective owners.
The Bluetooth® word mark and logos are registered trademarks owned by Bluetooth SIG, Inc., and any use of such marks by Infineon is under license.
PSOC™, formerly known as PSoC™, is a trademark of Infineon Technologies. Any references to PSoC™ in this document or others shall be deemed to refer to PSOC™.
© 2025 Infineon Technologies AG
All Rights Reserved.
The information given in this document shall in no event be regarded as a guarantee of conditions or characteristics. With respect to any examples or hints given herein, any typical values stated herein and/or any information regarding the application of the device, Infineon Technologies hereby disclaims any and all warranties and liabilities of any kind, including without limitation, warranties of non-infringement of intellectual property rights of any third party.
For further information on technology, delivery terms and conditions and prices, please contact the nearest Infineon Technologies Office (www.infineon.com).
Due to technical requirements, components may contain dangerous substances. For information on the types in question, please contact the nearest Infineon Technologies Office.
Infineon Technologies components may be used in life-support devices or systems only with the express written approval of Infineon Technologies, if a failure of such components can reasonably be expected to cause the failure of that life-support device or system or to affect the safety or effectiveness of that device or system. Life support devices or systems are intended to be implanted in the human body or to support and/or maintain and sustain and/or protect human life. If they fail, it is reasonable to assume that the health of the user or other persons may be endangered.
