LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
example_lwip_httpserver.c
Go to the documentation of this file.
1 /*
2  * @brief HTTP server dual core example using lwIP ethernet stack FreeRTOS version
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
39 /* General Includes */
40 #include <string.h>
41 #include "lpc43xx_dualcore_config.h"
42 #include "ipc_msg.h"
43 #include "ipc_example.h"
44 
45 /* lwIP include files */
46 #include "lwip/opt.h"
47 #include "lwip/sys.h"
48 #include "lwip/memp.h"
49 #include "lwip/tcpip.h"
50 #include "lwip/ip_addr.h"
51 #include "lwip/netif.h"
52 #include "lwip/timers.h"
53 
54 #include "netif/etharp.h"
55 
56 #if LWIP_DHCP
57 #include "lwip/dhcp.h"
58 #endif
59 
60 #include "lpc18xx_43xx_emac.h"
61 #include "lpc_arch.h"
62 #include "lpc_phy.h"/* For the PHY monitor support */
63 
64 #if (defined(OS_FREE_RTOS) || defined(OS_UCOS_III))
65 #include "httpserver-netconn.h"
66 #else
67 #include "httpd.h"
68 #endif
69 
70 #ifdef OS_FREE_RTOS
71 #include "FreeRTOS.h"
72 #include "task.h"
73 #endif
74 
75 #ifdef OS_UCOS_III
76 #include "os.h"
77 
78 #define UCOS_MIN_STACK_SZ 512
79 #define UCOS_BLINK_TASK_PRIORITY 12
80 #define UCOS_EVENT_TASK_PRIORITY 13
81 #endif
82 
107 /*****************************************************************************
108  * Private types/enumerations/variables
109  ****************************************************************************/
110 
111 /* LWIP Network interface object structure */
112 static struct netif lpc_netif;
113 
114 /*****************************************************************************
115  * Private functions
116  ****************************************************************************/
117 
118 #if (defined(OS_FREE_RTOS) || defined(OS_UCOS_III))
119 
124 static void tcpip_init_done_signal(void *arg)
125 {
126  /* Tell main thread TCP/IP init is done */
127  *(s32_t *) arg = 1;
128 }
129 
134 #ifdef OS_FREE_RTOS
135 static portTASK_FUNCTION(vSetupIFTask, pvParameters)
136 #elif OS_UCOS_III
137 static void lwip_app_task(void *arg)
138 #endif
139 {
140  ip_addr_t ipaddr, netmask, gw;
141  volatile s32_t tcpipdone = 0;
143  static int prt_ip = 0;
144 
145  /* Wait until the TCP/IP thread is finished before
146  continuing or wierd things may happen */
147  DEBUGSTR("Waiting for TCPIP thread to initialize...\r\n");
148  tcpip_init(tcpip_init_done_signal, (void *) &tcpipdone);
149  while (!tcpipdone) ;
150 
151  DEBUGSTR("Starting LWIP HTTP server...\r\n");
152 
153  /* Static IP assignment */
154 #if LWIP_DHCP
155  IP4_ADDR(&gw, 0, 0, 0, 0);
156  IP4_ADDR(&ipaddr, 0, 0, 0, 0);
157  IP4_ADDR(&netmask, 0, 0, 0, 0);
158 #else
159  IP4_ADDR(&gw, 10, 1, 10, 1);
160  IP4_ADDR(&ipaddr, 10, 1, 10, 234);
161  IP4_ADDR(&netmask, 255, 255, 255, 0);
162 #endif
163 
164  /* Add netif interface for lpc17xx_8x */
165  memset(&lpc_netif, 0, sizeof(lpc_netif));
166  if (!netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init,
167  tcpip_input)) {
168  DEBUGSTR("Net interface failed to initialize ..\r\n");
169  while (1) ;
170  }
171 
172  netif_set_default(&lpc_netif);
173  netif_set_up(&lpc_netif);
174 
175  /* Enable MAC interrupts only after LWIP is ready */
176  NVIC_SetPriority(ETHERNET_IRQn, IRQ_PRIO_ETHERNET);
177  NVIC_EnableIRQ(ETHERNET_IRQn);
178 
179 #if LWIP_DHCP
180  dhcp_start(&lpc_netif);
181 #endif
182 
183  /* Initialize and start application */
184  http_server_netconn_init();
185 
186  /* This loop monitors the PHY link and will handle cable events
187  via the PHY driver. */
188  while (1) {
189  /* Call the PHY status update state machine once in a while
190  to keep the link status up-to-date */
191  physts = lpcPHYStsPoll();
192 
193  /* Only check for connection state when the PHY status has changed */
194  if (physts & PHY_LINK_CHANGED) {
195  if (physts & PHY_LINK_CONNECTED) {
196  /* Set interface speed and duplex */
197  if (physts & PHY_LINK_SPEED100) {
198  NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000);
199  }
200  else {
201  NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000);
202  }
203  if (physts & PHY_LINK_FULLDUPLX) {
204  Chip_ENET_Set_Duplex(true);
205  }
206  else {
207  Chip_ENET_Set_Duplex(false);
208  }
209 
210  tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up,
211  (void *) &lpc_netif, 1);
212  }
213  else {
214  tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down,
215  (void *) &lpc_netif, 1);
216  }
217  }
218 
219  // DEBUGOUT("Link connect status: %d\n", ((physts & PHY_LINK_CONNECTED) != 0));
220 
221  /* Delay for link detection */
222  msDelay(250);
223 
224  if (!prt_ip) {
225  if (lpc_netif.ip_addr.addr) {
226  char tmp_buff[16];
227  DEBUGOUT("IP_ADDR : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.ip_addr, tmp_buff, 16));
228  DEBUGOUT("NET_MASK : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.netmask, tmp_buff, 16));
229  DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.gw, tmp_buff, 16));
230  /* Push the remote IP address to other core */
231  ipcex_msgPush(IPCEX_ID_USER1, lpc_netif.ip_addr.addr);
232  prt_ip = 1;
233  }
234  }
235  }
236 }
237 
238 #endif
239 
240 /*****************************************************************************
241  * Public functions
242  ****************************************************************************/
243 
244 #ifdef OS_FREE_RTOS
245 
251 void msDelay(uint32_t ms)
252 {
253  vTaskDelay((configTICK_RATE_HZ * ms) / 1000);
254 }
255 
256 #endif
257 
258 void LWIP_Init(void)
259 {
260  #ifdef OS_FREE_RTOS
261  return;
262  #elif defined(OS_UCOS_III)
263  return;
264  #else
265  extern void lwip_init(void);
266 
267  ip_addr_t ipaddr, netmask, gw;
268 
269  /* Setup a 1mS sysTick for the primary time base */
270  SysTick_Enable(1);
271 
272  /* Initialize LWIP */
273  DEBUGSTR("Starting lwip_init...\r\n");
274  lwip_init();
275 
276  DEBUGSTR("Starting LWIP HTTP server...\r\n");
277 
278  /* Static IP assignment */
279 #if LWIP_DHCP
280  IP4_ADDR(&gw, 0, 0, 0, 0);
281  IP4_ADDR(&ipaddr, 0, 0, 0, 0);
282  IP4_ADDR(&netmask, 0, 0, 0, 0);
283 #else
284  IP4_ADDR(&gw, 10, 1, 10, 1);
285  IP4_ADDR(&ipaddr, 10, 1, 10, 234);
286  IP4_ADDR(&netmask, 255, 255, 255, 0);
287 #endif
288 
289  /* Add netif interface for lpc17xx_8x */
290  netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init,
291  ethernet_input);
292  netif_set_default(&lpc_netif);
293  netif_set_up(&lpc_netif);
294 
295 #if LWIP_DHCP
296  dhcp_start(&lpc_netif);
297 #endif
298 
299  /* Initialize and start application */
300  httpd_init();
301 
302  return;
303 #endif
304 }
305 
306 #if (defined(OS_FREE_RTOS) || defined(OS_UCOS_III))
307 
308 void lwip_tasks(void)
309 {
310 #ifdef OS_FREE_RTOS
311  xTaskCreate(vSetupIFTask, (signed char *) "SetupIFx",
312  256, NULL, (tskIDLE_PRIORITY + 1UL),
313  (xTaskHandle *) NULL);
314 #elif OS_UCOS_III
315  OS_ERR os_err;
316  static OS_TCB lwip_app_taskTCB;
317  static CPU_STK lwip_app_taskSTK[UCOS_MIN_STACK_SZ];
318 
319  OSTaskCreate((OS_TCB *) &lwip_app_taskTCB, /* Create the start task */
320  (CPU_CHAR *) "Start",
321  (OS_TASK_PTR) lwip_app_task,
322  (void *) 0,
323  (OS_PRIO) (APP_CFG_TASK_START_PRIO),
324  (CPU_STK *) &lwip_app_taskSTK[0],
325  (CPU_STK_SIZE) 32,
326  (CPU_STK_SIZE) UCOS_MIN_STACK_SZ,
327  (OS_MSG_QTY) 0u,
328  (OS_TICK) 0u,
329  (void *) 0,
330  (OS_OPT) (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
331  (OS_ERR *) &os_err);
332 #endif
333 }
334 
335 #else
336 
345 void lwip_tasks(void)
346 {
348  static int prt_ip = 0;
349 
350  do {
351  /* Handle packets as part of this loop, not in the IRQ handler */
353 
354  /* Free TX buffers that are done sending */
356 
357  /* LWIP timers - ARP, DHCP, TCP, etc. */
358  sys_check_timeouts();
359 
360  /* Call the PHY status update state machine once in a while
361  to keep the link status up-to-date */
362  physts = lpcPHYStsPoll();
363 
364  /* Only check for connection state when the PHY status has changed */
365  if (physts & PHY_LINK_CHANGED) {
366  if (physts & PHY_LINK_CONNECTED) {
367  /* Set interface speed and duplex */
368  if (physts & PHY_LINK_SPEED100) {
369  NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000);
370  }
371  else {
372  NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000);
373  }
374  if (physts & PHY_LINK_FULLDUPLX) {
375  Chip_ENET_Set_Duplex(true);
376  }
377  else {
378  Chip_ENET_Set_Duplex(false);
379  }
380 
381  netif_set_link_up(&lpc_netif);
382  }
383  else {
384  netif_set_link_down(&lpc_netif);
385  }
386  }
387 
388  // DEBUGOUT("Link connect status: %d\r\n", ((physts & PHY_LINK_CONNECTED) != 0));
389 
390  if (!prt_ip) {
391  if (lpc_netif.ip_addr.addr) {
392  static char tmp_buff[16];
393  DEBUGOUT("IP_ADDR : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.ip_addr, tmp_buff, 16));
394  DEBUGOUT("NET_MASK : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.netmask, tmp_buff, 16));
395  DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.gw, tmp_buff, 16));
396  /* Push the remote IP address to other core */
397  ipcex_msgPush(IPCEX_ID_USER1, lpc_netif.ip_addr.addr);
398  prt_ip = 1;
399  }
400  }
401  } while (0);
402 }
403 
404 #endif
405