2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* Standard includes. */
\r
69 /* Scheduler includes. */
\r
70 #include "FreeRTOS.h"
\r
76 #include "net/uip.h"
\r
77 #include "net/uip_arp.h"
\r
78 #include "apps/httpd/httpd.h"
\r
79 #include "sys/timer.h"
\r
80 #include "net/clock-arch.h"
\r
83 /* Demo includes. */
\r
84 #include "ParTest.h"
\r
86 /* The buffer used by the uIP stack to both receive and send. In this case,
\r
87 because the Ethernet driver is implemented to be zero copy - the uip_buf
\r
88 variable is just a pointer to an Ethernet buffer, and not a buffer in its own
\r
90 extern unsigned char *uip_buf;
\r
92 /* The ARP timer and the periodic timer share a callback function, so the
\r
93 respective timer IDs are used to determine which timer actually expired. These
\r
94 constants are assigned to the timer IDs. */
\r
95 #define uipARP_TIMER 0
\r
96 #define uipPERIODIC_TIMER 1
\r
98 /* The length of the queue used to send events from timers or the Ethernet
\r
99 driver to the uIP stack. */
\r
100 #define uipEVENT_QUEUE_LENGTH 10
\r
102 /* A block time of zero simply means "don't block". */
\r
103 #define uipDONT_BLOCK 0UL
\r
105 /* Shortcut to the header within the Rx buffer. */
\r
106 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
108 /*-----------------------------------------------------------*/
\r
111 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
113 static void prvSetMACAddress( void );
\r
116 * Perform any uIP initialisation required to ready the stack for http
\r
119 static void prvInitialise_uIP( void );
\r
122 * The callback function that is assigned to both the periodic timer and the
\r
125 static void prvUIPTimerCallback( TimerHandle_t xTimer );
\r
128 * Port functions required by the uIP stack.
\r
130 clock_time_t clock_time( void );
\r
132 /*-----------------------------------------------------------*/
\r
134 /* The queue used to send TCP/IP events to the uIP stack. */
\r
135 QueueHandle_t xEMACEventQueue = NULL;
\r
137 /*-----------------------------------------------------------*/
\r
139 clock_time_t clock_time( void )
\r
141 return xTaskGetTickCount();
\r
143 /*-----------------------------------------------------------*/
\r
145 void vuIP_Task( void *pvParameters )
\r
148 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;
\r
149 unsigned short usPacketLength;
\r
151 /* Just to prevent compiler warnings about the unused parameter. */
\r
152 ( void ) pvParameters;
\r
154 /* Initialise the uIP stack, configuring for web server usage. */
\r
155 prvInitialise_uIP();
\r
157 /* Initialise the MAC and PHY. */
\r
162 /* Is there received data ready to be processed? */
\r
163 usPacketLength = usEMACRead();
\r
165 /* Statements to be executed if data has been received on the Ethernet. */
\r
166 if( ( usPacketLength > 0U ) && ( uip_buf != NULL ) )
\r
168 uip_len = usPacketLength;
\r
170 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
175 /* If the above function invocation resulted in data that
\r
176 should be sent out on the network, the global variable
\r
177 uip_len is set to a value > 0. */
\r
184 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
188 /* If the above function invocation resulted in data that
\r
189 should be sent out on the network, the global variable
\r
190 uip_len is set to a value > 0. */
\r
199 /* Clear the RX event latched in ulUIP_Events - if one was latched. */
\r
200 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
203 /* Statements to be executed if the TCP/IP period timer has expired. */
\r
204 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
206 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
208 if( uip_buf != NULL )
\r
210 for( i = 0; i < UIP_CONNS; i++ )
\r
214 /* If the above function invocation resulted in data that
\r
215 should be sent out on the network, the global variable
\r
216 uip_len is set to a value > 0. */
\r
226 /* Statements to be executed if the ARP timer has expired. */
\r
227 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
229 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
233 /* If all latched events have been cleared - block until another event
\r
235 if( ulUIP_Events == pdFALSE )
\r
237 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
238 ulUIP_Events |= ulNewEvent;
\r
242 /*-----------------------------------------------------------*/
\r
244 static void prvSetMACAddress( void )
\r
246 struct uip_eth_addr xAddr;
\r
248 /* Configure the MAC address in the uIP stack. */
\r
249 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
250 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
251 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
252 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
253 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
254 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
255 uip_setethaddr( xAddr );
\r
257 /*-----------------------------------------------------------*/
\r
259 static void prvInitialise_uIP( void )
\r
261 uip_ipaddr_t xIPAddr;
\r
262 TimerHandle_t xARPTimer, xPeriodicTimer;
\r
265 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
266 uip_sethostaddr( &xIPAddr );
\r
267 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
268 uip_setnetmask( &xIPAddr );
\r
269 prvSetMACAddress();
\r
272 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
273 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
275 /* Create and start the uIP timers. */
\r
276 xARPTimer = xTimerCreate( "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
277 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */
\r
278 pdTRUE, /* Autor-reload. */
\r
279 ( void * ) uipARP_TIMER,
\r
280 prvUIPTimerCallback
\r
283 xPeriodicTimer = xTimerCreate( "PeriodicTimer",
\r
284 ( 500UL / portTICK_PERIOD_MS ),
\r
285 pdTRUE, /* Autor-reload. */
\r
286 ( void * ) uipPERIODIC_TIMER,
\r
287 prvUIPTimerCallback
\r
290 /* Sanity check that the timers were indeed created. */
\r
291 configASSERT( xARPTimer );
\r
292 configASSERT( xPeriodicTimer );
\r
293 configASSERT( xEMACEventQueue );
\r
295 /* These commands will block indefinitely until they succeed, so there is
\r
296 no point in checking their return values. */
\r
297 xTimerStart( xARPTimer, portMAX_DELAY );
\r
298 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvUIPTimerCallback( TimerHandle_t xTimer )
\r
304 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
305 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
307 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
308 block. As this callback is assigned to both the ARP and Periodic timers, the
\r
309 first thing to do is ascertain which timer it was that actually expired. */
\r
310 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
312 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
315 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
318 default : /* Should not get here. */
\r
322 /*-----------------------------------------------------------*/
\r
324 void vApplicationProcessFormInput( char *pcInputString )
\r
327 const unsigned long ulYellowLED = 2UL;
\r
329 /* Only interested in processing form input if this is the IO page. */
\r
330 c = strstr( pcInputString, "io.shtml" );
\r
334 /* Is there a command in the string? */
\r
335 c = strstr( pcInputString, "?" );
\r
338 /* Turn the LEDs on or off in accordance with the check box status. */
\r
339 if( strstr( c, "LED0=1" ) != NULL )
\r
341 /* Turn the LEDs on. */
\r
342 vParTestSetLED( ulYellowLED, pdTRUE );
\r
346 /* Turn the LEDs off. */
\r
347 vParTestSetLED( ulYellowLED, pdFALSE );
\r
352 /* Some browsers will only imply that a check box is off. */
\r
353 vParTestSetLED( ulYellowLED, pdFALSE );
\r
357 /*-----------------------------------------------------------*/
\r