Great God teaches you: embedded LWIP network client design

introduction

The rise of embedded technology has made traditional PC-based Internet technology no longer advantageous. Embedded network client and server technology have become hot spots. This technology requires a protocol stack with high portability and low resource consumption. Lightweight TCP/ The LWIP (light weight Internet protocol) of the IP protocol stack is suitable for the case where the storage capacity of the embedded device is limited, and the basic function of the TCP/IP protocol stack can be implemented without affecting the network interconnection and transmission services of the device. Lightweight network protocols LWIP-attached hardware operating systems include RT_Thread[2-3] and uC/OS-II[4-5], given that uC/OS-II is a widely used and mature technology operating system. This article uses uC/OS-II operating system to design the network client. The experimental results show that: ping 32 bytes of data, the correct transmission and reception, the error rate is 0%.

1 system hardware

As shown in Figure 1, the hardware uses the STM32 processor as the main control chip, and is connected to the Internet through the Ethernet controller EN28J60 and RJ45 interfaces. In order to perform function expansion and test verification, the main control chip can also perform data transmission between the serial port and the computer. .

Great God teaches you: embedded LWIP network client design

STM32F107 is the main control chip. It not only has USB OTG and CAN2.0B interfaces, but also integrates the Ethernet 10/100 MAC module on the chip. The module supports both MII and RMII modes. Therefore, the host controller can implement a complete Ethernet transceiver simply by connecting a physical layer PHY chip; in the clock signal, only a 25MHz crystal oscillator can be used to give the entire The main controller provides the clock, and the main controller can also output a 25MHz or 50MHz clock to the external physical layer PHY chip, which can save an additional crystal oscillator for the system [6].

The Ethernet controller is the ENC28J60, which conforms to all the specifications of the IEEE 802.3 protocol and can use a robust packet filtering mechanism to restrict incoming data packets. The DMA module is integrated to support high-speed data throughput, and hardware IP address checksum calculation is implemented. Its communication with the master controller relies on two interrupt pins and SPI bus, data transmission rate up to 10 MB / s, in order to observe the active state of data transmission, can be connected to the two dedicated pins LED [7 ].

2 OS migration

The main contents involved in the uC/OS-II porting of the operating system are [4-5][8]: Modify and integrate the processor-related source files, including os_cpu_c.c, os_cpu_a.s, and os_cpu.h. The os_cpu.h file is responsible for defining the compiler-related data types and stack types. In addition, there are several macro definitions and function declarations. During the migration work, due to the same data type, different compilers support The data length is not the same, so you need to modify the original data type. The os_cpu_a.s file is mainly responsible for defining the task switching functions related to the processor, realizing the task context switching to meet the needs of the task scheduling, and also defining the clock interrupt processing function and the critical section advance and retreat macro. The os_cpu_c.c file is mainly responsible for defining the initialization function of the stack to facilitate stack protection of the operation data by the operating system when the task is switched or interrupted, and also defines the related HOOK function.

First modify the os_cpu_a.asm file and change the original RSEG CODE:CODE:NOROOT(2) to:

Great God teaches you: embedded LWIP network client design

AREA |.text|, CODE, READONLY, ALIGN=2; (where AREA|.text| represents a selection segment |.text|, CODE indicates a code segment, READONLY stands for the default case: read only. Since ALIGN=n, then the word The number of sections is 2^n, so ALIGN=2 here means that 4 bytes are aligned.)

THUMB ;Thumb instruction set

REQUIRE8 ; indicates that the current file is an eight-byte aligned stack requirement

PRESERVE8 ; indicates that the current file belongs to an eight-byte aligned stack

Modify the os_cpu.h file and comment out the following three functions: Void OS_CPU_SysTIckHandler(void); Void OS_CPU_SysTIckInit(void); UINT32 OS_CPU_SysTIckClkFreq(void);

Modify the os_cpu_c.c file and comment out the following definitions and functions:

#define OS_CPU_CM3_NVIC_ST_CTRL (*((volaTIle INT32U *)0xE000E010))

#define OS_CPU_CM3_NVIC_ST_RELOAD (*((volatile INT32U *)0xE000E014))

#define OS_CPU_CM3_NVIC_ST_CURRENT (*((volatile INT32U *)0xE000E018))

#define OS_CPU_CM3_NVIC_ST_CAL (*((volatile INT32U *)0xE000E01C))

#define OS_CPU_CM3_NVIC_PRIO_ST (*((volatile INT8U *)0xE000ED23))

#define OS_CPU_CM3_NVIC_ST_CTRL_COUNT 0x00010000

#define OS_CPU_CM3_NVIC_ST_CTRL_CLK_SRC 0x00000004

#define OS_CPU_CM3_NVIC_ST_CTRL_INTEN 0x00000002

#define OS_CPU_CM3_NVIC_ST_CTRL_ENABLE 0x00000001

#define OS_CPU_CM3_NVIC_PRIO_MIN 0xFF

Void OS_CPU_SysTickHandler (void) function

Void OS_CPU_SysTickInit (void) function

3 Lightweight protocol stack LWIP transplantation

LWIP is a lightweight TCP/IP protocol stack. The functions involved in the protocol stack are not related to the data structure and the operating system and hardware. If you need to use the functions of the uC/OS-II operating system, you must call it through the operating system simulation layer. . Therefore, transplanting the LWIP protocol stack is actually a migration to the uC/OS-II operating system. The operating system simulation layer provides a set of external interface functions for services such as timers, synchronization processing, and messaging mechanisms, and provides LwIP with two types of communication between processes: semaphores and mailboxes. The creation of task functions, critical protection functions, and semaphores and mailbox operation functions are all provided by uC/OS-II. When porting tasks to LwIP, the related interface functions (including semaphore operation functions, mailbox operation functions, and critical protection functions) are modified. , sys_thread_new() function, sys_arch_timeouts() function), in order to realize the function utilization of the simulation layer of LwIP operating system [4~5].

The packet header of LWIP is 14 bytes, and the packet format received by Ethernet based on LWIP is described by a data structure: PACK_STRUCT_BEGIN

Struct eth_hdr {

PACK_STRUCT_FIELD(struct eth_addr dest); //Target media access control layer address

PACK_STRUCT_FIELD(struct eth_addr src); // source media access control layer address

PACK_STRUCT_FIELD(u16_t type); //Type

} PACK_STRUCT_STRUCT;

PACK_STRUCT_END

Several of the PACK_STRUCT_xxx macro definitions are related to the compiler word alignment. The above three fields of the target dest, source src, and type type correspond to the target media access control layer address, source media access control layer address, and data type, respectively.

4 Conclusion

Before testing, the IP addresses of the computer and the embedded network interface module need to be on the same network segment. The IP addresses are 192.168.1.100 and 192.168.1.102 respectively. If you want to check the IP address configuration, run the CMD command line and run the ipconfig /all command. After the configuration is complete, ping the IP address of the embedded network port. The result is shown in Figure 2.

As can be seen from Figure 2, the host pings 32 bytes of data. The maximum time for 4 packets is 5 ms and the minimum time is 3 ms. The TTL value for 4 packets is 255. This is due to the optimal path selection algorithm. After it is down, after a period of stability, the network topology is also stable, and the routing path of the data packets will be correspondingly stabilized on an optimal path. The entire process of data transmission and reception is correct, and the error rate is 0%.

TR90 Glasses

Tr90 Glasses,Tr90 Perspective Glasses,Tr90 Sunglasses,Tr90 Material Glasses

Danyang Hengshi Optical Glasses Co., Ltd. , https://www.hengshi-optical.com

Posted on