-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnetconf.c
209 lines (181 loc) · 6.16 KB
/
netconf.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
******************************************************************************
* @file netconf.c
* @author MCD Application Team
* @version V1.0.0
* @date 31-October-2011
* @brief Network connection configuration
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/dhcp.h"
#include "ethernetif.h"
#include "main.h"
//#include "Debugging.h"
#include "netconf.h"
#include "lwip/tcpip.h"
#include <stdio.h>
/* Private typedef -----------------------------------------------------------*/
typedef enum
{
DHCP_START=0,
DHCP_WAIT_ADDRESS,
DHCP_ADDRESS_ASSIGNED,
DHCP_TIMEOUT
}
DHCP_State_TypeDef;
/* Private define ------------------------------------------------------------*/
#define MAX_DHCP_TRIES 2
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
struct netif xnetif; /* network interface structure */
char DHCP_Finished = 0;
uint8_t DHCP_iptab[4];
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes the lwIP stack
* @param None
* @retval None
*/
void LwIP_Init(void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
#ifndef USE_DHCP
uint8_t iptab[4];
uint8_t iptxt[20];
#endif
/* Create tcp_ip stack thread */
tcpip_init( NULL, NULL );
/* IP address setting & display on STM32_evalboard LCD*/
#ifdef USE_DHCP
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else
IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
#ifdef USE_LCD
iptab[0] = IP_ADDR3;
iptab[1] = IP_ADDR2;
iptab[2] = IP_ADDR1;
iptab[3] = IP_ADDR0;
sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
LCD_DisplayStringLine(Line8, (uint8_t*)" Static IP address ");
LCD_DisplayStringLine(Line9, iptxt);
#endif
#endif
/* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
struct ip_addr *netmask, struct ip_addr *gw,
void *state, err_t (* init)(struct netif *netif),
err_t (* input)(struct pbuf *p, struct netif *netif))
Adds your network interface to the netif_list. Allocate a struct
netif and pass a pointer to this structure as the first argument.
Give pointers to cleared ip_addr structures when using DHCP,
or fill them with sane numbers otherwise. The state pointer may be NULL.
The init function pointer must point to a initialization function for
your ethernet netif interface. The following code illustrates it's use.*/
netif_add(&xnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input);
/* Registers the default network interface. */
netif_set_default(&xnetif);
/* When the netif is fully configured this function must be called.*/
netif_set_up(&xnetif);
}
#ifdef USE_DHCP
/**
* @brief LwIP_DHCP_Process_Handle
* @param None
* @retval None
*/
void LwIP_DHCP_task(void * pvParameters)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
uint32_t IPaddress;
uint8_t iptxt[20];
uint8_t DHCP_state;
DHCP_state = DHCP_START;
for (;;)
{
switch (DHCP_state)
{
case DHCP_START:
{
dhcp_start(&xnetif);
IPaddress = 0;
DHCP_state = DHCP_WAIT_ADDRESS;
}
break;
case DHCP_WAIT_ADDRESS:
{
/* Read the new IP address */
IPaddress = xnetif.ip_addr.addr;
if (IPaddress!=0)
{
DHCP_state = DHCP_ADDRESS_ASSIGNED;
/* Stop DHCP */
dhcp_stop(&xnetif);
DHCP_iptab[0] = (uint8_t)(IPaddress >> 24);
DHCP_iptab[1] = (uint8_t)(IPaddress >> 16);
DHCP_iptab[2] = (uint8_t)(IPaddress >> 8);
DHCP_iptab[3] = (uint8_t)(IPaddress);
//printf("Assigned IP: %d.%d.%d.%d\r\n", iptab[3], iptab[2], iptab[1], iptab[0]);
/* end of DHCP process: LED1 stays ON*/
DHCP_Finished = 1;
vTaskDelete(NULL);
}
else
{
/* DHCP timeout */
if (xnetif.dhcp->tries > MAX_DHCP_TRIES)
{
DHCP_state = DHCP_TIMEOUT;
/* Stop DHCP */
dhcp_stop(&xnetif);
/* Static address used */
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(&xnetif, &ipaddr , &netmask, &gw);
/*DHCP_iptab[0] = IP_ADDR3;
DHCP_iptab[1] = IP_ADDR2;
DHCP_iptab[2] = IP_ADDR1;
DHCP_iptab[3] = IP_ADDR0;*/
DHCP_iptab[0] = 0;
DHCP_iptab[1] = 0;
DHCP_iptab[2] = 0;
DHCP_iptab[3] = 0;
//printf("Assigned IP: %d.%d.%d.%d\r\n", iptab[3], iptab[2], iptab[1], iptab[0]);
/* end of DHCP process: LED1 stays ON*/
DHCP_Finished = 1;
vTaskDelete(NULL);
}
}
}
break;
default: break;
}
/* Toggle LED1 */
//DEBUGGING_LED_TOGGLE;
/* wait 250 ms */
vTaskDelay(250);
}
}
#endif /* USE_DHCP */
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/