**LwIP Definition**
LwIP, short for Lightweight IP, is a small and 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 family. It includes protocols such as ARP, ICMP, TCP, UDP, IPv4, IPv6, and DHCP. Its main features are its rich functionality while requiring minimal RAM and ROM resources for operation.
**Programming Model**
All functions and performance can be configured and customized according to specific needs. The configuration file is `lwipopts.h`. LwIP supports both systems with and without an operating system. Its core architecture is based on an external single-threaded driver protocol stack state machine. At the lower level, it uses interrupts to receive data. It offers three API interfaces: 1) RAW API, 2) LwIP API, and 3) BSD API. The BSD API is the most familiar socket interface, similar to the socket APIs used in Linux and Windows platforms.
**Porting**
Porting LwIP to different platforms involves two main parts:
1. Migration of the MAC and PHY layer, including initialization, data transmission, and reception.
2. Porting the application layer framework, such as thread creation, timers, and message mailboxes from the operating system layer.
**Platform**
Hardware: STM32F107, PHY chip: DM9161AEP
Software: UCOS-II
**Porting Points**
ST provides a version of LwIP for the STM32F107 without an operating system, named `STM32F107_ETH_LwIP`, version V1.0.0. However, this version is outdated and no longer maintained, so it is not suitable for reference. The Ethernet drivers for STM32F1, STM32F2, and STM32F4 are similar. To port LwIP, download `STM32CubeF2` from the ST official website, which includes LwIP with FreeRTOS support. Since FreeRTOS and UCOS-II have similar structures, modifying the application layer framework will allow successful porting. Relevant code files include `STM32Cube_FW_F2_V1.1.0\Projects\STM322xG_EVAL\Applications\LwIP\LwIP_UDPTCP_Echo_Server_Netconn_RTOS`.
LwIP version 1.4.1 can be downloaded from the official site or found within the STM32CubeF2 package. The theoretical basis comes from the `doc` folder in the LwIP 1.4.1 source package. Additionally, the official example `contrib-1.4.1.zip` is also useful for cross-platform porting.
For MAC and PHY layer migration: Modify files like `app_ethernet.c/h` and `ethernetif.c/h`, and copy `stm32f2xx_hal_eth.c/h` from the STM32CubeF2 driver library. As long as these files compile correctly, the MAC and PHY layer is successfully ported.
For the application layer framework: Modify `sys_arch.c` located at `STM32Cube_FW_F2_V1.1.0\Middlewares\Third_Party\LwIP\system`. All porting tasks are now complete.
**Implementing LwIP to Automatically Obtain IP Address via DHCP**
On the LM3S series, the process involves enabling `#define LWIP_ARP 1` and `#define LWIP_DHCP 1` in `opt.h`, and `#define LWIP_DHCP 1` and `#define DHCP_DOES_ARP_CHECK 1` in `lwipopts.h`. Include `"lwip/dhcp.h"` in `lwiplib.c`, and set `static unsigned long g_ulIPMode = IPADDR_USE_DHCP` and call `itwIPInit(MACAddress, xIpAddr, xNetMask, xGateway, IPADDR_USE_DHCP);`.
During debugging, I noticed that the board broadcasted a DHCP request every few seconds. Initially, I was unsure why the router wasn’t responding. After checking the DHCP protocol, I realized the router had not been restarted after enabling the DHCP function. Once the router was restarted, the board sent a DHCP broadcast, and the router responded with an ARP packet containing the assigned IP address. The board then sent three requests to confirm the IP, avoiding conflicts. Although the board didn’t appear connected on the router, pinging it worked, confirming that the IP was successfully obtained. I still don’t know why the connection wasn’t shown on the router, but the network communication was functional.
**How DHCP Works**
The DHCP process varies depending on whether the client is logging in for the first time.
**First-Time Login**
When a client connects to the network for the first time, it sends a DHCP DISCOVER packet to the network. Since it doesn’t have an IP address, it broadcasts to `255.255.255.255`. If no response is received within the preset wait time (1 second), it will resend the request up to four times. If no server responds, the client displays an error.
**Offering IP Lease**
Upon receiving the DHCP DISCOVER, the server selects an available IP address and responds with a DHCP OFFER, including lease information. The client then sends a DHCP REQUEST to accept the offer.
**Accepting the Lease**
If multiple servers respond, the client chooses the first one and sends a DHCP REQUEST to confirm. It also performs an ARP check to ensure the IP isn't already in use. If the IP is taken, it rejects the offer and resends the DISCOVER.
**Lease Confirmation**
Once the server receives the DHCP REQUEST, it sends a DHCP ACK to confirm the lease.
**Subsequent Logins**
After the initial login, the client sends a DHCP REQUEST directly to the previous server. If the lease expires or the IP is unavailable, the server may send a DHCP NACK, prompting the client to restart the process.
**DHCP Across Networks**
Since DHCP DISCOVER packets are broadcasted, they only work within the same local network. For remote servers, a DHCP Proxy or relay agent is needed to forward the request. Alternatively, installing a DHCP server in each network is another option, though less efficient.
Dual Sided Digital Sign,Dual Sided Screen,Double Facing Display,Dual Facing Lcd Signage
Shenzhen Risingstar Outdoor High Light LCD Co., Ltd , https://www.risingstarlcd.com