app_ethernet.c 4.2 KB

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