lwip_eth.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "lwip_init.h"
  9. #include "stm32f4x7_phy.h"
  10. #include <stdio.h>
  11. #define MAX_DHCP_TRIES 4
  12. #ifdef USE_DHCP
  13. dhcp_state_enum dhcp_state = DHCP_START;
  14. ip4_addr_t ip_address = {0};
  15. #endif /* USE_DHCP */
  16. struct netif gnetif;
  17. // lwip 默认IP设置
  18. void lwip_eth_default_ip_set(__lwip_dev *lwipx)
  19. {
  20. //默认远端IP为:192.168.1.100
  21. lwipx->remoteip[0] = 192;
  22. lwipx->remoteip[1] = 168;
  23. lwipx->remoteip[2] = 1;
  24. lwipx->remoteip[3] = 10;
  25. // MAC地址设置(高三字节固定为:2.0.0,低三字节用STM32唯一ID)
  26. lwipx->mac[0] = 2; //高三字节(IEEE称之为组织唯一ID,OUI)地址固定为:2.0.0
  27. lwipx->mac[1] = 0;
  28. lwipx->mac[2] = 0;
  29. lwipx->mac[3] = 0; //低三字节用STM32的唯一ID
  30. lwipx->mac[4] = 0;
  31. lwipx->mac[5] = 0;
  32. //默认本地IP为:192.168.1.30
  33. lwipx->ip[0] = 192;
  34. lwipx->ip[1] = 168;
  35. lwipx->ip[2] = 1;
  36. lwipx->ip[3] = 40;
  37. //默认子网掩码:255.255.255.0
  38. lwipx->netmask[0] = 255;
  39. lwipx->netmask[1] = 255;
  40. lwipx->netmask[2] = 255;
  41. lwipx->netmask[3] = 0;
  42. //默认网关:192.168.1.1
  43. lwipx->gateway[0] = 192;
  44. lwipx->gateway[1] = 168;
  45. lwipx->gateway[2] = 1;
  46. lwipx->gateway[3] = 1;
  47. lwipx->dhcpstatus = 0; //没有DHCP
  48. }
  49. /*!
  50. \brief initializes the LwIP stack
  51. \param[in] none
  52. \param[out] none
  53. \retval none
  54. */
  55. void lwip_eth_setup(void)
  56. {
  57. ip4_addr_t ipaddr;
  58. ip4_addr_t netmask;
  59. ip4_addr_t gw;
  60. lwip_eth_default_ip_set(&ethdev); //设置默认IP等信息
  61. eth_init();
  62. /* IP address setting */
  63. #ifdef USE_DHCP
  64. ipaddr.addr = 0;
  65. netmask.addr = 0;
  66. gw.addr = 0;
  67. #else
  68. IP4_ADDR(&ipaddr, ethdev.ip[0], ethdev.ip[1], ethdev.ip[2], ethdev.ip[3]);
  69. IP4_ADDR(&netmask, ethdev.netmask[0], ethdev.netmask[1], ethdev.netmask[2], ethdev.netmask[3]);
  70. IP4_ADDR(&gw, ethdev.gateway[0], ethdev.gateway[1], ethdev.gateway[2], ethdev.gateway[3]);
  71. #endif /* USE_DHCP */
  72. /* - netif_add(struct netif *netif,
  73. struct ip_addr *ipaddr,
  74. struct ip_addr *netmask,
  75. struct ip_addr *gw,
  76. void *state,
  77. err_t (* init)(struct netif *netif),
  78. err_t (* input)(struct pbuf *p,
  79. struct netif *netif))
  80. 将您的网络接口添加到 netif_list。
  81. 分配结构netif,并将指向该结构的指针作为第一个参数传递。
  82. 使用DHCP时,请提供指向已清除的ip_addr结构的指针,否则,请使用健全的数字填充它们。
  83. 状态指针可以为NULL。
  84. 初始化函数指针必须指向您的以太网netif接口的初始化函数。 以下代码说明了它的用法。*/
  85. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_eth_init, &tcpip_input);
  86. printf("LwIP Registers the default network interface....\r\n");
  87. /* 注册默认网络接口.*/
  88. netif_set_default(&gnetif);
  89. if (EthStatus == (ETH_INIT_FLAG))
  90. {
  91. printf("LwIP Finish interface....\r\n");
  92. gnetif.flags |= NETIF_FLAG_LINK_UP;
  93. /* 配置完成网卡后启动网卡*/
  94. netif_set_up(&gnetif);
  95. }
  96. else
  97. {
  98. netif_set_down(&gnetif);
  99. }
  100. }
  101. #ifdef USE_DHCP
  102. /*!
  103. \brief lwip_dhcp_process_handle
  104. \param[in] none
  105. \param[out] none
  106. \retval none
  107. */
  108. void dhcp_task(void *pvParameters)
  109. {
  110. ip4_addr_t ipaddr;
  111. ip4_addr_t netmask;
  112. ip4_addr_t gw;
  113. struct dhcp *dhcp_client;
  114. dhcp_client = netif_dhcp_data(&gnetif);
  115. for (;;)
  116. {
  117. switch (dhcp_state)
  118. {
  119. case DHCP_START:
  120. dhcp_start(&gnetif);
  121. ip_address.addr = 0;
  122. dhcp_state = DHCP_WAIT_ADDRESS;
  123. break;
  124. case DHCP_WAIT_ADDRESS:
  125. /* read the new IP address */
  126. ip_address.addr = gnetif.ip_addr.u_addr.ip4.addr;
  127. if (ip_address.addr != 0)
  128. {
  129. dhcp_state = DHCP_ADDRESS_ASSIGNED;
  130. /* stop DHCP */
  131. dhcp_stop(&gnetif);
  132. printf("\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address),
  133. ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address));
  134. OSTaskSuspend(OS_PRIO_SELF);
  135. }
  136. else
  137. {
  138. /* DHCP timeout */
  139. if (dhcp_client->tries > MAX_DHCP_TRIES)
  140. {
  141. dhcp_state = DHCP_TIMEOUT;
  142. /* stop DHCP */
  143. dhcp_stop(&gnetif);
  144. /* static address used */
  145. IP4_ADDR(&ipaddr, ethdev.ip[0], ethdev.ip[1], ethdev.ip[2], ethdev.ip[3]);
  146. IP4_ADDR(&netmask, ethdev.netmask[0], ethdev.netmask[1], ethdev.netmask[2], ethdev.netmask[3]);
  147. IP4_ADDR(&gw, ethdev.gateway[0], ethdev.gateway[1], ethdev.gateway[2], ethdev.gateway[3]);
  148. netif_set_addr(&gnetif, &ipaddr, &netmask, &gw);
  149. OSTaskSuspend(OS_PRIO_SELF);
  150. }
  151. }
  152. break;
  153. default:
  154. break;
  155. }
  156. /* wait 500 ms */
  157. OSTimeDlyHMSM(0, 0, 0, 500);
  158. }
  159. }
  160. #endif /* USE_DHCP */