lwip_eth.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "lwip_eth.h"
  2. #include "ethernetif_eth.h"
  3. #include "lwip/dhcp.h"
  4. #include "lwip/mem.h"
  5. #include "lwip/memp.h"
  6. #include "lwip/netif.h"
  7. #include "lwip/tcpip.h"
  8. #include "main.h"
  9. #include <stdio.h>
  10. #define MAX_DHCP_TRIES 4
  11. typedef enum
  12. {
  13. DHCP_START = 0,
  14. DHCP_WAIT_ADDRESS,
  15. DHCP_ADDRESS_ASSIGNED,
  16. DHCP_TIMEOUT
  17. } dhcp_state_enum;
  18. #ifdef USE_DHCP
  19. dhcp_state_enum dhcp_state = DHCP_START;
  20. ip4_addr_t ip_address = {0};
  21. #endif /* USE_DHCP */
  22. struct netif gnetif;
  23. /*!
  24. \brief initializes the LwIP stack
  25. \param[in] none
  26. \param[out] none
  27. \retval none
  28. */
  29. void lwip_eth_setup(void)
  30. {
  31. ip4_addr_t ipaddr;
  32. ip4_addr_t netmask;
  33. ip4_addr_t gw;
  34. /* IP address setting */
  35. #ifdef USE_DHCP
  36. ipaddr.addr = 0;
  37. netmask.addr = 0;
  38. gw.addr = 0;
  39. #else
  40. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  41. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  42. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  43. #endif /* USE_DHCP */
  44. /* - netif_add(struct netif *netif,
  45. struct ip_addr *ipaddr,
  46. struct ip_addr *netmask,
  47. struct ip_addr *gw,
  48. void *state,
  49. err_t (* init)(struct netif *netif),
  50. err_t (* input)(struct pbuf *p,
  51. struct netif *netif))
  52. 将您的网络接口添加到 netif_list。
  53. 分配结构netif,并将指向该结构的指针作为第一个参数传递。
  54. 使用DHCP时,请提供指向已清除的ip_addr结构的指针,否则,请使用健全的数字填充它们。
  55. 状态指针可以为NULL。
  56. 初始化函数指针必须指向您的以太网netif接口的初始化函数。 以下代码说明了它的用法。*/
  57. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_eth_init, &tcpip_input);
  58. printf("LwIP Registers the default network interface....\r\n");
  59. /* 注册默认网络接口.*/
  60. netif_set_default(&gnetif);
  61. if (EthStatus == (ETH_INIT_FLAG))
  62. {
  63. printf("LwIP Finish interface....\r\n");
  64. gnetif.flags |= NETIF_FLAG_LINK_UP;
  65. /* 配置完成网卡后启动网卡*/
  66. netif_set_up(&gnetif);
  67. }
  68. else
  69. {
  70. netif_set_down(&gnetif);
  71. }
  72. // netif_set_link_callback(&gnetif, ETH_link_callback);
  73. }
  74. #ifdef USE_DHCP
  75. /*!
  76. \brief lwip_dhcp_process_handle
  77. \param[in] none
  78. \param[out] none
  79. \retval none
  80. */
  81. void dhcp_task(void *pvParameters)
  82. {
  83. ip4_addr_t ipaddr;
  84. ip4_addr_t netmask;
  85. ip4_addr_t gw;
  86. struct dhcp *dhcp_client;
  87. dhcp_client = netif_dhcp_data(&xnetif);
  88. for (;;)
  89. {
  90. switch (dhcp_state)
  91. {
  92. case DHCP_START:
  93. dhcp_start(&xnetif);
  94. ip_address.addr = 0;
  95. dhcp_state = DHCP_WAIT_ADDRESS;
  96. break;
  97. case DHCP_WAIT_ADDRESS:
  98. /* read the new IP address */
  99. ip_address.addr = xnetif.ip_addr.u_addr.ip4.addr;
  100. if (ip_address.addr != 0)
  101. {
  102. dhcp_state = DHCP_ADDRESS_ASSIGNED;
  103. /* stop DHCP */
  104. dhcp_stop(&xnetif);
  105. printf("\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address),
  106. ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address));
  107. OSTaskSuspend(OS_PRIO_SELF);
  108. }
  109. else
  110. {
  111. /* DHCP timeout */
  112. if (dhcp_client->tries > MAX_DHCP_TRIES)
  113. {
  114. dhcp_state = DHCP_TIMEOUT;
  115. /* stop DHCP */
  116. dhcp_stop(&xnetif);
  117. /* static address used */
  118. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  119. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  120. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  121. netif_set_addr(&xnetif, &ipaddr, &netmask, &gw);
  122. OSTaskSuspend(OS_PRIO_SELF);
  123. }
  124. }
  125. break;
  126. default:
  127. break;
  128. }
  129. /* wait 500 ms */
  130. OSTimeDlyHMSM(0, 0, 0, 500);
  131. }
  132. }
  133. #endif /* USE_DHCP */