app_ethernet.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "lwip/inet.h"
  23. #include "app_ethernet.h"
  24. /* Private typedef -----------------------------------------------------------*/
  25. /* Private define ------------------------------------------------------------*/
  26. /* Private macro -------------------------------------------------------------*/
  27. /* Private variables ---------------------------------------------------------*/
  28. #ifdef USE_DHCP
  29. #define MAX_DHCP_TRIES 4
  30. __IO uint8_t DHCP_state = DHCP_OFF;
  31. #endif
  32. /* Private function prototypes -----------------------------------------------*/
  33. /* Private functions ---------------------------------------------------------*/
  34. /**
  35. * @brief Notify the User about the network interface config status
  36. * @param netif: the network interface
  37. * @retval None
  38. */
  39. void User_notification(struct netif *netif)
  40. {
  41. if (netif_is_up(netif))
  42. {
  43. #ifdef USE_DHCP
  44. /* Update DHCP state machine */
  45. DHCP_state = DHCP_START;
  46. #else
  47. /* Turn On LED 1 to indicate ETH and LwIP init success*/
  48. #endif /* USE_DHCP */
  49. }
  50. else
  51. {
  52. #ifdef USE_DHCP
  53. /* Update DHCP state machine */
  54. DHCP_state = DHCP_LINK_DOWN;
  55. #else
  56. /* Turn On LED 2 to indicate ETH and LwIP init error */
  57. #endif /* USE_DHCP */
  58. }
  59. }
  60. #ifdef USE_DHCP
  61. volatile uint32_t dhcp_done;
  62. /**
  63. * @brief DHCP Process
  64. * @param argument: network interface
  65. * @retval None
  66. */
  67. void DHCP_thread(void const * argument)
  68. {
  69. struct netif *netif = (struct netif *) argument;
  70. ip_addr_t ipaddr;
  71. ip_addr_t netmask;
  72. ip_addr_t gw;
  73. struct dhcp *dhcp;
  74. for (;;)
  75. {
  76. switch (DHCP_state)
  77. {
  78. case DHCP_START:
  79. {
  80. ip_addr_set_zero_ip4(&netif->ip_addr);
  81. ip_addr_set_zero_ip4(&netif->netmask);
  82. ip_addr_set_zero_ip4(&netif->gw);
  83. dhcp_start(netif);
  84. DHCP_state = DHCP_WAIT_ADDRESS;
  85. }
  86. break;
  87. case DHCP_WAIT_ADDRESS:
  88. {
  89. if (dhcp_supplied_address(netif))
  90. {
  91. DHCP_state = DHCP_ADDRESS_ASSIGNED;
  92. dhcp_done=1;
  93. printf("dhcp ip_addr is %s \r\n", inet_ntoa(netif->ip_addr));
  94. }
  95. else
  96. {
  97. dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
  98. /* DHCP timeout */
  99. if (dhcp->tries > MAX_DHCP_TRIES)
  100. {
  101. DHCP_state = DHCP_TIMEOUT;
  102. /* Stop DHCP */
  103. dhcp_stop(netif);
  104. /* Static address used */
  105. IP_ADDR4(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
  106. IP_ADDR4(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  107. IP_ADDR4(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  108. netif_set_addr(netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw));
  109. }
  110. }
  111. }
  112. break;
  113. case DHCP_LINK_DOWN:
  114. {
  115. /* Stop DHCP */
  116. dhcp_stop(netif);
  117. DHCP_state = DHCP_OFF;
  118. }
  119. break;
  120. default: break;
  121. }
  122. /* wait 250 ms */
  123. osDelay(250);
  124. }
  125. }
  126. void DHCP_open()
  127. {
  128. DHCP_state = DHCP_START;
  129. }
  130. void DHCP_close()
  131. {
  132. DHCP_state = DHCP_LINK_DOWN;
  133. }
  134. #endif /* USE_DHCP */
  135. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/