app_ethernet.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. ******************************************************************************
  3. * @file LwIP/LwIP_HTTP_Server_Netconn_RTOS/Src/app_ethernet.c
  4. * @author MCD Application Team
  5. * @brief Ethernet specefic module
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "lwip/dhcp.h"
  22. #include "app_ethernet.h"
  23. /* Private typedef -----------------------------------------------------------*/
  24. /* Private define ------------------------------------------------------------*/
  25. /* Private macro -------------------------------------------------------------*/
  26. /* Private variables ---------------------------------------------------------*/
  27. #ifdef USE_DHCP
  28. #define MAX_DHCP_TRIES 4
  29. __IO uint8_t DHCP_state = DHCP_OFF;
  30. #endif
  31. /* Private function prototypes -----------------------------------------------*/
  32. /* Private functions ---------------------------------------------------------*/
  33. /**
  34. * @brief Notify the User about the network interface config status
  35. * @param netif: the network interface
  36. * @retval None
  37. */
  38. void User_notification(struct netif *netif)
  39. {
  40. if (netif_is_up(netif))
  41. {
  42. #ifdef USE_DHCP
  43. /* Update DHCP state machine */
  44. DHCP_state = DHCP_START;
  45. #else
  46. /* Turn On LED 1 to indicate ETH and LwIP init success*/
  47. #endif /* USE_DHCP */
  48. }
  49. else
  50. {
  51. #ifdef USE_DHCP
  52. /* Update DHCP state machine */
  53. DHCP_state = DHCP_LINK_DOWN;
  54. #else
  55. /* Turn On LED 2 to indicate ETH and LwIP init error */
  56. #endif /* USE_DHCP */
  57. }
  58. }
  59. #ifdef USE_DHCP
  60. volatile uint32_t dhcp_done;
  61. /**
  62. * @brief DHCP Process
  63. * @param argument: network interface
  64. * @retval None
  65. */
  66. void DHCP_thread(void const * argument)
  67. {
  68. struct netif *netif = (struct netif *) argument;
  69. ip_addr_t ipaddr;
  70. ip_addr_t netmask;
  71. ip_addr_t gw;
  72. struct dhcp *dhcp;
  73. for (;;)
  74. {
  75. switch (DHCP_state)
  76. {
  77. case DHCP_START:
  78. {
  79. ip_addr_set_zero_ip4(&netif->ip_addr);
  80. ip_addr_set_zero_ip4(&netif->netmask);
  81. ip_addr_set_zero_ip4(&netif->gw);
  82. dhcp_start(netif);
  83. DHCP_state = DHCP_WAIT_ADDRESS;
  84. }
  85. break;
  86. case DHCP_WAIT_ADDRESS:
  87. {
  88. if (dhcp_supplied_address(netif))
  89. {
  90. DHCP_state = DHCP_ADDRESS_ASSIGNED;
  91. dhcp_done=1;
  92. }
  93. else
  94. {
  95. dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
  96. /* DHCP timeout */
  97. if (dhcp->tries > MAX_DHCP_TRIES)
  98. {
  99. DHCP_state = DHCP_TIMEOUT;
  100. /* Stop DHCP */
  101. dhcp_stop(netif);
  102. /* Static address used */
  103. IP_ADDR4(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
  104. IP_ADDR4(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  105. IP_ADDR4(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  106. netif_set_addr(netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw));
  107. }
  108. }
  109. }
  110. break;
  111. case DHCP_LINK_DOWN:
  112. {
  113. /* Stop DHCP */
  114. dhcp_stop(netif);
  115. DHCP_state = DHCP_OFF;
  116. }
  117. break;
  118. default: break;
  119. }
  120. /* wait 250 ms */
  121. osDelay(250);
  122. }
  123. }
  124. #endif /* USE_DHCP */
  125. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/