Configuring EtherCAT Slaves using SOEM

For work we use an EtherCAT interface to connect our device. We use this interface by the german company Kunbus.
To test everything we use SOEM (Simple Open EtherCAT Master) for the EtherCAT Master which is integrated in a QT GUI application.

When no configuration is sent to the device it is initialized with only 32 bytes of data for the process image which is not enough for our purpose.
EtherCAT slaves can be configured using the CANOpen interface (using CoE - CANopen over EtherCAT). To do the configuration SDOs (Service Data Objects) are sent to the master. Additional information can be found in this issue on github.

The configuration in SOEM can be done by installing a callback function that sends the SDOs using the ec_SDOwrite function. The sync managers and the process data image first has to be deactivated (=set to zero) in order to load a new configuration. The datasheet for the EtherCAT-Module contains the EtherCAT objects you can configure starting from page 101.

First the sync managers are deactivated. After that the number of process data items is set to zero. When this is done we set it to 64. Since no further configuration of the process data is done. These data items are assumed to have a size of 8 bit.

Our callback function looks like this.

static int kunbus_setup(uint16 slave)
    int wkc = 1;

    uint8_t SM_out = 0;
    uint8_t reg_size = 64;

    uint16_t syncManagerAdressRX = 0x1600;
    uint16_t syncManagerAdressTX = 0x1A00;

    //deactivate both sync-managers
    wkc &= ec_SDOwrite(slave,0x1C12,0,FALSE,sizeof(SM_out),&SM_out,EC_TIMEOUTRXM);
    wkc &= ec_SDOwrite(slave,0x1C13,0,FALSE,sizeof(SM_out),&SM_out,EC_TIMEOUTRXM);

    //set number of  process data items to zero
    wkc &= ec_SDOwrite(slave,0x1600,0,FALSE,sizeof(SM_out),&SM_out,EC_TIMEOUTRXM);
    wkc &= ec_SDOwrite(slave,0x1A00,0,FALSE,sizeof(SM_out),&SM_out,EC_TIMEOUTRXM);
    //set number of  process data items to 64
    wkc &= ec_SDOwrite(slave,0x1600,0,FALSE,sizeof(reg_size),&reg_size,EC_TIMEOUTRXM);
    wkc &= ec_SDOwrite(slave,0x1A00,0,FALSE,sizeof(reg_size),&reg_size,EC_TIMEOUTRXM);

    SM_out = 1;
    //write adresses for sync manager 1
    wkc &= ec_SDOwrite(slave,0x1C12,1,FALSE,sizeof(syncManagerAdressRX),&syncManagerAdressRX,EC_TIMEOUTRXM);
    //activate it
    wkc &= ec_SDOwrite(slave,0x1C12,0,FALSE,sizeof(SM_out),&SM_out,EC_TIMEOUTRXM);

    //write adresses for sync manager 2
    wkc &= ec_SDOwrite(slave,0x1C13,1,FALSE,sizeof(syncManagerAdressTX),&syncManagerAdressTX,EC_TIMEOUTRXM);
    //activate it
    wkc &= ec_SDOwrite(slave,0x1C13,0,FALSE,sizeof(SM_out),&SM_out,EC_TIMEOUTRXM);

    if (wkc == 0)
        return -1;

    return 0;

If the ec_SDOwrite function returns zero if something went wrong. Check the slave ID, the objects address and that the EtherCAT slave is in the correct state. You can also read back the SDOs using the ec_SDOread to check if everything is correct.

Finally the callback routine static int kunbus_setup(uint16 slave) is installed during setup.

       if ( ec_config_init(FALSE) > 0 )
          //setup callback routine for configuration - assuming slave 1
          ec_slave[1].PO2SOconfig = kunbus_setup;


Please be aware that I'm a total beginner in the topic of EtherCAT but it works :).


Leave Comment: