**LwIP Definition**
LwIP, short for Lightweight IP, is a small, open-source TCP/IP protocol stack developed by Adam Dunkels. It is an open-source protocol that has been collaboratively developed worldwide and serves as a core component of the TCP/IP protocol suite. It includes essential protocols such as ARP, ICMP, TCP, UDP, IPv4, IPv6, and DHCP. Its key features include being full-featured while requiring minimal RAM and ROM, making it ideal for embedded systems.
**Programming Model**
LwIP allows users to configure and customize its functions based on their needs. The configuration file is `lwipopts.h`. It supports both systems with and without an operating system. Its internal architecture is built around an external single-threaded driver protocol stack state machine, and it uses interrupts for data reception at the lower level. LwIP provides three main APIs: RAW API, LwIP API, and BSD API. The BSD API is the most familiar one, similar to socket interfaces used in Linux and Windows platforms.
**Porting**
Porting LwIP to different platforms mainly involves two parts:
1. Porting the MAC + PHY layer, including initialization, data transmission, and reception.
2. Porting the application layer framework, such as thread creation, timer management, and message mailbox handling in the OS layer.
**Platform**
Hardware: STM32F107, PHY chip: DM9161AEP
Software: UCOS-II
**Porting Points**
STMicroelectronics provides an LwIP port for STM32F107 without an operating system, named `STM32F107_ETH_LwIP`, version V1.0.0. However, this version is no longer maintained and may not be compatible with current software environments, so it is not recommended for reference.
The Ethernet drivers for STM32F1, STM32F2, and STM32F4 are similar. To proceed, download `STM32CubeF2` from the ST website, which includes LwIP with FreeRTOS. Since FreeRTOS and UCOS-II have similar structures, modifying the application layer framework should allow successful integration. Relevant code can be found in the folder: `STM32Cube_FW_F2_V1.1.0\Projects\STM322xG_EVAL\Applications\LwIP\LwIP_UDPTCP_Echo_Server_Netconn_RTOS`.
LwIP version 1.4.1 is available on the official site and is also included in `STM32CubeF2`. The theoretical basis for the port comes from the documentation in the `doc` folder of the LwIP source package. Additionally, the official example files, `contrib-1.4.1.zip`, can be downloaded from the official site.
**MAC + PHY Porting**
Modify the following files:
- `app_ethernet.c/h`
- `ethernetif.c/h`
Also, copy `stm32f2xx_hal_eth.c/h` from the STM32CubeF2 driver library. These files only need basic configuration to ensure successful compilation, completing the MAC + PHY layer migration.
**Application Layer Framework Porting**
Modify a single file: `sys_arch.c`, located in `STM32Cube_FW_F2_V1.1.0\Middlewares\Third_Party\LwIP\system`. Once this is done, the porting process is complete.
**Implementing LwIP DHCP for Automatic IP Address Acquisition**
For the LM3S series, the implementation involves:
1. Enabling `#define LWIP_ARP 1` and `#define LWIP_DHCP 1` in `opt.h`.
2. Enabling `#define LWIP_DHCP 1` and `#define DHCP_DOES_ARP_CHECK 1` in `lwipopts.h`.
3. Adding `#include "lwip/dhcp.h"` in `lwiplib.c`.
4. Modifying the static variable `unsigned long g_ulIPMode = IPADDR_USE_DHCP` and calling `itwIPInit(MACAddress, xIpAddr, xNetMask, xGateway, IPADDR_USE_DHCP);` in `lwiplib.c`.
**Debugging Process**
During testing, I noticed that the board sent a DHCP broadcast every few seconds to `255.255.255.255`. Initially, I thought there was a problem. After reviewing the DHCP protocol, I realized the router wasn’t responding. I tried setting my PC to obtain an IP address automatically but had no success. Eventually, I discovered that the router hadn’t been restarted after enabling the DHCP function. After restarting, the board broadcasted DHCP again, and the router responded with an ARP packet containing the assigned IP address. The board then sent three requests to confirm the IP, avoiding duplicates. Afterward, the connection worked, even though the router didn’t show the device as connected. A ping test confirmed the IP was successfully obtained, and the board could communicate over the network.
**How DHCP Works**
DHCP operates differently depending on whether the client is connecting for the first time.
**First-Time Login**
When a client connects for the first time, it sends a DHCP DISCOVER packet to the network. This packet is broadcasted using `0.0.0.0` as the source and `255.255.255.255` as the destination. If no response is received within the default wait time (usually 1 second), the client will retry up to four times. If no server responds, the client displays an error.
**Offering an IP Lease**
Upon receiving a DISCOVER, the DHCP server selects an available IP address and sends a DHCP OFFER back to the client. The OFFER contains the IP address, subnet mask, gateway, and lease duration.
**Accepting the Lease**
If multiple servers respond, the client chooses the first one and sends a DHCP REQUEST to confirm the IP. It then performs an ARP check to ensure the IP is not already in use. If it is, the client rejects the offer and resends the DISCOVER.
**Lease Confirmation**
Once the server receives the REQUEST, it sends a DHCP ACK to confirm the lease. The client now has a valid IP address.
**Subsequent Logins**
After the initial setup, the client sends a DHCP REQUEST directly to the previous server, asking to renew the lease. If the lease expires or the IP is taken, the server may send a DHCP NACK, prompting the client to start the discovery process again.
**DHCP Across Networks**
DHCP DISCOVER packets are broadcasted and cannot cross routers unless a DHCP proxy is used. In large networks, deploying a DHCP server in each segment is common, but using a proxy can simplify management.
Full Outdoor Digital Signage,Full Outdoor Signage,Waterproof Lcd Display,High Brightness Digital Signage
Shenzhen Risingstar Outdoor High Light LCD Co., Ltd , https://www.risingstarlcd.com