經過上面的步驟,我們的工程基本就添加完成,但是要想LwIP跑起來,還需一些頭文件的支持,分別是lwipopts.h、cc.h、pref.h等。 lwipopts.h就是用于配置LwIP的相關參數的,一般來說LwIP默認會有參數的配置,存放在opt.h文件中,如果用戶沒有在lwipopts.h文件進行配置,那么LwIP就會使用opt.h默認的參數,注意,在移植的時候出現定義某些參數是非常重要的,這對我們LwIP的性能至關重要,甚至在配置錯誤的時候能直接導致LwIP的運行崩潰,下面一起來看看lwipopts.h文件的內容,這個文件的內容是由我們自己添加進來的,這里僅僅是介紹一部分配置。
我們先在工程的User文件夾下面新建一個arch文件夾,用于存放與底層接口相關的文件,然后打開LwIP的contrib包,把路徑“contrib-2.1.0\\examples\\example\_app”下的lwipopts.h文件拷貝到arch文件夾中;為了方便,我們把cc.h于pref.h也添加進來,這兩個文件也是存放在LwIP的contrib包中,路徑為“contrib-2.1.0\\ports\\unix\\port\\include\\arch”,我們把這些拷貝到arch中,并且將工程的頭文件路徑添加進去,這樣子就能對LwIP進行配置了,具體見圖 7?9。

然后我們打開lwipopts.h,并在文件內部寫入以下配置,具體見代碼清單 7?1:
```
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 0
10
11 /**
12 * NO_SYS==1: Provides VERY minimal functionality. Otherwise,
13 * use lwIP facilities.
14 */
15 #define NO_SYS 1 (1)
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 (2)
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 (30*1024) (3)
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 50
37 /* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
38 per active UDP "connection". */
39 #define MEMP_NUM_UDP_PCB 6
40 /* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP
41 connections. */
42 #define MEMP_NUM_TCP_PCB 10
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 12
49 /* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active
50 timeouts. */
51 #define MEMP_NUM_SYS_TIMEOUT 10
52
53
54 /* ---------- Pbuf options ---------- */
55 /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
56 #define PBUF_POOL_SIZE 10 (4)
57
58 /* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
59 #define PBUF_POOL_BUFSIZE 500 (5)
60
61
62 /* ---------- TCP options ---------- */
63 #define LWIP_TCP 1
64 #define TCP_TTL 255
65
66 /* Controls if TCP should queue segments that arrive out of
67 order. Define to 0 if your device is low on memory. */
68 #define TCP_QUEUE_OOSEQ 0
69
70 /* TCP Maximum segment size. */
71 #define TCP_MSS (1500 - 40) (6)
72
73 /* TCP sender buffer space (bytes). */
74 #define TCP_SND_BUF (4*TCP_MSS) (7)
75
76 /* TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at least
77 as much as (2 * TCP_SND_BUF/TCP_MSS) for things to work. */
78
79 #define TCP_SND_QUEUELEN (2* TCP_SND_BUF/TCP_MSS)
80
81 /* TCP receive window. */
82 #define TCP_WND (2*TCP_MSS) (8)
83
84
85 /* ---------- ICMP options ---------- */
86 #define LWIP_ICMP 1
87
88
89 /* ---------- DHCP options ---------- */
90 /* Define LWIP_DHCP to 1 if you want DHCP configuration of
91 interfaces. DHCP is not implemented in lwIP 0.5.1, however, so
92 turning this on does currently not work. */
93 #define LWIP_DHCP 1
94
95
96 /* ---------- UDP options ---------- */
97 #define LWIP_UDP 1
98 #define UDP_TTL 255
99
100
101 /* ---------- Statistics options ---------- */
102 #define LWIP_STATS 0
103 #define LWIP_PROVIDE_ERRNO 1
104
105 /* ---------- link callback options ---------- */
106 /* LWIP_NETIF_LINK_CALLBACK==1: Support a callback function from an interface
107 * whenever the link changes (i.e., link down)
108 */
109 #define LWIP_NETIF_LINK_CALLBACK 0
110 /*
111 --------------------------------------
112 ---------- Checksum options ----------
113 --------------------------------------
114 */
115
116 /*The STM32F4x7 allows comput
117 ing and verifying the IP, UDP, TCP and ICMP checksums by hardware:
118 - To use this feature let the following define uncommented.
119 - To disable it and process by CPU comment the the checksum.
120 */
121 #define CHECKSUM_BY_HARDWARE
122
123
124 #ifdef CHECKSUM_BY_HARDWARE
125 /* CHECKSUM_GEN_IP==0: Generate checksums by hardware for outgoing IP packets.*/
126 #define CHECKSUM_GEN_IP 0
127 /* CHECKSUM_GEN_UDP==0: Generate checksums by hardware for outgoing UDP packets.*/
128 #define CHECKSUM_GEN_UDP 0
129 /* CHECKSUM_GEN_TCP==0: Generate checksums by hardware for outgoing TCP packets.*/
130 #define CHECKSUM_GEN_TCP 0
131 /* CHECKSUM_CHECK_IP==0: Check checksums by hardware for incoming IP packets.*/
132 #define CHECKSUM_CHECK_IP 0
133 /* CHECKSUM_CHECK_UDP==0: Check checksums by hardware for incoming UDP packets.*/
134 #define CHECKSUM_CHECK_UDP 0
135 /* CHECKSUM_CHECK_TCP==0: Check checksums by hardware for incoming TCP packets.*/
136 #define CHECKSUM_CHECK_TCP 0
137 /*CHECKSUM_CHECK_ICMP==0: Check checksums by hardware for incoming ICMP packets.*/
138 #define CHECKSUM_GEN_ICMP 0
139 #else
140 /* CHECKSUM_GEN_IP==1: Generate checksums in software for outgoing IP packets.*/
141 #define CHECKSUM_GEN_IP 1
142 /* CHECKSUM_GEN_UDP==1: Generate checksums in software for outgoing UDP packets.*/
143 #define CHECKSUM_GEN_UDP 1
144 /* CHECKSUM_GEN_TCP==1: Generate checksums in software for outgoing TCP packets.*/
145 #define CHECKSUM_GEN_TCP 1
146 /* CHECKSUM_CHECK_IP==1: Check checksums in software for incoming IP packets.*/
147 #define CHECKSUM_CHECK_IP 1
148 /* CHECKSUM_CHECK_UDP==1: Check checksums in software for incoming UDP packets.*/
149 #define CHECKSUM_CHECK_UDP 1
150 /* CHECKSUM_CHECK_TCP==1: Check checksums in software for incoming TCP packets.*/
151 #define CHECKSUM_CHECK_TCP 1
152 /*CHECKSUM_CHECK_ICMP==1: Check checksums by hardware for incoming ICMP packets.*/
153 #define CHECKSUM_GEN_ICMP 1
154 #endif
155
156
157 /*
158 ----------------------------------------------
159 ---------- Sequential layer options ----------
160 ----------------------------------------------
161 */
162 /**
163 * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
164 */
165 #define LWIP_NETCONN 0 (9)
166
167 /*
168 ------------------------------------
169 ---------- Socket options ----------
170 ------------------------------------
171 */
172 /**
173 * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
174 */
175 #define LWIP_SOCKET 0 (10)
176
177
178 /*
179 ----------------------------------------
180 ---------- Lwip Debug options ----------
181 ----------------------------------------
182 */
183 //#define LWIP_DEBUG 1
184
185 #endif /* __LWIPOPTS_H__ */
186
187 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
```
(1):NO_SYS表示無操作系統模擬層,這個宏非常重要,因為無操作系統與有操作系統的移植和編寫是完全不一樣的,我們現在是無操作系統移植,所以將這個宏定義為1。
(2):內存對齊,按照4字節對齊。
(3):堆內存的大小。如果應用程序將發送很多需要復制的數據應該設置得大一點。
(4):PBUF_POOL內存池中內存塊數量。
(5):PBUF_POOL內存池中每個內存塊大小。
(6):TCP協議報文最大長度。
(7):允許TCP協議使用的最大發送緩沖區空間(字節)。
(8):TCP接收窗口大小。
(9)(10):因為現在是無操作系統,就不使能NETCONN API和Socket API編程。
cc.h文件中包含處理器相關的變量類型、數據結構及字節對齊的相關宏。
LwIP中使用的基本變量類型均以位數進行命名,為抽象的變量類型定義,開發者需要根據所用處理器及編譯器特性進行定義,一般我們直接將變量直接定義為C語言的基本類型,如unsigned char、int等,這樣子可以保證LwIP協議棧就與平臺無關了。除此之外我們還可以定義大小端模式,輸出調試的宏等,cc.h文件內容具體見代碼清單 7 2:
```
1 #ifndef __CC_H__
2 #define __CC_H__
3
4 #include "stdio.h"
5
6 #include "main.h"
7
8 #define LWIP_NO_STDINT_H 1
9
10 typedef unsigned char u8_t;
11 typedef signed char s8_t;
12 typedef unsigned short u16_t;
13 typedef signed short s16_t;
14 typedef unsigned long u32_t;
15 typedef signed long s32_t;
16 typedef u32_t mem_ptr_t;
17 typedef int sys_prot_t;
18
19
20 #define U16_F "hu"
21 #define S16_F "d"
22 #define X16_F "hx"
23 #define U32_F "u"
24 #define S32_F "d"
25 #define X32_F "x"
26 #define SZT_F "uz"
27
28
29
30 /* 選擇小端模式 */
31 #define BYTE_ORDER LITTLE_ENDIAN
32
33 /* define compiler specific symbols */
34 #if defined (__ICCARM__)
35
36 #define PACK_STRUCT_BEGIN
37 #define PACK_STRUCT_STRUCT
38 #define PACK_STRUCT_END
39 #define PACK_STRUCT_FIELD(x) x
40 #define PACK_STRUCT_USE_INCLUDES
41
42 #elif defined (__CC_ARM)
43
44 #define PACK_STRUCT_BEGIN __packed
45 #define PACK_STRUCT_STRUCT
46 #define PACK_STRUCT_END
47 #define PACK_STRUCT_FIELD(x) x
48
49 #elif defined (__GNUC__)
50
51 #define PACK_STRUCT_BEGIN
52 #define PACK_STRUCT_STRUCT __attribute__ ((__packed__))
53 #define PACK_STRUCT_END
54 #define PACK_STRUCT_FIELD(x) x
55
56 #elif defined (__TASKING__)
57
58 #define PACK_STRUCT_BEGIN
59 #define PACK_STRUCT_STRUCT
60 #define PACK_STRUCT_END
61 #define PACK_STRUCT_FIELD(x) x
62
63 #endif
64
65
66 #define LWIP_PLATFORM_ASSERT(x) do {printf(x);}while(0)
67
68
69 extern u32_t sys_now(void);
70
71 #endif /* __CC_H__ */
```
perf.h文件是與系統統計與測量相關的頭文件,我們暫時無需使用任何統計與測量功能,因此該頭文件的量宏定義直接為空即可,具體見代碼清單 7?3:
```
1 #ifndef __PERF_H__
2 #define __PERF_H__
3
4 #define PERF_START /* null definition */
5 #define PERF_STOP(x) /* null definition */
6
7 #endif /* __PERF_H__ */
```
- 說明
- 第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地址