Ground Embedded KO Subscribe

Notes / Communication Protocols

Ethernet and TCP/IP: networking on an MCU

How MAC, PHY, IP and TCP/UDP share the work, and the structure for adding networking with STM32 + LwIP.

GROUND EMBEDDED · NOTECommunication Protocols 10Ethernet and TCP/IPUARTSPII2CCAN

Role of each layer

App dataTCP · UDPport numberIPIP addressMACMAC addressPHYelectrical signal
As data goes down the layers, each one adds a header.
Layer What it does On an MCU
PHY Electrical signals on the wire External chip (LAN8742 and similar)
MAC Sending and receiving Ethernet frames Built into the MCU
IP Routing by address Software stack
TCP / UDP Connections and retransmission / simple delivery Software stack
Application HTTP, MQTT, Modbus TCP Your code

The MAC and PHY are connected by the RMII interface (50 MHz clock), and the PHY is configured over MDIO.

LwIP

A lightweight TCP/IP stack for MCUs. Enable ETH + LWIP in CubeMX and you get DHCP, ping and TCP sockets. Used with an RTOS, the socket API is convenient.

RoadmapSee where this note sits in Communication Protocols→

Choosing TCP or UDP

  • TCP: guaranteed order, retransmission. Configuration changes, file transfer, web
  • UDP: fast and simple, loss is possible. Real-time sensor streams, discovery broadcasts

Vehicles and industry

Automotive Ethernet (100BASE-T1) carries 100 Mbps over a single pair, and industry uses variants that guarantee real-time behaviour, such as EtherCAT. The hardware differs but the thinking in the upper layers is the same.

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →