LwIP協議棧裸機移植

1. 準備工作

LwIP正式移植之前,登錄LwIP官方網站:https://savannah.nongnu.org/projects/lwip/,此網站包含有關LwIP協議的各種信息,可根據興趣進行查看。若要下載源碼,點擊Alt進入到LwIP協議的源碼下載界面。這裏您可以看到LwIP協議各個版本的發佈包,根據需求選擇適當版本的“contrib”和“lwip”源碼包進行下載,下載完成後解壓便可看到LwIP的所有源碼。本文中選取的LwIP源碼包的版本分別是“contrib-2.0.1”和“lwip-2.0.3”。

2. 裸機移植LwIP

2.1 LwIP目錄創建

裸機移植LwIP主要應用到“lwip-2.0.3\src”目錄下的源碼文件夾(api、core、和netif)和頭文件夾(include),移植時可在自己的工程中創建“LwIP”目錄,並在此目錄下分別建立api、core、netif和include目錄,如下圖所示:
Alt

2.2 LwIP源文件移植

接下來需要往工程的各個目錄中添加LwIP源文件(以IPV4版本協議的移植爲例):
工程api目錄:添加“lwip-2.0.3\src\api”中的所有文件;
Alt
工程core目錄:添加“lwip-2.0.3\src\core”中除“ipv6”外的所有文件和目錄;
Alt
工程netif目錄:添加“lwip-2.0.3\src\netif”中的所有文件:
Alt
至此,裸機移植LwIP所需的所有源文件都已添加完成,接下來是往工程中添加頭文件。

2.3 LwIP頭文件移植

工程include目錄:添加“lwip-2.0.3\src\include”中的所有文件:
Alt
在工程中“LwIP”目錄下創建“port”目錄,並在工程“port”目錄下創建“include\arch”目錄。將“contrib-2.0.1\ports\unix\minimal”目錄下的“lwipopts.h”文件添加至工程“port\include”目錄中,將“contrib-2.0.1\ports\unix\port\include\arch”目錄下“cc.h”和“perf.h”文件添加至工程目錄“port\include\arch”下,添加完成後如下圖所示:
Alt
lwipopts.h是對LwIP協議使用功能的配置文件,類似於FreeRTOS系統文件中的FreeRTOSConfig.h文件。一個配置例程如下所示:

/**
 * @file
 *
 * lwIP Options Configuration
 */

/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 * 
 * Author: Adam Dunkels <[email protected]>
 *
 */
#ifndef LWIP_LWIPOPTS_H
#define LWIP_LWIPOPTS_H

/*
   -----------------------------------------------
   ---------- Platform specific locking ----------
   -----------------------------------------------
*/

/** 
 * NO_SYS==1: Provides VERY minimal functionality. Otherwise,
 * use lwIP facilities.
 */
#define NO_SYS                          1

/**
 * 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

/*
   ------------------------------------
   ---------- Memory options ----------
   ------------------------------------
*/
/**
 * MEM_ALIGNMENT: should be set to the alignment of the CPU
 *    4 byte alignment -> #define MEM_ALIGNMENT 4
 *    2 byte alignment -> #define MEM_ALIGNMENT 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                        (30 * 1024)

/*
   ------------------------------------------------
   ---------- Internal Memory Pool Sizes ----------
   ------------------------------------------------
*/
/**
 * MEMP_NUM_PBUF: the number of memp struct pbufs (used for PBUF_ROM and PBUF_REF).
 * If the application sends a lot of data out of ROM (or other static memory),
 * this should be set high.
 */
#define MEMP_NUM_PBUF                   30

/**
 * MEMP_NUM_RAW_PCB: Number of raw connection PCBs
 * (requires the LWIP_RAW option)
 */
#define MEMP_NUM_RAW_PCB                4

/**
 * MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One
 * per active UDP "connection".
 * (requires the LWIP_UDP option)
 */
#define MEMP_NUM_UDP_PCB                4

/**
 * MEMP_NUM_TCP_PCB: the number of simulatenously active TCP connections.
 * (requires the LWIP_TCP option)
 */
#define MEMP_NUM_TCP_PCB                2

/**
 * MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP connections.
 * (requires the LWIP_TCP option)
 */
#define MEMP_NUM_TCP_PCB_LISTEN         8

/**
 * MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP segments.
 * (requires the LWIP_TCP option)
 */
#define MEMP_NUM_TCP_SEG                16

/**
 * MEMP_NUM_ARP_QUEUE: the number of simulateously queued outgoing
 * packets (pbufs) that are waiting for an ARP request (to resolve
 * their destination address) to finish.
 * (requires the ARP_QUEUEING option)
 */
#define MEMP_NUM_ARP_QUEUE              2

/**
 * MEMP_NUM_SYS_TIMEOUT: the number of simultaneously active timeouts.
 * The default number of timeouts is calculated here for all enabled modules.
 * The formula expects settings to be either '0' or '1'.
 *
 * To this default value, 1 was added for the snmp_increment timer.
 */
#define MEMP_NUM_SYS_TIMEOUT            (LWIP_TCP + IP_REASSEMBLY + LWIP_ARP + (2*LWIP_DHCP) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + PPP_SUPPORT + (LWIP_IPV6 ? (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD) : 0)) + 1

/**
 * MEMP_NUM_NETBUF: the number of struct netbufs.
 * (only needed if you use the sequential API, like api_lib.c)
 */
#define MEMP_NUM_NETBUF                 0

/**
 * MEMP_NUM_NETCONN: the number of struct netconns.
 * (only needed if you use the sequential API, like api_lib.c)
 */
#define MEMP_NUM_NETCONN                0

/**
 * MEMP_NUM_TCPIP_MSG_API: the number of struct tcpip_msg, which are used
 * for callback/timeout API communication. 
 * (only needed if you use tcpip.c)
 */
#define MEMP_NUM_TCPIP_MSG_API          0

/**
 * MEMP_NUM_TCPIP_MSG_INPKT: the number of struct tcpip_msg, which are used
 * for incoming packets. 
 * (only needed if you use tcpip.c)
 */
#define MEMP_NUM_TCPIP_MSG_INPKT        0

/**
 * PBUF_POOL_SIZE: the number of buffers in the pbuf pool. 
 */
#define PBUF_POOL_SIZE                  32

/**
 * MEMP_OVERFLOW_CHECK: Memory pool overflow check switch.
 */
#define MEMP_OVERFLOW_CHECK             0

/*
   ---------------------------------
   ---------- ARP options ----------
   ---------------------------------
*/
/**
 * LWIP_ARP==1: Enable ARP functionality.
 */
#define LWIP_ARP                        1

/*
   --------------------------------
   ---------- IP options ----------
   --------------------------------
*/
/**
 * IP_FORWARD==1: Enables 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                      0

/**
 * IP_OPTIONS: Defines the behavior for IP options.
 *      IP_OPTIONS==0_ALLOWED: All packets with IP options are dropped.
 *      IP_OPTIONS==1_ALLOWED: IP options are allowed (but not parsed).
 */
#define IP_OPTIONS_ALLOWED              1

/**
 * IP_REASSEMBLY==1: Reassemble incoming fragmented IP packets. Note that
 * this option does not affect outgoing packet sizes, which can be controlled
 * via IP_FRAG.
 */
#define IP_REASSEMBLY                   1

/**
 * IP_FRAG==1: Fragment outgoing IP packets if their size exceeds MTU. Note
 * that this option does not affect incoming packet sizes, which can be
 * controlled via IP_REASSEMBLY.
 */
#define IP_FRAG                         1

/**
 * IP_REASS_MAXAGE: Maximum time (in multiples of IP_TMR_INTERVAL - so seconds, normally)
 * a fragmented IP packet waits for all fragments to arrive. If not all fragments arrived
 * in this time, the whole packet is discarded.
 */
#define IP_REASS_MAXAGE                 3

/**
 * IP_REASS_MAX_PBUFS: Total maximum amount of pbufs waiting to be reassembled.
 * Since the received pbufs are enqueued, be sure to configure
 * PBUF_POOL_SIZE > IP_REASS_MAX_PBUFS so that the stack is still able to receive
 * packets even if the maximum amount of fragments is enqueued for reassembly!
 */
#define IP_REASS_MAX_PBUFS              10

/**
 * IP_FRAG_USES_STATIC_BUF==1: Use a static MTU-sized buffer for IP
 * fragmentation. Otherwise pbufs are allocated and reference the original
 * packet data to be fragmented.
 */
#define IP_FRAG_USES_STATIC_BUF         0

/**
 * IP_DEFAULT_TTL: Default value for Time-To-Live used by transport layers.
 */
#define IP_DEFAULT_TTL                  255

/*
   ----------------------------------
   ---------- ICMP options ----------
   ----------------------------------
*/
/**
 * LWIP_ICMP==1: Enable ICMP module inside the IP stack.
 * Be careful, disable that make your product non-compliant to RFC1122
 */
#define LWIP_ICMP                       1

/**
 * ICMP_TTL: Default value for Time-To-Live used by ICMP packets.
 */
#define ICMP_TTL                       (IP_DEFAULT_TTL)

/*
   ---------------------------------
   ---------- RAW options ----------
   ---------------------------------
*/
/**
 * LWIP_RAW==1: Enable application layer to hook into the IP layer itself.
 */
#define LWIP_RAW                        1

/*
   ----------------------------------
   ---------- DHCP options ----------
   ----------------------------------
*/
/**
 * LWIP_DHCP==1: Enable DHCP module.
 */
#define LWIP_DHCP                       0

/*
   ------------------------------------
   ---------- AUTOIP options ----------
   ------------------------------------
*/
/**
 * LWIP_AUTOIP==1: Enable AUTOIP module.
 */
#define LWIP_AUTOIP                     0

/*
   ----------------------------------
   ---------- SNMP options ----------
   ----------------------------------
*/
/**
 * LWIP_SNMP==1: Turn on SNMP module. UDP must be available for SNMP
 * transport.
 */
#define LWIP_SNMP                       1
#define LWIP_MIB2_CALLBACKS             0
#define MIB2_STATS                      1

/*
   ----------------------------------
   ---------- IGMP options ----------
   ----------------------------------
*/
/**
 * LWIP_IGMP==1: Turn on IGMP module. 
 */
#define LWIP_IGMP                       1

/*
   ----------------------------------
   ---------- DNS options -----------
   ----------------------------------
*/
/**
 * LWIP_DNS==1: Turn on DNS module. UDP must be available for DNS
 * transport.
 */
#define LWIP_DNS                        0

/*
   ---------------------------------
   ---------- UDP options ----------
   ---------------------------------
*/
/**
 * LWIP_UDP==1: Turn on UDP.
 */
#define LWIP_UDP                        1

/**
 * LWIP_UDPLITE==1: Turn on UDP-Lite. (Requires LWIP_UDP)
 */
#define LWIP_UDPLITE                    0

/**
 * UDP_TTL: Default Time-To-Live value.
 */
#define UDP_TTL                         (IP_DEFAULT_TTL)

/*
   ---------------------------------
   ---------- TCP options ----------
   ---------------------------------
*/
/**
 * LWIP_TCP==1: Turn on TCP.
 */
#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 sender buffer space (bytes). */
#define TCP_SND_BUF                     (4 * 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                (2 * TCP_SND_BUF / TCP_MSS)

/* TCP receive window. */
#define TCP_WND                         (2 * TCP_MSS)

/*
   ----------------------------------
   ---------- Pbuf options ----------
   ----------------------------------
*/
/**
 * PBUF_LINK_HLEN: the number of bytes that should be allocated for a
 * link level header. The default is 14, the standard value for
 * Ethernet.
 */
#define PBUF_LINK_HLEN                  16

/*
   ------------------------------------
   ---------- LOOPIF options ----------
   ------------------------------------
*/
/**
 * LWIP_HAVE_LOOPIF==1: Support loop interface (127.0.0.1) and loopif.c
 */
#define LWIP_HAVE_LOOPIF                0

/*
   ----------------------------------------------
   ---------- Sequential layer options ----------
   ----------------------------------------------
*/

/**
 * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c)
 */
#define LWIP_NETCONN                    0

/*
   ------------------------------------
   ---------- Socket options ----------
   ------------------------------------
*/
/**
 * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c)
 */
#define LWIP_SOCKET                     0

/*
   ----------------------------------------
   ---------- Statistics options ----------
   ----------------------------------------
*/
/**
 * LWIP_STATS==1: Enable statistics collection in lwip_stats.
 */
#define LWIP_STATS                      1

/* Self-define data. */
/* LWIP MAC address define. */
#define LWIP_MAC_ADDR                   0x11, 0x22, 0x33, 0x44, 0x55, 0x66
/* LWIP net host name define. */
#define LWIP_NETIF_HOSTNAME             1
#define LWIP_NETIF_HOSTNAME_TEXT        "LwIP_Net"
/* LWIP use IPV4 address. */
#define LWIP_IPV4                       1
/* Ethernet link speed define. */
#define LWIP_LINK_SPEED_IN_BPS          100000000
/* LWIP gateway, IP address and netmask define. */
#define LWIP_INIT_IPADDR(addr)          IP4_ADDR((addr), 192,168,0,10)
#define LWIP_INIT_NETMASK(addr)         IP4_ADDR((addr), 255,255,255,0)
#define LWIP_INIT_GW(addr)              IP4_ADDR((addr), 192,168,0,1)

/*
   ---------------------------------------
   ---------- Debugging options ----------
   ---------------------------------------
*/
#define TAPIF_DEBUG      LWIP_DBG_ON
#define TUNIF_DEBUG      LWIP_DBG_OFF
#define UNIXIF_DEBUG     LWIP_DBG_OFF
#define DELIF_DEBUG      LWIP_DBG_OFF
#define SIO_FIFO_DEBUG   LWIP_DBG_OFF
#define TCPDUMP_DEBUG    LWIP_DBG_ON
#define API_LIB_DEBUG    LWIP_DBG_ON
#define API_MSG_DEBUG    LWIP_DBG_ON
#define TCPIP_DEBUG      LWIP_DBG_ON
#define NETIF_DEBUG      LWIP_DBG_ON
#define SOCKETS_DEBUG    LWIP_DBG_ON
#define DEMO_DEBUG       LWIP_DBG_ON
#define IP_DEBUG         LWIP_DBG_ON
#define IP_REASS_DEBUG   LWIP_DBG_ON
#define RAW_DEBUG        LWIP_DBG_ON
#define ICMP_DEBUG       LWIP_DBG_ON
#define UDP_DEBUG        LWIP_DBG_ON
#define TCP_DEBUG        LWIP_DBG_ON
#define TCP_INPUT_DEBUG  LWIP_DBG_ON
#define TCP_OUTPUT_DEBUG LWIP_DBG_ON
#define TCP_RTO_DEBUG    LWIP_DBG_ON
#define TCP_CWND_DEBUG   LWIP_DBG_ON
#define TCP_WND_DEBUG    LWIP_DBG_ON
#define TCP_FR_DEBUG     LWIP_DBG_ON
#define TCP_QLEN_DEBUG   LWIP_DBG_ON
#define TCP_RST_DEBUG    LWIP_DBG_ON

extern unsigned char debug_flags;
#define LWIP_DBG_TYPES_ON debug_flags

#endif /* LWIP_LWIPOPTS_H */

cc.h包含針對處理器和編譯器的一些數據類型、字節對齊方式和宏定義等內容。一個例程如下所示:

/*
 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
 * All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 * 
 * Author: Adam Dunkels <[email protected]>
 *
 */
#ifndef LWIP_ARCH_CC_H
#define LWIP_ARCH_CC_H

#include <stdio.h>
#include <stdint.h>

#define LWIP_NO_STDINT_H        1

/* Define generic types used in lwIP. */
typedef uint8_t   u8_t;
typedef int8_t    s8_t;
typedef uint16_t  u16_t;
typedef int16_t   s16_t;
typedef uint32_t  u32_t;
typedef int32_t   s32_t;
typedef uintptr_t mem_ptr_t;
typedef int sys_prot_t;

/* Define (sn)printf formatters for these lwIP types. */
#define X8_F  "02x"
#define U16_F "hu"
#define S16_F "hd"
#define X16_F "hx"
#define U32_F "lu"
#define S32_F "ld"
#define X32_F "lx"
#define SZT_F "lu"

/* Little endian mode. */
#define BYTE_ORDER              LITTLE_ENDIAN

/* define compiler specific symbols */
#if defined (__ICCARM__)
#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x
#define PACK_STRUCT_USE_INCLUDES

#elif defined (__CC_ARM)

#define PACK_STRUCT_BEGIN __packed
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x

#elif defined (__GNUC__)

#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT __attribute__ ((__packed__))
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x

#elif defined (__TASKING__)

#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_STRUCT
#define PACK_STRUCT_END
#define PACK_STRUCT_FIELD(x) x

#endif

#define LWIP_PLATFORM_ASSERT(x) do {printf("Assertion failed: ");\
                                    printf("Assertion failed: ");}while(0)

extern u32_t sys_now(void);


#endif /* LWIP_ARCH_CC_H */

cc.h文件中聲明的sys_now函數需藉助處理芯片(如MCU)的定時器實現。

perf.h是與系統統計和測量有關的文件,一般無需求是可不使用。如下所示:

/*
 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
 * All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 * 
 * Author: Adam Dunkels <[email protected]>
 *
 */
#ifndef LWIP_ARCH_PERF_H
#define LWIP_ARCH_PERF_H

#define PERF_START    /* null definition */
#define PERF_STOP(x)  /* null definition */

#endif /* LWIP_ARCH_PERF_H */

2.4 網口驅動開發

網口驅動主要包含兩部分的驅動:MAC模塊驅動和PHY模塊驅動。MAC和PHY位於ISO/OSI模型的最後兩層,完成數據鏈路層和物理層的數據傳輸。兩者之間的具體關係可在網上查詢學習,此處不做過多闡述。

2.4.1 MAC模塊驅動

一般MAC模塊位於MCU中,MCU廠商會提供相應的基本驅動程序,如若沒有,則需要開發人員參照Datasheet描述進行編寫,必須要實現的接口包括模塊初始化、數據發送和數據接收接口。其中模塊初始化的基本步驟大致如下:
1、Ethernet端口時鐘設置;
2、Ethernet端口引腳設置;
3、MAC模塊具體寄存器設置,涉及到MAC地址、接口模式(MII、RMII等)、通信速率、數據接收和發送緩存設置、數據接收和發送模式(輪詢、中斷或者DMA)等;
4、模塊功能(數據接收和發送)啓動。

2.4.2 PHY模塊驅動

PHY模塊通過MII、RMII等連接方式與MAC模塊進行連接,在編寫驅動時需要參考具體的硬件設計以及PHY芯片數據手冊完成驅動操作。可參考如下步驟進行:
1、PHY芯片電源控制,按照硬件電路設計完成對PHY芯片上電和下電的驅動控制;
2、參考PHY芯片數據手冊完成初始化設置,具體配置信息根據PHY芯片的不同或有差異,但是主要目的都是爲了打通網絡數據的傳輸鏈路。

開發人員可能會發現,有的硬件電路設計中找不到PHY芯片,與處理器MAC模塊的通信連接可能會被叫做Switch芯片的模塊代替,此處要說明的是Switch常用於多網口的電路設計中,由於Switch芯片內部集成了多個MAC和PHY模塊,可完成數據在多路網口之間的數據傳輸。此時只需參照PHY芯片的驅動開發步驟完成Switch芯片的驅動的開發。當然,Switch芯片的功能要比PHY芯片的功能複雜,驅動支持接口可能會相對較多,開發人員需根據使用需求選擇具體開發哪些功能驅動。

網口驅動開發一旦完成,便保證了網絡數據的傳輸鏈路已經打通,在移植過程中便不用再擔心硬件導致的LwIP數據傳輸不成功問題了,接下來便是將網口驅動程序加入到LwIP協議棧中的工作。

2.5 LwIP網卡接口適配

LwIP網卡驅動適配工作主要是完善“lwip-2.0.3\src\netif\ethernetif.c”文件中部分函數接口,包括以下函數:
low_level_init
low_level_output
low_level_input
在low_level_init函數中應當完成MAC地址的設置,同時調用網口驅動初始化函數,完成網絡的初始化工作,low_level_init函數中已經指示了應當在哪些地方進行修改,下述代碼是移植過程中的一個實例,其中帶有“Add by myself start ”和“Add by myself end”註釋部分是移植過程中添加的完善代碼;

static void
low_level_init(struct netif *netif)
{
  struct ethernetif *ethernetif = netif->state;
  /* Add by myself start */
  uint8_t aMacAddr[6] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
  /* Add by myself end */

  /* set MAC hardware address length */
  netif->hwaddr_len = ETHARP_HWADDR_LEN;

  /* Add by myself start */
  /* set MAC hardware address */
  netif->hwaddr[0] = aMacAddr[0];
  netif->hwaddr[1] = aMacAddr[1];
  netif->hwaddr[2] = aMacAddr[2];
  netif->hwaddr[3] = aMacAddr[3];
  netif->hwaddr[4] = aMacAddr[4];
  netif->hwaddr[5] = aMacAddr[5];
  /* Add by myself end */

  /* maximum transfer unit */
  netif->mtu = 1500;

  /* Add by myself start */
  /* Device ethernet port set. */
  netif->num = ETHERNET_PORT0;
  /* Add by myself end*/

  /* device capabilities */
  /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
#if LWIP_IPV6 && LWIP_IPV6_MLD
  /*
   * For hardware/netifs that implement MAC filtering.
   * All-nodes link-local is handled by default, so we must let the hardware know
   * to allow multicast packets in.
   * Should set mld_mac_filter previously. */
  if (netif->mld_mac_filter != NULL) {
    ip6_addr_t ip6_allnodes_ll;

    ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
    netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  }
#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */

  /* Do whatever else is needed to initialize interface. */
  /* Add by myself start */
  DevEthernetInit(netif->num);
  /* Add by myself end*/
}

在low_level_output函數中調用網卡數據發送接口,完成網絡數據的發送工作,low_level_output函數中已經指示了應當在哪些地方進行修改,下述代碼是移植過程中的一個實例,其中帶有“Add by myself start ”和“Add by myself end”註釋部分是移植過程中添加的完善代碼;

static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
  struct ethernetif *ethernetif = netif->state;
  struct pbuf *q;

//  initiate transfer();

#if ETH_PAD_SIZE
  pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
#endif

  for (q = p; q != NULL; q = q->next) {
    /* Send the data from the pbuf to the interface, one pbuf at a
       time. The size of the data in each pbuf is kept in the ->len
       variable. */
//    send data from(q->payload, q->len);
      /* Add by myself start */
      DevEthernetSend(netif->num, q->payload, q->len);
      /* Add by myself end */
  }

//  signal that packet should be sent();

  MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
  if (((u8_t*)p->payload)[0] & 1) {
    /* broadcast or multicast packet*/
    MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
  } else {
    /* unicast packet */
    MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
  }
  /* increase ifoutdiscards or ifouterrors on error */

#if ETH_PAD_SIZE
  pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif

  LINK_STATS_INC(link.xmit);

  return ERR_OK;
}

在low_level_input函數中調用網卡數據接收接口,完成網絡數據的接收工作,low_level_input函數中已經指示了應當在哪些地方進行修改,下述代碼是移植過程中的一個實例,其中帶有“Add by myself start ”和“Add by myself end”註釋部分是移植過程中添加的完善代碼;

static struct pbuf *
low_level_input(struct netif *netif)
{
  struct ethernetif *ethernetif = netif->state;
  struct pbuf *p, *q;
  u16_t len;
  /* Add by myself start */
  uint8_t *pFrame = NULL;
  /* Add by myself end */

  /* Obtain the size of the packet and put it into the "len"
     variable. */
  /* Add by myself start */
  len = DevEthernetGetRecvLength(netif->num);
  /* Add by myself end */

#if ETH_PAD_SIZE
  len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
#endif

  /* We allocate a pbuf chain of pbufs from the pool. */
  p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);

  if (p != NULL) {

#if ETH_PAD_SIZE
    pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
#endif

    /* We iterate over the pbuf chain until we have read the entire
     * packet into the pbuf. */
    for (q = p; q != NULL; q = q->next) {
      /* Read enough bytes to fill this pbuf in the chain. The
       * available data in the pbuf is given by the q->len
       * variable.
       * This does not necessarily have to be a memcpy, you can also preallocate
       * pbufs for a DMA-enabled MAC and after receiving truncate it to the
       * actually received size. In this case, ensure the tot_len member of the
       * pbuf is the sum of the chained pbuf len members.
       */
      //read data into(q->payload, q->len);
      /* Add by myself start */
      /* Receive data from MCU ethernet port. */
      pFrame = (uint8_t *)DevEthernetRecv(netif->num);
      if (NULL != pFrame)
      {
          memcpy((uint8_t *)q->payload, pFrame, len);
      }
      /* Add by myself end */
    }
    //acknowledge that packet has been read();
    /* Add by myself start */
    DevEthernetFreeRecvBuf(netif->num);
    /* Add by myself end */

    MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
    if (((u8_t*)p->payload)[0] & 1) {
      /* broadcast or multicast packet*/
      MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
    } else {
      /* unicast packet*/
      MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
    }
#if ETH_PAD_SIZE
    pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif

    LINK_STATS_INC(link.recv);
  } else {
    drop packet();
    LINK_STATS_INC(link.memerr);
    LINK_STATS_INC(link.drop);
    MIB2_STATS_NETIF_INC(netif, ifindiscards);
  }

  return p;
}

上述接口完善結束,意味着離LwIP裸機移植的成功又進了一步,接下來便是想辦法將LwIP協議棧調度運行起來,測試運行效果。

2.6 LwIP運行和測試

在調度運行LwIP之前還需完成兩個重要的接口:LwIP_InitLwIP_Test
LwIP_Init用於完成LwIP協議棧的初始化工作,如網關設置、IP地址設置、子網掩碼設置、初始化處理和網絡鏈接啓動等。有關網關、IP地址和子網掩碼等信息的配置已在lwipopts.h文件中體現,如下所示:

/* Self-define data. */
/* LWIP MAC address define. */
#define LWIP_MAC_ADDR                   0x11, 0x22, 0x33, 0x44, 0x55, 0x66
/* LWIP net host name define. */
#define LWIP_NETIF_HOSTNAME             1
#define LWIP_NETIF_HOSTNAME_TEXT        "LwIP_Net"
/* LWIP use IPV4 address. */
#define LWIP_IPV4                       1
/* Ethernet link speed define. */
#define LWIP_LINK_SPEED_IN_BPS          100000000
/* LWIP gateway, IP address and netmask define. */
#define LWIP_INIT_IPADDR(addr)          IP4_ADDR((addr), 192,168,0,10)
#define LWIP_INIT_NETMASK(addr)         IP4_ADDR((addr), 255,255,255,0)
#define LWIP_INIT_GW(addr)              IP4_ADDR((addr), 192,168,0,1)

LwIP_Init函數的實現例程如下所示:

void LwIP_Init(void)
{
#if LWIP_IPV4
    ip4_addr_t ipaddr, netmask, gw;
#if (!LWIP_DHCP) && (!LWIP_AUTOIP)
    LWIP_INIT_GW(&gw);
    LWIP_INIT_IPADDR(&ipaddr);
    LWIP_INIT_NETMASK(&netmask);
#endif /* (!LWIP_DHCP) && (!LWIP_AUTOIP) */
#endif /* LWIP_IPV4 */

    /* LwIP service init process. */
    lwip_init();

    /* Config netif layer parameters. */
    netif_set_default(netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, netif_input));

    /* Check to set netif up flag. */
    if (netif_is_link_up(&gnetif))
    {
        /* If netif link is fully configured, this function must be called. */
        netif_set_up(&gnetif);
    }
    else
    {
        /* If netif link is down, this function must be called. */
        netif_set_down(&gnetif);
    }
}

LwIP_Test函數則用於接收網絡數據和監測LwIP的timeout事件,此函數要週期性地被調用,保證網絡模塊接收到的網絡數據及時傳入LwIP協議棧中進行處理。LwIP_Test函數示例如下所示:

void LwIPTestProcess(void)
{
    /* Ethernet data receiving process. */
    ethernetif_input(&gnetif);
    
    /* Check LwIP timeout event. */
    sys_check_timeouts();
}

進行到這一步,LwIP裸機移植工作已全部完成,如無意外,此時應當可以通過電腦端cmd窗口與燒錄了LwIP程序的硬件開發板進行ping通信,前提是需設置電腦IPv4的網關地址、IP地址和子網掩碼,保證電腦和開發板在同一IP段內。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章