netconf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #define MAX_DHCP_TRIES 4
  43. typedef enum
  44. {
  45. DHCP_START = 0,
  46. DHCP_WAIT_ADDRESS,
  47. DHCP_ADDRESS_ASSIGNED,
  48. DHCP_TIMEOUT
  49. }dhcp_state_enum;
  50. volatile uint8_t EthLinkStatus = 0;
  51. #ifdef USE_DHCP
  52. dhcp_state_enum dhcp_state = DHCP_START;
  53. #endif /* USE_DHCP */
  54. struct netif g_mynetif;
  55. void ETH_link_callback(struct netif *netif);
  56. void ETH_CheckLinkStatus(uint16_t PHYAddress);
  57. void ETH_CheckLinkStatus_timer(void *arg);
  58. /*!
  59. \brief initializes the LwIP stack
  60. \param[in] none
  61. \param[out] none
  62. \retval none
  63. */
  64. void lwip_stack_init(void)
  65. {
  66. struct ip_addr ipaddr;
  67. struct ip_addr netmask;
  68. struct ip_addr gw;
  69. /* create tcp_ip stack thread */
  70. tcpip_init( NULL, NULL );
  71. /* IP address setting */
  72. #ifdef USE_DHCP
  73. ipaddr.addr = 0;
  74. netmask.addr = 0;
  75. gw.addr = 0;
  76. #else
  77. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  78. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
  79. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  80. #endif /* USE_DHCP */
  81. netif_add(&g_mynetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  82. /* registers the default network interface */
  83. netif_set_default(&g_mynetif);
  84. /* when the netif is fully configured this function must be called */
  85. netif_set_up(&g_mynetif);
  86. uint16_t data;
  87. enet_phy_write_read(ENET_PHY_READ, DP83848_PHY_ADDRESS,PHY_REG_BSR,&data);
  88. if(data & 0x4)
  89. {
  90. EthStatus |= ETH_LINK_FLAG;
  91. }
  92. if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
  93. {
  94. /* Set Ethernet link flag */
  95. g_mynetif.flags |= NETIF_FLAG_LINK_UP;
  96. /* When the netif is fully configured this function must be called.*/
  97. netif_set_up(&g_mynetif);
  98. #ifdef USE_DHCP
  99. dhcp_state = DHCP_START;
  100. #else
  101. #ifdef SERIAL_DEBUG
  102. printf("\n Static IP address \n");
  103. printf("IP: %d.%d.%d.%d\n",IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
  104. printf("NETMASK: %d.%d.%d.%d\n",NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
  105. printf("Gateway: %d.%d.%d.%d\n",GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
  106. #endif /* SERIAL_DEBUG */
  107. #endif /* USE_DHCP */
  108. }
  109. else
  110. {
  111. /* When the netif link is down this function must be called.*/
  112. netif_set_down(&g_mynetif);
  113. #ifdef USE_DHCP
  114. dhcp_state = DHCP_TIMEOUT;
  115. #endif /* USE_DHCP */
  116. #ifdef SERIAL_DEBUG
  117. printf("\n Network Cable is \n");
  118. printf(" not connected \n");
  119. #endif /* SERIAL_DEBUG */
  120. }
  121. /* Set the link callback function, this function is called on change of link status*/
  122. netif_set_link_callback(&g_mynetif, ETH_link_callback);
  123. sys_timeout(2000, ETH_CheckLinkStatus_timer, NULL);
  124. }
  125. #ifdef USE_DHCP
  126. /*!
  127. \brief dhcp_task
  128. \param[in] none
  129. \param[out] none
  130. \retval none
  131. */
  132. void dhcp_task(void * pvParameters)
  133. {
  134. struct ip_addr ipaddr;
  135. struct ip_addr netmask;
  136. struct ip_addr gw;
  137. uint32_t ip_address = 0;
  138. for(;;){
  139. switch(dhcp_state){
  140. case DHCP_START:
  141. dhcp_start(&g_mynetif);
  142. /* IP address should be set to 0 every time we want to assign a new DHCP address*/
  143. ip_address = 0;
  144. dhcp_state = DHCP_WAIT_ADDRESS;
  145. break;
  146. case DHCP_WAIT_ADDRESS:
  147. /* read the new IP address */
  148. ip_address = g_mynetif.ip_addr.addr;
  149. if(0 != ip_address){
  150. dhcp_state = DHCP_ADDRESS_ASSIGNED;
  151. /* stop DHCP */
  152. dhcp_stop(&g_mynetif);
  153. char p[100];
  154. sprintf(p,"\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address), \
  155. ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address));
  156. }else{
  157. /* DHCP timeout */
  158. if(g_mynetif.dhcp->tries > MAX_DHCP_TRIES){
  159. dhcp_state = DHCP_TIMEOUT;
  160. /* stop DHCP */
  161. dhcp_stop(&g_mynetif);
  162. /* static address used */
  163. IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
  164. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  165. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  166. netif_set_addr(&g_mynetif, &ipaddr , &netmask, &gw);
  167. }
  168. }
  169. break;
  170. default:
  171. break;
  172. }
  173. /* wait 250 ms */
  174. vTaskDelay(1000);
  175. }
  176. }
  177. #endif /* USE_DHCP */
  178. //监测以太网热拔插定时器
  179. void ETH_CheckLinkStatus_timer(void *arg)
  180. {
  181. LWIP_UNUSED_ARG(arg);
  182. ETH_CheckLinkStatus(DP83848_PHY_ADDRESS);
  183. sys_timeout(1000, ETH_CheckLinkStatus_timer, NULL);
  184. }
  185. /* This function is called periodically each second */
  186. /* It checks link status for ethernet controller */
  187. void ETH_CheckLinkStatus(uint16_t PHYAddress)
  188. {
  189. static uint8_t status = 0;
  190. uint16_t data;
  191. uint8_t linksta;
  192. //读取以太网链接状态
  193. if(enet_phy_write_read(ENET_PHY_READ, DP83848_PHY_ADDRESS,PHY_REG_BSR,&data)==1)
  194. {
  195. linksta=data & 0x4;
  196. }
  197. if(EthStatus & ETH_INIT_FLAG)
  198. {
  199. if(EthStatus & ETH_LINK_FLAG)
  200. {
  201. if(linksta == 0)
  202. {
  203. netif_set_link_down(&g_mynetif);
  204. EthStatus &= ~ETH_LINK_FLAG;
  205. }
  206. }
  207. else
  208. {
  209. if(linksta != 0)
  210. {
  211. netif_set_link_up(&g_mynetif);
  212. EthStatus |= ETH_LINK_FLAG;
  213. }
  214. }
  215. }
  216. else//处理未初始化的问题
  217. {
  218. if(linksta != 0)
  219. {
  220. low_level_init(&g_mynetif);
  221. netif_set_link_up(&g_mynetif);
  222. EthStatus |= ETH_INIT_FLAG;
  223. #if LWIP_DHCP == 1
  224. dhcp_start(&g_mynetif);
  225. #endif
  226. }
  227. }
  228. }
  229. /**
  230. * @brief Link callback function, this function is called on change of link status.
  231. * @param The network interface
  232. * @retval None
  233. */
  234. void ETH_link_callback(struct netif *netif)
  235. {
  236. __IO uint32_t timeout = 0;
  237. uint32_t tmpreg,RegValue;
  238. uint32_t media_temp = 0U;
  239. uint16_t phy_value;
  240. if(netif_is_link_up(netif))
  241. {
  242. printf("network is up");
  243. //进入自协商模式
  244. do{
  245. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
  246. phy_value &= PHY_AUTONEGO_COMPLETE;
  247. timeout++;
  248. }while((RESET == phy_value) && (timeout < PHY_READ_TO));
  249. /*
  250. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_SR, &phy_value);
  251. if((uint16_t)RESET != (phy_value & PHY_DUPLEX_STATUS)){
  252. media_temp = ENET_MODE_FULLDUPLEX;
  253. }else{
  254. media_temp = ENET_MODE_HALFDUPLEX;
  255. }
  256. if((uint16_t)RESET !=(phy_value & PHY_SPEED_STATUS)){
  257. media_temp |= ENET_SPEEDMODE_10M;
  258. }else{
  259. media_temp |= ENET_SPEEDMODE_100M;
  260. }
  261. */
  262. enet_enable();
  263. netif_set_up(&g_mynetif);
  264. }
  265. else
  266. {
  267. printf("network is down");
  268. enet_disable();
  269. netif_set_down(&g_mynetif);
  270. }
  271. }