NO_SYS_SampleCode.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. void eth_mac_irq()
  2. {
  3. /* Service MAC IRQ here */
  4. /* Allocate pbuf */
  5. struct pbuf* p = pbuf_alloc(PBUF_RAW, eth_data_count, PBUF_RAM);
  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->tryPut(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. LockInterrupts();
  28. pbuf_copy_partial(p, mac_send_buffer, p->tot_len, 0);
  29. /* Start MAC transmit here */
  30. UnlockInterrupts();
  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->name[0] = 'e';
  42. netif->name[1] = '0';
  43. netif->mtu = ETHERNET_MTU;
  44. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP;
  45. MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);
  46. memcpy(netif->hwaddr, your_mac_address_goes_here, sizeof(netif->hwaddr));
  47. netif->hwaddr_len = sizeof(netif->hwaddr);
  48. return ERR_OK;
  49. }
  50. void main(void)
  51. {
  52. struct netif netif;
  53. lwip_init();
  54. netif_add(&netif, IPADDR_ANY, IPADDR_ANY, IPADDR_ANY, NULL, netif_init, netif_input);
  55. netif_set_status_callback(&netif, netif_status_callback);
  56. netif_set_default(&netif);
  57. netif_set_up(&netif);
  58. /* Start DHCP */
  59. dhcp_init();
  60. while(1) {
  61. /* Check link state, e.g. via MDIO communication with PHY */
  62. if(linkStateChanged()) {
  63. if(linkIsUp()) {
  64. netif_set_link_up(&netif);
  65. } else {
  66. netif_set_link_down(&netif);
  67. }
  68. }
  69. /* Check for received frames, feed them to lwIP */
  70. LockInterrupts();
  71. struct pbuf* p = queue->tryGet();
  72. UnlockInterrupts();
  73. if(p != NULL) {
  74. LINK_STATS_INC(link.recv);
  75. /* Update SNMP stats (only if you use SNMP) */
  76. MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
  77. int unicast = ((p->payload[0] & 0x01) == 0);
  78. if (unicast) {
  79. MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
  80. } else {
  81. MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
  82. }
  83. if(netif.input(p, &netif) != ERR_OK) {
  84. pbuf_free(p);
  85. }
  86. }
  87. /* Cyclic lwIP timers check */
  88. sys_check_timeouts();
  89. /* your application goes here */
  90. }
  91. }