netconf.c 4.6 KB

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