S32K3+lwip裸机(四)Lwip2.1.2版本说明及移植前准备
LWIP的版本
lwip从0.7一直更新到目前的2.2.0.由于2.2.0是刚出来的,我已经移植好了2.1.2所以在这里就不过多涉及2.1.2后续版本的问题。
LWIP的问题
刚开始拿到Lwip是一头雾水,好像明白了可是无从下手,网上说的哪儿都是对的,但是看实际的哪儿都不对
首先从2.0.0版本开始lwip里面取消了配置文件,需要自己添加(惊喜不?意外不?)从哪儿来呢?可以根据test里面找,可以在网上找,可以在1.4版本里面找。我这里是在网上找的。利用剪贴复制起始很多宏定义都不是有用没,这也是一大问题。
第一步:需要自己建立一个文件夹“arch”,里面添加的内容为配置文件及相关中间件。配置文件含“lwipopts.h"“epstuct.h”“perf.h”“cc.h”“bpstruct.h”
"lwipopts.h"比较重要,绝大部分配置都可以在里面进行调整包括各种协议的启用,停用。
下面是我的文件配置,仔细查看会发现不同的协议配置都会定义在一起。我在这里只实现了ping的功能
/*************************************************
File name: lwipopts.h
Author:
Version:
Date:
Description:
Others:
History:
*************************************************/
#ifndef ARCH_LWIPOPTS_H_
#define ARCH_LWIPOPTS_H_
#define NO_SYS 1
#define LWIP_SOCKET (NO_SYS==0)
#define LWIP_NETCONN (NO_SYS==0)
#define LWIP_IGMP 0
#define LWIP_ICMP 1
#define LWIP_SNMP 0
#define LWIP_DNS 0
#define LWIP_HAVE_LOOPIF 0
#define LWIP_NETIF_LOOPBACK 0
#define LWIP_LOOPBACK_MAX_PBUFS 0//10
#define TCP_LISTEN_BACKLOG 0
#define LWIP_COMPAT_SOCKETS 1
#define LWIP_SO_RCVTIMEO 1
#define LWIP_SO_RCVBUF 1
#define LWIP_TCPIP_CORE_LOCKING 0
#define LWIP_NETIF_LINK_CALLBACK 1
#define LWIP_NETIF_STATUS_CALLBACK 1
#ifdef LWIP_DEBUG
#define LWIP_DBG_MIN_LEVEL 0
#define PPP_DEBUG LWIP_DBG_OFF
#define MEM_DEBUG LWIP_DBG_OFF
#define MEMP_DEBUG LWIP_DBG_OFF
#define PBUF_DEBUG LWIP_DBG_OFF
#define API_LIB_DEBUG LWIP_DBG_OFF
#define API_MSG_DEBUG LWIP_DBG_OFF
#define TCPIP_DEBUG LWIP_DBG_OFF
#define NETIF_DEBUG LWIP_DBG_OFF
#define SOCKETS_DEBUG LWIP_DBG_OFF
#define DNS_DEBUG LWIP_DBG_OFF
#define AUTOIP_DEBUG LWIP_DBG_OFF
#define DHCP_DEBUG LWIP_DBG_OFF
#define IP_DEBUG LWIP_DBG_OFF
#define IP_REASS_DEBUG LWIP_DBG_OFF
#define ICMP_DEBUG LWIP_DBG_OFF
#define IGMP_DEBUG LWIP_DBG_OFF
#define UDP_DEBUG LWIP_DBG_OFF
#define TCP_DEBUG LWIP_DBG_OFF
#define TCP_INPUT_DEBUG LWIP_DBG_OFF
#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF
#define TCP_RTO_DEBUG LWIP_DBG_OFF
#define TCP_CWND_DEBUG LWIP_DBG_OFF
#define TCP_WND_DEBUG LWIP_DBG_OFF
#define TCP_FR_DEBUG LWIP_DBG_OFF
#define TCP_QLEN_DEBUG LWIP_DBG_OFF
#define TCP_RST_DEBUG LWIP_DBG_OFF
#endif
#define LWIP_DBG_TYPES_ON (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT)
/* ---------- Memory options ---------- */
/* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2
byte alignment -> define MEM_ALIGNMENT to 2. */
/* MSVC port: intel processors don't need 4-byte alignment,
but are faster that way! */
#define MEM_ALIGNMENT 4
/* MEM_SIZE: the size of the heap memory. If the application will send
a lot of data that needs to be copied, this should be set high. */
#define MEM_SIZE 10240
/* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application
sends a lot of data out of ROM (or other static memory), this
should be set high. */
#define MEMP_NUM_PBUF 16
/* MEMP_NUM_RAW_PCB: the number of UDP protocol control blocks. One
per active RAW "connection". */
#define MEMP_NUM_RAW_PCB 3
/* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
per active UDP "connection". */
#define MEMP_NUM_UDP_PCB 4
/* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP
connections. */
#define MEMP_NUM_TCP_PCB 5
/* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP
connections. */
#define MEMP_NUM_TCP_PCB_LISTEN 8
/* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP
segments. */
#define MEMP_NUM_TCP_SEG 16
/* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active
timeouts. */
#define MEMP_NUM_SYS_TIMEOUT 15
/* The following four are used only with the sequential API and can be
set to 0 if the application only will use the raw API. */
/* MEMP_NUM_NETBUF: the number of struct netbufs. */
#define MEMP_NUM_NETBUF 2
/* MEMP_NUM_NETCONN: the number of struct netconns. */
#define MEMP_NUM_NETCONN 10
/* MEMP_NUM_TCPIP_MSG_*: the number of struct tcpip_msg, which is used
for sequential API communication and incoming packets. Used in
src/api/tcpip.c. */
#define MEMP_NUM_TCPIP_MSG_API 16
#define MEMP_NUM_TCPIP_MSG_INPKT 16
/* ---------- Pbuf options ---------- */
/* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */
#define PBUF_POOL_SIZE 100
/* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */
#define PBUF_POOL_BUFSIZE 128
/* PBUF_LINK_HLEN: the number of bytes that should be allocated for a
link level header. */
#define PBUF_LINK_HLEN 16
/** SYS_LIGHTWEIGHT_PROT
* define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection
* for certain critical regions during buffer allocation, deallocation and memory
* allocation and deallocation.
*/
#define SYS_LIGHTWEIGHT_PROT (NO_SYS==0)
/* ---------- TCP options ---------- */
#define LWIP_TCP 1
#define TCP_TTL 255
/* Controls if TCP should queue segments that arrive out of
order. Define to 0 if your device is low on memory. */
#define TCP_QUEUE_OOSEQ 1
/* TCP Maximum segment size. */
#define TCP_MSS 1024
/* TCP sender buffer space (bytes). */
#define TCP_SND_BUF 2048
/* TCP sender buffer space (pbufs). This must be at least = 2 *
TCP_SND_BUF/TCP_MSS for things to work. */
#define TCP_SND_QUEUELEN (4 * TCP_SND_BUF/TCP_MSS)
/* TCP writable space (bytes). This must be less than or equal
to TCP_SND_BUF. It is the amount of space which must be
available in the tcp snd_buf for select to return writable */
#define TCP_SNDLOWAT (TCP_SND_BUF/2)
/* TCP receive window. */
#define TCP_WND 8096
/* Maximum number of retransmissions of data segments. */
#define TCP_MAXRTX 12
/* Maximum number of retransmissions of SYN segments. */
#define TCP_SYNMAXRTX 4
/* ---------- ARP options ---------- */
#define LWIP_ARP 1
#define ARP_TABLE_SIZE 10
#define ARP_QUEUEING 1
/* ---------- IP options ---------- */
/* Define IP_FORWARD to 1 if you wish to have the ability to forward
IP packets across network interfaces. If you are going to run lwIP
on a device with only one network interface, define this to 0. */
#define IP_FORWARD 1
/* IP reassembly and segmentation.These are orthogonal even
* if they both deal with IP fragments */
#define IP_REASSEMBLY 1
#define IP_REASS_MAX_PBUFS 10
#define MEMP_NUM_REASSDATA 10
#define IP_FRAG 1
/* ---------- ICMP options ---------- */
#define ICMP_TTL 255
#define CHECKSUM_CHECK_ICMP 0
/* ---------- DHCP options ---------- */
/* Define LWIP_DHCP to 1 if you want DHCP configuration of
interfaces. */
#define LWIP_DHCP 0
/* 1 if you want to do an ARP check on the offered address
(recommended). */
#define DHCP_DOES_ARP_CHECK (LWIP_DHCP)
/* ---------- AUTOIP options ------- */
#define LWIP_AUTOIP 0
#define LWIP_DHCP_AUTOIP_COOP (LWIP_DHCP && LWIP_AUTOIP)
/* ---------- UDP options ---------- */
#define LWIP_UDP 1
#define LWIP_UDPLITE 1
#define UDP_TTL 255
/* ---------- Statistics options ---------- */
#define LWIP_STATS 1
#define LWIP_STATS_DISPLAY 1
#if LWIP_STATS
#define LINK_STATS 1
#define IP_STATS 1
#define ICMP_STATS 1
#define IGMP_STATS 1
#define IPFRAG_STATS 1
#define UDP_STATS 1
#define TCP_STATS 1
#define MEM_STATS 1
#define MEMP_STATS 1
#define PBUF_STATS 1
#define SYS_STATS 1
#endif /* LWIP_STATS */
/* ---------- PPP options ---------- */
#define PPP_SUPPORT 0 /* Set > 0 for PPP */
#if PPP_SUPPORT
#define NUM_PPP 1 /* Max PPP sessions. */
/* Select modules to enable. Ideally these would be set in the makefile but
* we're limited by the command line length so you need to modify the settings
* in this file.
*/
#define PPPOE_SUPPORT 1
#define PPPOS_SUPPORT 1
#define PAP_SUPPORT 1 /* Set > 0 for PAP. */
#define CHAP_SUPPORT 1 /* Set > 0 for CHAP. */
#define MSCHAP_SUPPORT 0 /* Set > 0 for MSCHAP (NOT FUNCTIONAL!) */
#define CBCP_SUPPORT 0 /* Set > 0 for CBCP (NOT FUNCTIONAL!) */
#define CCP_SUPPORT 0 /* Set > 0 for CCP (NOT FUNCTIONAL!) */
#define VJ_SUPPORT 1 /* Set > 0 for VJ header compression. */
#define MD5_SUPPORT 1 /* Set > 0 for MD5 (see also CHAP) */
#endif /* PPP_SUPPORT */
#endif /* ARCH_LWIPOPTS_H_ */
"epstuct.h"是个目前来说没有实际作用的文件夹,只定义了一个参数目的是为了编译不报错误
/*************************************************
File name: epstruct.h
Author:
Version:
Date:
Description:
Others:
History:
*************************************************/
#ifndef ARCH_EPSTRUCT_H_
#define ARCH_EPSTRUCT_H_
#pragma pack(pop)
#endif /* ARCH_EPSTRUCT_H_ */
"perf.h"是个目前来说没有实际作用的文件夹,只定义了一个参数目的是为了编译不报错误
/*************************************************
File name: per.h
Author:
Version:
Date:
Description:
Others:
History:
*************************************************/
#ifndef ARCH_PERF_H_
#define ARCH_PERF_H_
#ifndef LWIP_ARCH_PERF_H
#define LWIP_ARCH_PERF_H
/* 定义为空 */
#define PERF_START
#define PERF_STOP(x)
#endif /* LWIP_ARCH_PERF_H */
#endif /* ARCH_PERF_H_ */
"cc.h"基本没有做更改,基本都是打包的一些配置参数
/*************************************************
File name: cc.h
Author:
Version:
Date:
Description:
Others:
History:
*************************************************/
#ifndef ARCH_CC_H_
#define ARCH_CC_H_
#include <stdlib.h>
/*
定义系统的字节顺序。 需要将网络数据转换为主机字节顺序。
允许的值:LITTLE_ENDIAN和BIG_ENDIAN */
#define BYTE_ORDER LITTLE_ENDIAN
/* 定义系统的随机数生成器功能 */
#define LWIP_RAND() ((u32_t)rand())
/* 平台特定的诊断输出。 */
#define LWIP_PLATFORM_DIAG(x) NULL //do {printf x;} while(0) wan_change_20230809
/* 特定于平台的断言处理。 */
#define LWIP_PLATFORM_ASSERT(x)
/* 标准C库 是否包含 stddef.h ,默认使用标准库 */
#define LWIP_NO_STDDEF_H 0
/* 标准C库 是否包含 stdint.h ,默认使用标准库 */
#define LWIP_NO_STDINT_H 0
/* 标准C库 是否包含 inttypes.h ,默认使用标准库 */
#define LWIP_NO_INTTYPES_H 0
/* 标准C库 是否包含 limits.h ,默认使用标准库 */
#define LWIP_NO_LIMITS_H 0
/* 标准C库 是否包含 ctype.h ,默认使用标准库 */
#define LWIP_NO_CTYPE_H 0
#define LWIP_CONST_CAST(target_type, val) ((target_type)((ptrdiff_t)val))
#define LWIP_ALIGNMENT_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
#define LWIP_PTR_NUMERIC_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
#define LWIP_PACKED_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
/* 分配指定大小的内存缓冲区,其大小足以使用LWIP_MEM_ALIGN对齐其起始地址。 */
#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[LWIP_MEM_ALIGN_BUFFER(size)]
/*
计算对齐缓冲区的内存大小 - 返回MEM_ALIGNMENT的下一个最高倍数
(例如,LWIP_MEM_ALIGN_SIZE(3)和LWIP_MEM_ALIGN_SIZE(4)将为MEM_ALIGNMENT == 4产生4)。
*/
#define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U))
/* 使用未对齐类型作为存储时,计算对齐缓冲区的安全内存大小。 这包括开始时的(MEM_ALIGNMENT - 1)安全边际 */
#define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1U))
/* 将内存指针以MEM_ALIGNMENT定义的对齐方式对齐,以使ADDR%MEM_ALIGNMENT == 0 */
#define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1)))
/* 打包结构支持。 在声明打包结构之前放置BEFORE。 */
#define PACK_STRUCT_BEGIN
/* 打包结构支持。 在声明打包结构之前放置AFTER 。 */
#define PACK_STRUCT_END
/* 打包结构支持。 放置在打包结构的声明结束和尾随分号之间。 */
#define PACK_STRUCT_STRUCT
/* 打包结构支持。 封装u32_t和u16_t成员。 */
#define PACK_STRUCT_FIELD(x) x
/* 打包结构支持。 包含u8_t成员,其中一些编译器警告说不需要打包。 */
#define PACK_STRUCT_FLD_8(x) PACK_STRUCT_FIELD(x)
/* 打包结构支持。 包装结构本身的成员,一些编译器警告不必包装。 */
#define PACK_STRUCT_FLD_S(x) PACK_STRUCT_FIELD(x)
/*
PACK_STRUCT_USE_INCLUDES == 1:在打包struct之前和之后使用#include文件支持打包结构。
该文件包含在结构为“arch / bpstruct.h”之前。
该结构为“arch / epstruct.h”后包含的文件。
*/
#define PACK_STRUCT_USE_INCLUDES
/* 消除有关未使用参数的编译器警告 */
#define LWIP_UNUSED_ARG(x) (void)x
/*
LWIP_PROVIDE_ERRNO == 1:让lwIP提供ERRNO值和'errno'变量。
如果禁用此选项,cc.h必须定义'errno',include <errno.h>,定义LWIP_ERRNO_STDINCLUDE以包含 <errno.h>或将LWIP_ERRNO_INCLUDE定义为<errno.h>或等效。
*/
#define LWIP_PROVIDE_ERRNO
#endif /* ARCH_CC_H_ */
"bpstruct.h"是个目前来说没有实际作用的文件夹,只定义了一个参数目的是为了编译不报错误
/*************************************************
File name: bpstruct.h
Author:
Version:
Date:
Description:
Others:
History:
*************************************************/
#ifndef ARCH_BPSTRUCT_H_
#define ARCH_BPSTRUCT_H_
#pragma pack(push,1)
#endif /* ARCH_BPSTRUCT_H_ */
把上诉几个文件及文件夹添加进2.0后面的工程中基本的裸机配置就算结束了,下面描述下怎么lwip的文件结构。
lwip核心的部分包含"core"“include”"netif"文件夹,"core"里面包含的各种协议及调度接口等但是不包含头文件,“include"包含几乎所有协议的头文件,”netif“文件夹所含的与网卡直接关系的文件夹,所以"include"和"core”"netif"是匹配的,在这里netif文件夹内的内容用的很少,是因为网卡驱动部分直接与单片机的网卡驱动匹配,所以抛弃了lwip的网卡驱动接口及逻辑。从单片机中断接收过来的数据直接可以传入。(这里没有进行深入探讨IPV6、bridgeif或ppp协议)。如若要进行常规如交换机的或其他复炸网络设备的驱动这里需要深入地讨论。