netconf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*!
  2. \file netconf.c
  3. \brief network connection configuration
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "lwip/mem.h"
  33. #include "lwip/memp.h"
  34. #include "lwip/tcp.h"
  35. #include "lwip/udp.h"
  36. #include "netif/etharp.h"
  37. #include "lwip/dhcp.h"
  38. #include "ethernetif.h"
  39. #include "stdint.h"
  40. #include "main.h"
  41. #include "netconf.h"
  42. #include <stdio.h>
  43. #include "lwip/priv/tcp_priv.h"
  44. #include "lwip/timeouts.h"
  45. #define DHCP_TRIES_MAX_TIMES 3
  46. typedef enum {
  47. DHCP_ADDR_NONE = 0,
  48. DHCP_ADDR_BEGIN,
  49. DHCP_ADDR_GOT,
  50. DHCP_ADDR_FAIL
  51. } dhcp_addr_status_enum;
  52. #ifdef USE_DHCP
  53. uint32_t finecurtime = 0;
  54. uint32_t coarsecurtime = 0;
  55. dhcp_addr_status_enum dhcp_addr_status = DHCP_ADDR_NONE;
  56. #endif /* USE_DHCP */
  57. struct netif g_mynetif;
  58. uint32_t tcpcurtime = 0;
  59. uint32_t arpcurtime = 0;
  60. ip_addr_t ip_address = {0};
  61. void lwip_dhcp_address_get(void);
  62. /*!
  63. \brief initializes the LwIP stack
  64. \param[in] none
  65. \param[out] none
  66. \retval none
  67. */
  68. void lwip_stack_init(void)
  69. {
  70. ip_addr_t gd_ipaddr;
  71. ip_addr_t gd_netmask;
  72. ip_addr_t gd_gw;
  73. /* initialize the lwIP dynamic memory heap and memory pools */
  74. mem_init();
  75. memp_init();
  76. #ifdef TIMEOUT_CHECK_USE_LWIP
  77. sys_timeouts_init();
  78. #endif /* TIMEOUT_CHECK_USE_LWIP */
  79. #ifdef USE_DHCP
  80. gd_ipaddr.addr = 0;
  81. gd_netmask.addr = 0;
  82. gd_gw.addr = 0;
  83. #else
  84. IP4_ADDR(&gd_ipaddr, BORAD_IP_ADDR0, BORAD_IP_ADDR1, BORAD_IP_ADDR2, BORAD_IP_ADDR3);
  85. IP4_ADDR(&gd_netmask, BORAD_NETMASK_ADDR0, BORAD_NETMASK_ADDR1, BORAD_NETMASK_ADDR2, BORAD_NETMASK_ADDR3);
  86. IP4_ADDR(&gd_gw, BORAD_GW_ADDR0, BORAD_GW_ADDR1, BORAD_GW_ADDR2, BORAD_GW_ADDR3);
  87. #endif /* USE_DHCP */
  88. /* add a new network interface */
  89. netif_add(&g_mynetif, &gd_ipaddr, &gd_netmask, &gd_gw, NULL, &ethernetif_init, &ethernet_input);
  90. /* set a default network interface */
  91. netif_set_default(&g_mynetif);
  92. /* set a callback when interface is up/down */
  93. netif_set_status_callback(&g_mynetif, lwip_netif_status_callback);
  94. /* set the flag of netif as NETIF_FLAG_LINK_UP */
  95. netif_set_link_up(&g_mynetif);
  96. /* bring an interface up and set the flag of netif as NETIF_FLAG_UP */
  97. netif_set_up(&g_mynetif);
  98. }
  99. /*!
  100. \brief called when a farme is received from the interface
  101. \param[in] none
  102. \param[out] none
  103. \retval none
  104. */
  105. void lwip_frame_recv(void)
  106. {
  107. /* get frame from the interface and pass it to the LwIP stack */
  108. ethernetif_input(&g_mynetif);
  109. }
  110. /*!
  111. \brief call the time-related function periodicallytasks
  112. \param[in] curtime: the value of current time
  113. \param[out] none
  114. \retval none
  115. */
  116. void lwip_timeouts_check(__IO uint32_t curtime)
  117. {
  118. #if LWIP_TCP
  119. /* called periodically to dispatch TCP timers every 250 ms */
  120. if(curtime - tcpcurtime >= TCP_TMR_INTERVAL) {
  121. tcpcurtime = curtime;
  122. tcp_tmr();
  123. }
  124. #endif /* LWIP_TCP */
  125. /* called periodically to dispatch ARP timers every 1s */
  126. if((curtime - arpcurtime) >= ARP_TMR_INTERVAL) {
  127. arpcurtime = curtime;
  128. etharp_tmr();
  129. }
  130. #ifdef USE_DHCP
  131. /* called periodically to check whether an outstanding DHCP request is timed out every 500 ms */
  132. if(curtime - finecurtime >= DHCP_FINE_TIMER_MSECS) {
  133. finecurtime = curtime;
  134. dhcp_fine_tmr();
  135. if((DHCP_ADDR_GOT != dhcp_addr_status) && (DHCP_ADDR_FAIL != dhcp_addr_status)) {
  136. /* process DHCP state machine */
  137. lwip_dhcp_address_get();
  138. }
  139. }
  140. /* called periodically to check for lease renewal/rebind timeouts every 60s */
  141. if(curtime - coarsecurtime >= DHCP_COARSE_TIMER_MSECS) {
  142. coarsecurtime = curtime;
  143. dhcp_coarse_tmr();
  144. }
  145. #endif /* USE_DHCP */
  146. }
  147. #ifdef USE_DHCP
  148. /*!
  149. \brief get IP address through DHCP function
  150. \param[in] none
  151. \param[out] none
  152. \retval none
  153. */
  154. void lwip_dhcp_address_get(void)
  155. {
  156. ip_addr_t gd_ipaddr;
  157. ip_addr_t gd_netmask;
  158. ip_addr_t gd_gw;
  159. struct dhcp *dhcp_client;
  160. switch(dhcp_addr_status) {
  161. case DHCP_ADDR_NONE:
  162. dhcp_start(&g_mynetif);
  163. dhcp_addr_status = DHCP_ADDR_BEGIN;
  164. break;
  165. case DHCP_ADDR_BEGIN:
  166. /* got the IP address */
  167. ip_address.addr = g_mynetif.ip_addr.addr;
  168. if(0 != ip_address.addr) {
  169. dhcp_addr_status = DHCP_ADDR_GOT;
  170. printf("\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address), \
  171. ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address));
  172. } else {
  173. /* DHCP timeout */
  174. dhcp_client = netif_dhcp_data(&g_mynetif);
  175. if(dhcp_client->tries > DHCP_TRIES_MAX_TIMES) {
  176. dhcp_addr_status = DHCP_ADDR_FAIL;
  177. /* stop DHCP */
  178. dhcp_stop(&g_mynetif);
  179. /* use static address as IP address */
  180. IP4_ADDR(&gd_ipaddr, BORAD_IP_ADDR0, BORAD_IP_ADDR1, BORAD_IP_ADDR2, BORAD_IP_ADDR3);
  181. IP4_ADDR(&gd_netmask, BORAD_NETMASK_ADDR0, BORAD_NETMASK_ADDR1, BORAD_NETMASK_ADDR2, BORAD_NETMASK_ADDR3);
  182. IP4_ADDR(&gd_gw, BORAD_GW_ADDR0, BORAD_GW_ADDR1, BORAD_GW_ADDR2, BORAD_GW_ADDR3);
  183. netif_set_addr(&g_mynetif, &gd_ipaddr, &gd_netmask, &gd_gw);
  184. }
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. #endif /* USE_DHCP */
  192. unsigned long sys_now(void)
  193. {
  194. extern volatile unsigned int g_localtime;
  195. return g_localtime;
  196. }