netconf.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. ******************************************************************************
  3. * @file netconf.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 31-July-2013
  7. * @brief Network connection configuration
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "lwip/mem.h"
  29. #include "lwip/memp.h"
  30. #include "lwip/tcp.h"
  31. #include "lwip/priv/tcp_priv.h"
  32. #include "lwip/udp.h"
  33. #include "netif/etharp.h"
  34. #include "lwip/dhcp.h"
  35. #include "ethernetif.h"
  36. #include "main_lwip.h"
  37. #include "netconf.h"
  38. #include <stdio.h>
  39. //#include "param.h" /* 需要使用存储在 EEPROM中的网络参数 */
  40. /* Private typedef -----------------------------------------------------------*/
  41. #define MAX_DHCP_TRIES 4
  42. /* Private define ------------------------------------------------------------*/
  43. /* Private macro -------------------------------------------------------------*/
  44. /* Private variables ---------------------------------------------------------*/
  45. struct netif gnetif;
  46. uint32_t TCPTimer = 0;
  47. uint32_t ARPTimer = 0;
  48. uint32_t IPaddress = 0;
  49. #ifdef USE_DHCP
  50. uint32_t DHCPfineTimer = 0;
  51. uint32_t DHCPcoarseTimer = 0;
  52. __IO uint8_t DHCP_state;
  53. #endif
  54. extern __IO uint32_t EthStatus;
  55. /* Private functions ---------------------------------------------------------*/
  56. void LwIP_DHCP_Process_Handle(void);
  57. /**
  58. * @brief Initializes the lwIP stack
  59. * @param None
  60. * @retval None
  61. */
  62. void LwIP_Init(void)
  63. {
  64. struct ip4_addr ipaddr;
  65. struct ip4_addr netmask;
  66. struct ip4_addr gw;
  67. /* 打印调试信息 */
  68. lwip_printf("LwIP_Init()...\r\n");
  69. /* Initializes the dynamic memory heap defined by MEM_SIZE.*/
  70. mem_init();
  71. /* Initializes the memory pools defined by MEMP_NUM_x.*/
  72. memp_init();
  73. #ifdef USE_DHCP
  74. ipaddr.addr = 0;
  75. netmask.addr = 0;
  76. gw.addr = 0;
  77. #else
  78. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  79. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  80. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  81. #endif
  82. /* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
  83. struct ip_addr *netmask, struct ip_addr *gw,
  84. void *state, err_t (* init)(struct netif *netif),
  85. err_t (* input)(struct pbuf *p, struct netif *netif))
  86. Adds your network interface to the netif_list. Allocate a struct
  87. netif and pass a pointer to this structure as the first argument.
  88. Give pointers to cleared ip_addr structures when using DHCP,
  89. or fill them with sane numbers otherwise. The state pointer may be NULL.
  90. The init function pointer must point to a initialization function for
  91. your ethernet netif interface. The following code illustrates it's use.*/
  92. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
  93. /* 打印调试信息 */
  94. lwip_printf("LwIP Registers the default network interface....\r\n");
  95. /* Registers the default network interface.*/
  96. netif_set_default(&gnetif);
  97. // if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
  98. if (EthStatus == (ETH_INIT_FLAG)) /* armfly 修改 */
  99. {
  100. /* Set Ethernet link flag */
  101. gnetif.flags |= NETIF_FLAG_LINK_UP;
  102. /* When the netif is fully configured this function must be called */
  103. netif_set_up(&gnetif);
  104. #ifdef USE_DHCP
  105. DHCP_state = DHCP_START;
  106. #else
  107. lwip_printf("LwIP Static IP address = %d.%d.%d.%d\r\n", IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  108. #endif /* USE_DHCP */
  109. }
  110. else
  111. {
  112. /* When the netif link is down this function must be called */
  113. netif_set_down(&gnetif);
  114. #ifdef USE_DHCP
  115. DHCP_state = DHCP_LINK_DOWN;
  116. #endif /* USE_DHCP */
  117. /* 打印调试信息 */
  118. lwip_printf("LwIP Network Cable is not connected \r\n");
  119. }
  120. /* Set the link callback function, this function is called on change of link status*/
  121. netif_set_link_callback(&gnetif, ETH_link_callback);
  122. }
  123. /* 重设网络参数 */
  124. void LwIP_ChangeNetParam(void)
  125. {
  126. ETH_link_callback(&gnetif);
  127. }
  128. /**
  129. * @brief Called when a frame is received
  130. * @param None
  131. * @retval None
  132. */
  133. void LwIP_Pkt_Handle(void)
  134. {
  135. /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
  136. ethernetif_input(&gnetif);
  137. }
  138. /**
  139. * @brief LwIP periodic tasks
  140. * @param localtime the current LocalTime value
  141. * @retval None
  142. */
  143. void LwIP_Periodic_Handle(__IO uint32_t localtime)
  144. {
  145. #if LWIP_TCP
  146. /* TCP periodic process every 250 ms */
  147. if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
  148. {
  149. TCPTimer = localtime;
  150. tcp_tmr();
  151. }
  152. #endif
  153. /* ARP periodic process every 5s */
  154. if ((localtime - ARPTimer) >= ARP_TMR_INTERVAL)
  155. {
  156. ARPTimer = localtime;
  157. etharp_tmr();
  158. }
  159. #ifdef USE_DHCP
  160. /* Fine DHCP periodic process every 500ms */
  161. if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
  162. {
  163. DHCPfineTimer = localtime;
  164. dhcp_fine_tmr();
  165. if ((DHCP_state != DHCP_ADDRESS_ASSIGNED) &&
  166. (DHCP_state != DHCP_TIMEOUT) &&
  167. (DHCP_state != DHCP_LINK_DOWN))
  168. {
  169. /* toggle LED1 to indicate DHCP on-going process */
  170. STM_EVAL_LEDToggle(LED1);
  171. /* process DHCP state machine */
  172. LwIP_DHCP_Process_Handle();
  173. }
  174. }
  175. /* DHCP Coarse periodic process every 60s */
  176. if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS)
  177. {
  178. DHCPcoarseTimer = localtime;
  179. dhcp_coarse_tmr();
  180. }
  181. #endif
  182. }
  183. #ifdef USE_DHCP
  184. /**
  185. * @brief LwIP_DHCP_Process_Handle
  186. * @param None
  187. * @retval None
  188. */
  189. void LwIP_DHCP_Process_Handle()
  190. {
  191. struct ip_addr ipaddr;
  192. struct ip_addr netmask;
  193. struct ip_addr gw;
  194. uint8_t iptab[4] = {0};
  195. uint8_t iptxt[20];
  196. switch (DHCP_state)
  197. {
  198. case DHCP_START:
  199. {
  200. DHCP_state = DHCP_WAIT_ADDRESS;
  201. dhcp_start(&gnetif);
  202. /* IP address should be set to 0
  203. every time we want to assign a new DHCP address */
  204. IPaddress = 0;
  205. #ifdef USE_LCD
  206. LCD_DisplayStringLine(Line4, (uint8_t *)" Looking for ");
  207. LCD_DisplayStringLine(Line5, (uint8_t *)" DHCP server ");
  208. LCD_DisplayStringLine(Line6, (uint8_t *)" please wait... ");
  209. #endif
  210. }
  211. break;
  212. case DHCP_WAIT_ADDRESS:
  213. {
  214. /* Read the new IP address */
  215. IPaddress = gnetif.ip_addr.addr;
  216. if (IPaddress != 0)
  217. {
  218. DHCP_state = DHCP_ADDRESS_ASSIGNED;
  219. /* Stop DHCP */
  220. dhcp_stop(&gnetif);
  221. #ifdef USE_LCD
  222. iptab[0] = (uint8_t)(IPaddress >> 24);
  223. iptab[1] = (uint8_t)(IPaddress >> 16);
  224. iptab[2] = (uint8_t)(IPaddress >> 8);
  225. iptab[3] = (uint8_t)(IPaddress);
  226. sprintf((char *)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
  227. LCD_ClearLine(Line4);
  228. LCD_ClearLine(Line5);
  229. LCD_ClearLine(Line6);
  230. /* Display the IP address */
  231. LCD_DisplayStringLine(Line7, (uint8_t *)"IP address assigned ");
  232. LCD_DisplayStringLine(Line8, (uint8_t *)" by a DHCP server ");
  233. LCD_DisplayStringLine(Line9, iptxt);
  234. #endif
  235. STM_EVAL_LEDOn(LED1);
  236. }
  237. else
  238. {
  239. /* DHCP timeout */
  240. if (gnetif.dhcp->tries > MAX_DHCP_TRIES)
  241. {
  242. DHCP_state = DHCP_TIMEOUT;
  243. /* Stop DHCP */
  244. dhcp_stop(&gnetif);
  245. /* Static address used */
  246. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  247. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  248. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  249. netif_set_addr(&gnetif, &ipaddr, &netmask, &gw);
  250. #ifdef USE_LCD
  251. LCD_DisplayStringLine(Line7, (uint8_t *)" DHCP timeout ");
  252. iptab[0] = IP_ADDR3;
  253. iptab[1] = IP_ADDR2;
  254. iptab[2] = IP_ADDR1;
  255. iptab[3] = IP_ADDR0;
  256. sprintf((char *)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
  257. LCD_ClearLine(Line4);
  258. LCD_ClearLine(Line5);
  259. LCD_ClearLine(Line6);
  260. LCD_DisplayStringLine(Line8, (uint8_t *)" Static IP address ");
  261. LCD_DisplayStringLine(Line9, iptxt);
  262. #endif
  263. STM_EVAL_LEDOn(LED1);
  264. }
  265. }
  266. }
  267. break;
  268. default:
  269. break;
  270. }
  271. }
  272. #endif
  273. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/