1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
44
45
46
51
52
53
54
55
56
58
59
62
63
65
66
67
68
69
70
72
73
74
75
77
78
79
80
81
82
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
141
142
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
165
166
168
169
170
171
172
173
174
175
176
177
178
179
181
182
183
184
185
186
187
188
193
203
/* ... */
#include "lwip/opt.h"
#include "main.h"
#if LWIP_DHCP
#include "lwip/dhcp.h"
#endif
#include "app_ethernet.h"
#include "ethernetif.h"
#ifdef USE_LCD
#include "lcd_log.h"
#endif
Includes
uint32_t EthernetLinkTimer;
#if LWIP_DHCP
#define MAX_DHCP_TRIES 4
uint32_t DHCPfineTimer = 0;
uint8_t DHCP_state = DHCP_OFF;/* ... */
#endif
Private variables
/* ... */
void ethernet_link_status_updated(struct netif *netif)
{
if (netif_is_link_up(netif))
{
#if LWIP_DHCP
DHCP_state = DHCP_START;/* ... */
#elif defined(USE_LCD)
uint8_t iptxt[20];
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
LCD_UsrTrace ("Static IP address: %s\n", iptxt);/* ... */
#else
BSP_LED_On(LED1);
BSP_LED_Off(LED2);/* ... */
#endif
}if (netif_is_link_up(netif)) { ... }
else
{
#if LWIP_DHCP
DHCP_state = DHCP_LINK_DOWN;/* ... */
#elif defined(USE_LCD)
LCD_UsrTrace ("The network cable is not connected \n");
#else
BSP_LED_Off(LED1);
BSP_LED_On(LED2);/* ... */
#endif
}else { ... }
}{ ... }
#if LWIP_NETIF_LINK_CALLBACK
/* ... */
void Ethernet_Link_Periodic_Handle(struct netif *netif)
{
if (HAL_GetTick() - EthernetLinkTimer >= 100)
{
EthernetLinkTimer = HAL_GetTick();
ethernet_link_check_state(netif);
}if (HAL_GetTick() - EthernetLinkTimer >= 100) { ... }
}{ ... }
/* ... */#endif
#if LWIP_DHCP
/* ... */
void DHCP_Process(struct netif *netif)
{
ip_addr_t ipaddr;
ip_addr_t netmask;
ip_addr_t gw;
struct dhcp *dhcp;
#ifdef USE_LCD
uint8_t iptxt[20];
#endif
switch (DHCP_state)
{
case DHCP_START:
{
#ifdef USE_LCD
LCD_UsrLog (" State: Looking for DHCP server ...\n");
#else
BSP_LED_Off(LED1);
BSP_LED_Off(LED2);/* ... */
#endif
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_START:
case DHCP_WAIT_ADDRESS:
{
if (dhcp_supplied_address(netif))
{
DHCP_state = DHCP_ADDRESS_ASSIGNED;
#ifdef USE_LCD
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
LCD_UsrLog ("IP address assigned by a DHCP server: %s\n", iptxt);/* ... */
#else
BSP_LED_On(LED1);
BSP_LED_Off(LED2);/* ... */
#endif
}if (dhcp_supplied_address(netif)) { ... }
else
{
dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
if (dhcp->tries > MAX_DHCP_TRIES)
{
DHCP_state = DHCP_TIMEOUT;
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, &ipaddr, &netmask, &gw);
#ifdef USE_LCD
sprintf((char *)iptxt, "%s", ip4addr_ntoa(netif_ip4_addr(netif)));
LCD_UsrLog ("DHCP Timeout !! \n");
LCD_UsrLog ("Static IP address: %s\n", iptxt);/* ... */
#else
BSP_LED_On(LED1);
BSP_LED_Off(LED2);/* ... */
#endif
}if (dhcp->tries > MAX_DHCP_TRIES) { ... }
}else { ... }
...}
break;case DHCP_WAIT_ADDRESS:
case DHCP_LINK_DOWN:
{
DHCP_state = DHCP_OFF;
#ifdef USE_LCD
LCD_UsrLog ("The network cable is not connected \n");
#else
BSP_LED_Off(LED1);
BSP_LED_On(LED2);/* ... */
#endif
...}
break;case DHCP_LINK_DOWN:
default: break;
}switch (DHCP_state) { ... }
}{ ... }
/* ... */
void DHCP_Periodic_Handle(struct netif *netif)
{
if (HAL_GetTick() - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
{
DHCPfineTimer = HAL_GetTick();
DHCP_Process(netif);
}if (HAL_GetTick() - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS) { ... }
}{ ... }
#endif/* ... */