2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Standard includes. */
\r
57 /* Scheduler includes. */
\r
58 #include "FreeRTOS.h"
\r
64 #include "net/uip.h"
\r
65 #include "net/uip_arp.h"
\r
66 #include "apps/httpd/httpd.h"
\r
67 #include "sys/timer.h"
\r
68 #include "net/clock-arch.h"
\r
70 /* Demo includes. */
\r
71 #include "ParTest.h"
\r
73 /* Hardware driver includes. */
\r
74 #include "mss_ethernet_mac_regs.h"
\r
75 #include "mss_ethernet_mac.h"
\r
77 /* The buffer used by the uIP stack to both receive and send. In this case,
\r
78 because the Ethernet driver has been modified to be zero copy - the uip_buf
\r
79 variable is just a pointer to an Ethernet buffer, and not a buffer in its own
\r
81 extern unsigned char *uip_buf;
\r
83 /* The ARP timer and the periodic timer share a callback function, so the
\r
84 respective timer IDs are used to determine which timer actually expired. These
\r
85 constants are assigned to the timer IDs. */
\r
86 #define uipARP_TIMER 0
\r
87 #define uipPERIODIC_TIMER 1
\r
89 /* The length of the queue used to send events from timers or the Ethernet
\r
90 driver to the uIP stack. */
\r
91 #define uipEVENT_QUEUE_LENGTH 10
\r
93 /* A block time of zero simply means "don't block". */
\r
94 #define uipDONT_BLOCK 0UL
\r
96 /* How long to wait before attempting to connect the MAC again. */
\r
97 #define uipINIT_WAIT ( 100 / portTICK_RATE_MS )
\r
99 /* Shortcut to the header within the Rx buffer. */
\r
100 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
102 /* Standard constant. */
\r
103 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
105 /*-----------------------------------------------------------*/
\r
108 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
110 static void prvSetMACAddress( void );
\r
113 * Perform any uIP initialisation required to ready the stack for http
\r
116 static void prvInitialise_uIP( void );
\r
119 * Handles Ethernet interrupt events.
\r
121 static void prvEMACEventListener( unsigned long ulISREvents );
\r
124 * The callback function that is assigned to both the periodic timer and the
\r
127 static void prvUIPTimerCallback( xTimerHandle xTimer );
\r
130 * Initialise the MAC hardware.
\r
132 static void prvInitEmac( void );
\r
135 * Write data to the Ethener. Note that this actually writes data twice for the
\r
136 * to get around delayed ack issues when communicating with a non real-time
\r
137 * peer (for example, a Windows machine).
\r
139 void vEMACWrite( void );
\r
142 * Port functions required by the uIP stack.
\r
144 clock_time_t clock_time( void );
\r
146 /*-----------------------------------------------------------*/
\r
148 /* The queue used to send TCP/IP events to the uIP stack. */
\r
149 xQueueHandle xEMACEventQueue = NULL;
\r
151 /*-----------------------------------------------------------*/
\r
153 clock_time_t clock_time( void )
\r
155 return xTaskGetTickCount();
\r
157 /*-----------------------------------------------------------*/
\r
159 void vuIP_Task( void *pvParameters )
\r
162 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;
\r
163 long lPacketLength;
\r
165 /* Just to prevent compiler warnings about the unused parameter. */
\r
166 ( void ) pvParameters;
\r
168 /* Initialise the uIP stack, configuring for web server usage. */
\r
169 prvInitialise_uIP();
\r
171 /* Initialise the MAC and PHY. */
\r
176 /* Is there received data ready to be processed? */
\r
177 lPacketLength = MSS_MAC_rx_packet();
\r
179 /* Statements to be executed if data has been received on the Ethernet. */
\r
180 if( ( lPacketLength > 0 ) && ( uip_buf != NULL ) )
\r
182 uip_len = ( u16_t ) lPacketLength;
\r
184 /* Standard uIP loop taken from the uIP manual. */
\r
185 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
190 /* If the above function invocation resulted in data that
\r
191 should be sent out on the network, the global variable
\r
192 uip_len is set to a value > 0. */
\r
199 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
203 /* If the above function invocation resulted in data that
\r
204 should be sent out on the network, the global variable
\r
205 uip_len is set to a value > 0. */
\r
214 /* Clear the RX event latched in ulUIP_Events - if one was latched. */
\r
215 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
218 /* Statements to be executed if the TCP/IP period timer has expired. */
\r
219 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
221 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
223 if( uip_buf != NULL )
\r
225 for( i = 0; i < UIP_CONNS; i++ )
\r
229 /* If the above function invocation resulted in data that
\r
230 should be sent out on the network, the global variable
\r
231 uip_len is set to a value > 0. */
\r
241 /* Statements to be executed if the ARP timer has expired. */
\r
242 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
244 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
248 /* If all latched events have been cleared - block until another event
\r
250 if( ulUIP_Events == pdFALSE )
\r
252 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
253 ulUIP_Events |= ulNewEvent;
\r
257 /*-----------------------------------------------------------*/
\r
259 static void prvSetMACAddress( void )
\r
261 struct uip_eth_addr xAddr;
\r
263 /* Configure the MAC address in the uIP stack. */
\r
264 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
265 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
266 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
267 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
268 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
269 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
270 uip_setethaddr( xAddr );
\r
272 /*-----------------------------------------------------------*/
\r
274 static void prvInitialise_uIP( void )
\r
276 uip_ipaddr_t xIPAddr;
\r
277 xTimerHandle xARPTimer, xPeriodicTimer;
\r
280 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
281 uip_sethostaddr( &xIPAddr );
\r
282 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
283 uip_setnetmask( &xIPAddr );
\r
284 prvSetMACAddress();
\r
287 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
288 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
290 /* Create and start the uIP timers. */
\r
291 xARPTimer = xTimerCreate( ( signed char * ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
292 ( 10000UL / portTICK_RATE_MS ), /* Timer period. */
\r
293 pdTRUE, /* Autor-reload. */
\r
294 ( void * ) uipARP_TIMER,
\r
295 prvUIPTimerCallback
\r
298 xPeriodicTimer = xTimerCreate( ( signed char * ) "PeriodicTimer",
\r
299 ( 500UL / portTICK_RATE_MS ),
\r
300 pdTRUE, /* Autor-reload. */
\r
301 ( void * ) uipPERIODIC_TIMER,
\r
302 prvUIPTimerCallback
\r
305 /* Sanity check that the timers were indeed created. */
\r
306 configASSERT( xARPTimer );
\r
307 configASSERT( xPeriodicTimer );
\r
309 /* These commands will block indefinitely until they succeed, so there is
\r
310 no point in checking their return values. */
\r
311 xTimerStart( xARPTimer, portMAX_DELAY );
\r
312 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
314 /*-----------------------------------------------------------*/
\r
316 static void prvEMACEventListener( unsigned long ulISREvents )
\r
318 long lHigherPriorityTaskWoken = pdFALSE;
\r
319 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;
\r
321 /* Sanity check that the event queue was indeed created. */
\r
322 configASSERT( xEMACEventQueue );
\r
324 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )
\r
326 /* An Ethernet Tx event has occurred. */
\r
327 MSS_MAC_FreeTxBuffers();
\r
330 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )
\r
332 /* An Ethernet Rx event has occurred. */
\r
333 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );
\r
336 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
\r
338 /*-----------------------------------------------------------*/
\r
340 static void prvInitEmac( void )
\r
342 const unsigned char ucPHYAddress = 1;
\r
344 /* Initialise the MAC and PHY hardware. */
\r
345 MSS_MAC_init( ucPHYAddress );
\r
347 /* Register the event listener. The Ethernet interrupt handler will call
\r
348 this listener whenever an Rx or a Tx interrupt occurs. */
\r
349 MSS_MAC_set_callback( ( MSS_MAC_callback_t ) prvEMACEventListener );
\r
351 /* Setup the EMAC and the NVIC for MAC interrupts. */
\r
352 NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
353 NVIC_EnableIRQ( EthernetMAC_IRQn );
\r
355 /*-----------------------------------------------------------*/
\r
357 void vEMACWrite( void )
\r
359 const long lMaxAttempts = 10;
\r
361 const portTickType xShortDelay = ( 5 / portTICK_RATE_MS );
\r
363 /* Try to send data to the Ethernet. Keep trying for a while if data cannot
\r
364 be sent immediately. Note that this will actually cause the data to be sent
\r
365 twice to get around delayed ACK problems when communicating with non real-
\r
366 time TCP/IP stacks (such as a Windows machine). */
\r
367 for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )
\r
369 if( MSS_MAC_tx_packet( uip_len ) != 0 )
\r
375 vTaskDelay( xShortDelay );
\r
379 /*-----------------------------------------------------------*/
\r
381 static void prvUIPTimerCallback( xTimerHandle xTimer )
\r
383 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
384 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
386 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
387 block. As this callback is assigned to both the ARP and Periodic timers, the
\r
388 first thing to do is ascertain which timer it was that actually expired. */
\r
389 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
391 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
394 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
397 default : /* Should not get here. */
\r
401 /*-----------------------------------------------------------*/
\r
403 void vApplicationProcessFormInput( char *pcInputString )
\r
407 /* Only interested in processing form input if this is the IO page. */
\r
408 c = strstr( pcInputString, "io.shtml" );
\r
412 /* Is there a command in the string? */
\r
413 c = strstr( pcInputString, "?" );
\r
416 /* Turn the LED's on or off in accordance with the check box status. */
\r
417 if( strstr( c, "LED0=1" ) != NULL )
\r
419 /* Turn the LEDs on. */
\r
420 vParTestSetLED( 3, 1 );
\r
421 vParTestSetLED( 4, 1 );
\r
425 /* Turn the LEDs off. */
\r
426 vParTestSetLED( 3, 0 );
\r
427 vParTestSetLED( 4, 0 );
\r
432 /* Commands to turn LEDs off are not always explicit. */
\r
433 vParTestSetLED( 3, 0 );
\r
434 vParTestSetLED( 4, 0 );
\r
438 /*-----------------------------------------------------------*/
\r