netconf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "lwip/mem.h"
  10. #include "lwip/memp.h"
  11. #include "lwip/dhcp.h"
  12. #include "ethernetif.h"
  13. #include "main.h"
  14. #include "netconf.h"
  15. #include "lwip/tcpip.h"
  16. #include <stdio.h>
  17. #define MAX_DHCP_TRIES 4
  18. typedef enum
  19. {
  20. DHCP_START = 0,
  21. DHCP_WAIT_ADDRESS,
  22. DHCP_ADDRESS_ASSIGNED,
  23. DHCP_TIMEOUT
  24. } dhcp_state_enum;
  25. #ifdef USE_DHCP
  26. dhcp_state_enum dhcp_state = DHCP_START;
  27. ip4_addr_t ip_address = {0};
  28. #endif /* USE_DHCP */
  29. struct netif xnetif;
  30. /*!
  31. \brief initializes the LwIP stack
  32. \param[in] none
  33. \param[out] none
  34. \retval none
  35. */
  36. void lwip_stack_init(void)
  37. {
  38. ip4_addr_t ipaddr;
  39. ip4_addr_t netmask;
  40. ip4_addr_t gw;
  41. /* create tcp_ip stack thread */
  42. tcpip_init(NULL, NULL);
  43. /* IP address setting */
  44. #ifdef USE_DHCP
  45. ipaddr.addr = 0;
  46. netmask.addr = 0;
  47. gw.addr = 0;
  48. #else
  49. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  50. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  51. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  52. #endif /* USE_DHCP */
  53. /* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
  54. struct ip_addr *netmask, struct ip_addr *gw,
  55. void *state, err_t (* init)(struct netif *netif),
  56. err_t (* input)(struct pbuf *p, struct netif *netif))
  57. Adds your network interface to the netif_list. Allocate a struct
  58. netif and pass a pointer to this structure as the first argument.
  59. Give pointers to cleared ip_addr structures when using DHCP,
  60. or fill them with sane numbers otherwise. The state pointer may be NULL.
  61. The init function pointer must point to a initialization function for
  62. your ethernet netif interface. The following code illustrates it's use. */
  63. netif_add(&xnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  64. /* registers the default network interface */
  65. netif_set_default(&xnetif);
  66. /* when the netif is fully configured this function must be called */
  67. netif_set_up(&xnetif);
  68. }
  69. #ifdef USE_DHCP
  70. /*!
  71. \brief lwip_dhcp_process_handle
  72. \param[in] none
  73. \param[out] none
  74. \retval none
  75. */
  76. void dhcp_task(void *pvParameters)
  77. {
  78. ip4_addr_t ipaddr;
  79. ip4_addr_t netmask;
  80. ip4_addr_t gw;
  81. struct dhcp *dhcp_client;
  82. dhcp_client = netif_dhcp_data(&xnetif);
  83. for (;;)
  84. {
  85. switch (dhcp_state)
  86. {
  87. case DHCP_START:
  88. dhcp_start(&xnetif);
  89. ip_address.addr = 0;
  90. dhcp_state = DHCP_WAIT_ADDRESS;
  91. break;
  92. case DHCP_WAIT_ADDRESS:
  93. /* read the new IP address */
  94. ip_address.addr = xnetif.ip_addr.u_addr.ip4.addr;
  95. if (ip_address.addr != 0)
  96. {
  97. dhcp_state = DHCP_ADDRESS_ASSIGNED;
  98. /* stop DHCP */
  99. dhcp_stop(&xnetif);
  100. printf("\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address),
  101. ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address));
  102. OSTaskSuspend(OS_PRIO_SELF);
  103. }
  104. else
  105. {
  106. /* DHCP timeout */
  107. if (dhcp_client->tries > MAX_DHCP_TRIES)
  108. {
  109. dhcp_state = DHCP_TIMEOUT;
  110. /* stop DHCP */
  111. dhcp_stop(&xnetif);
  112. /* static address used */
  113. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  114. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  115. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  116. netif_set_addr(&xnetif, &ipaddr, &netmask, &gw);
  117. OSTaskSuspend(OS_PRIO_SELF);
  118. }
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. /* wait 500 ms */
  125. OSTimeDlyHMSM(0, 0, 0, 500);
  126. }
  127. }
  128. #endif /* USE_DHCP */