lwIP Study Log
lwIP Study Log
I plan to learn lwIP based on the tutorials from ALIENTEK (正点原子). The board I'm using is an ALIENTEK stm32f407zgt6 minimum system board plus a LAN8720A for network communication.
The development IDE is CLion, using the open-source embedded toolchain: arm-gnu-toolchain-15.3.rel1-mingw-w64-i686-arm-none-eabi
Reference book: 《lwIP开发指南_V1.7.pdf》
How Network Data Flows

Looking at this diagram, the data goes from the Ethernet cable connected to the MCU to being processed inside the MCU. The overall logic is like this:
The data travels through the cable as differential signals until it reaches the RJ45 port (the external PHY), then the LAN8720A converts it into 0/1 binary data and passes it through its RMII interface to the STM32's MAC core. The MAC has transmit and receive buffers, one each, 2KB apiece (IEEE specifies a maximum frame of about 1520B, so 2KB is more than enough). After the MAC core acquires a frame of data, it tells the MCU's DMA to move that frame into a designated memory region. There are multiple such memory regions, and access to each one is controlled by a DMA descriptor (a struct).
- What is a PHY?
The physical layer in computer networking; it handles the conversion between optical/electrical analog signals and digital signals.
There are two commonly used PHY chips: LAN8720A and YT8512C.
- What is RMII?
RMII is the "streamlined high-speed data channel" between the MAC inside the STM32 and the external PHY.
- What is a MAC, then?
MAC (Media Access Control) is the hardware controller inside the STM32 specifically responsible for "packing/unpacking Ethernet data".
At the data link layer, it ensures proper communication between two hosts connected by a single cable, uses MAC addresses to determine the target host, and also handles splitting datagrams, error detection, CSMA/CD, and so on. See the data link layer chapter of any computer networking textbook for details.
Ethernet DMA Descriptors
The so-called descriptor is the DMA struct used to control the memory regions mentioned above.
They come in primary and enhanced forms; the primary form only uses the first 4 members, each member being 32 bits and split for different uses.
DESC3 at the end tells you where the next descriptor is. Once you know the descriptor, you know which memory region to put the data into. The DESC3 of the last descriptor is the address of the first descriptor, forming a ring, so data can be pulled continuously from the MAC into memory.
The role of this descriptor is essentially that of a "helper", so who uses this helper? Refer to this struct:
If we compare configuring Ethernet to opening a courier station, then ETH_DMADescTypeDef (the descriptor) is the "shipping label", while ETH_InitTypeDef is the "opening-configuration checklist" for the station. It tells the STM32's Ethernet peripheral: "what your address is, which interface to use, where the labels are stored, and how big the parcel lockers are".
- uint8_t *MACAddr;
Points to an array containing 6 bytes, used to set the board's physical address (MAC address).
- ETH_MediaInterfaceTypeDef MediaInterfac
Selects the hardware interface type between the MAC and the PHY. Usually ETH_MEDIA_INTERFACE_RMII
- ETH_DMADescTypeDef *TxDesc;
The "helper" mentioned just now — it specifies which memory region the data is transferred to
- ETH_DMADescTypeDef *RxDesc;
Same as above
- uint32_t RxBuffLen;
Sets the size of the receive buffer (usually 1524 or 1536 bytes, slightly larger than the Ethernet maximum frame of 1518 bytes).
Here you can see the definitions of these descriptors, along with their assignments, etc.


Porting lwIP
Although lwIP can be ported extremely quickly via CubeMX, I still wanted to experience doing it by hand — I feel it'll leave a deeper impression.
lwIP source code site

Adding the Network Card Driver
Never mind, it's still too much hassle — I'll skip it. I'll write it another time if fate allows.
lwIP Memory Management
lwIP provides two simple and efficient memory management mechanisms: dynamic memory pool management and dynamic memory heap management.
- Dynamic memory heap (Heap): like the standard C malloc/free, it can allocate memory blocks of any size — flexible, but prone to fragmentation.
- Dynamic memory pool (Pool): a bunch of fixed-size blocks are cut up in advance, so allocation and freeing are extremely fast and there's no fragmentation, but it only suits fixed-size data structures.
lwIP also supports memory allocation using malloc and free. However, lwIP does not recommend using the C standard library's memory allocation strategy.
Changelog
0ca31-on