企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
经过上面的步骤,我们的工程基本就添加完成,但是要想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。 ![](https://box.kancloud.cn/08942d95d44778e9631db3bc03c88338_475x152.png) 然后我们打开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__ */ ```