2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* Standard includes. */
\r
68 /* Scheduler includes. */
\r
69 #include "FreeRTOS.h"
\r
75 #include "net/uip.h"
\r
76 #include "net/uip_arp.h"
\r
77 #include "apps/httpd/httpd.h"
\r
78 #include "sys/timer.h"
\r
79 #include "net/clock-arch.h"
\r
81 /* Demo includes. */
\r
82 #include "ParTest.h"
\r
84 /* Hardware driver includes. */
\r
85 #include "mss_ethernet_mac_regs.h"
\r
86 #include "mss_ethernet_mac.h"
\r
88 /* The buffer used by the uIP stack to both receive and send. In this case,
\r
89 because the Ethernet driver has been modified to be zero copy - the uip_buf
\r
90 variable is just a pointer to an Ethernet buffer, and not a buffer in its own
\r
92 extern unsigned char *uip_buf;
\r
94 /* The ARP timer and the periodic timer share a callback function, so the
\r
95 respective timer IDs are used to determine which timer actually expired. These
\r
96 constants are assigned to the timer IDs. */
\r
97 #define uipARP_TIMER 0
\r
98 #define uipPERIODIC_TIMER 1
\r
100 /* The length of the queue used to send events from timers or the Ethernet
\r
101 driver to the uIP stack. */
\r
102 #define uipEVENT_QUEUE_LENGTH 10
\r
104 /* A block time of zero simply means "don't block". */
\r
105 #define uipDONT_BLOCK 0UL
\r
107 /* How long to wait before attempting to connect the MAC again. */
\r
108 #define uipINIT_WAIT ( 100 / portTICK_RATE_MS )
\r
110 /* Shortcut to the header within the Rx buffer. */
\r
111 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
113 /* Standard constant. */
\r
114 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
116 /*-----------------------------------------------------------*/
\r
119 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
121 static void prvSetMACAddress( void );
\r
124 * Perform any uIP initialisation required to ready the stack for http
\r
127 static void prvInitialise_uIP( void );
\r
130 * Handles Ethernet interrupt events.
\r
132 static void prvEMACEventListener( unsigned long ulISREvents );
\r
135 * The callback function that is assigned to both the periodic timer and the
\r
138 static void prvUIPTimerCallback( xTimerHandle xTimer );
\r
141 * Initialise the MAC hardware.
\r
143 static void prvInitEmac( void );
\r
146 * Write data to the Ethener. Note that this actually writes data twice for the
\r
147 * to get around delayed ack issues when communicating with a non real-time
\r
148 * peer (for example, a Windows machine).
\r
150 void vEMACWrite( void );
\r
153 * Port functions required by the uIP stack.
\r
155 clock_time_t clock_time( void );
\r
157 /*-----------------------------------------------------------*/
\r
159 /* The queue used to send TCP/IP events to the uIP stack. */
\r
160 xQueueHandle xEMACEventQueue = NULL;
\r
162 /*-----------------------------------------------------------*/
\r
164 clock_time_t clock_time( void )
\r
166 return xTaskGetTickCount();
\r
168 /*-----------------------------------------------------------*/
\r
170 void vuIP_Task( void *pvParameters )
\r
173 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;
\r
174 long lPacketLength;
\r
176 /* Just to prevent compiler warnings about the unused parameter. */
\r
177 ( void ) pvParameters;
\r
179 /* Initialise the uIP stack, configuring for web server usage. */
\r
180 prvInitialise_uIP();
\r
182 /* Initialise the MAC and PHY. */
\r
187 /* Is there received data ready to be processed? */
\r
188 lPacketLength = MSS_MAC_rx_packet();
\r
190 /* Statements to be executed if data has been received on the Ethernet. */
\r
191 if( ( lPacketLength > 0 ) && ( uip_buf != NULL ) )
\r
193 uip_len = ( u16_t ) lPacketLength;
\r
195 /* Standard uIP loop taken from the uIP manual. */
\r
196 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
201 /* If the above function invocation resulted in data that
\r
202 should be sent out on the network, the global variable
\r
203 uip_len is set to a value > 0. */
\r
210 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\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
225 /* Clear the RX event latched in ulUIP_Events - if one was latched. */
\r
226 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
229 /* Statements to be executed if the TCP/IP period timer has expired. */
\r
230 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
232 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
234 if( uip_buf != NULL )
\r
236 for( i = 0; i < UIP_CONNS; i++ )
\r
240 /* If the above function invocation resulted in data that
\r
241 should be sent out on the network, the global variable
\r
242 uip_len is set to a value > 0. */
\r
252 /* Statements to be executed if the ARP timer has expired. */
\r
253 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
255 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
259 /* If all latched events have been cleared - block until another event
\r
261 if( ulUIP_Events == pdFALSE )
\r
263 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
264 ulUIP_Events |= ulNewEvent;
\r
268 /*-----------------------------------------------------------*/
\r
270 static void prvSetMACAddress( void )
\r
272 struct uip_eth_addr xAddr;
\r
274 /* Configure the MAC address in the uIP stack. */
\r
275 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
276 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
277 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
278 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
279 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
280 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
281 uip_setethaddr( xAddr );
\r
283 /*-----------------------------------------------------------*/
\r
285 static void prvInitialise_uIP( void )
\r
287 uip_ipaddr_t xIPAddr;
\r
288 xTimerHandle xARPTimer, xPeriodicTimer;
\r
291 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
292 uip_sethostaddr( &xIPAddr );
\r
293 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
294 uip_setnetmask( &xIPAddr );
\r
295 prvSetMACAddress();
\r
298 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
299 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
301 /* Create and start the uIP timers. */
\r
302 xARPTimer = xTimerCreate( ( signed char * ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
303 ( 10000UL / portTICK_RATE_MS ), /* Timer period. */
\r
304 pdTRUE, /* Autor-reload. */
\r
305 ( void * ) uipARP_TIMER,
\r
306 prvUIPTimerCallback
\r
309 xPeriodicTimer = xTimerCreate( ( signed char * ) "PeriodicTimer",
\r
310 ( 500UL / portTICK_RATE_MS ),
\r
311 pdTRUE, /* Autor-reload. */
\r
312 ( void * ) uipPERIODIC_TIMER,
\r
313 prvUIPTimerCallback
\r
316 /* Sanity check that the timers were indeed created. */
\r
317 configASSERT( xARPTimer );
\r
318 configASSERT( xPeriodicTimer );
\r
320 /* These commands will block indefinitely until they succeed, so there is
\r
321 no point in checking their return values. */
\r
322 xTimerStart( xARPTimer, portMAX_DELAY );
\r
323 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
325 /*-----------------------------------------------------------*/
\r
327 static void prvEMACEventListener( unsigned long ulISREvents )
\r
329 long lHigherPriorityTaskWoken = pdFALSE;
\r
330 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;
\r
332 /* Sanity check that the event queue was indeed created. */
\r
333 configASSERT( xEMACEventQueue );
\r
335 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )
\r
337 /* An Ethernet Tx event has occurred. */
\r
338 MSS_MAC_FreeTxBuffers();
\r
341 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )
\r
343 /* An Ethernet Rx event has occurred. */
\r
344 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );
\r
347 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
\r
349 /*-----------------------------------------------------------*/
\r
351 static void prvInitEmac( void )
\r
353 const unsigned char ucPHYAddress = 1;
\r
355 /* Initialise the MAC and PHY hardware. */
\r
356 MSS_MAC_init( ucPHYAddress );
\r
358 /* Register the event listener. The Ethernet interrupt handler will call
\r
359 this listener whenever an Rx or a Tx interrupt occurs. */
\r
360 MSS_MAC_set_callback( ( MSS_MAC_callback_t ) prvEMACEventListener );
\r
362 /* Setup the EMAC and the NVIC for MAC interrupts. */
\r
363 NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
364 NVIC_EnableIRQ( EthernetMAC_IRQn );
\r
366 /*-----------------------------------------------------------*/
\r
368 void vEMACWrite( void )
\r
370 const long lMaxAttempts = 10;
\r
372 const portTickType xShortDelay = ( 5 / portTICK_RATE_MS );
\r
374 /* Try to send data to the Ethernet. Keep trying for a while if data cannot
\r
375 be sent immediately. Note that this will actually cause the data to be sent
\r
376 twice to get around delayed ACK problems when communicating with non real-
\r
377 time TCP/IP stacks (such as a Windows machine). */
\r
378 for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )
\r
380 if( MSS_MAC_tx_packet( uip_len ) != 0 )
\r
386 vTaskDelay( xShortDelay );
\r
390 /*-----------------------------------------------------------*/
\r
392 static void prvUIPTimerCallback( xTimerHandle xTimer )
\r
394 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
395 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
397 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
398 block. As this callback is assigned to both the ARP and Periodic timers, the
\r
399 first thing to do is ascertain which timer it was that actually expired. */
\r
400 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
402 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
405 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
408 default : /* Should not get here. */
\r
412 /*-----------------------------------------------------------*/
\r
414 void vApplicationProcessFormInput( char *pcInputString )
\r
418 /* Only interested in processing form input if this is the IO page. */
\r
419 c = strstr( pcInputString, "io.shtml" );
\r
423 /* Is there a command in the string? */
\r
424 c = strstr( pcInputString, "?" );
\r
427 /* Turn the LED's on or off in accordance with the check box status. */
\r
428 if( strstr( c, "LED0=1" ) != NULL )
\r
430 /* Turn the LEDs on. */
\r
431 vParTestSetLED( 3, 1 );
\r
432 vParTestSetLED( 4, 1 );
\r
436 /* Turn the LEDs off. */
\r
437 vParTestSetLED( 3, 0 );
\r
438 vParTestSetLED( 4, 0 );
\r
443 /* Commands to turn LEDs off are not always explicit. */
\r
444 vParTestSetLED( 3, 0 );
\r
445 vParTestSetLED( 4, 0 );
\r
449 /*-----------------------------------------------------------*/
\r