2017-03-15 19:16:52 +00:00
|
|
|
void
|
|
|
|
eth_mac_irq()
|
2016-08-05 07:53:50 +00:00
|
|
|
{
|
|
|
|
/* Service MAC IRQ here */
|
|
|
|
|
2016-08-06 18:28:01 +00:00
|
|
|
/* Allocate pbuf from pool (avoid using heap in interrupts) */
|
|
|
|
struct pbuf* p = pbuf_alloc(PBUF_RAW, eth_data_count, PBUF_POOL);
|
2016-08-05 07:53:50 +00:00
|
|
|
|
|
|
|
if(p != NULL) {
|
|
|
|
/* Copy ethernet frame into pbuf */
|
|
|
|
pbuf_take(p, eth_data, eth_data_count);
|
|
|
|
|
|
|
|
/* Put in a queue which is processed in main loop */
|
2016-08-25 20:23:11 +00:00
|
|
|
if(!queue_try_put(&queue, p)) {
|
2016-08-05 07:53:50 +00:00
|
|
|
/* queue is full -> packet loss */
|
|
|
|
pbuf_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:16:52 +00:00
|
|
|
static err_t
|
|
|
|
netif_output(struct netif *netif, struct pbuf *p)
|
2016-08-05 07:53:50 +00:00
|
|
|
{
|
|
|
|
LINK_STATS_INC(link.xmit);
|
|
|
|
|
|
|
|
/* Update SNMP stats (only if you use SNMP) */
|
2016-08-05 07:55:48 +00:00
|
|
|
MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
|
2016-08-05 07:53:50 +00:00
|
|
|
int unicast = ((p->payload[0] & 0x01) == 0);
|
|
|
|
if (unicast) {
|
|
|
|
MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
|
|
|
|
} else {
|
|
|
|
MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:23:11 +00:00
|
|
|
lock_interrupts();
|
2016-08-05 07:53:50 +00:00
|
|
|
pbuf_copy_partial(p, mac_send_buffer, p->tot_len, 0);
|
|
|
|
/* Start MAC transmit here */
|
2016-08-25 20:23:11 +00:00
|
|
|
unlock_interrupts();
|
2016-08-05 07:53:50 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:16:52 +00:00
|
|
|
static void
|
|
|
|
netif_status_callback(struct netif *netif)
|
2016-08-05 07:53:50 +00:00
|
|
|
{
|
|
|
|
printf("netif status changed %s\n", ip4addr_ntoa(netif_ip4_addr(netif)));
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:16:52 +00:00
|
|
|
static err_t
|
|
|
|
netif_init(struct netif *netif)
|
2016-08-05 07:53:50 +00:00
|
|
|
{
|
|
|
|
netif->linkoutput = netif_output;
|
2016-09-30 07:35:27 +00:00
|
|
|
netif->output = etharp_output;
|
|
|
|
netif->output_ip6 = ethip6_output;
|
|
|
|
netif->mtu = ETHERNET_MTU;
|
|
|
|
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
|
2016-08-05 07:53:50 +00:00
|
|
|
MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);
|
|
|
|
|
2018-06-07 18:48:18 +00:00
|
|
|
SMEMCPY(netif->hwaddr, your_mac_address_goes_here, ETH_HWADDR_LEN);
|
|
|
|
netif->hwaddr_len = ETH_HWADDR_LEN;
|
2016-08-05 07:53:50 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:16:52 +00:00
|
|
|
void
|
|
|
|
main(void)
|
2016-08-05 07:53:50 +00:00
|
|
|
{
|
|
|
|
struct netif netif;
|
|
|
|
|
|
|
|
lwip_init();
|
|
|
|
|
2016-09-30 07:35:27 +00:00
|
|
|
netif_add(&netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY, NULL, netif_init, netif_input);
|
|
|
|
netif.name[0] = 'e';
|
|
|
|
netif.name[1] = '0';
|
|
|
|
netif_create_ip6_linklocal_address(&netif, 1);
|
2016-08-05 07:53:50 +00:00
|
|
|
netif_set_status_callback(&netif, netif_status_callback);
|
|
|
|
netif_set_default(&netif);
|
|
|
|
netif_set_up(&netif);
|
|
|
|
|
2016-08-25 20:23:11 +00:00
|
|
|
/* Start DHCP and HTTPD */
|
2017-03-15 19:09:39 +00:00
|
|
|
dhcp_start(&netif );
|
2016-08-25 20:23:11 +00:00
|
|
|
httpd_init();
|
2016-08-05 07:53:50 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
/* Check link state, e.g. via MDIO communication with PHY */
|
2016-08-25 20:23:11 +00:00
|
|
|
if(link_state_changed()) {
|
|
|
|
if(link_is_up()) {
|
2016-08-05 07:53:50 +00:00
|
|
|
netif_set_link_up(&netif);
|
|
|
|
} else {
|
|
|
|
netif_set_link_down(&netif);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for received frames, feed them to lwIP */
|
2016-08-25 20:23:11 +00:00
|
|
|
lock_interrupts();
|
|
|
|
struct pbuf* p = queue_try_get(&queue);
|
|
|
|
unlock_interrupts();
|
2016-08-05 07:53:50 +00:00
|
|
|
|
|
|
|
if(p != NULL) {
|
|
|
|
LINK_STATS_INC(link.recv);
|
2016-08-05 07:55:48 +00:00
|
|
|
|
2016-08-05 07:53:50 +00:00
|
|
|
/* Update SNMP stats (only if you use SNMP) */
|
|
|
|
MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
|
|
|
|
int unicast = ((p->payload[0] & 0x01) == 0);
|
|
|
|
if (unicast) {
|
|
|
|
MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
|
|
|
|
} else {
|
|
|
|
MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(netif.input(p, &netif) != ERR_OK) {
|
|
|
|
pbuf_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cyclic lwIP timers check */
|
|
|
|
sys_check_timeouts();
|
|
|
|
|
|
|
|
/* your application goes here */
|
|
|
|
}
|
|
|
|
}
|