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

📄 xemacif.c

📁 NXPl788上lwip的无操作系统移植,基于Embest开发板
💻 C
📖 第 1 页 / 共 2 页
字号:
   }
   xil_printf ("\r\n\r\n");
#endif

#ifdef LINK_STATS
   lwip_stats.link.xmit++;
#endif /* LINK_STATS */

   return ERR_OK;
}

/*---------------------------------------------------------------------------*/
/* low_level_input()                                                         */
/*                                                                           */
/* Allocates a pbuf pool and transfers bytes of                              */
/* incoming packet from the interface into the pbuf.                         */
/*---------------------------------------------------------------------------*/
static struct pbuf * low_level_input(XEmacIf_Config *xemacif_ptr)
{
   struct pbuf *p = NULL, *q = NULL;
   XEmac *EmacPtr = (XEmac *) xemacif_ptr->instance_ptr;
   
   Xuint32 RecvBuffer[XEM_MAX_FRAME_SIZE_IN_WORDS];
   Xuint32 FrameLen = XEM_MAX_FRAME_SIZE;
   Xuint32 i, Options;
   u8_t * frame_bytes = (u8_t *) RecvBuffer;
   XStatus Result;

   Result = XEmac_PollRecv(EmacPtr, (Xuint8 *)RecvBuffer, &FrameLen);

   if (Result != XST_SUCCESS)
   {
      if (!(Result == XST_NO_DATA || Result == XST_BUFFER_TOO_SMALL))
      {
         XEmac_Reset(xemacif_ptr->instance_ptr);
         XEmac_SetMacAddress(xemacif_ptr->instance_ptr, 
               (Xuint8*) xemacif_ptr->ethaddr.addr);
         Options = ( XEM_INSERT_FCS_OPTION | 
                     XEM_INSERT_PAD_OPTION | 
                     XEM_UNICAST_OPTION | 
                     XEM_BROADCAST_OPTION | 
                     XEM_POLLED_OPTION | 
                     XEM_STRIP_PAD_FCS_OPTION);
         XEmac_SetOptions(xemacif_ptr->instance_ptr, Options);
         XEmac_Start(xemacif_ptr->instance_ptr);
      }
      return p;
   }

#if 0
   xil_printf("\r\n");
   for (i=0 ; i < FrameLen ; i++) {
      xil_printf("%2X", frame_bytes[i]);
      if (! (i%20) && i) xil_printf("\r\n");
      else xil_printf(" ");
   }
   xil_printf ("\r\n");
#endif

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

   if (p != NULL) {
   /* 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. */
         for (i = 0 ; i < q->len ; i++) {
            ((u8_t *)q->payload)[i] = *(frame_bytes++);
         }
      }

#ifdef LINK_STATS
      lwip_stats.link.recv++;
#endif /* LINK_STATS */      

   } else {

#ifdef LINK_STATS
      lwip_stats.link.memerr++;
      lwip_stats.link.drop++;
#endif /* LINK_STATS */ 
      ;
   }
   return p;  
}

/*---------------------------------------------------------------------------*/
/* xemacif_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.                                                                */
/*---------------------------------------------------------------------------*/
err_t xemacif_input(void *CallBackRef)
{
   struct netif * netif_ptr = (struct netif *) CallBackRef;
   XEmacIf_Config * xemacif_ptr;
   struct eth_hdr * ethernet_header;
   struct pbuf *p;

   xemacif_ptr = netif_ptr->state;

   p = low_level_input(xemacif_ptr);

   if (p != NULL) {
      ethernet_header = p->payload;

      q = NULL;
      switch (htons(ethernet_header->type)) {
      case ETHTYPE_IP:
         etharp_ip_input(netif_ptr, p);
         pbuf_header(p, -14);
         netif_ptr->input(p, netif_ptr);
         break;
      case ETHTYPE_ARP:
         etharp_arp_input(netif_ptr, &(xemacif_ptr->ethaddr), p);
         break;
      default:
         pbuf_free(p);
         break;
      }
   }

   return ERR_OK;
}

/*---------------------------------------------------------------------------*/
/* xemacif_setmac():                                                         */
/*                                                                           */
/* Sets the MAC address of the system.                                       */
/* Note:  Should only be called before xemacif_init is called.               */
/*          - the stack calls xemacif_init after the user calls netif_add    */
/*---------------------------------------------------------------------------*/
void xemacif_setmac(u32_t index, u8_t *addr)
{
   XEmacIf_ConfigTable[index].ethaddr.addr[0] = addr[0];
   XEmacIf_ConfigTable[index].ethaddr.addr[1] = addr[1];
   XEmacIf_ConfigTable[index].ethaddr.addr[2] = addr[2];
   XEmacIf_ConfigTable[index].ethaddr.addr[3] = addr[3];
   XEmacIf_ConfigTable[index].ethaddr.addr[4] = addr[4];
   XEmacIf_ConfigTable[index].ethaddr.addr[5] = addr[5];
}

/*---------------------------------------------------------------------------*/
/* xemacif_getmac():                                                         */
/*                                                                           */
/* Returns a pointer to the ethaddr variable in  the ConfigTable             */
/* (6 bytes in length)                                                       */
/*---------------------------------------------------------------------------*/
u8_t * xemacif_getmac(u32_t index) {
   return &(XEmacIf_ConfigTable[index].ethaddr.addr[0]);
}

/*---------------------------------------------------------------------------*/
/* xemacif_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_t xemacif_init(struct netif *netif_ptr)
{
   XEmacIf_Config *xemacif_ptr;

   xemacif_ptr = (XEmacIf_Config *) netif_ptr->state;

   netif_ptr->mtu = 1500;
   netif_ptr->hwaddr_len = 6;
   netif_ptr->hwaddr[0] = xemacif_ptr->ethaddr.addr[0];
   netif_ptr->hwaddr[1] = xemacif_ptr->ethaddr.addr[1];
   netif_ptr->hwaddr[2] = xemacif_ptr->ethaddr.addr[2];
   netif_ptr->hwaddr[3] = xemacif_ptr->ethaddr.addr[3];
   netif_ptr->hwaddr[4] = xemacif_ptr->ethaddr.addr[4];
   netif_ptr->hwaddr[5] = xemacif_ptr->ethaddr.addr[5];
   netif_ptr->name[0] = IFNAME0;
   netif_ptr->name[1] = IFNAME1;
   netif_ptr->output = etharp_output;
   netif_ptr->linkoutput = low_level_output;

   /* removed this statement because the ethaddr in the XEmacIf_Config
    * structure is now a struct not a pointer to a struct
    */
   //xemacif_ptr->ethaddr = (struct eth_addr *)&(netif_ptr->hwaddr[0]);

   low_level_init(netif_ptr);
   etharp_init();

   return ERR_OK;
}

⌨️ 快捷键说明

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