NO_SYS_SampleCode.c 2.9 KB

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