LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lwip_tcpecho_freertos.c
Go to the documentation of this file.
1 /*
2  * @brief LWIP FreeRTOS TCP Echo example
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 
32 #include "lwip/init.h"
33 #include "lwip/opt.h"
34 #include "lwip/sys.h"
35 #include "lwip/memp.h"
36 #include "lwip/tcpip.h"
37 #include "lwip/ip_addr.h"
38 #include "lwip/netif.h"
39 #include "lwip/timers.h"
40 #include "netif/etharp.h"
41 
42 #if LWIP_DHCP
43 #include "lwip/dhcp.h"
44 #endif
45 
46 #include "board.h"
47 #include "arch\lpc17xx_40xx_emac.h"
48 #include "arch\lpc_arch.h"
49 #include "arch\sys_arch.h"
50 #include "lpc_phy.h"/* For the PHY monitor support */
51 #include "tcpecho.h"
52 
89 /* When building the example to run in FLASH, the number of available
90  pbufs and memory size (in lwipopts.h) and the number of descriptors
91  (in lpc_17xx40xx_emac_config.h) can be increased due to more available
92  IRAM. */
93 
94 /*****************************************************************************
95  * Private types/enumerations/variables
96  ****************************************************************************/
97 
98 /* NETIF data */
99 static struct netif lpc_netif;
100 
101 /*****************************************************************************
102  * Public types/enumerations/variables
103  ****************************************************************************/
104 
105 /*****************************************************************************
106  * Private functions
107  ****************************************************************************/
108 
109 /* Sets up system hardware */
110 static void prvSetupHardware(void)
111 {
112  /* LED0 is used for the link status, on = PHY cable detected */
113  Board_Init();
114 
115  /* Initial LED state is off to show an unconnected cable state */
116  Board_LED_Set(0, false);
117 }
118 
119 /* Callback for TCPIP thread to indicate TCPIP init is done */
120 static void tcpip_init_done_signal(void *arg)
121 {
122  /* Tell main thread TCP/IP init is done */
123  *(s32_t *) arg = 1;
124 }
125 
126 /* LWIP kickoff and PHY link monitor thread */
127 static portTASK_FUNCTION(vSetupIFTask, pvParameters) {
128  ip_addr_t ipaddr, netmask, gw;
129  volatile s32_t tcpipdone = 0;
131  static int prt_ip = 0;
132 
133  /* Wait until the TCP/IP thread is finished before
134  continuing or wierd things may happen */
135  LWIP_DEBUGF(LWIP_DBG_ON, ("Waiting for TCPIP thread to initialize...\n"));
136  tcpip_init(tcpip_init_done_signal, (void *) &tcpipdone);
137  while (!tcpipdone) {
138  msDelay(1);
139  }
140 
141  LWIP_DEBUGF(LWIP_DBG_ON, ("Starting LWIP TCP echo server...\n"));
142 
143  /* Static IP assignment */
144 #if LWIP_DHCP
145  IP4_ADDR(&gw, 0, 0, 0, 0);
146  IP4_ADDR(&ipaddr, 0, 0, 0, 0);
147  IP4_ADDR(&netmask, 0, 0, 0, 0);
148 #else
149  IP4_ADDR(&gw, 10, 1, 10, 1);
150  IP4_ADDR(&ipaddr, 10, 1, 10, 234);
151  IP4_ADDR(&netmask, 255, 255, 255, 0);
152 #endif
153 
154  /* Add netif interface for lpc17xx_8x */
155  if (!netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init,
156  tcpip_input)) {
157  LWIP_ASSERT("Net interface failed to initialize\r\n", 0);
158  }
159  netif_set_default(&lpc_netif);
160  netif_set_up(&lpc_netif);
161 
162  /* Enable MAC interrupts only after LWIP is ready */
163  NVIC_SetPriority(ETHERNET_IRQn, config_ETHERNET_INTERRUPT_PRIORITY);
164  NVIC_EnableIRQ(ETHERNET_IRQn);
165 
166 #if LWIP_DHCP
167  dhcp_start(&lpc_netif);
168 #endif
169 
170  /* Initialize and start application */
171  tcpecho_init();
172 
173  /* This loop monitors the PHY link and will handle cable events
174  via the PHY driver. */
175  while (1) {
176  /* Call the PHY status update state machine once in a while
177  to keep the link status up-to-date */
178  physts = lpcPHYStsPoll();
179 
180  /* Only check for connection state when the PHY status has changed */
181  if (physts & PHY_LINK_CHANGED) {
182  if (physts & PHY_LINK_CONNECTED) {
183  Board_LED_Set(0, true);
184  prt_ip = 0;
185 
186  /* Set interface speed and duplex */
187  if (physts & PHY_LINK_SPEED100) {
189  NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000);
190  }
191  else {
193  NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000);
194  }
195  if (physts & PHY_LINK_FULLDUPLX) {
197  }
198  else {
200  }
201 
202  tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up,
203  (void *) &lpc_netif, 1);
204  }
205  else {
206  Board_LED_Set(0, false);
207  tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down,
208  (void *) &lpc_netif, 1);
209  }
210 
211  DEBUGOUT("Link connect status: %d\r\n", ((physts & PHY_LINK_CONNECTED) != 0));
212 
213  /* Delay for link detection (250mS) */
214  vTaskDelay(configTICK_RATE_HZ / 4);
215  }
216 
217  /* Print IP address info */
218  if (!prt_ip) {
219  if (lpc_netif.ip_addr.addr) {
220  static char tmp_buff[16];
221  DEBUGOUT("IP_ADDR : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.ip_addr, tmp_buff, 16));
222  DEBUGOUT("NET_MASK : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.netmask, tmp_buff, 16));
223  DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.gw, tmp_buff, 16));
224  prt_ip = 1;
225  }
226  }
227  }
228 }
229 
230 /*****************************************************************************
231  * Public functions
232  ****************************************************************************/
233 
241 {
242  vTaskDelay((configTICK_RATE_HZ * ms) / 1000);
243 }
244 
249 int main(void)
250 {
252 
253  /* Add another thread for initializing physical interface. This
254  is delayed from the main LWIP initialization. */
255  xTaskCreate(vSetupIFTask, (signed char *) "SetupIFx",
256  configMINIMAL_STACK_SIZE, NULL, (tskIDLE_PRIORITY + 1UL),
257  (xTaskHandle *) NULL);
258 
259  /* Start the scheduler */
260  vTaskStartScheduler();
261 
262  /* Should never arrive here */
263  return 1;
264 }
265