• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
3  *
4  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
5  * You may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *        http://www.st.com/software_license_agreement_liberty_v2
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "lwip.h"
18 #include "usart.h"
19 #include "ethernetif.h"
20 #include "stm32f4xx.h"
21 #include "netif/etharp.h"
22 #include "lwip/dhcp.h"
23 #include "lwip/timeouts.h"
24 #include "lwip/tcp.h"
25 #include "lwip/ip4_frag.h"
26 #include "lwip/tcpip.h"
27 #include "lwip/priv/tcp_priv.h"
28 #include "lwip/netifapi.h"
29 #include "lwip/netif.h"
30 #include "lwipopts.h"
31 #include "prt_module.h"
32 #include "prt_task.h"
33 #include "os_cpu_armv7_m_external.h"
34 #include "prt_hwi.h"
35 #include <stdio.h>
36 
37 #define NETIF_SETUP_OVERTIME       100
38 
39 struct lwipDev lwipdev;
40 struct netif lwip_netif;
41 TskHandle lwipDhcpTaskId;
42 
43 #if LWIP_DHCP
LwipDhcpDelete(void)44 void LwipDhcpDelete(void)
45 {
46 	U32 ret;
47 	dhcp_stop(&lwip_netif);
48 	ret = PRT_TaskDelete(lwipDhcpTaskId);
49 	if (ret != OS_OK) {
50 		printf("LwipDhcpDelete failed!!!\n\r");
51 	}
52 }
53 
LwipDhcpTask(void * pdata)54 void LwipDhcpTask(void *pdata)
55 {
56 	U32 ip = 0; /* 0, initial value */
57 	U32 netmask = 0; /* 0, initial value */
58 	U32 gw = 0; /* 0, initial value */
59 
60 	dhcp_start(&lwip_netif);
61 
62 	while(1) {
63 		ip = lwip_netif.ip_addr.u_addr.ip4.addr;
64 		netmask	= lwip_netif.netmask.u_addr.ip4.addr;
65 		gw = lwip_netif.gw.u_addr.ip4.addr;
66 		struct dhcp *dhcp = netif_dhcp_data(&lwip_netif);
67 
68 		if(ip != 0) {
69 			lwipdev.ip[3] = (uint8_t)(ip >> 24);
70 			lwipdev.ip[2] = (uint8_t)(ip >> 16);
71 			lwipdev.ip[1] = (uint8_t)(ip >> 8);
72 			lwipdev.ip[0] = (uint8_t)(ip);
73 			lwipdev.netmask[3] = (uint8_t)(netmask >> 24);
74 			lwipdev.netmask[2] = (uint8_t)(netmask >> 16);
75 			lwipdev.netmask[1] = (uint8_t)(netmask >> 8);
76 			lwipdev.netmask[0] = (uint8_t)(netmask);
77 			lwipdev.gateway[3] = (uint8_t)(gw >> 24);
78 			lwipdev.gateway[2] = (uint8_t)(gw >> 16);
79 			lwipdev.gateway[1] = (uint8_t)(gw >> 8);
80 			lwipdev.gateway[0] = (uint8_t)(gw);
81 			break;
82 		} else if (dhcp->tries > LWIP_MAX_DHCP_TRIES) {
83 			IP4_ADDR(&(lwip_netif.ip_addr.u_addr.ip4), lwipdev.ip[0],
84 					 lwipdev.ip[1], lwipdev.ip[2], lwipdev.ip[3]);
85 			IP4_ADDR(&(lwip_netif.netmask.u_addr.ip4), lwipdev.netmask[0],
86 					 lwipdev.netmask[1], lwipdev.netmask[2],lwipdev.netmask[3]);
87 			IP4_ADDR(&(lwip_netif.gw.u_addr.ip4), lwipdev.gateway[0], lwipdev.gateway[1],
88 					 lwipdev.gateway[2], lwipdev.gateway[3]);
89 			break;
90 		}
91 	}
92 	LwipDhcpDelete();
93 }
94 
LwipDhcpCreate(void)95 U32 LwipDhcpCreate(void)
96 {
97     U32 ret;
98     struct TskInitParam taskParam;
99 
100     taskParam.taskEntry = (TskEntryFunc)LwipDhcpTask;
101     taskParam.stackSize = 0x800;
102     taskParam.name = "lwipDhcpTask";
103     taskParam.taskPrio = OS_TSK_PRIORITY_06;
104     taskParam.stackAddr = 0;
105 
106     ret = PRT_TaskCreate(&lwipDhcpTaskId, &taskParam);
107     if (ret != OS_OK) {
108         return ret;
109     }
110 
111     ret = PRT_TaskResume(lwipDhcpTaskId);
112     if (ret != OS_OK) {
113         return ret;
114     }
115 
116     return OS_OK;
117 }
118 #endif	/* #if LWIP_DHCP */
119 
LwipPktHandle(void)120 void LwipPktHandle(void)
121 {
122 	EthernetifInput(&lwip_netif);
123 }
124 
LwipDefaultIpSet(struct lwipDev * lwipx)125 void LwipDefaultIpSet(struct lwipDev *lwipx)
126 {
127 	u32 sn = (*(u32*)(0x1FFF7A10));
128 
129 	lwipx->remoteip[0] = 192;
130 	lwipx->remoteip[1] = 168;
131 	lwipx->remoteip[2] = 1;
132 	lwipx->remoteip[3] = 11;
133 
134 	lwipx->mac[0] = 2;
135 	lwipx->mac[1] = 0;
136 	lwipx->mac[2] = 0;
137 	lwipx->mac[3] = (sn >> 16) & 0XFF;
138 	lwipx->mac[4] = (sn >> 8) & 0XFFF;;
139 	lwipx->mac[5] = sn & 0XFF;
140 
141 	lwipx->ip[0] = 192;
142 	lwipx->ip[1] = 168;
143 	lwipx->ip[2] = 1;
144 	lwipx->ip[3] = 30;
145 
146 	lwipx->netmask[0] = 255;
147 	lwipx->netmask[1] = 255;
148 	lwipx->netmask[2] = 255;
149 	lwipx->netmask[3] = 0;
150 
151 	lwipx->gateway[0] = 192;
152 	lwipx->gateway[1] = 168;
153 	lwipx->gateway[2] = 1;
154 	lwipx->gateway[3] = 1;
155 }
156 
LwipInit(void)157 u8 LwipInit(void)
158 {
159 	printf("	LwipInit:\n\r");
160 	u8 res;
161 	uintptr_t intSave;
162 	struct netif *netifInitFlag;
163 	static uint32_t overtime = 0;
164 	ip4_addr_t ipaddr;
165 	ip4_addr_t netmask;
166 	ip4_addr_t gw;
167 
168 	res = EthMemMalloc();
169 	if(res != 0) {
170 		printf("		ErhMemMalloc failed\n\r");
171 		return 1;
172 	}
173 
174 	res = LAN8720Init();
175 	if(res != 0) {
176 		printf("		LAN8720Init failed\n\r");
177 		return 2;
178 	}
179 
180 	tcpip_init(NULL,NULL);
181 
182 	LwipDefaultIpSet(&lwipdev);
183 
184 #if LWIP_DHCP
185 	ip4_addr_set_zero(&gw);
186 	ip4_addr_set_zero(&ipaddr);
187 	ip4_addr_set_zero(&netmask);
188 #else
189 	IP4_ADDR(&ipaddr, lwipdev.ip[0], lwipdev.ip[1],
190 			 lwipdev.ip[2], lwipdev.ip[3]);
191 	IP4_ADDR(&netmask, lwipdev.netmask[0], lwipdev.netmask[1],
192 			 lwipdev.netmask[2], lwipdev.netmask[3]);
193 	IP4_ADDR(&gw,lwipdev.gateway[0], lwipdev.gateway[1],
194 			 lwipdev.gateway[2], lwipdev.gateway[3]);
195 	printf("LwipDefaultIpSet OK: \n\r");
196 	printf("MAC......%d.%d.%d.%d.%d.%d\r\n", lwipdev.mac[0], lwipdev.mac[1],
197 											 lwipdev.mac[2], lwipdev.mac[3],
198 											 lwipdev.mac[4], lwipdev.mac[5]);
199 	printf("IP.......%d.%d.%d.%d\r\n", lwipdev.ip[0], lwipdev.ip[1],
200 									   lwipdev.ip[2], lwipdev.ip[3]);
201 	printf("MASK.....%d.%d.%d.%d\r\n", lwipdev.netmask[0], lwipdev.netmask[1],
202 									   lwipdev.netmask[2], lwipdev.netmask[3]);
203 	printf("GATE.....%d.%d.%d.%d\r\n", lwipdev.gateway[0], lwipdev.gateway[1],
204 									   lwipdev.gateway[2], lwipdev.gateway[3]);
205 #endif
206 
207 	netifInitFlag = netif_add(&lwip_netif, &ipaddr, &netmask, &gw,
208 							  NULL, &EthernetifInit, &tcpip_input);
209 	if (netifInitFlag == NULL) {
210 		printf("		netif_add failed!!!\n\r");
211 	}
212     netif_set_default(&lwip_netif);
213     netif_set_up(&lwip_netif);
214     do {
215 		for (int i = 0; i < 2000; i++);
216         overtime++;
217         if (overtime > NETIF_SETUP_OVERTIME) {
218             printf("		netif_is_link_up overtime!\n\r");
219             break;
220         }
221     } while (netif_is_link_up(&lwip_netif) == 0);
222 	if (overtime <= NETIF_SETUP_OVERTIME) {
223 		printf("		netif init succeed!\n\r");
224 	}
225 
226 	intSave = PRT_HwiSetAttr(ETH_IRQn, OS_HWI_PRI_HIGHEST, OS_HWI_MODE_ENGROSS);
227 	if (intSave != OS_OK) {
228 		printf("		PRT_HwiSetAttr failed!!!\n\r");
229 	}
230 	intSave = PRT_HwiCreate(ETH_IRQn, EthIrqHandler, NULL);
231 	if (intSave != OS_OK) {
232 		printf("		PRT_HwiCreate failed!!!\n\r");
233 	}
234 	PRT_HwiEnable(ETH_IRQn);
235 
236 #if LWIP_DHCP
237 	U32 ret = LwipDhcpCreate();
238 	if (ret != OS_OK) {
239 		printf("LwipDhcpCreate error, ret = 0x%x!!!", ret);
240 	}
241 #endif
242 
243 	return 0;
244 }
245