Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NO_SYS_SampleCode.c Source File

NO_SYS_SampleCode.c

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