在LWIP中需要添加的驱动的位置:ethernetif.c文件
void ethernetif_input(struct netif *netif)
{
struct ethernetif *ethernetif;
struct eth_hdr *ethhdr;
struct pbuf *p;
while(1)
{
ethernetif = netif->state;
do{
p = low_level_input(netif);
if (p == NULL)
OSTimeDlyHMSM(0,0,0,100);
}while(p == NULL);
#if LINK_STATS
lwip_stats.link.recv++;
#endif
ethhdr = p->payload;
switch (htons(ethhdr->type)) {
case ETHTYPE_IP:
#if 1
etharp_ip_input(netif, p); //更新ARP表
#endif
//跳过以太网头部字段
pbuf_header(p, -sizeof(struct eth_hdr));
//传递到网络层
netif->input(p, netif);
break;
case ETHTYPE_ARP:
//将netif传递到ARP模块
p = etharp_arp_input(netif, ethernetif->ethaddr, p);
if(p != NULL) {
low_level_output(netif, p);
pbuf_free(p);
p = NULL;
}
break;
default:
pbuf_free(p);
p = NULL;
break;
}
}
}
static struct pbuf * low_level_input(struct netif *netif)
{
struct ethernetif *ethernetif;
struct pbuf *p = NULL, *q;
u16_t len;
int flag;
unsigned int i;
unsigned char buffer[4096];
unsigned char *des;
unsigned char *data;
data = buffer;
ethernetif = netif->state;
flag = board_eth_rcv(data,&len);
#if 0
if(flag == 1) {
des = data;
LWIP_DEBUGF(RTL8019_DEBUG, ("\r\n"));
for(i=0; i<len; i++) {
LWIP_DEBUGF(RTL8019_DEBUG, ("0x%02X ",*des++));
}
LWIP_DEBUGF(RTL8019_DEBUG, ("\r\n"));
LWIP_DEBUGF(RTL8019_DEBUG, ("\r\n"));
LWIP_DEBUGF(RTL8019_DEBUG, ("the receive data length is %d\r\n",len));
LWIP_DEBUGF(RTL8019_DEBUG, ("\r\n"));
}
#endif
if(flag == 1) {
#if ETH_PAD_SIZE
len += ETH_PAD_SIZE;
#endif
//p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
//Modify by Small.Box at 06.10.16
if(p == NULL) {
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
}
if (p != NULL) {
#if ETH_PAD_SIZE
pbuf_header(p, -ETH_PAD_SIZE);
#endif
for(q = p; q != NULL; q = q->next) {
memcpy(q->payload,(unsigned char *)data, q->len);
data += q->len;
}
#if ETH_PAD_SIZE
pbuf_header(p, ETH_PAD_SIZE);
#endif
#if LINK_STATS
lwip_stats.link.recv++;
#endif
} else {
#if LINK_STATS
lwip_stats.link.memerr++;
lwip_stats.link.drop++;
#endif
}
}
return p;
}
static err_t low_level_output(struct netif *netif, struct pbuf *p)
{
struct ethernetif *ethernetif;
struct pbuf *q;
ethernetif= netif->state;
#if ETH_PAD_SIZE
pbuf_header(p, -ETH_PAD_SIZE);
#endif
for(q = p; q != NULL; q = q->next) {
board_eth_send(q->payload,q->len);
}
#if ETH_PAD_SIZE
pbuf_header(p, ETH_PAD_SIZE);
#endif
#if LINK_STATS
lwip_stats.link.xmit++;
#endif /* LINK_STATS */
return ERR_OK;
}