netconf.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Link callback function, this function is called on change of link status.
  33. * @param The network interface
  34. * @retval None
  35. */
  36. void ETH_link_callback(struct netif *netif)
  37. {
  38. if (netif_is_link_up(netif))
  39. {
  40. /* When the netif is fully configured this function must be called.*/
  41. netif_set_up(&gnetif);
  42. }
  43. else
  44. {
  45. ETH_Stop();
  46. #ifdef USE_DHCP
  47. DHCP_state = DHCP_LINK_DOWN;
  48. dhcp_stop(netif);
  49. #endif /* USE_DHCP */
  50. /* When the netif link is down this function must be called.*/
  51. netif_set_down(&gnetif);
  52. }
  53. }
  54. /*!
  55. \brief initializes the LwIP stack
  56. \param[in] none
  57. \param[out] none
  58. \retval none
  59. */
  60. void lwip_stack_init(void)
  61. {
  62. ip4_addr_t ipaddr;
  63. ip4_addr_t netmask;
  64. ip4_addr_t gw;
  65. /* create tcp_ip stack thread */
  66. tcpip_init(NULL, NULL);
  67. /* IP address setting */
  68. #ifdef USE_DHCP
  69. ipaddr.addr = 0;
  70. netmask.addr = 0;
  71. gw.addr = 0;
  72. #else
  73. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  74. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  75. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  76. #endif /* USE_DHCP */
  77. /* - netif_add(struct netif *netif,
  78. struct ip_addr *ipaddr,
  79. struct ip_addr *netmask,
  80. struct ip_addr *gw,
  81. void *state,
  82. err_t (* init)(struct netif *netif),
  83. err_t (* input)(struct pbuf *p,
  84. struct netif *netif))
  85. 将您的网络接口添加到 netif_list。
  86. 分配结构netif,并将指向该结构的指针作为第一个参数传递。
  87. 使用DHCP时,请提供指向已清除的ip_addr结构的指针,否则,请使用健全的数字填充它们。
  88. 状态指针可以为NULL。
  89. 初始化函数指针必须指向您的以太网netif接口的初始化函数。 以下代码说明了它的用法。*/
  90. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  91. /* 注册默认网络接口.*/
  92. netif_set_default(&gnetif);
  93. if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
  94. {
  95. gnetif.flags |= NETIF_FLAG_LINK_UP;
  96. /* 配置完成网卡后启动网卡*/
  97. netif_set_up(&gnetif);
  98. }
  99. else
  100. {
  101. netif_set_down(&gnetif);
  102. }
  103. netif_set_link_callback(&gnetif, ETH_link_callback);
  104. }
  105. #ifdef USE_DHCP
  106. /*!
  107. \brief lwip_dhcp_process_handle
  108. \param[in] none
  109. \param[out] none
  110. \retval none
  111. */
  112. void dhcp_task(void *pvParameters)
  113. {
  114. ip4_addr_t ipaddr;
  115. ip4_addr_t netmask;
  116. ip4_addr_t gw;
  117. struct dhcp *dhcp_client;
  118. dhcp_client = netif_dhcp_data(&xnetif);
  119. for (;;)
  120. {
  121. switch (dhcp_state)
  122. {
  123. case DHCP_START:
  124. dhcp_start(&xnetif);
  125. ip_address.addr = 0;
  126. dhcp_state = DHCP_WAIT_ADDRESS;
  127. break;
  128. case DHCP_WAIT_ADDRESS:
  129. /* read the new IP address */
  130. ip_address.addr = xnetif.ip_addr.u_addr.ip4.addr;
  131. if (ip_address.addr != 0)
  132. {
  133. dhcp_state = DHCP_ADDRESS_ASSIGNED;
  134. /* stop DHCP */
  135. dhcp_stop(&xnetif);
  136. printf("\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address),
  137. ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address));
  138. OSTaskSuspend(OS_PRIO_SELF);
  139. }
  140. else
  141. {
  142. /* DHCP timeout */
  143. if (dhcp_client->tries > MAX_DHCP_TRIES)
  144. {
  145. dhcp_state = DHCP_TIMEOUT;
  146. /* stop DHCP */
  147. dhcp_stop(&xnetif);
  148. /* static address used */
  149. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  150. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  151. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  152. netif_set_addr(&xnetif, &ipaddr, &netmask, &gw);
  153. OSTaskSuspend(OS_PRIO_SELF);
  154. }
  155. }
  156. break;
  157. default:
  158. break;
  159. }
  160. /* wait 500 ms */
  161. OSTimeDlyHMSM(0, 0, 0, 500);
  162. }
  163. }
  164. #endif /* USE_DHCP */