2 FreeRTOS V8.0.1 - 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
82 /* Demo includes. */
\r
83 #include "ParTest.h"
\r
85 /* Hardware driver includes. */
\r
86 #include "mss_ethernet_mac_regs.h"
\r
87 #include "mss_ethernet_mac.h"
\r
89 /* The buffer used by the uIP stack to both receive and send. In this case,
\r
90 because the Ethernet driver has been modified to be zero copy - the uip_buf
\r
91 variable is just a pointer to an Ethernet buffer, and not a buffer in its own
\r
93 extern unsigned char *uip_buf;
\r
95 /* The ARP timer and the periodic timer share a callback function, so the
\r
96 respective timer IDs are used to determine which timer actually expired. These
\r
97 constants are assigned to the timer IDs. */
\r
98 #define uipARP_TIMER 0
\r
99 #define uipPERIODIC_TIMER 1
\r
101 /* The length of the queue used to send events from timers or the Ethernet
\r
102 driver to the uIP stack. */
\r
103 #define uipEVENT_QUEUE_LENGTH 10
\r
105 /* A block time of zero simply means "don't block". */
\r
106 #define uipDONT_BLOCK 0UL
\r
108 /* How long to wait before attempting to connect the MAC again. */
\r
109 #define uipINIT_WAIT ( 100 / portTICK_PERIOD_MS )
\r
111 /* Shortcut to the header within the Rx buffer. */
\r
112 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
114 /* Standard constant. */
\r
115 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
117 /*-----------------------------------------------------------*/
\r
120 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
122 static void prvSetMACAddress( void );
\r
125 * Perform any uIP initialisation required to ready the stack for http
\r
128 static void prvInitialise_uIP( void );
\r
131 * Handles Ethernet interrupt events.
\r
133 static void prvEMACEventListener( unsigned long ulISREvents );
\r
136 * The callback function that is assigned to both the periodic timer and the
\r
139 static void prvUIPTimerCallback( TimerHandle_t xTimer );
\r
142 * Initialise the MAC hardware.
\r
144 static void prvInitEmac( void );
\r
147 * Write data to the Ethener. Note that this actually writes data twice for the
\r
148 * to get around delayed ack issues when communicating with a non real-time
\r
149 * peer (for example, a Windows machine).
\r
151 void vEMACWrite( void );
\r
154 * Port functions required by the uIP stack.
\r
156 clock_time_t clock_time( void );
\r
158 /*-----------------------------------------------------------*/
\r
160 /* The queue used to send TCP/IP events to the uIP stack. */
\r
161 QueueHandle_t xEMACEventQueue = NULL;
\r
163 /*-----------------------------------------------------------*/
\r
165 clock_time_t clock_time( void )
\r
167 return xTaskGetTickCount();
\r
169 /*-----------------------------------------------------------*/
\r
171 void vuIP_Task( void *pvParameters )
\r
174 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;
\r
175 long lPacketLength;
\r
177 /* Just to prevent compiler warnings about the unused parameter. */
\r
178 ( void ) pvParameters;
\r
180 /* Initialise the uIP stack, configuring for web server usage. */
\r
181 prvInitialise_uIP();
\r
183 /* Initialise the MAC and PHY. */
\r
188 /* Is there received data ready to be processed? */
\r
189 lPacketLength = MSS_MAC_rx_packet();
\r
191 /* Statements to be executed if data has been received on the Ethernet. */
\r
192 if( ( lPacketLength > 0 ) && ( uip_buf != NULL ) )
\r
194 uip_len = ( u16_t ) lPacketLength;
\r
196 /* Standard uIP loop taken from the uIP manual. */
\r
197 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
202 /* If the above function invocation resulted in data that
\r
203 should be sent out on the network, the global variable
\r
204 uip_len is set to a value > 0. */
\r
211 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
215 /* If the above function invocation resulted in data that
\r
216 should be sent out on the network, the global variable
\r
217 uip_len is set to a value > 0. */
\r
226 /* Clear the RX event latched in ulUIP_Events - if one was latched. */
\r
227 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
230 /* Statements to be executed if the TCP/IP period timer has expired. */
\r
231 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
233 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
235 if( uip_buf != NULL )
\r
237 for( i = 0; i < UIP_CONNS; i++ )
\r
241 /* If the above function invocation resulted in data that
\r
242 should be sent out on the network, the global variable
\r
243 uip_len is set to a value > 0. */
\r
253 /* Statements to be executed if the ARP timer has expired. */
\r
254 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
256 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
260 /* If all latched events have been cleared - block until another event
\r
262 if( ulUIP_Events == pdFALSE )
\r
264 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
265 ulUIP_Events |= ulNewEvent;
\r
269 /*-----------------------------------------------------------*/
\r
271 static void prvSetMACAddress( void )
\r
273 struct uip_eth_addr xAddr;
\r
275 /* Configure the MAC address in the uIP stack. */
\r
276 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
277 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
278 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
279 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
280 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
281 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
282 uip_setethaddr( xAddr );
\r
284 /*-----------------------------------------------------------*/
\r
286 static void prvInitialise_uIP( void )
\r
288 uip_ipaddr_t xIPAddr;
\r
289 TimerHandle_t xARPTimer, xPeriodicTimer;
\r
292 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
293 uip_sethostaddr( &xIPAddr );
\r
294 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
295 uip_setnetmask( &xIPAddr );
\r
296 prvSetMACAddress();
\r
299 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
300 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
302 /* Create and start the uIP timers. */
\r
303 xARPTimer = xTimerCreate( "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
304 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */
\r
305 pdTRUE, /* Autor-reload. */
\r
306 ( void * ) uipARP_TIMER,
\r
307 prvUIPTimerCallback
\r
310 xPeriodicTimer = xTimerCreate( "PeriodicTimer",
\r
311 ( 500UL / portTICK_PERIOD_MS ),
\r
312 pdTRUE, /* Autor-reload. */
\r
313 ( void * ) uipPERIODIC_TIMER,
\r
314 prvUIPTimerCallback
\r
317 /* Sanity check that the timers were indeed created. */
\r
318 configASSERT( xARPTimer );
\r
319 configASSERT( xPeriodicTimer );
\r
321 /* These commands will block indefinitely until they succeed, so there is
\r
322 no point in checking their return values. */
\r
323 xTimerStart( xARPTimer, portMAX_DELAY );
\r
324 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
326 /*-----------------------------------------------------------*/
\r
328 static void prvEMACEventListener( unsigned long ulISREvents )
\r
330 long lHigherPriorityTaskWoken = pdFALSE;
\r
331 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;
\r
333 /* Sanity check that the event queue was indeed created. */
\r
334 configASSERT( xEMACEventQueue );
\r
336 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )
\r
338 /* An Ethernet Tx event has occurred. */
\r
339 MSS_MAC_FreeTxBuffers();
\r
342 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )
\r
344 /* An Ethernet Rx event has occurred. */
\r
345 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );
\r
348 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
\r
350 /*-----------------------------------------------------------*/
\r
352 static void prvInitEmac( void )
\r
354 const unsigned char ucPHYAddress = 1;
\r
356 /* Initialise the MAC and PHY hardware. */
\r
357 MSS_MAC_init( ucPHYAddress );
\r
359 /* Register the event listener. The Ethernet interrupt handler will call
\r
360 this listener whenever an Rx or a Tx interrupt occurs. */
\r
361 MSS_MAC_set_callback( ( MSS_MAC_callback_t ) prvEMACEventListener );
\r
363 /* Setup the EMAC and the NVIC for MAC interrupts. */
\r
364 NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
365 NVIC_EnableIRQ( EthernetMAC_IRQn );
\r
367 /*-----------------------------------------------------------*/
\r
369 void vEMACWrite( void )
\r
371 const long lMaxAttempts = 10;
\r
373 const TickType_t xShortDelay = ( 5 / portTICK_PERIOD_MS );
\r
375 /* Try to send data to the Ethernet. Keep trying for a while if data cannot
\r
376 be sent immediately. Note that this will actually cause the data to be sent
\r
377 twice to get around delayed ACK problems when communicating with non real-
\r
378 time TCP/IP stacks (such as a Windows machine). */
\r
379 for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )
\r
381 if( MSS_MAC_tx_packet( uip_len ) != 0 )
\r
387 vTaskDelay( xShortDelay );
\r
391 /*-----------------------------------------------------------*/
\r
393 static void prvUIPTimerCallback( TimerHandle_t xTimer )
\r
395 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
396 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
398 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
399 block. As this callback is assigned to both the ARP and Periodic timers, the
\r
400 first thing to do is ascertain which timer it was that actually expired. */
\r
401 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
403 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
406 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
409 default : /* Should not get here. */
\r
413 /*-----------------------------------------------------------*/
\r
415 void vApplicationProcessFormInput( char *pcInputString )
\r
419 /* Only interested in processing form input if this is the IO page. */
\r
420 c = strstr( pcInputString, "io.shtml" );
\r
424 /* Is there a command in the string? */
\r
425 c = strstr( pcInputString, "?" );
\r
428 /* Turn the LED's on or off in accordance with the check box status. */
\r
429 if( strstr( c, "LED0=1" ) != NULL )
\r
431 /* Turn the LEDs on. */
\r
432 vParTestSetLED( 3, 1 );
\r
433 vParTestSetLED( 4, 1 );
\r
437 /* Turn the LEDs off. */
\r
438 vParTestSetLED( 3, 0 );
\r
439 vParTestSetLED( 4, 0 );
\r
444 /* Commands to turn LEDs off are not always explicit. */
\r
445 vParTestSetLED( 3, 0 );
\r
446 vParTestSetLED( 4, 0 );
\r
450 /*-----------------------------------------------------------*/
\r