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