123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /**
- ******************************************************************************
- * @file LwIP/LwIP_HTTP_Server_Netconn_RTOS/Src/app_ethernet.c
- * @author MCD Application Team
- * @brief Ethernet specefic module
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2017 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "lwip/dhcp.h"
- #include "lwip/inet.h"
- #include "app_ethernet.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- #ifdef USE_DHCP
- #define MAX_DHCP_TRIES 4
- __IO uint8_t DHCP_state = DHCP_OFF;
- #endif
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Notify the User about the network interface config status
- * @param netif: the network interface
- * @retval None
- */
- void User_notification(struct netif *netif)
- {
- if (netif_is_up(netif))
- {
- #ifdef USE_DHCP
- /* Update DHCP state machine */
- DHCP_state = DHCP_START;
- #else
- /* Turn On LED 1 to indicate ETH and LwIP init success*/
- #endif /* USE_DHCP */
- }
- else
- {
- #ifdef USE_DHCP
- /* Update DHCP state machine */
- DHCP_state = DHCP_LINK_DOWN;
- #else
- /* Turn On LED 2 to indicate ETH and LwIP init error */
- #endif /* USE_DHCP */
- }
- }
- #ifdef USE_DHCP
- volatile uint32_t dhcp_done;
- /**
- * @brief DHCP Process
- * @param argument: network interface
- * @retval None
- */
- void DHCP_thread(void const * argument)
- {
- struct netif *netif = (struct netif *) argument;
- ip_addr_t ipaddr;
- ip_addr_t netmask;
- ip_addr_t gw;
- struct dhcp *dhcp;
-
- for (;;)
- {
- switch (DHCP_state)
- {
- case DHCP_START:
- {
- ip_addr_set_zero_ip4(&netif->ip_addr);
- ip_addr_set_zero_ip4(&netif->netmask);
- ip_addr_set_zero_ip4(&netif->gw);
- dhcp_start(netif);
- DHCP_state = DHCP_WAIT_ADDRESS;
- }
- break;
-
- case DHCP_WAIT_ADDRESS:
- {
- if (dhcp_supplied_address(netif))
- {
- DHCP_state = DHCP_ADDRESS_ASSIGNED;
- dhcp_done=1;
- printf("dhcp ip_addr is %s \r\n", inet_ntoa(netif->ip_addr));
- }
- else
- {
- dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
- /* DHCP timeout */
- if (dhcp->tries > MAX_DHCP_TRIES)
- {
- DHCP_state = DHCP_TIMEOUT;
-
- /* Stop DHCP */
- dhcp_stop(netif);
-
- /* Static address used */
- IP_ADDR4(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
- IP_ADDR4(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
- IP_ADDR4(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
- netif_set_addr(netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw));
-
- }
- }
- }
- break;
- case DHCP_LINK_DOWN:
- {
- /* Stop DHCP */
- dhcp_stop(netif);
- DHCP_state = DHCP_OFF;
- }
- break;
- default: break;
- }
-
- /* wait 250 ms */
- osDelay(250);
- }
- }
- void DHCP_open()
- {
- DHCP_state = DHCP_START;
- }
- void DHCP_close()
- {
- DHCP_state = DHCP_LINK_DOWN;
- }
- #endif /* USE_DHCP */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|