1 void
2 eth_mac_irq()
3 {
4   /* Service MAC IRQ here */
5 
6   /* Allocate pbuf from pool (avoid using heap in interrupts) */
7   struct pbuf* p = pbuf_alloc(PBUF_RAW, eth_data_count, PBUF_POOL);
8 
9   if(p != NULL) {
10     /* Copy ethernet frame into pbuf */
11     pbuf_take(p, eth_data, eth_data_count);
12 
13     /* Put in a queue which is processed in main loop */
14     if(!queue_try_put(&queue, p)) {
15       /* queue is full -> packet loss */
16       pbuf_free(p);
17     }
18   }
19 }
20 
21 static err_t
22 netif_output(struct netif *netif, struct pbuf *p)
23 {
24   LINK_STATS_INC(link.xmit);
25 
26   /* Update SNMP stats (only if you use SNMP) */
27   MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
28   int unicast = ((p->payload[0] & 0x01) == 0);
29   if (unicast) {
30     MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
31   } else {
32     MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
33   }
34 
35   lock_interrupts();
36   pbuf_copy_partial(p, mac_send_buffer, p->tot_len, 0);
37   /* Start MAC transmit here */
38   unlock_interrupts();
39 
40   return ERR_OK;
41 }
42 
43 static void
44 netif_status_callback(struct netif *netif)
45 {
46   printf("netif status changed %s\n", ip4addr_ntoa(netif_ip4_addr(netif)));
47 }
48 
49 static err_t
50 netif_init(struct netif *netif)
51 {
52   netif->linkoutput = netif_output;
53   netif->output     = etharp_output;
54   netif->output_ip6 = ethip6_output;
55   netif->mtu        = ETHERNET_MTU;
56   netif->flags      = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
57   MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);
58 
59   SMEMCPY(netif->hwaddr, your_mac_address_goes_here, ETH_HWADDR_LEN);
60   netif->hwaddr_len = ETH_HWADDR_LEN;
61 
62   return ERR_OK;
63 }
64 
65 void
66 main(void)
67 {
68   struct netif netif;
69 
70   lwip_init();
71 
72   netif_add(&netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY, NULL, netif_init, netif_input);
73   netif.name[0] = 'e';
74   netif.name[1] = '0';
75   netif_create_ip6_linklocal_address(&netif, 1);
76   netif_set_status_callback(&netif, netif_status_callback);
77   netif_set_default(&netif);
78   netif_set_up(&netif);
79 
80   /* Start DHCP and HTTPD */
81   dhcp_start(&netif );
82   httpd_init();
83 
84   while(1) {
85     /* Check link state, e.g. via MDIO communication with PHY */
86     if(link_state_changed()) {
87       if(link_is_up()) {
88         netif_set_link_up(&netif);
89       } else {
90         netif_set_link_down(&netif);
91       }
92     }
93 
94     /* Check for received frames, feed them to lwIP */
95     lock_interrupts();
96     struct pbuf* p = queue_try_get(&queue);
97     unlock_interrupts();
98 
99     if(p != NULL) {
100       LINK_STATS_INC(link.recv);
101 
102       /* Update SNMP stats (only if you use SNMP) */
103       MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
104       int unicast = ((p->payload[0] & 0x01) == 0);
105       if (unicast) {
106         MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
107       } else {
108         MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
109       }
110 
111       if(netif.input(p, &netif) != ERR_OK) {
112         pbuf_free(p);
113       }
114     }
115 
116     /* Cyclic lwIP timers check */
117     sys_check_timeouts();
118 
119     /* your application goes here */
120   }
121 }
122