netconf.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "lwip/mem.h"
  2. #include "lwip/memp.h"
  3. #include "lwip/dhcp.h"
  4. #include "ethernetif.h"
  5. #include "common_config.h"
  6. #include "netconf.h"
  7. #include "tcpip.h"
  8. #include "main.h"
  9. #include "settings_api.h"
  10. #include "parameters.h"
  11. #include "stm32f4x7_eth.h"
  12. #ifdef PRINTF_STDLIB
  13. #include <stdio.h>
  14. #endif
  15. #ifdef PRINTF_CUSTOM
  16. #include "tinystdio.h"
  17. #endif
  18. #include <string.h>
  19. #define MAX_DHCP_TRIES 10
  20. #define TIME_COUNTER_10_MIN 600
  21. #define TIME_COUNTER_1_MIN 60
  22. /**
  23. * @brief Network interface structure
  24. */
  25. struct netif xnetif;
  26. /**
  27. * @brief Задача получения сетевый параметров по DHCP протоколу
  28. */
  29. TaskHandle_t xHandleDHCP = NULL;
  30. void LwIP_Init(void)
  31. {
  32. struct ip4_addr ipaddr;
  33. struct ip4_addr netmask;
  34. struct ip4_addr gw;
  35. uint16_t reg = 0;
  36. tcpip_init( NULL, NULL );
  37. ipaddr.addr = 0;
  38. netmask.addr = 0;
  39. gw.addr = 0;
  40. netif_add(&xnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  41. netif_set_default(&xnetif);
  42. netif_set_link_up(&xnetif);
  43. netif_set_up(&xnetif);
  44. /* Проверяем наличие линка */
  45. // reg = ETH_ReadPHYRegister(0, 1);
  46. // if (reg & 4)
  47. xTaskCreate(LwIP_DHCP_task, "DHCPClient", configMINIMAL_STACK_SIZE * 2, NULL,
  48. tskIDLE_PRIORITY + 2, &xHandleDHCP);
  49. }
  50. /**
  51. * @brief LwIP_DHCP_Process_Handle
  52. * @param None
  53. * @retval None
  54. */
  55. void LwIP_DHCP_task(void * pvParameters)
  56. {
  57. struct ip4_addr ipaddr;
  58. struct ip4_addr netmask;
  59. struct ip4_addr gw;
  60. uint8_t DHCP_state;
  61. DHCP_state = DHCP_START;
  62. static uint8_t dhcpTry = 0;
  63. for (;;)
  64. {
  65. switch (DHCP_state)
  66. {
  67. case DHCP_START:
  68. {
  69. dhcp_start(&xnetif);
  70. DHCP_state = DHCP_WAIT_ADDRESS;
  71. //PRINT_USART("\n\rLooking for DHCP server please wait...\n\r");
  72. }
  73. break;
  74. case DHCP_WAIT_ADDRESS:
  75. {
  76. /* Read the new IP address */
  77. ipaddr = xnetif.ip_addr;
  78. netmask = xnetif.netmask;
  79. gw = xnetif.gw;
  80. if (ipaddr.addr != 0)
  81. {
  82. DHCP_state = DHCP_ADDRESS_ASSIGNED;
  83. dhcp_stop(&xnetif);
  84. PRINT_USART("Parameters assigned by a DHCP server:\n\r IP: ");
  85. PRINT_USART(ipaddr_ntoa(&ipaddr));
  86. PRINT_USART("\n\r");
  87. PRINT_USART("Netmask: ");
  88. PRINT_USART(ipaddr_ntoa(&netmask));
  89. PRINT_USART("\n\r");
  90. PRINT_USART("Gateway: ");
  91. PRINT_USART(ipaddr_ntoa(&gw));
  92. PRINT_USART("\n\r");
  93. printf("ETH OK\r\n");
  94. vTaskDelete(xHandleDHCP);
  95. }
  96. else
  97. {
  98. /* DHCP timeout */
  99. if (dhcpTry++ > MAX_DHCP_TRIES)
  100. {
  101. DHCP_state = DHCP_TIMEOUT;
  102. /* Stop DHCP */
  103. dhcp_stop(&xnetif);
  104. /* Сообщение о неудачной попытке получить IP */
  105. printf("ETH FAIL\r\n");
  106. vTaskDelete(xHandleDHCP);
  107. }
  108. }
  109. }
  110. break;
  111. default: break;
  112. }
  113. vTaskDelay(250);
  114. }
  115. }
  116. /*********** Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.*****END OF FILE****/