netconf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  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/dhcp.h"
  35. #include "ethernetif.h"
  36. #include "main.h"
  37. #include "netconf.h"
  38. #include "tcpip.h"
  39. #include <stdio.h>
  40. #include "lwip/inet.h"
  41. #include "hd_eth.h"
  42. #include "gateway_message.h"
  43. #include "string.h"
  44. #include "led.h"
  45. #define MAX_DHCP_TRIES 4
  46. typedef enum
  47. {
  48. DHCP_START = 0,
  49. DHCP_WAIT_ADDRESS,
  50. DHCP_ADDRESS_ASSIGNED,
  51. DHCP_TIMEOUT
  52. }dhcp_state_enum;
  53. volatile uint8_t EthLinkStatus = 0;
  54. #ifdef USE_DHCP
  55. dhcp_state_enum dhcp_state = DHCP_START;
  56. #endif /* USE_DHCP */
  57. struct netif g_mynetif;
  58. void ETH_link_callback(struct netif *netif);
  59. void ETH_CheckLinkStatus(uint16_t PHYAddress);
  60. void ETH_CheckLinkStatus_timer(void *arg);
  61. /*!
  62. \brief initializes the LwIP stack
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void lwip_stack_init(void)
  68. {
  69. struct ip_addr ipaddr;
  70. struct ip_addr netmask;
  71. struct ip_addr gw;
  72. /* create tcp_ip stack thread */
  73. tcpip_init( NULL, NULL );
  74. /* IP address setting */
  75. #ifdef USE_DHCP
  76. ipaddr.addr = 0;
  77. netmask.addr = 0;
  78. gw.addr = 0;
  79. #else
  80. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  81. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
  82. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  83. #endif /* USE_DHCP */
  84. netif_add(&g_mynetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  85. /* registers the default network interface */
  86. netif_set_default(&g_mynetif);
  87. /* when the netif is fully configured this function must be called */
  88. netif_set_up(&g_mynetif);
  89. uint16_t data;
  90. enet_phy_write_read(ENET_PHY_READ, DP83848_PHY_ADDRESS,PHY_REG_BSR,&data);
  91. if(data & 0x4)
  92. {
  93. EthStatus |= ETH_LINK_FLAG;
  94. }
  95. if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
  96. {
  97. /* Set Ethernet link flag */
  98. g_mynetif.flags |= NETIF_FLAG_LINK_UP;
  99. /* When the netif is fully configured this function must be called.*/
  100. netif_set_up(&g_mynetif);
  101. #ifdef USE_DHCP
  102. dhcp_state = DHCP_START;
  103. #else
  104. #ifdef SERIAL_DEBUG
  105. printf("\n Static IP address \n");
  106. printf("IP: %d.%d.%d.%d\n",IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
  107. printf("NETMASK: %d.%d.%d.%d\n",NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
  108. printf("Gateway: %d.%d.%d.%d\n",GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
  109. #endif /* SERIAL_DEBUG */
  110. #endif /* USE_DHCP */
  111. }
  112. else
  113. {
  114. /* When the netif link is down this function must be called.*/
  115. netif_set_down(&g_mynetif);
  116. #ifdef USE_DHCP
  117. dhcp_state = DHCP_TIMEOUT;
  118. #endif /* USE_DHCP */
  119. #ifdef SERIAL_DEBUG
  120. printf("\n Network Cable is \n");
  121. printf(" not connected \n");
  122. #endif /* SERIAL_DEBUG */
  123. }
  124. /* Set the link callback function, this function is called on change of link status*/
  125. netif_set_link_callback(&g_mynetif, ETH_link_callback);
  126. sys_timeout(2000, ETH_CheckLinkStatus_timer, NULL);
  127. }
  128. /*!
  129. \brief dhcp_task
  130. \param[in] none
  131. \param[out] none
  132. \retval none
  133. */
  134. volatile int dhcp_done;
  135. #ifdef USE_DHCP
  136. void dhcp_task(void * pvParameters)
  137. {
  138. struct ip_addr ipaddr;
  139. struct ip_addr netmask;
  140. struct ip_addr gw;
  141. uint32_t ip_address = 0;
  142. for(;;){
  143. switch(dhcp_state){
  144. case DHCP_START:
  145. dhcp_start(&g_mynetif);
  146. /* IP address should be set to 0 every time we want to assign a new DHCP address*/
  147. ip_address = 0;
  148. dhcp_state = DHCP_WAIT_ADDRESS;
  149. break;
  150. case DHCP_WAIT_ADDRESS:
  151. /* read the new IP address */
  152. ip_address = g_mynetif.ip_addr.addr;
  153. if(0 != ip_address){
  154. dhcp_done=1;
  155. dhcp_state = DHCP_ADDRESS_ASSIGNED;
  156. /* stop DHCP */
  157. dhcp_stop(&g_mynetif);
  158. char p[100];
  159. sprintf(p,"\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address), \
  160. ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address));
  161. printf("%s",p);
  162. }else{
  163. /* DHCP timeout */
  164. if(g_mynetif.dhcp->tries > MAX_DHCP_TRIES){
  165. dhcp_state = DHCP_TIMEOUT;
  166. /* stop DHCP */
  167. dhcp_stop(&g_mynetif);
  168. /* static address used */
  169. IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
  170. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  171. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  172. netif_set_addr(&g_mynetif, &ipaddr , &netmask, &gw);
  173. }
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. /* wait 250 ms */
  180. vTaskDelay(250);
  181. //vTaskDelay(1000);
  182. }
  183. }
  184. #endif /* USE_DHCP */
  185. //监测以太网热拔插定时器
  186. void ETH_CheckLinkStatus_timer(void *arg)
  187. {
  188. LWIP_UNUSED_ARG(arg);
  189. ETH_CheckLinkStatus(DP83848_PHY_ADDRESS);
  190. sys_timeout(1000, ETH_CheckLinkStatus_timer, NULL);
  191. }
  192. /* This function is called periodically each second */
  193. /* It checks link status for ethernet controller */
  194. void ETH_CheckLinkStatus(uint16_t PHYAddress)
  195. {
  196. static uint8_t status = 0;
  197. uint16_t data;
  198. uint8_t linksta;
  199. //读取以太网链接状态
  200. if(enet_phy_write_read(ENET_PHY_READ, DP83848_PHY_ADDRESS,PHY_REG_BSR,&data)==1)
  201. {
  202. linksta=data & 0x4;
  203. }
  204. if(EthStatus & ETH_INIT_FLAG)
  205. {
  206. if(EthStatus & ETH_LINK_FLAG)
  207. {
  208. if(linksta == 0)
  209. {
  210. netif_set_link_down(&g_mynetif);
  211. EthStatus &= ~ETH_LINK_FLAG;
  212. }
  213. }
  214. else
  215. {
  216. if(linksta != 0)
  217. {
  218. netif_set_link_up(&g_mynetif);
  219. EthStatus |= ETH_LINK_FLAG;
  220. }
  221. }
  222. }
  223. else//处理未初始化的问题
  224. {
  225. if(linksta != 0)
  226. {
  227. low_level_init(&g_mynetif);
  228. netif_set_link_up(&g_mynetif);
  229. EthStatus |= ETH_INIT_FLAG;
  230. #if LWIP_DHCP == 1
  231. dhcp_start(&g_mynetif);
  232. #endif
  233. }
  234. }
  235. }
  236. /**
  237. * @brief Link callback function, this function is called on change of link status.
  238. * @param The network interface
  239. * @retval None
  240. */
  241. void ETH_link_callback(struct netif *netif)
  242. {
  243. __IO uint32_t timeout = 0;
  244. uint32_t tmpreg,RegValue;
  245. uint32_t media_temp = 0U;
  246. uint16_t phy_value;
  247. if(netif_is_link_up(netif))
  248. {
  249. printf("network is up");
  250. //进入自协商模式
  251. do{
  252. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
  253. phy_value &= PHY_AUTONEGO_COMPLETE;
  254. timeout++;
  255. }while((RESET == phy_value) && (timeout < PHY_READ_TO));
  256. /*
  257. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_SR, &phy_value);
  258. if((uint16_t)RESET != (phy_value & PHY_DUPLEX_STATUS)){
  259. media_temp = ENET_MODE_FULLDUPLEX;
  260. }else{
  261. media_temp = ENET_MODE_HALFDUPLEX;
  262. }
  263. if((uint16_t)RESET !=(phy_value & PHY_SPEED_STATUS)){
  264. media_temp |= ENET_SPEEDMODE_10M;
  265. }else{
  266. media_temp |= ENET_SPEEDMODE_100M;
  267. }
  268. */
  269. enet_enable();
  270. netif_set_up(&g_mynetif);
  271. __set_PRIMASK(1);
  272. NVIC_SystemReset();
  273. }
  274. else
  275. {
  276. printf("network is down");
  277. enet_disable();
  278. netif_set_down(&g_mynetif);
  279. }
  280. }
  281. void DHCP_open()
  282. {
  283. #ifdef USE_DHCP
  284. dhcp_state = DHCP_START;
  285. #endif
  286. }
  287. void DHCP_close()
  288. {
  289. #ifdef USE_DHCP
  290. dhcp_stop(&g_mynetif);
  291. #endif
  292. }
  293. void set_ipaddr(char* buf)
  294. {
  295. struct ip_addr ipaddr;
  296. struct ip_addr netmask;
  297. struct ip_addr gw;
  298. int ipaddr0,ipaddr1,ipaddr2,ipaddr3;
  299. int maskaddr0,maskaddr1,maskaddr2,maskaddr3;
  300. int gwaddr0,gwaddr1,gwaddr2,gwaddr3;
  301. ipaddr0 = parseIntField(buf,"\"ipv4\":\"" );
  302. ipaddr1 = parseIntField(strstr(buf,"\"ipv4\":\"") + 1,"." );
  303. ipaddr2 = parseIntField(strstr(strstr(buf,"\"ipv4\":\"") + 1,"." ) + 1,".");
  304. ipaddr3 = parseIntField(strstr(strstr(strstr(buf,"\"ipv4\":\"") + 1,"." ) + 1,".") + 1 ,".");
  305. maskaddr0 = parseIntField(buf,"\"subnetMask\":\"" );
  306. maskaddr1 = parseIntField(strstr(buf,"\"subnetMask\":\"") + 1,"." );
  307. maskaddr2 = parseIntField(strstr(strstr(buf,"\"subnetMask\":\"") + 1,"." ) + 1,".");
  308. maskaddr3 = parseIntField(strstr(strstr(strstr(buf,"\"subnetMask\":\"") + 1,"." ) + 1,".") + 1 ,".");
  309. gwaddr0 = parseIntField(buf,"\"defaultGateway\":\"" );
  310. gwaddr1 = parseIntField(strstr(buf,"\"defaultGateway\":\"") + 1,"." );
  311. gwaddr2 = parseIntField(strstr(strstr(buf,"\"defaultGateway\":\"") + 1,"." ) + 1,".");
  312. gwaddr3 = parseIntField(strstr(strstr(strstr(buf,"\"defaultGateway\":\"") + 1,"." ) + 1,".") + 1 ,".");
  313. IP4_ADDR(&ipaddr, ipaddr0 ,ipaddr1 , ipaddr2 , ipaddr3 );
  314. IP4_ADDR(&netmask, maskaddr0, maskaddr1, maskaddr2, maskaddr3);
  315. IP4_ADDR(&gw, gwaddr0, gwaddr1, gwaddr2,gwaddr3);
  316. // 设置网络接口的IP地址
  317. netif_set_addr(&g_mynetif, &ipaddr , &netmask, &gw);
  318. // 停止DHCP客户端
  319. dhcp_stop(&g_mynetif);
  320. // 重启网络接口以使更改生效
  321. netif_set_up(&g_mynetif);
  322. netif_set_down(&g_mynetif);
  323. netif_set_up(&g_mynetif);
  324. }