File access over EtherCAT (FoE) is a straightforward protocol that shares similarities with TFTP (Trivial File Transfer Protocol) but does not require a full TCP/IP stack.
The FoE implementation supports storing and retriving of a generic file onto/from the flash as well as the firmware update over FoE. The generic file is stored at the offset in the flash specified by the macro APP_OSPI_FLASH_OFFSET_BASE and the firmware file will be flashed at the offset in the flash defined by the macro APP_OSPI_FLASH_OFFSET_APPIMAGE, at which by default the SBL bootloader will look for the firmware.
This page briefly describes the file download/upload procedure in TwinCAT, API call sequence during file download/upload and testing FoE with TwinCAT. The motivation of the code snippets is to provide a better understanding of how to handle the FoE file data. For a detailed overview please refer to the example code.
The APIs called for the FoE file download and upload are
The API EC_SLV_APP_FoE_fileWrite is called during the file download/write which calls the API NVM_APP_write to write the file data into the flash. The API EC_SLV_APP_FoE_fileOpen is called prior to the API EC_SLV_APP_FoE_fileWrite, where the file name, file name length and file access password is received and stored in a file header along with the file data.
If the file data size is less than the FoE mailbox data payload size (1012 bytes), the API EC_SLV_APP_FoE_fileWrite is called once with the complete file data. If the file data size is larger, the API EC_SLV_APP_FoE_fileWrite is called multiple times with the file data segments of maximum size of 1012 bytes.
The last file data segment will trigger the API EC_SLV_APP_FoE_fileClose which will complete the file write pending operations by writing remaining file data and the file header into the flash.
The below diagram shows the overview of the downloaded file storage in the flash. The file header information is necessary for retriving the complete file during the file upload. The file header has the file name, file name length, file password and the file size information. The maximum file name length is defined by the macro FOE_FILE_NAME_MAX_LEN.
The API EC_SLV_APP_FoE_fileRead is called during the file upload which calls the API NVM_APP_read to read the file data from the flash. The API EC_SLV_APP_FoE_fileOpen is called prior to the API EC_SLV_APP_FoE_fileread, where the check for the password authentication to access the file and valid file length is performed. If the stored file data size is less than the FoE mailbox frame size (1012 bytes), the API EC_SLV_APP_FoE_fileRead is called once to retrieve the complete file data. If the stored file data size is larger, the API EC_SLV_APP_FoE_fileRead is called multiple times to retrieve the file data in segments of maximum size of 1012 bytes.
The below diagram describes the overview of the API call sequence in case of file download/upload from TwinCAT.
The Simple demo example implementation supports the firmware update over FoE. The firmware update is supported only in the Bootstrap state. The firmware file *.appimage.hs_fs must be renamed as ECATFW__* and this file naming convention shall be used to identify the firmware file. The firmware file flashed at the offset defined by the macro APP_OSPI_FLASH_OFFSET_APPIMAGE is not available for the file upload. That means the firmware file cannot be read back using file upload procedure.
Note: If the firmware file with the naming convention ECATFW__* is downloaded in any other states (Preop or Op) different from Bootstrap state then the firmware file will be saved as a generic file at the offset specified by the macro APP_OSPI_FLASH_OFFSET_BASE in the flash and the file will be available for the file upload.
The APIs which trigger at the state transition from Init to Bootstrap and vice-versa will support the firmware update over FoE.
The API EC_SLV_APP_FoE_startBL which is called during the state transition from Init to Bootstrap can be used to prepare for the firware update activity.
The API EC_SLV_APP_FoE_startBL which is called during the state transition from Init to Bootstrap can be used to check the firmware file download status and reboot activity.
The below diagram describes the overview of the API call sequence in case of firmware download from TwinCAT.
The same procedure applies for firmware update as well but the only difference will be the naming convention of the firmware file and the state in which the firmware file download is being performed. A sample text file FoE_Test_File.txt having 510 bytes shall be used to download into the flash memory of the EtherCAT SubDevice from TwinCAT. This file access shall be password protected and the password shall be 32bit length, 0x1234ABFE for example. In case, the file access is required without the password protection then leave the password to its default state 0x00000000.
Follow the below steps to download a sample file to EtherCAT device from TwinCAT.
A text file FoE_Test_File.txt which has been downloaded successfully in the previous step, can be uploaded back to TwinCAT from the EtherCAT SubDevice.
Follow the below steps to upload a sample file from EtherCAT device into TwinCAT.