esp32-w5500-ethercat-slave
A from-scratch software EtherCAT slave running on an ESP32-WROOM-32 with a WIZnet W5500 Ethernet controller over SPI.
0
Project description
A Software EtherCAT Slave on a $5 ESP32 โ Spoken Fluently to a Real Beckhoff TwinCAT 3 Master, Over a W5500 in MACRAW
#EtherCAT #IndustrialFieldbus #TwinCAT3 #SoftwareESC #MACRAW #ESP32 #FactoryAutomation #FMMU #ProofOfConcept #PromiscuousCapture
๐ Hobby / educational proof-of-concept โ independent implementation, verified against a real Beckhoff TwinCAT 3 master โ Verified: device enumerates in TwinCAT as
ESP32-DO8, climbs INIT โ PreOp โ SafeOp โ OP, Working Counter valid, 8 GPIO outputs driven live from the master
01 โ What is this project?
EtherCAT is the high-speed fieldbus that runs a huge share of the world's factory automation โ robot cells, CNC machines, motion control. But getting onto an EtherCAT network as a slave device normally means buying a dedicated EtherCAT Slave Controller (ESC) chip like the Beckhoff ET1100 or Microchip LAN9252. That hardware, and the documentation behind it, is a wall between a curious engineer and the protocol.
This project knocks a hole in that wall. It implements an EtherCAT slave entirely in software on a plain ESP32-WROOM-32, using a WIZnet W5500 as the raw Ethernet front end. The firmware emulates an ESC from scratch โ datagram parsing, the register file, Working Counter arithmetic, the AL state machine, SII EEPROM emulation, and FMMU process-data mapping โ and it's not a simulation: it was built up in four layers, each one verified against a real Beckhoff TwinCAT 3 master before the next was added.
The reference application is deliberately simple so the protocol is the star: tick a checkbox in TwinCAT, and a physical GPIO on the ESP32 goes high. Eight digital outputs, mapped through real FMMU logical addressing, driven live from an industrial PLC master over a commodity Ethernet chip.
02 โ Why software EtherCAT (and where it stops)?
๐ท The honesty is the feature
This curation usually celebrates capability. Here, the limitations section is what earns trust. The author is explicit: the W5500 is a store-and-forward chip, so this slave is a stable but slow node (~5โ10 ms cycle times). It cannot do Distributed Clocks (DC) or sub-millisecond cycles โ that genuinely requires a hardware ESC like the LAN9252. The Vendor ID is a placeholder (0x0000DEAD), valid for the lab, not for production interoperability. Nothing is oversold. For a learning artifact about a notoriously opaque protocol, that candor is exactly what makes it useful.
๐ท A learning ladder for an opaque protocol
The architecture doc walks the ESC emulation as four verified layers: W5500 MACRAW transport โ datagram parser + Working Counter + AL FSM โ SII/EEPROM identity handshake โ FMMU logical-to-physical mapping. Each layer is the smallest thing that TwinCAT will accept before moving on. That's a roadmap anyone can follow to actually understand EtherCAT, not just use it.
๐ท A portable protocol core
The cleanest design decision: the EtherCAT logic (ecat.c, sii.c, the ESI file) is independent of transport. The author notes that porting to a real LAN9252 for DC-capable timing changes only the W5500 transport layer โ the protocol brain carries over. The W5500 is what makes the learning version cheap and accessible.
03 โ System architecture

04 โ Why W5500? โญ
๐ท Mode: MACRAW, promiscuous โ verified in code
This is the heart of why the W5500 fits. An EtherCAT slave must see every frame on the wire, not just frames addressed to its IP โ because EtherCAT doesn't use IP at all; the master sweeps datagrams past every node. The W5500's MACRAW socket mode (Sn_MR_MACRAW, 0x04) does exactly that: raw Layer-2 Ethernet frames, no TCP/IP stack in the way. The firmware opens socket 0 in MACRAW with the MAC filter bit cleared (promiscuous), so the ESP32 captures all of the master's EtherCAT traffic:
/* MACRAW mode. MF (bit 7) cleared => promiscuous: capture ALL frames. */ w5500_wr8(W5500_BSB_S0_REG, W5500_Sn_MR, W5500_MR_MACRAW); socket_cmd(W5500_CMD_OPEN); /* verifies Sn_SR == 0x42 (SOCK_MACRAW) before proceeding */This is the rare project where the W5500 is used not for its famous hardware TCP/IP offload (TOE), but for the opposite: getting completely out of the way and handing the CPU raw frames. The same chip that's usually prized for doing the network stack for you is here prized for not doing it โ pure L2 access is exactly what a fieldbus needs.
๐ท Why a $5 SPI chip beats a dedicated ESC for learning
A real ESC (ET1100/LAN9252) is a specialized, harder-to-source part with its own steep documentation. The W5500 is cheap, ubiquitous, 3.3 V, and speaks SPI to any MCU. By choosing MACRAW + promiscuous capture, the author turned a commodity Ethernet chip into the physical layer of an industrial fieldbus node โ the entry barrier collapses from "buy and learn an ESC" to "wire up a W5500 you probably already own."
๐ท The honest trade-off, stated by the hardware itself
The W5500 is store-and-forward: a frame is fully received into the buffer before the CPU sees it, then fully sent back. That's why DC and sub-ms cycles are off the table โ and why the author is upfront about ~5โ10 ms operation. The chip's nature defines the project's scope, and the project documents it precisely rather than hiding it.
๐ท Verified evidence โ
- Boot log confirms the chip:
VERSIONR = 0x04, socket reachesSn_SR = 0x42(SOCK_MACRAW) - TwinCAT enumerates the device by name (
ESP32-DO8) via the standard SII/EEPROM identity handshake - AL state machine climbs all the way to OP (
AL state -> 0x08) WcState = 0in TwinCAT confirms the Working Counter is being incremented correctly- Writing an output bit in TwinCAT drives the matching physical GPIO high โ closed-loop, master-to-pin
05 โ Key components
๐ WIZnet W5500 โ MACRAW (promiscuous) raw-Ethernet front end
Socket 0 opened in Sn_MR_MACRAW with MAC filter disabled; 16 KB RX/TX buffer assigned to the single socket. Native ESP-IDF SPI master driver (no Arduino), VSPI pins, manual CS so each [addr-hi][addr-lo][control][data] frame is atomic. Handles the MACRAW RX quirk where the 2-byte big-endian length header includes itself.
๐ง ESP32-WROOM-32 (original, not S3)
net_task pinned to CPU1 in a busy-poll loop to keep frame turnaround tight and the EtherCAT link stable; CPU0 left for system housekeeping.
โ๏ธ Software ESC core (ecat.c, 338 lines)
Datagram parser for all four EtherCAT addressing modes (auto-increment AP*, configured FP*, broadcast B*, logical L*), correct Working Counter rules, AL finite state machine, and FMMU logicalโphysical mapping into process RAM that drives the GPIOs.
๐พ SII EEPROM emulation (sii.c)
Emulates the ESC EEPROM register interface with CRC8 so the master reads device identity through the exact standard handshake โ including a custom 16ร14 device icon embedded in the ESI.
๐ ESI device description (esi/ESP32_DO8.xml)
Installed on the TwinCAT side so the slave appears as a named device with its 8-output I/O map.
06 โ Application scenarios
01. Hands-on EtherCAT education
The intended use: a cheap, fully-documented, layer-by-layer way to actually learn how an EtherCAT slave talks to a master โ without buying ESC silicon.
02. Rapid fieldbus prototyping
Need a throwaway EtherCAT I/O node on the bench to test a master configuration or an ESI file? An ESP32 + W5500 stands in for a real slave at relaxed cycle times.
03. Bridge to production via LAN9252
Because the protocol core is transport-independent, this is a natural first step before committing to a DC-capable LAN9252 design โ the ecat.c/sii.c/ESI work carries straight over.
04. Teaching MACRAW itself
A clean, real-world reference for using the W5500 in promiscuous MACRAW mode โ useful to anyone building packet sniffers, custom L2 protocols, or fieldbus front ends on the same chip.
Conclusion
An industrial EtherCAT slave, emulated in firmware on a hobby-grade ESP32, made possible because the W5500 will โ when asked โ stop being a TCP/IP chip and just hand you raw Ethernet frames.
- โ Full software ESC: datagram parser, Working Counter, AL FSM, SII EEPROM, FMMU
- โ Verified against a real Beckhoff TwinCAT 3 master โ climbs to OP, drives live I/O
- โ
W5500 in MACRAW promiscuous mode (
Sn_MR=0x04,Sn_SR=0x42) confirmed in code and boot log - โ Transport-independent protocol core โ portable to a LAN9252 for DC-capable timing
- โ Honest, precise limitations (store-and-forward โ ~5โ10 ms, no DC) that make it trustworthy as a learning artifact
- โ Complete docs: wiring map, layer-by-layer architecture, TwinCAT setup walkthrough
From hardware TCP/IP offload to raw Layer-2 industrial fieldbus access โ the W5500 turns up wherever Ethernet needs to happen, even inside an EtherCAT node where its usual headline feature is deliberately switched off. The most surprising use of a TOE chip is the one that doesn't use TOE at all.
Q&A
Q. Which W5500 socket mode does this use, and why not TOE? MACRAW (Sn_MR_MACRAW, value 0x04), with the MAC filter disabled for promiscuous capture โ verified directly in w5500.c. EtherCAT runs on raw Layer-2 Ethernet with no IP, and the master's datagrams sweep past every node, so the slave must capture all frames. The W5500's hardware TCP/IP (TOE) would actively get in the way; MACRAW is the only correct choice here.
Q. Is it real or simulated? Real, on hardware. The boot log reads back the W5500 VERSIONR and MACRAW socket status, and the device enumerates and reaches OP under an actual TwinCAT 3 master driving physical GPIOs. It is, by the author's own framing, a proof-of-concept โ not a certified product.
Q. Why can't it do hard real-time EtherCAT? Because the W5500 is store-and-forward: frames are buffered in full before the CPU sees them and before they're sent back, which precludes Distributed Clocks and sub-millisecond cycles. That's an inherent property of the chip, and the project is explicit about it (~5โ10 ms cycle times). For DC-capable timing the author points to a LAN9252 port, reusing the same protocol code.
Q. Is the code complete and buildable? Yes. ESP-IDF project (developed on v6.0.1) with CMakeLists.txt, sdkconfig.defaults, full main/ sources (W5500 driver, ESC core, SII emulation), the ESI file, plus WIRING.md and ARCHITECTURE.md. Build/flash/monitor steps and the complete TwinCAT setup are documented.
5์ฒ ์์ง๋ฆฌ ESP32 ์์ ๊ตฌํํ ์ํํธ์จ์ด EtherCAT ์ฌ๋ ์ด๋ธ โ W5500์ MACRAW๋ก ์ง์ง Beckhoff TwinCAT 3 ๋ง์คํฐ์ ๋ํํ๋ค
#EtherCAT #์ฐ์
์ฉํ๋๋ฒ์ค #TwinCAT3 #์ํํธ์จ์ดESC #MACRAW #ESP32 #๊ณต์ฅ์๋ํ #FMMU #๊ฐ๋
๊ฒ์ฆ #ํ๋ก๋ฏธ์คํ์ด์ค์บก์ฒ
๐ ์ทจ๋ฏธ/๊ต์ก์ฉ ๊ฐ๋ ๊ฒ์ฆ(PoC) โ ๋ ๋ฆฝ ๊ตฌํ, ์ค์ Beckhoff TwinCAT 3 ๋ง์คํฐ๋ก ๊ฒ์ฆ โ ๊ฒ์ฆ๋จ: TwinCAT์์
ESP32-DO8๋ก ์ธ์, INITโPreOpโSafeOpโOP ๋จ๊ณ ์ง์ , Working Counter ์ ์, ๋ง์คํฐ์์ GPIO 8์ฑ๋ ์ค์๊ฐ ๊ตฌ๋
01 โ ์ด๋ค ํ๋ก์ ํธ์ธ๊ฐ?
EtherCAT์ ์ ์ธ๊ณ ๊ณต์ฅ ์๋ํ์ ์๋น ๋ถ๋ถ โ ๋ก๋ด ์ , CNC ๊ณต์๊ธฐ๊ณ, ๋ชจ์ ์ ์ด โ ์ ๋๋ฆฌ๋ ๊ณ ์ ํ๋๋ฒ์ค๋ค. ๊ทธ๋ฐ๋ฐ ์ฌ๋ ์ด๋ธ ์ฅ์น๋ก EtherCAT ๋คํธ์ํฌ์ ์ฌ๋ผํ๋ ค๋ฉด ๋ณดํต Beckhoff ET1100์ด๋ Microchip LAN9252 ๊ฐ์ ์ ์ฉ EtherCAT ์ฌ๋ ์ด๋ธ ์ปจํธ๋กค๋ฌ(ESC) ์นฉ์ ์ฌ์ผ ํ๋ค. ๊ทธ ํ๋์จ์ด์ ๊ทธ ๋ค์ ์จ์ ๋ฐฉ๋ํ ๋ฌธ์๊ฐ, ํธ๊ธฐ์ฌ ๋ง์ ์์ง๋์ด์ ์ด ํ๋กํ ์ฝ ์ฌ์ด๋ฅผ ๊ฐ๋ก๋ง๋ ๋ฒฝ์ด๋ค.
์ด ํ๋ก์ ํธ๋ ๊ทธ ๋ฒฝ์ ๊ตฌ๋ฉ์ ๋ธ๋ค. ํ๋ฒํ ESP32-WROOM-32 ์์์ EtherCAT ์ฌ๋ ์ด๋ธ๋ฅผ ์์ ์ํํธ์จ์ด๋ก ๊ตฌํํ๊ณ , WIZnet W5500์ ์(raw) ์ด๋๋ท ํ๋ฐํธ์๋๋ก ์ด๋ค. ํ์จ์ด๊ฐ ESC๋ฅผ ๋ฐ๋ฐ๋ฅ๋ถํฐ ์๋ฎฌ๋ ์ด์ ํ๋ค โ ๋ฐ์ดํฐ๊ทธ๋จ ํ์ฑ, ๋ ์ง์คํฐ ํ์ผ, Working Counter ์ฐ์ฐ, AL ์ํ๋จธ์ , SII EEPROM ์๋ฎฌ๋ ์ด์ , FMMU ํ๋ก์ธ์ค ๋ฐ์ดํฐ ๋งคํ๊น์ง. ๊ทธ๋ฆฌ๊ณ ์๋ฎฌ๋ ์ด์ ์ด ์๋๋ค: ๋ค ๊ฐ์ ๊ณ์ธต์ผ๋ก ์์ ์ฌ๋ฆฌ๋, ๊ฐ ๊ณ์ธต์ ์ค์ Beckhoff TwinCAT 3 ๋ง์คํฐ๋ก ๊ฒ์ฆํ ๋ค์์ผ ๋ค์ ๊ณ์ธต์ ์ฌ๋ ธ๋ค.
๋ ํผ๋ฐ์ค ์ ํ๋ฆฌ์ผ์ด์ ์ ์ผ๋ถ๋ฌ ๋จ์ํ๊ฒ ๋ง๋ค์ด ํ๋กํ ์ฝ์ด ์ฃผ์ธ๊ณต์ด ๋๊ฒ ํ๋ค: TwinCAT์์ ์ฒดํฌ๋ฐ์ค ํ๋๋ฅผ ์ผ๋ฉด ESP32์ ๋ฌผ๋ฆฌ GPIO ํ์ด High๋ก ์ฌ๋ผ๊ฐ๋ค. ์ง์ง FMMU ๋ ผ๋ฆฌ ์ฃผ์์ง์ ์ ๊ฑฐ์น ๋์งํธ ์ถ๋ ฅ 8์ฑ๋์, ์ฐ์ ์ฉ PLC ๋ง์คํฐ๊ฐ ๋ฒ์ฉ ์ด๋๋ท ์นฉ ๋๋จธ๋ก ์ค์๊ฐ ๊ตฌ๋ํ๋ค.
02 โ ์ ์ํํธ์จ์ด EtherCAT์ธ๊ฐ (๊ทธ๋ฆฌ๊ณ ์ด๋์ ๋ฉ์ถ๋๊ฐ)?
๐ท ์์งํจ์ด ๊ณง ๊ฐ์ ์ด๋ค
์ด ํ๋ ์ด์
์ ๋ณดํต "ํ ์ ์๋ ๊ฒ"์ ์นญ์กํ๋ค. ๊ทธ๋ฐ๋ฐ ์ด ํ๋ก์ ํธ๋ ํ๊ณ ์น์
์ด ์ ๋ขฐ๋ฅผ ๋ง๋ ๋ค. ์ ์๋ ๋ถ๋ช
ํ ๋ชป ๋ฐ๋๋ค: W5500์ store-and-forward ์นฉ์ด๋ผ ์ด ์ฌ๋ ์ด๋ธ๋ **์์ ์ ์ด์ง๋ง ๋๋ฆฐ ๋
ธ๋(์ฝ 5~10 ms ์ฌ์ดํด)**๋ผ๊ณ . Distributed Clocks(DC)๋ sub-ms ์ฌ์ดํด์ ๋ถ๊ฐ๋ฅํ๋ฉฐ, ๊ทธ๊ฑด LAN9252 ๊ฐ์ ํ๋์จ์ด ESC๊ฐ ์์ด์ผ ํ๋ค. Vendor ID๋ ํ๋ ์ด์คํ๋(0x0000DEAD)๋ก ์คํ์ค์ฉ์ผ ๋ฟ ์์ฐ ์ํธ์ด์ฉ์๋ ๋ชป ์ด๋ค. ๋ฌด์ ํ๋ ๊ณผ์ฅํ์ง ์๋๋ค. ์
๋ช
๋๊ฒ ๋ถํฌ๋ช
ํ ํ๋กํ ์ฝ์ ๋ฐฐ์ฐ๋ ํ์ต ์๋ฃ๋ก์, ์ด ์์งํจ์ด์ผ๋ง๋ก ์ด ํ๋ก์ ํธ๋ฅผ ์ธ๋ชจ ์๊ฒ ๋ง๋ ๋ค.
๐ท ๋ถํฌ๋ช ํ ํ๋กํ ์ฝ์ ์ํ ํ์ต ์ฌ๋ค๋ฆฌ
์ํคํ ์ฒ ๋ฌธ์๋ ESC ์๋ฎฌ๋ ์ด์ ์ ๊ฒ์ฆ๋ 4๊ณ์ธต์ผ๋ก ํ์ด๋ธ๋ค: W5500 MACRAW ์ ์ก โ ๋ฐ์ดํฐ๊ทธ๋จ ํ์ + Working Counter + AL FSM โ SII/EEPROM ์ ์ ํธ๋์ ฐ์ดํฌ โ FMMU ๋ ผ๋ฆฌ-๋ฌผ๋ฆฌ ๋งคํ. ๊ฐ ๊ณ์ธต์ TwinCAT์ด ๋ฐ์๋ค์ด๋ "๋ค์์ผ๋ก ๋์ด๊ฐ๊ธฐ ์ํ ์ต์ ๋จ์"๋ค. EtherCAT์ ๊ทธ๋ฅ ์ฐ๋ ๊ฒ ์๋๋ผ ์ดํดํ๋ ค๋ ๋๊ตฌ๋ ๋ฐ๋ผ๊ฐ ์ ์๋ ๋ก๋๋งต์ด๋ค.
๐ท ์ด์ ๊ฐ๋ฅํ ํ๋กํ ์ฝ ์ฝ์ด
๊ฐ์ฅ ๊น๋ํ ์ค๊ณ ํ๋จ: EtherCAT ๋ก์ง(ecat.c, sii.c, ESI ํ์ผ)์ด ์ ์ก ๊ณ์ธต๊ณผ ๋
๋ฆฝ์ ์ด๋ผ๋ ์ ์ด๋ค. ์ ์๋ DC ๊ฐ๋ฅํ ํ์ด๋ฐ์ ์ํด ์ค์ LAN9252๋ก ํฌํ
ํ ๋ ๋ฐ๋๋ ๊ฑด W5500 ์ ์ก ๊ณ์ธต๋ฟ์ด๊ณ ํ๋กํ ์ฝ ๋๋๋ ๊ทธ๋๋ก ๋์ด๊ฐ๋ค๊ณ ์ง๋๋ค. W5500์ ํ์ต์ฉ ๋ฒ์ ์ ์ธ๊ณ ์ ๊ทผ ๊ฐ๋ฅํ๊ฒ ๋ง๋๋ ์ญํ ์ด๋ค.
03 โ ์์คํ ์ํคํ ์ฒ

04 โ ์ W5500์ธ๊ฐ? โญ
๐ท ๋ชจ๋: MACRAW, ํ๋ก๋ฏธ์คํ์ด์ค โ ์ฝ๋๋ก ๊ฒ์ฆ๋จ
W5500์ด ๋ค์ด๋ง๋ ์ด์ ์ ํต์ฌ์ด ์ฌ๊ธฐ๋ค. EtherCAT ์ฌ๋ ์ด๋ธ๋ ์๊ธฐ IP๋ก ์จ ํ๋ ์๋ง์ด ์๋๋ผ ํ์ ์ ๋ชจ๋ ํ๋ ์์ ๋ด์ผ ํ๋ค โ EtherCAT์ ์ ์ด์ IP๋ฅผ ์ฐ์ง ์๊ณ , ๋ง์คํฐ๊ฐ ๋ฐ์ดํฐ๊ทธ๋จ์ ๋ชจ๋ ๋
ธ๋์ ํ๊ณ ์ง๋๊ฐ๊ธฐ ๋๋ฌธ์ด๋ค. W5500์ **MACRAW ์์ผ ๋ชจ๋(Sn_MR_MACRAW, 0x04)**๊ฐ ๋ฐ๋ก ๊ทธ ์ผ์ ํ๋ค: TCP/IP ์คํ์ด ๋ผ์ด๋ค์ง ์๋ ์ L2 ์ด๋๋ท ํ๋ ์. ํ์จ์ด๋ ์์ผ 0์ MACRAW๋ก ์ด๋ MAC ํํฐ ๋นํธ๋ฅผ ๋(ํ๋ก๋ฏธ์คํ์ด์ค) ์ํ๋ก ๋์ด, ESP32๊ฐ ๋ง์คํฐ์ ๋ชจ๋ EtherCAT ํธ๋ํฝ์ ์บก์ฒํ๋ค:
/* MACRAW mode. MF(bit 7) clear => promiscuous: ๋ชจ๋ ํ๋ ์ ์บก์ฒ */ w5500_wr8(W5500_BSB_S0_REG, W5500_Sn_MR, W5500_MR_MACRAW); socket_cmd(W5500_CMD_OPEN); /* ์งํ ์ ์ Sn_SR == 0x42(SOCK_MACRAW) ํ์ธ */์ด ํ๋ก์ ํธ๋ W5500์ ๊ทธ ์ ๋ช ํ ํ๋์จ์ด TCP/IP ์คํ๋ก๋(TOE)๋ก ์ฐ๋ ๊ฒ ์๋๋ผ, ์ ๋ฐ๋๋ก โ ์์ ํ ๋น์ผ์์ CPU์ ์ ํ๋ ์์ ๋๊ธฐ๋ ์ฉ๋๋ก ์ฐ๋ ๋๋ฌธ ์ฌ๋ก๋ค. ๋ณดํต์ ๋คํธ์ํฌ ์คํ์ ๋์ ์ฒ๋ฆฌํด์ค์ ์นญ์ก๋ฐ๋ ๋ฐ๋ก ๊ทธ ์นฉ์ด, ์ฌ๊ธฐ์๋ ๊ทธ ์ผ์ ์ ํด์ฃผ๊ธฐ ๋๋ฌธ์ ๊ฐ์น๊ฐ ์๋ค. ์์ L2 ์ ๊ทผ โ ๊ทธ๊ฒ์ด ํ๋๋ฒ์ค๊ฐ ํ์๋ก ํ๋ ์ ๋ถ๋ค.
๐ท ์ ํ์ต์ฉ์ผ๋ก๋ 5์ฒ ์์ง๋ฆฌ SPI ์นฉ์ด ์ ์ฉ ESC๋ฅผ ์ด๊ธฐ๋๊ฐ
์ค์ ESC(ET1100/LAN9252)๋ ๊ตฌํ๊ธฐ ์ด๋ ต๊ณ ์์ฒด ๋ฌธ์๋ ๊ฐํ๋ฅธ ํน์ ๋ถํ์ด๋ค. W5500์ ์ธ๊ณ , ์ด๋์๋ ์๊ณ , 3.3 V์ด๋ฉฐ, ์ด๋ค MCU์๋ SPI๋ก ๋ํํ๋ค. MACRAW + ํ๋ก๋ฏธ์คํ์ด์ค ์บก์ฒ๋ฅผ ์ ํํจ์ผ๋ก์จ, ์ ์๋ ๋ฒ์ฉ ์ด๋๋ท ์นฉ์ ์ฐ์ ์ฉ ํ๋๋ฒ์ค ๋ ธ๋์ ๋ฌผ๋ฆฌ ๊ณ์ธต์ผ๋ก ๋ฐ๊ฟ๋์๋ค. ์ง์ ์ฅ๋ฒฝ์ด "ESC๋ฅผ ์ฌ์ ์ตํ๋ผ"์์ "์ด๋ฏธ ๊ฐ๊ณ ์์ W5500์ ์ฐ๊ฒฐํ๋ผ"๋ก ๋ฌด๋์ง๋ค.
๐ท ํ๋์จ์ด ์ค์ค๋ก๊ฐ ๋งํ๋ ์์งํ ํธ๋ ์ด๋์คํ
W5500์ store-and-forward๋ค: ํ๋ ์์ ๋ฒํผ์ ์์ ํ ๋ฐ์ ๋ค์์ผ CPU๊ฐ ๋ณด๊ณ , ์์ ํ ๋ฐ์ ๋ค์์ผ ํ์ ํ๋ค. ๊ทธ๋์ DC์ sub-ms ์ฌ์ดํด์ ๋ถ๊ฐ๋ฅํ๊ณ , ์ ์๊ฐ ์ฝ 5~10 ms ๋์์ด๋ผ๊ณ ๋ฏธ๋ฆฌ ๋ฐํ๋ ์ด์ ๋ ๊ทธ๊ฒ์ด๋ค. ์นฉ์ ๋ณธ์ฑ์ด ํ๋ก์ ํธ์ ๋ฒ์๋ฅผ ์ ์ํ๋ฉฐ, ํ๋ก์ ํธ๋ ๊ทธ๊ฒ์ ์จ๊ธฐ์ง ์๊ณ ์ ํํ ๋ฌธ์ํํ๋ค.
๐ท ๊ฒ์ฆ๋ ์ฆ๊ฑฐ โ
- ๋ถํ
๋ก๊ทธ๊ฐ ์นฉ์ ํ์ธ:
VERSIONR = 0x04, ์์ผ์ดSn_SR = 0x42(SOCK_MACRAW) ๋๋ฌ - TwinCAT์ด ํ์ค SII/EEPROM ์ ์ ํธ๋์
ฐ์ดํฌ๋ก ์ฅ์น๋ฅผ ์ด๋ฆ(
ESP32-DO8)์ผ๋ก ์ธ์ - AL ์ํ๋จธ์ ์ด OP๊น์ง ์ง์
(
AL state -> 0x08) - TwinCAT์
WcState = 0์ด Working Counter๊ฐ ์ ์ ์ฆ๊ฐํจ์ ํ์ธ - TwinCAT์์ ์ถ๋ ฅ ๋นํธ๋ฅผ ์ฐ๋ฉด ํด๋น ๋ฌผ๋ฆฌ GPIO๊ฐ High๋ก โ ๋ง์คํฐ์์ ํ๊น์ง ํ๋ฃจํ
05 โ ํต์ฌ ๊ตฌ์ฑ์์
๐ WIZnet W5500 โ MACRAW(ํ๋ก๋ฏธ์คํ์ด์ค) ์ ์ด๋๋ท ํ๋ฐํธ์๋
์์ผ 0์ Sn_MR_MACRAW๋ก ์ด๊ณ MAC ํํฐ ๋นํ์ฑํ, ๋จ์ผ ์์ผ์ 16 KB RX/TX ๋ฒํผ ํ ๋น. ๋ค์ดํฐ๋ธ ESP-IDF SPI ๋ง์คํฐ ๋๋ผ์ด๋ฒ(Arduino ๋ฏธ์ฌ์ฉ), VSPI ํ, ์๋ CS๋ก [์ฃผ์์์][์ฃผ์ํ์][์ ์ด][๋ฐ์ดํฐ] ํ๋ ์์ ์์์ ์ผ๋ก ์ ์ก. 2๋ฐ์ดํธ ๋น
์๋์ธ ๊ธธ์ด ํค๋๊ฐ ์๊ธฐ ์์ ์ ํฌํจํ๋ MACRAW RX ํน์ ์ ํจ์ ๋ ์ฒ๋ฆฌ.
๐ง ESP32-WROOM-32 (S3 ์๋, ์ค๋ฆฌ์ง๋)
net_task๋ฅผ CPU1์ ๊ณ ์ ํด busy-poll ๋ฃจํ๋ก ๋๋ ค ํ๋ ์ ์๋ณต์ ์ด์ดํ ์ ์งํ๊ณ EtherCAT ๋งํฌ๋ฅผ ์์ ํ; CPU0๋ ์์คํ
์์
์ฉ.
โ๏ธ ์ํํธ์จ์ด ESC ์ฝ์ด (ecat.c, 338์ค)
EtherCAT 4๊ฐ์ง ์ฃผ์์ง์ ๋ชจ๋(์๋์ฆ๊ฐ AP*, ์ค์ FP*, ๋ธ๋ก๋์บ์คํธ B*, ๋
ผ๋ฆฌ L*) ๋ฐ์ดํฐ๊ทธ๋จ ํ์, ์ฌ๋ฐ๋ฅธ Working Counter ๊ท์น, AL ์ ํ์ํ๋จธ์ , ํ๋ก์ธ์ค RAM์ผ๋ก์ FMMU ๋
ผ๋ฆฌโ๋ฌผ๋ฆฌ ๋งคํ(์ด๊ฒ GPIO๋ฅผ ๊ตฌ๋).
๐พ SII EEPROM ์๋ฎฌ๋ ์ด์
(sii.c)
ESC EEPROM ๋ ์ง์คํฐ ์ธํฐํ์ด์ค๋ฅผ CRC8๊ณผ ํจ๊ป ์๋ฎฌ๋ ์ด์ ํด ๋ง์คํฐ๊ฐ ์ ํํ ํ์ค ํธ๋์ ฐ์ดํฌ๋ก ์ฅ์น ์ ์์ ์ฝ๊ฒ ํจ โ ESI์ ์๋ฒ ๋ํ ์ปค์คํ 16ร14 ์ฅ์น ์์ด์ฝ ํฌํจ.
๐ ESI ์ฅ์น ๊ธฐ์ ์ (esi/ESP32_DO8.xml)
TwinCAT ์ธก์ ์ค์นํด ์ฌ๋ ์ด๋ธ๊ฐ 8์ถ๋ ฅ I/O ๋งต์ ๊ฐ์ง ์ด๋ฆ ์๋ ์ฅ์น๋ก ๋ํ๋๊ฒ ํจ.
06 โ ์์ฉ ์๋๋ฆฌ์ค
01. ์ค์ตํ EtherCAT ๊ต์ก
์๋๋ ์ฉ๋: ESC ์ค๋ฆฌ์ฝ์ ์ฌ์ง ์๊ณ ๋, EtherCAT ์ฌ๋ ์ด๋ธ๊ฐ ๋ง์คํฐ์ ์ด๋ป๊ฒ ๋ํํ๋์ง ๊ณ์ธต๋ณ๋ก ์ค์ ๋ก ๋ฐฐ์ฐ๋ ์ธ๊ณ ์์ ํ ๋ฌธ์ํ๋ ๋ฐฉ๋ฒ.
02. ๋น ๋ฅธ ํ๋๋ฒ์ค ํ๋กํ ํ์ดํ
๋ง์คํฐ ์ค์ ์ด๋ ESI ํ์ผ์ ํ ์คํธํ๋ ค๊ณ ๋ฒค์น์ ๋ฒ๋ ค๋ ๋๋ EtherCAT I/O ๋ ธ๋๊ฐ ํ์ํ ๋, ESP32 + W5500์ด ์ํ๋ ์ฌ์ดํด ํ์์์ ์ค์ ์ฌ๋ ์ด๋ธ๋ฅผ ๋์ ํ๋ค.
03. LAN9252๋ฅผ ํฅํ ์์ฐ ๋๋ค๋
ํ๋กํ ์ฝ ์ฝ์ด๊ฐ ์ ์ก ๋
๋ฆฝ์ ์ด๋ผ, DC ๊ฐ๋ฅํ LAN9252 ์ค๊ณ๋ก ๋์ด๊ฐ๊ธฐ ์ ์์ฐ์ค๋ฌ์ด ์ฒซ๊ฑธ์์ด๋ค โ ecat.c/sii.c/ESI ์์
์ด ๊ทธ๋๋ก ๋์ด๊ฐ๋ค.
04. MACRAW ๊ทธ ์์ฒด์ ๊ต๋ณธ
W5500์ ํ๋ก๋ฏธ์คํ์ด์ค MACRAW ๋ชจ๋๋ก ์ฐ๋ ๊น๋ํ ์ค์ ๋ ํผ๋ฐ์ค โ ๊ฐ์ ์นฉ์ผ๋ก ํจํท ์ค๋ํผ, ์ปค์คํ L2 ํ๋กํ ์ฝ, ํ๋๋ฒ์ค ํ๋ฐํธ์๋๋ฅผ ๋ง๋๋ ๋๊ตฌ์๊ฒ๋ ์ ์ฉ.
๊ฒฐ๋ก
์ทจ๋ฏธ์ฉ ESP32 ์์ ํ์จ์ด๋ก ์๋ฎฌ๋ ์ด์ ํ ์ฐ์ ์ฉ EtherCAT ์ฌ๋ ์ด๋ธ โ W5500์ด ๋ถํ๋ฐ์ผ๋ฉด TCP/IP ์นฉ์ด๊ธฐ๋ฅผ ๋ฉ์ถ๊ณ ๊ทธ๋ฅ ์ ์ด๋๋ท ํ๋ ์์ ๊ฑด๋ค์ฃผ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ๋ค.
- โ ์์ ํ ์ํํธ์จ์ด ESC: ๋ฐ์ดํฐ๊ทธ๋จ ํ์, Working Counter, AL FSM, SII EEPROM, FMMU
- โ ์ค์ Beckhoff TwinCAT 3 ๋ง์คํฐ๋ก ๊ฒ์ฆ โ OP๊น์ง ์ง์ , ์ค์๊ฐ I/O ๊ตฌ๋
- โ
W5500 MACRAW ํ๋ก๋ฏธ์คํ์ด์ค ๋ชจ๋(
Sn_MR=0x04,Sn_SR=0x42) ์ฝ๋ยท๋ถํ ๋ก๊ทธ๋ก ํ์ธ - โ ์ ์ก ๋ ๋ฆฝ์ ํ๋กํ ์ฝ ์ฝ์ด โ DC ๊ฐ๋ฅ ํ์ด๋ฐ์ ์ํด LAN9252๋ก ์ด์ ๊ฐ๋ฅ
- โ ์์งํ๊ณ ์ ํํ ํ๊ณ ๋ช ์(store-and-forward โ ์ฝ 5~10 ms, DC ๋ถ๊ฐ)๊ฐ ํ์ต ์๋ฃ๋ก์์ ์ ๋ขฐ๋ฅผ ๋ง๋ฆ
- โ ์๋น๋ ๋ฌธ์: ๋ฐฐ์ ๋, ๊ณ์ธต๋ณ ์ํคํ ์ฒ, TwinCAT ์ค์ ๊ฐ์ด๋
ํ๋์จ์ด TCP/IP ์คํ๋ก๋์์ ์ L2 ์ฐ์ ์ฉ ํ๋๋ฒ์ค ์ ๊ทผ๊น์ง โ W5500์ ์ด๋๋ท์ด ํ์ํ ๊ณณ์ด๋ผ๋ฉด ์ด๋๋ , ์ฌ์ง์ด ์๊ธฐ ๊ฐํ ๊ธฐ๋ฅ์ ์ผ๋ถ๋ฌ ๋ EtherCAT ๋ ธ๋ ์์์๋ ๋ชจ์ต์ ๋๋ฌ๋ธ๋ค. TOE ์นฉ์ ๊ฐ์ฅ ์์ธ์ ํ์ฉ์, TOE๋ฅผ ์ ํ ์ฐ์ง ์๋ ํ์ฉ์ด๋ค.
Q&A
Q. W5500์ ์ด๋ค ์์ผ ๋ชจ๋๋ฅผ ์ฐ๋ฉฐ, ์ TOE๊ฐ ์๋๊ฐ์? MACRAW(Sn_MR_MACRAW, ๊ฐ 0x04), MAC ํํฐ๋ฅผ ๋ ํ๋ก๋ฏธ์คํ์ด์ค ์บก์ฒ์
๋๋ค โ w5500.c์์ ์ง์ ํ์ธ๋ฉ๋๋ค. EtherCAT์ IP ์๋ ์ L2 ์ด๋๋ท์ผ๋ก ๋์ํ๊ณ ๋ง์คํฐ์ ๋ฐ์ดํฐ๊ทธ๋จ์ด ๋ชจ๋ ๋
ธ๋๋ฅผ ํ๊ณ ์ง๋๊ฐ๋ฏ๋ก, ์ฌ๋ ์ด๋ธ๋ ๋ชจ๋ ํ๋ ์์ ์บก์ฒํด์ผ ํฉ๋๋ค. W5500์ ํ๋์จ์ด TCP/IP(TOE)๋ ์คํ๋ ค ๋ฐฉํด๊ฐ ๋๋ฉฐ, ์ฌ๊ธฐ์๋ MACRAW๊ฐ ์ ์ผํ๊ฒ ์ฌ๋ฐ๋ฅธ ์ ํ์
๋๋ค.
Q. ์ค์ ์ธ๊ฐ์, ์๋ฎฌ๋ ์ด์
์ธ๊ฐ์? ํ๋์จ์ด ์์ ์ค์ ์
๋๋ค. ๋ถํ
๋ก๊ทธ๊ฐ W5500 VERSIONR๊ณผ MACRAW ์์ผ ์ํ๋ฅผ ์ฝ์ด ํ์ธํ๊ณ , ์ค์ TwinCAT 3 ๋ง์คํฐ ์๋์์ ์ฅ์น๊ฐ ์ธ์๋์ด OP์ ๋๋ฌํ๋ฉฐ ๋ฌผ๋ฆฌ GPIO๋ฅผ ๊ตฌ๋ํฉ๋๋ค. ์ ์ ์ค์ค๋ก์ ํํ๋๋ก ๊ฐ๋
๊ฒ์ฆ์ด๋ฉฐ ์ธ์ฆ ์ ํ์ ์๋๋๋ค.
Q. ์ ํ๋ ๋ฆฌ์ผํ์ EtherCAT์ ์ ๋๋์? W5500์ด store-and-forward์ด๊ธฐ ๋๋ฌธ์ ๋๋ค: ํ๋ ์์ CPU๊ฐ ๋ณด๊ธฐ ์ ์, ๊ทธ๋ฆฌ๊ณ ํ์ ํ๊ธฐ ์ ์ ๋ฒํผ์ ์์ ํ ๋ฐ์์ผ ํ๋ฏ๋ก Distributed Clocks์ sub-ms ์ฌ์ดํด์ด ๋ถ๊ฐ๋ฅํฉ๋๋ค. ์ด๋ ์นฉ ๊ณ ์ ์ ํน์ฑ์ด๊ณ , ํ๋ก์ ํธ๋ ์ด๋ฅผ ๋ช ํํ(์ฝ 5~10 ms ์ฌ์ดํด) ๋ฐํ๋๋ค. DC ๊ฐ๋ฅ ํ์ด๋ฐ์ ๋์ผ ํ๋กํ ์ฝ ์ฝ๋๋ฅผ ์ฌ์ฌ์ฉํ๋ LAN9252 ํฌํ ์ผ๋ก ์๋ดํฉ๋๋ค.
Q. ์ฝ๋๋ ์์ ํ๊ณ ๋น๋ ๊ฐ๋ฅํ๊ฐ์? ๋ค. ESP-IDF ํ๋ก์ ํธ(v6.0.1์์ ๊ฐ๋ฐ)๋ก CMakeLists.txt, sdkconfig.defaults, ์์ ํ main/ ์์ค(W5500 ๋๋ผ์ด๋ฒ, ESC ์ฝ์ด, SII ์๋ฎฌ๋ ์ด์
), ESI ํ์ผ, ๊ทธ๋ฆฌ๊ณ WIRING.mdยทARCHITECTURE.md๋ฅผ ๊ฐ์ท์ต๋๋ค. ๋น๋/ํ๋์/๋ชจ๋ํฐ ์ ์ฐจ์ ์ ์ฒด TwinCAT ์ค์ ์ด ๋ฌธ์ํ๋์ด ์์ต๋๋ค.