2 FreeRTOS V7.0.0 - 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. This points to
\r
78 one of the Ethernet buffers when its actually in use. */
\r
79 extern unsigned char *uip_buf;
\r
81 static const unsigned char ucMACAddress[] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };
\r
83 #define uipARP_TIMER 0
\r
84 #define uipPERIODIC_TIMER 1
\r
86 #define uipEVENT_QUEUE_LENGTH 10
\r
88 #define uipETHERNET_RX_EVENT 0x01UL
\r
89 #define uipETHERNET_TX_EVENT 0x02UL
\r
90 #define uipARP_TIMER_EVENT 0x04UL
\r
91 #define uipPERIODIC_TIMER_EVENT 0x08UL
\r
92 #define uipAPPLICATION_SEND_EVENT 0x10UL
\r
94 #define uipDONT_BLOCK 0UL
\r
96 /*-----------------------------------------------------------*/
\r
98 /* How long to wait before attempting to connect the MAC again. */
\r
99 #define uipINIT_WAIT ( 100 / portTICK_RATE_MS )
\r
101 /* Shortcut to the header within the Rx buffer. */
\r
102 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
104 /* Standard constant. */
\r
105 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
107 /*-----------------------------------------------------------*/
\r
110 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
112 static void prvSetMACAddress( void );
\r
115 * Perform any uIP initialisation required to ready the stack for http
\r
118 static void prvInitialise_uIP( void );
\r
121 * Handles Ethernet interrupt events.
123 static void prvEMACEventListener( unsigned long ulISREvents );
\r
125 static void prvUIPTimerCallback( xTimerHandle xTimer );
\r
128 * Initialise the MAC hardware.
130 static void prvInitEmac( void );
\r
132 void vEMACWrite( void );
\r
134 long lEMACWaitForLink( void );
\r
137 * Port functions required by the uIP stack.
\r
139 void clock_init( void );
\r
140 clock_time_t clock_time( void );
\r
142 /*-----------------------------------------------------------*/
\r
144 /* The queue used to send TCP/IP events to the uIP stack. */
\r
145 xQueueHandle xEMACEventQueue = NULL;
\r
147 static unsigned long ulUIP_Events = 0UL;
\r
149 /*-----------------------------------------------------------*/
\r
151 void clock_init(void)
\r
153 /* This is done when the scheduler starts. */
\r
155 /*-----------------------------------------------------------*/
\r
157 clock_time_t clock_time( void )
\r
159 return xTaskGetTickCount();
\r
161 /*-----------------------------------------------------------*/
\r
163 void vuIP_Task( void *pvParameters )
\r
166 unsigned long ulNewEvent;
\r
168 ( void ) pvParameters;
\r
170 /* Initialise the uIP stack, configuring for web server usage. */
\r
171 prvInitialise_uIP();
\r
173 /* Initialise the MAC. */
\r
178 if( ( ulUIP_Events & uipETHERNET_TX_EVENT ) != 0UL )
\r
180 ulUIP_Events &= ~uipETHERNET_TX_EVENT;
\r
181 MSS_MAC_TxBufferCompleted();
\r
184 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )
\r
186 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
188 /* Is there received data ready to be processed? */
\r
189 uip_len = MSS_MAC_rx_packet();
\r
191 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
\r
193 /* Standard uIP loop taken from the uIP manual. */
\r
194 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
199 /* If the above function invocation resulted in data that
\r
200 should be sent out on the network, the global variable
\r
201 uip_len is set to a value > 0. */
\r
208 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
212 /* If the above function invocation resulted in data that
\r
213 should be sent out on the network, the global variable
\r
214 uip_len is set to a value > 0. */
\r
223 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
225 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
227 for( i = 0; i < UIP_CONNS; i++ )
\r
231 /* If the above function invocation resulted in data that
\r
232 should be sent out on the network, the global variable
\r
233 uip_len is set to a value > 0. */
\r
242 /* Call the ARP timer function every 10 seconds. */
\r
243 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
245 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
249 if( ulUIP_Events == pdFALSE )
\r
251 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
252 ulUIP_Events |= ulNewEvent;
\r
256 /*-----------------------------------------------------------*/
\r
258 static void prvSetMACAddress( void )
\r
260 struct uip_eth_addr xAddr;
\r
262 /* Configure the MAC address in the uIP stack. */
\r
263 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
264 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
265 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
266 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
267 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
268 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
269 uip_setethaddr( xAddr );
\r
271 /*-----------------------------------------------------------*/
\r
273 void vApplicationProcessFormInput( char *pcInputString )
\r
277 /* Only interested in processing form input if this is the IO page. */
\r
278 c = strstr( pcInputString, "io.shtml" );
\r
282 /* Is there a command in the string? */
\r
283 c = strstr( pcInputString, "?" );
\r
286 /* Turn the LED's on or off in accordance with the check box status. */
\r
287 if( strstr( c, "LED0=1" ) != NULL )
\r
289 /* Turn the LEDs on. */
\r
290 vParTestSetLED( 7, 1 );
\r
291 vParTestSetLED( 8, 1 );
\r
292 vParTestSetLED( 9, 1 );
\r
293 vParTestSetLED( 10, 1 );
\r
297 /* Turn the LEDs off. */
\r
298 vParTestSetLED( 7, 0 );
\r
299 vParTestSetLED( 8, 0 );
\r
300 vParTestSetLED( 9, 0 );
\r
301 vParTestSetLED( 10, 0 );
\r
306 /* Commands to turn LEDs off are not always explicit. */
\r
307 vParTestSetLED( 7, 0 );
\r
308 vParTestSetLED( 8, 0 );
\r
309 vParTestSetLED( 9, 0 );
\r
310 vParTestSetLED( 10, 0 );
\r
314 /*-----------------------------------------------------------*/
\r
316 static void prvInitialise_uIP( void )
\r
318 uip_ipaddr_t xIPAddr;
\r
319 xTimerHandle xARPTimer, xPeriodicTimer;
\r
322 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
323 uip_sethostaddr( &xIPAddr );
\r
324 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
325 uip_setnetmask( &xIPAddr );
\r
326 prvSetMACAddress();
\r
329 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
330 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
332 /* Create and start the uIP timers. */
\r
333 xARPTimer = xTimerCreate( ( const signed char * const ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
334 ( 500 / portTICK_RATE_MS ), /* Timer period. */
\r
335 pdTRUE, /* Autor-reload. */
\r
336 ( void * ) uipARP_TIMER,
\r
337 prvUIPTimerCallback
\r
340 xPeriodicTimer = xTimerCreate( ( const signed char * const ) "PeriodicTimer",
\r
341 ( 5000 / portTICK_RATE_MS ),
\r
342 pdTRUE, /* Autor-reload. */
\r
343 ( void * ) uipPERIODIC_TIMER,
\r
344 prvUIPTimerCallback
\r
347 configASSERT( xARPTimer );
\r
348 configASSERT( xPeriodicTimer );
\r
350 xTimerStart( xARPTimer, portMAX_DELAY );
\r
351 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
353 /*-----------------------------------------------------------*/
\r
355 static void prvEMACEventListener( unsigned long ulISREvents )
\r
357 long lHigherPriorityTaskWoken = pdFALSE;
\r
358 unsigned long ulUIPEvents = 0UL;
\r
360 configASSERT( xEMACEventQueue );
\r
362 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )
\r
364 ulUIP_Events |= uipETHERNET_TX_EVENT;
\r
367 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )
\r
369 /* Wake the uIP task as new data has arrived. */
\r
370 ulUIPEvents |= uipETHERNET_RX_EVENT;
\r
373 if( ulUIPEvents != 0UL )
\r
375 xQueueSendFromISR( xEMACEventQueue, &ulUIPEvents, &lHigherPriorityTaskWoken );
\r
378 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
\r
380 /*-----------------------------------------------------------*/
\r
382 static void prvInitEmac( void )
\r
384 const unsigned char ucPHYAddress = 1;
\r
386 MSS_MAC_init( ucPHYAddress );
\r
388 MSS_MAC_set_callback( prvEMACEventListener );
\r
390 /* Setup the EMAC and the NVIC for MAC interrupts. */
\r
391 NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
392 NVIC_EnableIRQ( EthernetMAC_IRQn );
\r
394 /*-----------------------------------------------------------*/
\r
396 void vEMACWrite( void )
\r
398 const long lMaxAttempts = 10;
\r
400 const portTickType xShortDelay = ( 10 / portTICK_RATE_MS );
\r
402 for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )
\r
404 if( MSS_MAC_tx_packet( uip_len ) != 0 )
\r
410 vTaskDelay( xShortDelay );
\r
414 /*-----------------------------------------------------------*/
\r
416 long lEMACWaitForLink( void )
\r
418 long lReturn = pdFAIL;
\r
419 unsigned long ulStatus;
\r
421 ulStatus = MSS_MAC_link_status();
\r
422 if( ( ulStatus & ( unsigned long ) MSS_MAC_LINK_STATUS_LINK ) != 0UL )
\r
429 /*-----------------------------------------------------------*/
\r
431 static void prvUIPTimerCallback( xTimerHandle xTimer )
\r
433 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
434 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
436 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
438 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
440 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
443 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
446 default : /* Should not get here. */
\r
450 /*-----------------------------------------------------------*/
\r