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