📄 ethernetif.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.h"#include "lwip/opt.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/pbuf.h"#include "lwip/sys.h"#include "lwip/stats.h"#include "lib_emac.h"#include "netif/etharp.h"///#include "mac_dm9000.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. */};OS_EVENT* 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;//0x00,0x19,0x21,0xcb,0x60,0x0b //* 设置MAC地址 pstNetif->hwaddr[0] = 0x00; pstNetif->hwaddr[1] = 0x19; pstNetif->hwaddr[2] = 0x21; pstNetif->hwaddr[3] = 0xcb; pstNetif->hwaddr[4] = 0x60; pstNetif->hwaddr[5] = 0x0b; pstNetif->mtu = 1500; pstNetif->flags = NETIF_FLAG_BROADCAST; //* 初始化EMAC。EMACInit()函数包含查询状态位代码,并且这些查询代码并没有使用OSTimeDly()等函数主动释放 //* CPU使用权。如果网线在当时并没有接触良好,则这个过程需要相当长的时间。为了避免阻塞其它低优先级任务的 //* 正常运行,我们使用了uCOS提供的任务管理函数,先将其所在任务的优先级降低,等初始化完成之后再恢复其优 //* 先级。 __ubOldPrio = OSTCBCur->OSTCBPrio; __ubErr = OSTaskChangePrio(OS_PRIO_SELF, OS_TASK_IDLE_PRIO-3); /* 实际的网卡初始化程序 纯粹的网卡初始化 不涉及到协议栈的初始化 */ //*这个函数是调用的MAC层函数,MAC层也有一个任务,是负责处理发送和接受的链路数据的,最为底层,优先级最高的任务 EMACInit(); if(__ubErr == OS_NO_ERR) OSTaskChangePrio(OS_PRIO_SELF, __ubOldPrio); OSTaskCreate(ethernetif_input, pstNetif, &T_ETHERNETIF_INPUT_STK[T_ETHERNETIF_INPUT_STKSIZE-1], 5);}/* * 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 LWIP_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((char *)__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=0; struct buffer_pool * temp=NULL;// OSAPISemWait(hEthernetInput, 0); //* 获取收到的信息包的结构体 temp = GetInputPacketLen(); if(temp) { __uwLen=temp->leng; //* 从pbuf pool中获取一个pbuf链 __pstPbuf = pbuf_alloc(PBUF_RAW, __uwLen, PBUF_POOL); if(__pstPbuf != NULL) { //* 复制数据 for(__pstCurPbuf=__pstPbuf; __pstCurPbuf!=NULL; __pstCurPbuf=__pstCurPbuf->next) EMACReadPacket(temp,(char *)__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. * */ /*这个函数是一个被任务调用的函数*/ OS_EVENT* EthernetInput;static voidethernetif_input(void *pReserved){ struct ethernetif *__pstEthernetif; struct pbuf *__pstPbuf; struct eth_hdr *__pstEthhdr; struct netif *__pstNetif; /* 传进来的网卡资料 */ __pstNetif = (struct netif*)pReserved; while(1) { __pstEthernetif = (struct ethernetif*)__pstNetif->state; //* 从EMAC循环读取数据 do{ __pstPbuf = low_level_input(__pstNetif); if(__pstPbuf == NULL) ///*采用查询的方式工作,一直在查看底层的网卡是否有新的数据过来了 OSAPISemWait(EthernetInput, 300); //Printf("wait Sem! \n"); }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){ static struct ethernetif ethernetif; netif->state = ðernetif; 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 + -