LwIP协议栈移植说明

LWIP 的手动移植可以说非常麻烦,需要我们自己实现好多文件。最主要的是,需要实现的文件还没有个统一的说明,必须要东拼西凑! LWIP 的移植由两部分组成,分别为 LWIP 协议栈和 contrib 应用实例 。目前,这两部分是由两个独立的版本库控制,并且由不同的人来负责的(好消息是从2.1.x之后,这俩要合并了)!我们在移植使用 LWIP 时,需要从官网分别下载这两部分的源代码!
其中,contrib 中是一些和平台移植相关的代码以及一些使用 LWIP 实现的应用,在移植时非常有用;LWIP 则是 TCP/IP 协议栈的核心源码!综上所述,LwIP 的移植主要分为以下这些部分:

  • 运行环境的搭建: 有其需要注意的是,针对嵌入式的移植时,无论哪种模式,都需要首先需要处理硬件资源,例如在 STM32Fx 系列芯片移植时,需要提供文件:stm32f4x7_eth_bsp.c/h,已处理使用的硬件资源。
  • 源代码文件的整理: 主要就是东拼西凑LWIP需要的文件(LWIP需要用户实现很多文件)
  • LwIP 的基本配置: 根据自己的需要来配置功能
  • 与 CPU 或编译器相关源文件: LWIP 需要用户的文件之一
  • 与操作系统的接口源文件(仅在系统模式下使用): LWIP 需要用户的文件之一
  • 与底层网卡驱动的接口源文件: LWIP 需要用户的文件之一
  • 初始化 LwIP:然后正常使用各功能即可。

LwIP协议栈移植说明_第1张图片

第一步:LwIP的基本配置

LwIP 的配置主要通过文件 lwipopts.h 实现,移植时需要根据自己的需要,进行相关的配置,然后在自己的项目中添加该文件。一个在 STM32F4 芯片移植是的配置文件如下:

/**
  ******************************************************************************
  * @file    lwipopts.h
  * @author  MCD Application Team
  * @version V1.1.0
  * @date    31-July-2013
  * @brief   lwIP Options Configuration.
  *          This file is based on Utilities\lwip_v1.4.1\src\include\lwip\opt.h 
  *          and contains the lwIP configuration for the STM32F4x7 demonstration.
  ******************************************************************************
  * @attention
  *
  * 

© COPYRIGHT 2013 STMicroelectronics

* * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); * You may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.st.com/software_license_agreement_liberty_v2 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ****************************************************************************** */ #ifndef __LWIPOPTS_H__ #define __LWIPOPTS_H__ /** * SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain * critical regions during buffer allocation, deallocation and memory * allocation and deallocation. */ #define SYS_LIGHTWEIGHT_PROT 0 #define ETHARP_TRUST_IP_MAC 0 #define IP_REASSEMBLY 0 #define IP_FRAG 0 #define ARP_QUEUEING 0 /** * NO_SYS==1: Provides VERY minimal functionality. Otherwise, * use lwIP facilities. */ #define NO_SYS 0 /* ---------- 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. */ #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 (5*1024) /* 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 100 /* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One per active UDP "connection". */ #define MEMP_NUM_UDP_PCB 6 /* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP connections. */ #define MEMP_NUM_TCP_PCB 10 /* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP connections. */ #define MEMP_NUM_TCP_PCB_LISTEN 5 /* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP segments. */ #define MEMP_NUM_TCP_SEG 20 /* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active timeouts. */ #define MEMP_NUM_SYS_TIMEOUT 10 /* ---------- Pbuf options ---------- */ /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */ #define PBUF_POOL_SIZE 20 /* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */ #define PBUF_POOL_BUFSIZE 500 /* ---------- 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 0 /* TCP Maximum segment size. */ #define TCP_MSS (1500 - 40) /* TCP_MSS = (Ethernet MTU - IP header size - TCP header size) */ /* TCP sender buffer space (bytes). */ #define TCP_SND_BUF (5*TCP_MSS) /* TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at least as much as (2 * TCP_SND_BUF/TCP_MSS) for things to work. */ #define TCP_SND_QUEUELEN (4* TCP_SND_BUF/TCP_MSS) /* TCP receive window. */ #define TCP_WND (2*TCP_MSS) /* ---------- ICMP options ---------- */ #define LWIP_ICMP 1 /* ---------- DHCP options ---------- */ /* Define LWIP_DHCP to 1 if you want DHCP configuration of interfaces. DHCP is not implemented in lwIP 0.5.1, however, so turning this on does currently not work. */ #define LWIP_DHCP 1 /* ---------- UDP options ---------- */ #define LWIP_UDP 1 #define UDP_TTL 255 /* ---------- Statistics options ---------- */ #define LWIP_STATS 0 #define LWIP_PROVIDE_ERRNO 1 /* ---------- link callback options ---------- */ /* LWIP_NETIF_LINK_CALLBACK==1: Support a callback function from an interface * whenever the link changes (i.e., link down) */ #define LWIP_NETIF_LINK_CALLBACK 1 /* -------------------------------------- ---------- Checksum options ---------- -------------------------------------- */ /* The STM32F4x7 allows computing and verifying the IP, UDP, TCP and ICMP checksums by hardware: - To use this feature let the following define uncommented. - To disable it and process by CPU comment the the checksum. */ /* 注意:校验和可以选择 由硬件来处理。如果硬件支持该功能,则加载其配置文件(其中需要定义 CHECKSUM_BY_HARDWARE ) */ #include "stm32f4x7_eth_conf.h" #ifdef CHECKSUM_BY_HARDWARE /* 如果芯片的配置中开启了硬件处理校验和,则对LwIP进行配置 */ /* CHECKSUM_GEN_IP==0: Generate checksums by hardware for outgoing IP packets.*/ #define CHECKSUM_GEN_IP 0 /* CHECKSUM_GEN_UDP==0: Generate checksums by hardware for outgoing UDP packets.*/ #define CHECKSUM_GEN_UDP 0 /* CHECKSUM_GEN_TCP==0: Generate checksums by hardware for outgoing TCP packets.*/ #define CHECKSUM_GEN_TCP 0 /* CHECKSUM_CHECK_IP==0: Check checksums by hardware for incoming IP packets.*/ #define CHECKSUM_CHECK_IP 0 /* CHECKSUM_CHECK_UDP==0: Check checksums by hardware for incoming UDP packets.*/ #define CHECKSUM_CHECK_UDP 0 /* CHECKSUM_CHECK_TCP==0: Check checksums by hardware for incoming TCP packets.*/ #define CHECKSUM_CHECK_TCP 0 /* CHECKSUM_CHECK_ICMP==0: Check checksums by hardware for incoming ICMP packets.*/ #define CHECKSUM_GEN_ICMP 0 #else /* CHECKSUM_GEN_IP==1: Generate checksums in software for outgoing IP packets.*/ #define CHECKSUM_GEN_IP 1 /* CHECKSUM_GEN_UDP==1: Generate checksums in software for outgoing UDP packets.*/ #define CHECKSUM_GEN_UDP 1 /* CHECKSUM_GEN_TCP==1: Generate checksums in software for outgoing TCP packets.*/ #define CHECKSUM_GEN_TCP 1 /* CHECKSUM_CHECK_IP==1: Check checksums in software for incoming IP packets.*/ #define CHECKSUM_CHECK_IP 1 /* CHECKSUM_CHECK_UDP==1: Check checksums in software for incoming UDP packets.*/ #define CHECKSUM_CHECK_UDP 1 /* CHECKSUM_CHECK_TCP==1: Check checksums in software for incoming TCP packets.*/ #define CHECKSUM_CHECK_TCP 1 /* CHECKSUM_CHECK_ICMP==1: Check checksums by hardware for incoming ICMP packets.*/ #define CHECKSUM_GEN_ICMP 1 #endif /* ---------------------------------------------- ---------- Sequential layer options ---------- ---------------------------------------------- */ /** * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c) */ #define LWIP_NETCONN 1 /* ------------------------------------ ---------- Socket options ---------- ------------------------------------ */ /** * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c) */ #define LWIP_SOCKET 0 /* ----------------------------------- ---------- DEBUG options ---------- ----------------------------------- */ #define LWIP_DEBUG 0 /* --------------------------------- ---------- OS options ---------- --------------------------------- */ #define TCPIP_THREAD_NAME "TCP/IP" #define TCPIP_THREAD_STACKSIZE 1000 #define TCPIP_MBOX_SIZE 5 #define DEFAULT_UDP_RECVMBOX_SIZE 2000 #define DEFAULT_TCP_RECVMBOX_SIZE 2000 #define DEFAULT_ACCEPTMBOX_SIZE 2000 #define DEFAULT_THREAD_STACKSIZE 500 #define TCPIP_THREAD_PRIO (configMAX_PRIORITIES - 2) #define LWIP_COMPAT_MUTEX 1 #define LWIP_COMPAT_MUTEX_ALLOWED 1 #endif /* __LWIPOPTS_H__ */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

LwIP协议栈移植说明_第2张图片

LwIP协议栈移植说明_第3张图片

第五步:初始化LwIP

在处理完以上需要移植的个文件后,接下来就可以在自己的项目中建立初始化函数来对LwIP进行初始化了,以下为使用系统时的一个初始化例子。

void LwIP_Init(void)
{
	ip_addr_t ipaddr;
	ip_addr_t netmask;
	ip_addr_t gw;
#ifdef USE_DHCP
	uint8_t iptab[4] = {0};
	uint8_t iptxt[20];
#endif
	/* 创建tcp_ip线程,初始化LwIP的各部分 */
	tcpip_init(NULL, NULL);

	/* 设置IP地址等信息 */
#ifdef USE_DHCP
	ipaddr.addr = 0;
	netmask.addr = 0;
	gw.addr = 0;
#else
	IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
	IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
	IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
#endif
	/* 以下就是添加网络接口了 */
	/* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
	struct ip_addr *netmask, struct ip_addr *gw,
	void *state, err_t (* init)(struct netif *netif),
	err_t (* input)(struct pbuf *p, struct netif *netif))

	Adds your network interface to the netif_list. Allocate a struct
	netif and pass a pointer to this structure as the first argument.
	Give pointers to cleared ip_addr structures when using DHCP,
	or fill them with sane numbers otherwise. The state pointer may be NULL.

	The init function pointer must point to a initialization function for
	your ethernet netif interface. The following code illustrates it's use.*/
	netif_add(&xnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input);

	/*  Registers the default network interface.*/
	netif_set_default(&xnetif);

	if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
	{
		/* Set Ethernet link flag */
		xnetif.flags |= NETIF_FLAG_LINK_UP;

		/* When the netif is fully configured this function must be called.*/
		netif_set_up(&xnetif);
	#ifdef USE_DHCP
		DHCP_state = DHCP_START;
	#endif /* USE_DHCP */
	}
	else
	{
		/*  When the netif link is down this function must be called.*/
		netif_set_down(&xnetif);
	#ifdef USE_DHCP
		DHCP_state = DHCP_LINK_DOWN;
	#endif /* USE_DHCP */
	}
	/* Set the link callback function, this function is called on change of link status*/
	netif_set_link_callback(&xnetif, ETH_link_callback);
}

首先看以下tcpip_init函数,该函数负责建立tcp_ip线程,并且初始化LwIP的各部分功能,具体看以下图片。

LwIP协议栈移植说明_第4张图片

设置IP地址这块没啥可说的,接下来就是添加网络接口,具体参见 LwIP 之 ethernetif.c。

 

你可能感兴趣的:(STM32)