Introduction

The Raspberry Pi 4 can be used as an EtherCAT slave device when combined with the EasyCAT HAT add-on board. The following guide will provide a comprehensive walkthrough of the implementation process.

Hardware Description

In this guide, the following hardware components will be used:

The Compute Module is plugged into its designated place on the IO Board. Next, the EasyCAT HAT is plugged into the pin header of the IO Board, positioning its RJ45 ports over the HDMI ports of the IO Board. The “IN” RJ45 Port of the EasyCAT HAT is connected to the development PC using an Ethernet cable (pictured in yellow). The Ethernet port on the IO Board is connected to the local network with an Ethernet cable (pictured in grey) for the SSH connection.

EasyCAT HAT Configuration

First, Device Emulation must be turned off on the EasyCAT HAT. This must be written to the EEPROM of the EasyCAT HAT. Set bit 0 of the ESC Configuration Register or bit 8 of the PDI Control Register to 0. Either of the bits will work as both refer to the same bit. More information about this step can be found on the datasheet for the LAN9252.

In this guide, the acontis EtherCAT network configuration tool, known as EC-Engineer, will be used to write data to the EEPROM of the EasyCAT HAT. Open EC-Engineer and connect to the slave device. An error will appear stating that no ESI was found. Ignore this error message and enter Diagnosis Mode. Select the slave in the device tree and then select the EEPROM tab in the Device Editor window. Find the register with the name “PDI Control” and change the value so that bit 8 is a 0. In our case, the new value is 0x0280h (0010 1000 0000b).

Slave Stack Code Tool

Download and install the Slave Stack Code (SSC) Tool. This tool makes it possible to generate the source code and EtherCAT Slave Information (ESI) files for EtherCAT Slave devices. The tool helps simplify the development process by automatically generating the required code and configuration files based on the user inputs and specific settings.  To simplify this process, acontis has prepared an SSC config file, called EasyCAT_HAT_config, which allows us to simply load this config without editing too many of the settings.

Generate Source Code and ESI File

Begin by creating a new project. The New Project Wizard will appear. When prompted, select “Custom” for the configuration type, import the EasyCAT_HAT_config, and select EasyCAT HAT.

Depending on the location the program is run from, it might ask for a file called 9252_HW.c; simply select the included file with the same name. All other necessary files should now be automatically loaded. To verify this, look for files called 9252_HW.c, 9252_HW.h, SPIDriver.c, SPIDriver.h, and GenericTypeDefs.h as part of the list in the SSC Tool.

Next, import the Process Data Object (PDO) mapping of our slave. If you already have a config file, go to Tool -> Application -> Import and select the desired config file. If you do not have a config file and need to create one, go to Tool -> Application -> Create new.

If you selected “Create new,” a spreadsheet program will open with a templated PDO mapping. Here, we can define our mapping. For this demonstration, we will simply define two UDINTs, one as an Input and one as an Output.

Save the file and close the spreadsheet. The SSC Tool will now automatically import the newly created configuration. The next dialog box will ask for a name for the new application.

Next, ensure that MAX_PD_INPUT_SIZE and MAX_PD_OUTPUT_SIZE are sufficient in size for the Inputs and Outputs specified and that the Vendor ID is the same as the EasyCAT HAT’s.

To generate the ESI file and source code, go to Project -> Create new Slave Files.

Writing the Application

In the Src folder generated by the SSC Tool, there should be a .c file with the name of the application that was assigned in the previous section. There, you can add custom application code to be executed on the Raspberry Pi. This walkthrough will use a simple application that will read the single output and write it to the single input. To do this, three functions will need to be modified.

  1. In APPL_InputMapping, copy the input variable to the corresponding location in RAM:

    void APPL_InputMapping(UINT16* pData)
    {
        MEMCPY(pData, &EXAMPLE_INPUT0x6001, SIZEOF(EXAMPLE_INPUT0x6001));
    }

  2. In APPL_OutputMapping, copy the output from the corresponding location in RAM to the output variable:

    void APPL_OutputMapping(UINT16* pData)
    {
        MEMCPY(&EXAMPLE_OUTPUT0x7010, pData, SIZEOF(EXAMPLE_OUTPUT0x7010));
    }

  3. In APPL_Application, assign the input variable the value of our output variable:

    void APPL_Application(void)
    {
        EXAMPLE_INPUT0x6001 = EXAMPLE_OUTPUT0x7010;
    }

Compiling

The Src folder can now be transferred to the Raspberry Pi. Navigate to the copied Src folder on the Raspberry Pi and run the following command to compile the source code.

gcc -w -O3 -o EasyCAT_HAT *.c -I ./

Enabling SPI

Serial Peripheral Interface (SPI) must be enabled on the Raspberry Pi as this establishes the communication channel with the EasyCAT HAT. To verify if SPI is already enabled, search for a device named spidev0.0 in the /dev directory. If this device is not found in this location, SPI must be manually enabled. It is important to note that the method in which SPI is enabled depends on the Raspberry Pi’s configuration. Utilizing the raspi-config tool is typically the most straightforward approach when using the default device tree. However, for a custom device tree, using raspi-config may cause an override. In such cases, either incorporate the SPI device into the device tree or generate an overlay.

Running the Application

Run the following executable from the compilation folder:

./EasyCAT_HAT

The Raspberry Pi is now functioning as a fully operational EtherCAT slave. To verify its operation, open EC-Engineer, add the generated ESI file, and force the Example Output. Observe whether the Example Input changes accordingly to ensure proper functioning.