Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NO_SYS_SampleCode.c Source File

NO_SYS_SampleCode.c

00001 void eth_mac_irq()
00002 {
00003   /* Service MAC IRQ here */
00004 
00005   /* Allocate pbuf from pool (avoid using heap in interrupts) */
00006   struct pbuf* p = pbuf_alloc(PBUF_RAW, eth_data_count, PBUF_POOL);
00007 
00008   if(p != NULL) {
00009     /* Copy ethernet frame into pbuf */
00010     pbuf_take(p, eth_data, eth_data_count);
00011 
00012     /* Put in a queue which is processed in main loop */
00013     if(!queue_try_put(&queue, p)) {
00014       /* queue is full -> packet loss */
00015       pbuf_free(p);
00016     }
00017   }
00018 }
00019 
00020 static err_t netif_output(struct netif *netif, struct pbuf *p)
00021 {
00022   LINK_STATS_INC(link.xmit);
00023 
00024   /* Update SNMP stats (only if you use SNMP) */
00025   MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
00026   int unicast = ((p->payload[0] & 0x01) == 0);
00027   if (unicast) {
00028     MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
00029   } else {
00030     MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
00031   }
00032 
00033   lock_interrupts();
00034   pbuf_copy_partial(p, mac_send_buffer, p->tot_len, 0);
00035   /* Start MAC transmit here */
00036   unlock_interrupts();
00037 
00038   return ERR_OK;
00039 }
00040 
00041 static void netif_status_callback(struct netif *netif)
00042 {
00043   printf("netif status changed %s\n", ip4addr_ntoa(netif_ip4_addr(netif)));
00044 }
00045 
00046 static err_t netif_init(struct netif *netif)
00047 {
00048   netif->linkoutput = netif_output;
00049   netif->output     = etharp_output;
00050   netif->output_ip6 = ethip6_output;
00051   netif->mtu        = ETHERNET_MTU;
00052   netif->flags      = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
00053   MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);
00054 
00055   SMEMCPY(netif->hwaddr, your_mac_address_goes_here, sizeof(netif->hwaddr));
00056   netif->hwaddr_len = sizeof(netif->hwaddr);
00057 
00058   return ERR_OK;
00059 }
00060 
00061 void main(void)
00062 {
00063   struct netif netif;
00064 
00065   lwip_init();
00066 
00067   netif_add(&netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY, NULL, netif_init, netif_input);
00068   netif.name[0] = 'e';
00069   netif.name[1] = '0';
00070   netif_create_ip6_linklocal_address(&netif, 1);
00071   netif.ip6_autoconfig_enabled = 1;
00072   netif_set_status_callback(&netif, netif_status_callback);
00073   netif_set_default(&netif);
00074   netif_set_up(&netif);
00075   
00076   /* Start DHCP and HTTPD */
00077   dhcp_init();
00078   httpd_init();
00079 
00080   while(1) {
00081     /* Check link state, e.g. via MDIO communication with PHY */
00082     if(link_state_changed()) {
00083       if(link_is_up()) {
00084         netif_set_link_up(&netif);
00085       } else {
00086         netif_set_link_down(&netif);
00087       }
00088     }
00089 
00090     /* Check for received frames, feed them to lwIP */
00091     lock_interrupts();
00092     struct pbuf* p = queue_try_get(&queue);
00093     unlock_interrupts();
00094 
00095     if(p != NULL) {
00096       LINK_STATS_INC(link.recv);
00097  
00098       /* Update SNMP stats (only if you use SNMP) */
00099       MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
00100       int unicast = ((p->payload[0] & 0x01) == 0);
00101       if (unicast) {
00102         MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
00103       } else {
00104         MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
00105       }
00106 
00107       if(netif.input(p, &netif) != ERR_OK) {
00108         pbuf_free(p);
00109       }
00110     }
00111      
00112     /* Cyclic lwIP timers check */
00113     sys_check_timeouts();
00114      
00115     /* your application goes here */
00116   }
00117 }