在前面的章节也说了lwipopts.h文件的作用,而此刻在操作系统中移植,我们首先要将添加了操作系统的工程拿过来,把lwipopts.h文件修改一下,该文件最重要的宏定义就是NO\_SYS,我们把它定义为0就表示使用操作系统,当然,在使用操作系统的时候我们一般都会使用NETCONN API 与Socket API编程,那么就需要将宏LWIP\_NETCONN与LWIP\_SOCKET定义为1,表示使能这两种API编程,lwipopts.h简单修改一下即可,然后再添加一下线程运行的一些宏定义,必须修改的部分具体见代码清单 8‑2加粗部分,而其他宏定义是根据实际情况进行修改即可。
```
1 #ifndef __LWIPOPTS_H__
2 #define __LWIPOPTS_H__
3
4 /**
5 * SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain
6 * critical regions during buffer allocation, deallocation and memory
7 * allocation and deallocation.
8 */
9 #define SYS_LIGHTWEIGHT_PROT 1
10
11 /**
12 * NO_SYS==1: Provides VERY minimal functionality. Otherwise,
13 * use lwIP facilities.
14 */
15 #define NO_SYS 0
16
17 /**
18 * NO_SYS_NO_TIMERS==1: Drop support for sys_timeout when NO_SYS==1
19 * Mainly for compatibility to old versions.
20 */
21 #define NO_SYS_NO_TIMERS 0
22
23 /* ---------- Memory options ---------- */
24 /* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
25 lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2
26 byte alignment -> define MEM_ALIGNMENT to 2. */
27 #define MEM_ALIGNMENT 4
28
29 /* MEM_SIZE: the size of the heap memory. If the application will send
30 a lot of data that needs to be copied, this should be set high. */
31 #define MEM_SIZE (15*1024)
32
33 /* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application
34 sends a lot of data out of ROM (or other static memory), this
35 should be set high. */
36 #define MEMP_NUM_PBUF 25
37 /* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
38 per active UDP "connection". */
39 #define MEMP_NUM_UDP_PCB 4
40 /* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP
41 connections. */
42 #define MEMP_NUM_TCP_PCB 6
43 /* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP
44 connections. */
45 #define MEMP_NUM_TCP_PCB_LISTEN 6
46 /* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP
47 segments. */
48 #define MEMP_NUM_TCP_SEG 150
49 /* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active
50 timeouts. */
51 #define MEMP_NUM_SYS_TIMEOUT 6
52
53
54 /* ---------- Pbuf options ---------- */
55 /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
56 #define PBUF_POOL_SIZE 45
57 /* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
58 #define PBUF_POOL_BUFSIZE \
59 LWIP_MEM_ALIGN_SIZE(TCP_MSS+40+PBUF_LINK_ENCAPSULATION_HLEN+PBUF_LINK_HLEN)
60
61 /* ---------- TCP options ---------- */
62 #define LWIP_TCP 1
63 #define TCP_TTL 255
64
65 /* Controls if TCP should queue segments that arrive out of
66 order. Define to 0 if your device is low on memory. */
67 #define TCP_QUEUE_OOSEQ 0
68
69 /* TCP Maximum segment size. */
70 #define TCP_MSS (1500 - 40)
71
72 /* TCP sender buffer space (bytes). */
73 #define TCP_SND_BUF (10*TCP_MSS)
74
75 /* TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at least
76 as much as (2 * TCP_SND_BUF/TCP_MSS) for things to work. */
77
78 #define TCP_SND_QUEUELEN (8* TCP_SND_BUF/TCP_MSS)
79
80 /* TCP receive window. */
81 #define TCP_WND (11*TCP_MSS)
82
83
84 /* ---------- ICMP options ---------- */
85 #define LWIP_ICMP 1
86
87
88 /* ---------- DHCP options ---------- */
89 /* Define LWIP_DHCP to 1 if you want DHCP configuration of
90 interfaces. DHCP is not implemented in lwIP 0.5.1, however, so
91 turning this on does currently not work. */
92 #define LWIP_DHCP 1
93
94
95 /* ---------- UDP options ---------- */
96 #define LWIP_UDP 1
97 #define UDP_TTL 255
98
99
100 /* ---------- Statistics options ---------- */
101 #define LWIP_STATS 0
102 #define LWIP_PROVIDE_ERRNO 1
103
104 /* ---------- link callback options ---------- */
105 /* LWIP_NETIF_LINK_CALLBACK==1: Support a callback function from an interface
106 * whenever the link changes (i.e., link down)
107 */
108 #define LWIP_NETIF_LINK_CALLBACK 0
109 /*
110 --------------------------------------
111 ---------- Checksum options ----------
112 --------------------------------------
113 */
114
115 /* The STM32F4x7 allows computing and verifying the IP,
116 UDP, TCP and ICMP checksums by hardware:
117 - To use this feature let the following define uncommented.
118 - To disable it and process by CPU comment the the checksum.
119 */
120 #define CHECKSUM_BY_HARDWARE
121
122
123 #ifdef CHECKSUM_BY_HARDWARE
124 /* CHECKSUM_GEN_IP==0: Generate checksums by hardware for outgoing IP packets.*/
125 #define CHECKSUM_GEN_IP 0
126 /* CHECKSUM_GEN_UDP==0: Generate checksums by hardware for outgoing UDP packets.*/
127 #define CHECKSUM_GEN_UDP 0
128 /* CHECKSUM_GEN_TCP==0: Generate checksums by hardware for outgoing TCP packets.*/
129 #define CHECKSUM_GEN_TCP 0
130 /* CHECKSUM_CHECK_IP==0: Check checksums by hardware for incoming IP packets.*/
131 #define CHECKSUM_CHECK_IP 0
132 /* CHECKSUM_CHECK_UDP==0: Check checksums by hardware for incoming UDP packets.*/
133 #define CHECKSUM_CHECK_UDP 0
134 /* CHECKSUM_CHECK_TCP==0: Check checksums by hardware for incoming TCP packets.*/
135 #define CHECKSUM_CHECK_TCP 0
136 /*CHECKSUM_CHECK_ICMP==0: Check checksums by hardware for incoming ICMP packets.*/
137 #define CHECKSUM_GEN_ICMP 0
138 #else
139 /* CHECKSUM_GEN_IP==1: Generate checksums in software for outgoing IP packets.*/
140 #define CHECKSUM_GEN_IP 1
141 /* CHECKSUM_GEN_UDP==1: Generate checksums in software for outgoing UDP packets.*/
142 #define CHECKSUM_GEN_UDP 1
143 /* CHECKSUM_GEN_TCP==1: Generate checksums in software for outgoing TCP packets.*/
144 #define CHECKSUM_GEN_TCP 1
145 /* CHECKSUM_CHECK_IP==1: Check checksums in software for incoming IP packets.*/
146 #define CHECKSUM_CHECK_IP 1
147 /* CHECKSUM_CHECK_UDP==1: Check checksums in software for incoming UDP packets.*/
148 #define CHECKSUM_CHECK_UDP 1
149 /* CHECKSUM_CHECK_TCP==1: Check checksums in software for incoming TCP packets.*/
150 #define CHECKSUM_CHECK_TCP 1
151 /*CHECKSUM_CHECK_ICMP==1: Check checksums by hardware for incoming ICMP packets.*/
152 #define CHECKSUM_GEN_ICMP 1
153 #endif
154
155
156 /*
157 ----------------------------------------------
158 ---------- Sequential layer options ----------
159 ----------------------------------------------
160 */
161 /**
162 * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
163 */
164 #define LWIP_NETCONN 1
165
166 /*
167 ------------------------------------
168 ---------- Socket options ----------
169 ------------------------------------
170 */
171 /**
172 * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
173 */
174 #define LWIP_SOCKET 1
175
176 /*
177 ---------------------------------
178 ---------- OS options ----------
179 ---------------------------------
180 */
181
182
183 #define DEFAULT_UDP_RECVMBOX_SIZE 10
184 #define DEFAULT_TCP_RECVMBOX_SIZE 10
185 #define DEFAULT_ACCEPTMBOX_SIZE 10
186 #define DEFAULT_THREAD_STACKSIZE 1024
187
188
189 #define TCPIP_THREAD_NAME "lwip"
190 #define TCPIP_THREAD_STACKSIZE 512
191 #define TCPIP_MBOX_SIZE 8
192 #define TCPIP_THREAD_PRIO 3
```
- 说明
- 第1章:网络协议简介
- 1.1:常用网络协议
- 1.2:网络协议的分层模型
- 1.3:协议层报文间的封装与拆封
- 第2章:LwIP简介
- 2.1:LwIP的优缺点
- 2.2:LwIP的文件说明
- 2.2.1:如何获取LwIP源码文件
- 2.2.2:LwIP文件说明
- 2.3:查看LwIP的说明文档
- 2.4:使用vscode查看源码
- 2.4.1:查看文件中的符号列表(函数列表)
- 2.4.2:函数定义跳转
- 2.5:LwIP源码里的example
- 2.6:LwIP的三种编程接口
- 2.6.1:RAW/Callback API
- 2.6.2:NETCONN API
- 2.6.3:SOCKET API
- 第3章:开发平台介绍
- 3.1:以太网简介
- 3.1.1:PHY层
- 3.1.2:MAC子层
- 3.2:STM32的ETH外设
- 3.3:MII 和 RMII 接口
- 3.4:PHY:LAN8720A
- 3.5:硬件设计
- 3.6:软件设计
- 3.6.1:获取STM32的裸机工程模板
- 3.6.2:添加bsp_eth.c与bsp_eth.h
- 3.6.3:修改stm32f4xx_hal_conf.h文件
- 第4章:LwIP的网络接口管理
- 4.1:netif结构体
- 4.2:netif使用
- 4.3:与netif相关的底层函数
- 4.4:ethernetif.c文件内容
- 4.4.1:ethernetif数据结构
- 4.4.2:ethernetif_init()
- 4.4.3:low_level_init()
- 第5章:LwIP的内存管理
- 5.1:几种内存分配策略
- 5.1.1:固定大小的内存块
- 5.1.2:可变长度分配
- 5.2:动态内存池(POOL)
- 5.2.1:内存池的预处理
- 5.2.2:内存池的初始化
- 5.2.3:内存分配
- 5.2.4:内存释放
- 5.3:动态内存堆
- 5.3.1:内存堆的组织结构
- 5.3.2:内存堆初始化
- 5.3.3:内存分配
- 5.3.4:内存释放
- 5.4:使用C库的malloc和free来管理内存
- 5.5:LwIP中的配置
- 第6章:网络数据包
- 6.1:TCP/IP协议的分层思想
- 6.2:LwIP的线程模型
- 6.3:pbuf结构体说明
- 6.4:pbuf的类型
- 6.4.1:PBUF_RAM类型的pbuf
- 6.4.2:PBUF_POOL类型的pbuf
- 6.4.3:PBUF_ROM和PBUF_REF类型pbuf
- 6.5:pbuf_alloc()
- 6.6:pbuf_free()
- 6.7:其它pbuf操作函数
- 6.7.1:pbuf_realloc()
- 6.7.2:pbuf_header()
- 6.7.3:pbuf_take()
- 6.8:网卡中使用的pbuf
- 6.8.1:low_level_output()
- 6.8.2:low_level_input()
- 6.8.3:ethernetif_input()
- 第7章:无操作系统移植LwIP
- 7.1:将LwIP添加到裸机工程
- 7.2:移植头文件
- 7.3:移植网卡驱动
- 7.4:LwIP时基
- 7.5:协议栈初始化
- 7.6:获取数据包
- 7.6.1:查询方式
- 7.6.2:ping命令详解
- 7.6.3:中断方式
- 第8章:有操作系统移植LwIP
- 8.1:LwIP中添加操作系统
- 8.1.1:拷贝FreeRTOS源码到工程文件夹
- 8.1.2:添加FreeRTOS源码到工程组文件夹
- 8.1.3:指定FreeRTOS头文件的路径
- 8.1.4:修改stm32f10x_it.c
- 8.2:lwipopts.h文件需要加入的配置
- 8.3:sys_arch.c/h文件的编写
- 8.4:网卡底层的编写
- 8.5:协议栈初始化
- 8.6:移植后使用ping测试基本响应
- 第9章:LwIP一探究竟
- 9.1:网卡接收数据的流程
- 9.2:内核超时处理
- 9.2.1:sys_timeo结构体与超时链表
- 9.2.2:注册超时事件
- 9.2.3:超时检查
- 9.3:tcpip_thread线程
- 9.4:LwIP中的消息
- 9.4.1:消息结构
- 9.4.2:数据包消息
- 9.4.3:API消息
- 9.5:揭开LwIP神秘的面纱
- 第10章:ARP协议
- 10.1:链路层概述
- 10.2:MAC地址的基本概念
- 10.3:初识ARP
- 10.4:以太网帧结构
- 10.5:IP地址映射为物理地址
- 10.6:ARP缓存表
- 10.7:ARP缓存表的超时处理
- 10.8:ARP报文
- 10.9:发送ARP请求包
- 10.10:数据包接收流程
- 10.10.1:以太网之数据包接收
- 10.10.2:ARP数据包处理
- 10.10.3:更新ARP缓存表
- 10.11:数据包发送流程
- 10.11.1:etharp_output()函数
- 10.11.2:etharp_output_to_arp_index()函数
- 10.11.3:etharp_query()函数
- 第11章:IP协议
- 11.1:IP地址.md
- 11.1.1:概述
- 11.1.2:IP地址编址
- 11.1.3:特殊IP地址