猪八戒网做网站如何/广告发布
一目了然
- 1 准备工作
- 2 裸机移植LwIP
- 2.1 LwIP目录创建
- 2.2 LwIP源文件移植
- 2.3 LwIP头文件移植
- 2.4 网口驱动开发
- 2.4.1 MAC模块驱动
- 2.4.2 PHY模块驱动
- 2.5 LwIP网卡接口适配
- 2.6 LwIP运行和测试
- 3 TC297 Ethernet中断传输
1 准备工作
LwIP协议栈(下文中简称LwIP)正式移植之前,登录LwIP官方网站:https://savannah.nongnu.org/projects/lwip/,此网站包含有关LwIP的各种信息,可根据兴趣进行查看。若要下载源码,点击进入到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目录,如下图所示:
2.2 LwIP源文件移植
接下来需要往工程的各个目录中添加LwIP源文件(以IPV4版本协议的移植为例):
工程api目录:添加“lwip-2.0.3\src\api”中的所有文件;
工程core目录:添加“lwip-2.0.3\src\core”中除“ipv6”外的所有文件和目录;
工程netif目录:添加“lwip-2.0.3\src\netif”中的所有文件:
至此,裸机移植LwIP所需的所有源文件都已添加完成,接下来是往工程中添加头文件。
2.3 LwIP头文件移植
工程include目录:添加“lwip-2.0.3\src\include”中的所有文件:
在工程中“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”下,添加完成后如下图所示:
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 <adam@sics.se>**/
#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 oforder. 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 atleast 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_ONextern 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 <adam@sics.se>**/
#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 <adam@sics.se>**/
#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、模块功能(数据接收和发送)启动。
参考驱动下载链接如下:https://download.csdn.net/download/weixin_43986229/20101546
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_SIZEpbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
#endiffor (q = p; q != NULL; q = q->next) {/* Send the data from the pbuf to the interface, one pbuf at atime. The size of the data in each pbuf is kept in the ->lenvariable. */
// 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_SIZEpbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endifLINK_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_SIZElen += 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_SIZEpbuf_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_SIZEpbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endifLINK_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_Init和LwIP_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_IPV4ip4_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段内。
3 TC297 Ethernet中断传输
使用TC297 Ethernet中断传输,开启Ethernet传输和发送模块之前,应当注意以下事项:
1、配置Ethernet中断优先级,TC297的中断优先级数越大,代表优先级越高,注意配置适当的优先级,注意优先级数值为1~256,例程中设置的优先级为15;
2、使能Ethernet接收和发送中断,若Ethernet中断优先级正常配置,此步骤会在IfxEth_init函数中处理;
3、注册中断函数,使用InterruptInstall函数注册;
Ethernet中断处理函数的实现注意事项:
由于TC297的Ethernet只对应一个中断服务请求(SRC_ID_ETH),因此Ethernet发送和接收中断(或者Ethernet模块的其他中断)都对应着同一个中断服务函数,因此在中断服务函数中注意判断相应的中断原因,并进行对应的处理和清除中断状态位(若不清除,可能导致无法再次进入中断)。一个简单的示例如下图所示:
/*** Abstract:* Ethernet module interrupt handler function.** Parameters:* i32Arg: Arguments passing into the function.** Return:* None.*/
static void IsrEthHandler(int i32Arg)
{uint32_t u32Ret = 0;/* Just to avoid compiler warnings about unused parameters. */(void) i32Arg;/* Check for ethernet RX interrupt. */if (TRUE == IfxEth_isRxInterrupt(&sEth)){/* Clear Rx interrupt status bit. */IfxEth_clearRxInterrupt(&sEth);}/* Check for ethernet TX interrupt. */if (TRUE == IfxEth_isTxInterrupt(&sEth)){/* Clear Tx interrupt status bit. */IfxEth_clearTxInterrupt(&sEth);}
}