netconf.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 3
  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 ip_addr ipaddr;
  33. struct ip_addr netmask;
  34. struct ip_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_up(&xnetif);
  43. /* Проверяем наличие линка */
  44. // reg = ETH_ReadPHYRegister(0, 1);
  45. // if (reg & 4)
  46. xTaskCreate(LwIP_DHCP_task, "DHCPClient", configMINIMAL_STACK_SIZE * 2, NULL,
  47. tskIDLE_PRIORITY + 2, &xHandleDHCP);
  48. }
  49. /**
  50. * @brief LwIP_DHCP_Process_Handle
  51. * @param None
  52. * @retval None
  53. */
  54. void LwIP_DHCP_task(void * pvParameters)
  55. {
  56. struct ip_addr ipaddr;
  57. struct ip_addr netmask;
  58. struct ip_addr gw;
  59. uint8_t DHCP_state;
  60. DHCP_state = DHCP_START;
  61. for (;;)
  62. {
  63. switch (DHCP_state)
  64. {
  65. case DHCP_START:
  66. {
  67. dhcp_start(&xnetif);
  68. DHCP_state = DHCP_WAIT_ADDRESS;
  69. //PRINT_USART("\n\rLooking for DHCP server please wait...\n\r");
  70. }
  71. break;
  72. case DHCP_WAIT_ADDRESS:
  73. {
  74. /* Read the new IP address */
  75. ipaddr = xnetif.ip_addr;
  76. netmask = xnetif.netmask;
  77. gw = xnetif.gw;
  78. if (ipaddr.addr != 0)
  79. {
  80. DHCP_state = DHCP_ADDRESS_ASSIGNED;
  81. dhcp_stop(&xnetif);
  82. PRINT_USART("Parameters assigned by a DHCP server:\n\r IP: ");
  83. PRINT_USART(ipaddr_ntoa(&ipaddr));
  84. PRINT_USART("\n\r");
  85. PRINT_USART("Netmask: ");
  86. PRINT_USART(ipaddr_ntoa(&netmask));
  87. PRINT_USART("\n\r");
  88. PRINT_USART("Gateway: ");
  89. PRINT_USART(ipaddr_ntoa(&gw));
  90. PRINT_USART("\n\r");
  91. printf("ETH OK\r\n");
  92. vTaskDelete(xHandleDHCP);
  93. }
  94. else
  95. {
  96. /* DHCP timeout */
  97. if (xnetif.dhcp->tries > MAX_DHCP_TRIES)
  98. {
  99. DHCP_state = DHCP_TIMEOUT;
  100. /* Stop DHCP */
  101. dhcp_stop(&xnetif);
  102. /* Сообщение о неудачной попытке получить IP */
  103. printf("ETH FAIL\r\n");
  104. vTaskDelete(xHandleDHCP);
  105. }
  106. }
  107. }
  108. break;
  109. default: break;
  110. }
  111. vTaskDelay(250);
  112. }
  113. }
  114. /*********** Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.*****END OF FILE****/