Embedded Development – Porting EtherCAT on LAN9253

1. Overview of the Porting Process

  1. Hardware Platform Setup
  • Selection of the main control chip: It is recommended to use the STM32F4 series (such as STM32F407) or Microchip SAMD5x series, which must support SPI/SQI or HBI interfaces to communicate with LAN9253.
  • Hardware connection: LAN9253 connects to the main control via the SPI interface, requiring configuration of SCK/MOSI/MISO/CS pins, while an external PHY chip is connected to achieve dual-port Ethernet communication.
  • Protocol Stack Generation
    • Use the SSC (Slave Stack Code) tool (such as SSC-5.11) to generate the ESC (EtherCAT Slave Controller) protocol stack files.
      • Select the 8bit I/O template in SSC, configure the XML file to define PDO (Process Data Object) mapping.
      • Edit the predefined protocol stack file using Excel to generate protocol stack code adapted to the target hardware.
  • Code Porting
    • Development Environment Configuration
      • STM32 platform: Use CubeMX to initialize the SPI peripherals, and Keil5 to complete code compilation.
      • Microchip platform: Configure MPLab Harmony to set up the EtherCAT components and integrate the LAN9253 driver.
    • Data Exchange Implementation
      • Configure the SYNC Manager to manage periodic data exchange.
      • Implement communication between the master (such as TwinCAT3) and the slave’s PDO through SM0 (Mailbox communication) and SM1 (Process Data) channels.

    2. Key Configuration Steps

    1. LAN9253 Initialization

    // SPI Initialization Example (STM32

    hspi.Instance = SPI1;

    hspi.Init.Mode = SPI_MODE_MASTER;

    hspi.Init.Direction = SPI_DIRECTION_2LINES;

    hspi.Init.DataSize = SPI_DATASIZE_8BIT;

    hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; // 10MHz Clock

    HAL_SPI_Init(&hspi);

    • Configure the ESC registers of LAN9253 (such as 0x0000-0x0FFF), setting the distributed clock synchronization parameters.28
  • PDO Mapping Configuration
    • Define input/output PDO in the XML file:

    <Sm ParamIndex=”0″ StartAddress=”0x1000″ DefaultSize=”64″/>

    <Sm ParamIndex=”1″ StartAddress=”0x1100″ DefaultSize=”128″/>

    • Define the device description through the ESI (EtherCAT Slave Information) file, and import it into TwinCAT3 to complete topology recognition.
  • Interrupt Handling Optimization
    • Enable SYNC0 interrupt response for periodic data exchange:

    // Configure the ESC interrupt register

    write_esc_reg(0x0220, 0x0001); // Enable SYNC0 interrupt

    // MCU Interrupt Service Routine

    void EXTI0_IRQHandler() {

    process_pdo_data(); // Process data

    clear_esc_interrupt(0x01);

    }

    • Use DMA transfer to reduce CPU load.

    3. Verification and Debugging

    1. Protocol Verification Tool
    • Use TwinCAT3 for online diagnostics:
      • Scan the network topology to confirm the slave state machine enters the OP state.
      • Real-time monitor the PDO data flow through Scope View.
    • Analyze the EtherCAT frame structure through Wireshark to verify DC (Distributed Clock) synchronization accuracy.
  • Troubleshooting Common Issues
  • Phenomenon

    Troubleshooting Points

    Solutions

    Slave not recognized

    SPI timing error / ESI file mismatch

    Check SCLK phase configuration4, regenerate the XML file.1

    Data periodically lost

    Distributed clock not synchronized

    Configure the ESC register 0x0910 to start DC synchronization.8

    Mailbox communication timeout

    Interrupt response delay

    Optimize interrupt priority, shorten service routine execution time.6

    4. Hardware Design Considerations

    1. Signal Integrity
    • SPI trace length ≤10cm, add 10Ω series resistors to suppress reflections.4
    • Implement 100Ω impedance matching for Ethernet differential pairs, maintaining a spacing of 3W rules.
  • Power Design
    • Provide a separate 3.3V power supply for LAN9253, with ripple ≤50mV.
    • It is recommended to use TPS7A series LDO, configured with 10μF+0.1μF decoupling capacitors.

    Using the above methods, the development of an EtherCAT slave from scratch to the OP state can be completed within 4-6 weeks. In actual projects, it is crucial to focus on SPI timing consistency (recommended ≤10MHz) and PDO mapping logic.

    Leave a Comment