⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ethernetif.c

📁 lwip1.1.1 + ucos2.76 for AT91SAM7X256 + RTL8201BL(PHY)
💻 C
字号:
/* * 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> * *//* * This file is a skeleton for developing Ethernet network interface * drivers for lwIP. Add code to the low_level functions and do a * search-and-replace for the word "ethernetif" to replace it with * something that better describes your network interface. */#include "/uCOS_II/includes.h"#include "lwip/opt.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/pbuf.h"#include "lwip/sys.h"#include "/LwIP/include/lwip/stats.h"#include "/LwIP/include/netif/etharp.h"/* Define those to better describe your network interface. */#define IFNAME0 'e'#define IFNAME1 'n'struct ethernetif {  struct eth_addr *ethaddr;  /* Add whatever per-interface state that is needed here. */};HANDLER hEthernetInput;static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};//* 接收线程堆栈#define	T_ETHERNETIF_INPUT_STKSIZE		256OS_STK T_ETHERNETIF_INPUT_STK[T_ETHERNETIF_INPUT_STKSIZE];/* Forward declarations. */static void ethernetif_input(void *pReserved);static err_t ethernetif_output(struct netif *netif, struct pbuf *p,             struct ip_addr *ipaddr);static voidlow_level_init(struct netif *pstNetif){	UBYTE		__ubOldPrio;	UBYTE		__ubErr;	pstNetif->hwaddr_len = NETIF_MAX_HWADDR_LEN;	//* 设置MAC地址	pstNetif->hwaddr[0] = 0xBC;	pstNetif->hwaddr[1] = 0x20;	pstNetif->hwaddr[2] = 0x06;	pstNetif->hwaddr[3] = 0x09;	pstNetif->hwaddr[4] = 0x30;	pstNetif->hwaddr[5] = 0x11;		pstNetif->mtu = 1500;	pstNetif->flags = NETIF_FLAG_BROADCAST;	//* 初始化EMAC。EMACInit()函数包含查询状态位代码,并且这些查询代码并没有使用OSTimeDly()等函数主动释放	//* CPU使用权。如果网线在当时并没有接触良好,则这个过程需要相当长的时间。为了避免阻塞其它低优先级任务的	//* 正常运行,我们使用了uCOS提供的任务管理函数,先将其所在任务的优先级降低,等初始化完成之后再恢复其优	//* 先级。	__ubOldPrio = OSTCBCur->OSTCBPrio;	__ubErr = OSTaskChangePrio(OS_PRIO_SELF, OS_IDLE_PRIO - 1);	EMACInit();	if(__ubErr == OS_NO_ERR)		OSTaskChangePrio(OS_PRIO_SELF, __ubOldPrio);		OSTaskCreate(ethernetif_input, pstNetif, &T_ETHERNETIF_INPUT_STK[T_ETHERNETIF_INPUT_STKSIZE-1], 6);}/* * low_level_output(): * * Should do the actual transmission of the packet. The packet is * contained in the pbuf that is passed to the function. This pbuf * might be chained. *  */static err_tlow_level_output(struct netif *pstNetif, struct pbuf *pstPbuf){		struct pbuf *__pstSendPbuf = pstPbuf;	static HANDLER __hBlockOutput = NULL;	err_t __errReturn = ERR_OK;	if(__hBlockOutput == NULL)		__hBlockOutput = OSAPIBlockNew(5);	//* 阻塞对EMAC的访问,以避免不同的任务同时访问EMAC造成访问冲突的问题,最长等待时间是2秒	if(OS_NO_ERR == OSAPIBlockEnter(__hBlockOutput, 2000))	{		for(; __pstSendPbuf!=NULL; __pstSendPbuf=__pstSendPbuf->next)		{			//* 发送pbuf中的数据,每次一个pbuf,如果__pstSendPbuf->next指针为空则表明已经到达pbuf链表的末尾			if(!EMACSendPacket(__pstSendPbuf->payload, __pstSendPbuf->len, (__pstSendPbuf->next == NULL)))			{				__errReturn = ~ERR_OK;				break;			}		}        OSAPIBlockExit(__hBlockOutput);	}    return __errReturn;}/* * low_level_input(): * * Should allocate a pbuf and transfer the bytes of the incoming * packet from the interface into the pbuf. * */static struct pbuf *low_level_input(struct netif *netif){	struct pbuf     *__pstPbuf = NULL, *__pstCurPbuf;	UWORD			__uwLen;		//* 获取收到的信息包的长度	__uwLen = GetInputPacketLen();	if(__uwLen)	{		//* 从pbuf pool中获取一个pbuf链		__pstPbuf = pbuf_alloc(PBUF_RAW, __uwLen, PBUF_POOL);		if(__pstPbuf != NULL)		{			//* 复制数据			for(__pstCurPbuf=__pstPbuf; __pstCurPbuf!=NULL; __pstCurPbuf=__pstCurPbuf->next)				EMACReadPacket(__pstCurPbuf->payload, __pstCurPbuf->len, (__pstCurPbuf->next == NULL));		}		else;	}		return __pstPbuf;}/* * ethernetif_output(): * * This function is called by the TCP/IP stack when an IP packet * should be sent. It calls the function called low_level_output() to * do the actual transmission of the packet. * */static err_tethernetif_output(struct netif *netif, struct pbuf *p,      struct ip_addr *ipaddr){   /* resolve hardware address, then send (or queue) packet */  return etharp_output(netif, ipaddr, p); }/* * ethernetif_input(): * * This function should be called when a packet is ready to be read * from the interface. It uses the function low_level_input() that * should handle the actual reception of bytes from the network * interface. * */static voidethernetif_input(void *pReserved){	struct ethernetif		*__pstEthernetif;	struct pbuf				*__pstPbuf;	struct eth_hdr			*__pstEthhdr;	struct netif			*__pstNetif;		__pstNetif = (struct netif*)pReserved;				while(TRUE)	{		__pstEthernetif = (struct ethernetif*)__pstNetif->state;				//* 从EMAC循环读取数据		do{			__pstPbuf = low_level_input(__pstNetif);			if(__pstPbuf == NULL)				OSAPISemWait(hEthernetInput, 100);		}while(__pstPbuf == NULL);					__pstEthhdr = __pstPbuf->payload;				switch(htons(__pstEthhdr->type))		{			case ETHTYPE_IP:				//* 更新ARP表				etharp_ip_input(__pstNetif, __pstPbuf);				//* 跳过以太网头部字段				pbuf_header(__pstPbuf, -sizeof(struct eth_hdr) );				 													//* 传递到网络层				__pstNetif->input(__pstPbuf, __pstNetif);								break;					case ETHTYPE_ARP:				//* 将__pstPbuf传递到ARP模块				etharp_arp_input(__pstNetif, __pstEthernetif->ethaddr, __pstPbuf);				break;					default:				pbuf_free(__pstPbuf);				__pstPbuf = NULL;				break;		}		}}static voidarp_timer(void *arg){  etharp_tmr();  sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);}/* * ethernetif_init(): * * Should be called at the beginning of the program to set up the * network interface. It calls the function low_level_init() to do the * actual setup of the hardware. * */err_tethernetif_init(struct netif *netif){  struct ethernetif *ethernetif;      ethernetif = mem_malloc(sizeof(struct ethernetif));    if (ethernetif == NULL)  {  	return ERR_MEM;  }    netif->state = ethernetif;  netif->name[0] = IFNAME0;  netif->name[1] = IFNAME1;  netif->output = ethernetif_output;  netif->linkoutput = low_level_output;    ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);    low_level_init(netif);  etharp_init();  sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);  return ERR_OK;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -