NO_SYS_SampleCode.c 3.0 KB

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