1 void
eth_mac_irq()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
netif_output(struct netif * netif,struct pbuf * p)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
netif_status_callback(struct netif * netif)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
netif_init(struct netif * netif)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
main(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.ip6_autoconfig_enabled = 1;
77 netif_set_status_callback(&netif, netif_status_callback);
78 netif_set_default(&netif);
79 netif_set_up(&netif);
80
81 /* Start DHCP and HTTPD */
82 dhcp_start(&netif );
83 httpd_init();
84
85 while(1) {
86 /* Check link state, e.g. via MDIO communication with PHY */
87 if(link_state_changed()) {
88 if(link_is_up()) {
89 netif_set_link_up(&netif);
90 } else {
91 netif_set_link_down(&netif);
92 }
93 }
94
95 /* Check for received frames, feed them to lwIP */
96 lock_interrupts();
97 struct pbuf* p = queue_try_get(&queue);
98 unlock_interrupts();
99
100 if(p != NULL) {
101 LINK_STATS_INC(link.recv);
102
103 /* Update SNMP stats (only if you use SNMP) */
104 MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
105 int unicast = ((p->payload[0] & 0x01) == 0);
106 if (unicast) {
107 MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
108 } else {
109 MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
110 }
111
112 if(netif.input(p, &netif) != ERR_OK) {
113 pbuf_free(p);
114 }
115 }
116
117 /* Cyclic lwIP timers check */
118 sys_check_timeouts();
119
120 /* your application goes here */
121 }
122 }
123