1 unsigned long ulRxed = 0, ulRxISR = 0, ulTxed = 0, ulTxISR = 0;
\r
3 FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
6 ***************************************************************************
\r
8 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
9 * Complete, revised, and edited pdf reference manuals are also *
\r
12 * Purchasing FreeRTOS documentation will not only help you, by *
\r
13 * ensuring you get running as quickly as possible and with an *
\r
14 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
15 * the FreeRTOS project to continue with its mission of providing *
\r
16 * professional grade, cross platform, de facto standard solutions *
\r
17 * for microcontrollers - completely free of charge! *
\r
19 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
21 * Thank you for using FreeRTOS, and thank you for your support! *
\r
23 ***************************************************************************
\r
26 This file is part of the FreeRTOS distribution.
\r
28 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
29 the terms of the GNU General Public License (version 2) as published by the
\r
30 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
31 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
32 distribute a combined work that includes FreeRTOS without being obliged to
\r
33 provide the source code for proprietary components outside of the FreeRTOS
\r
34 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
35 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
36 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
37 more details. You should have received a copy of the GNU General Public
\r
38 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
39 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
40 by writing to Richard Barry, contact details for whom are available on the
\r
45 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
48 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
51 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
52 licensing and training services.
\r
55 /* Standard includes. */
\r
58 /* Scheduler includes. */
\r
59 #include "FreeRTOS.h"
\r
65 #include "net/uip.h"
\r
66 #include "net/uip_arp.h"
\r
67 #include "apps/httpd/httpd.h"
\r
68 #include "sys/timer.h"
\r
69 #include "net/clock-arch.h"
\r
71 /* Demo includes. */
\r
72 #include "ParTest.h"
\r
74 /* Hardware driver includes. */
\r
75 #include "mss_ethernet_mac_regs.h"
\r
76 #include "mss_ethernet_mac.h"
\r
78 /* The buffer used by the uIP stack to both receive and send. This points to
\r
79 one of the Ethernet buffers when its actually in use. */
\r
80 extern unsigned char *uip_buf;
\r
82 static const unsigned char ucMACAddress[] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };
\r
84 #define uipARP_TIMER 0
\r
85 #define uipPERIODIC_TIMER 1
\r
87 #define uipEVENT_QUEUE_LENGTH 10
\r
89 #define uipETHERNET_RX_EVENT 0x01UL
\r
90 #define uipETHERNET_TX_EVENT 0x02UL
\r
91 #define uipARP_TIMER_EVENT 0x04UL
\r
92 #define uipPERIODIC_TIMER_EVENT 0x08UL
\r
93 #define uipAPPLICATION_SEND_EVENT 0x10UL
\r
95 #define uipDONT_BLOCK 0UL
\r
97 /*-----------------------------------------------------------*/
\r
99 /* How long to wait before attempting to connect the MAC again. */
\r
100 #define uipINIT_WAIT ( 100 / portTICK_RATE_MS )
\r
102 /* Shortcut to the header within the Rx buffer. */
\r
103 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
105 /* Standard constant. */
\r
106 #define uipTOTAL_FRAME_HEADER_SIZE 54
\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 * Handles Ethernet interrupt events.
124 static void prvEMACEventListener( unsigned long ulISREvents );
\r
126 static void prvUIPTimerCallback( xTimerHandle xTimer );
\r
129 * Initialise the MAC hardware.
131 static void prvInitEmac( void );
\r
133 void vEMACWrite( void );
\r
135 long lEMACWaitForLink( void );
\r
138 * Port functions required by the uIP stack.
\r
140 void clock_init( void );
\r
141 clock_time_t clock_time( void );
\r
143 /*-----------------------------------------------------------*/
\r
145 /* The queue used to send TCP/IP events to the uIP stack. */
\r
146 xQueueHandle xEMACEventQueue = NULL;
\r
148 static unsigned long ulUIP_Events = 0UL;
\r
150 /*-----------------------------------------------------------*/
\r
152 void clock_init(void)
\r
154 /* This is done when the scheduler starts. */
\r
156 /*-----------------------------------------------------------*/
\r
158 clock_time_t clock_time( void )
\r
160 return xTaskGetTickCount();
\r
162 /*-----------------------------------------------------------*/
\r
164 void vuIP_Task( void *pvParameters )
\r
167 unsigned long ulNewEvent;
\r
169 ( void ) pvParameters;
\r
171 /* Initialise the uIP stack, configuring for web server usage. */
\r
172 prvInitialise_uIP();
\r
174 /* Initialise the MAC. */
\r
179 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )
\r
181 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
183 /* Is there received data ready to be processed? */
\r
184 uip_len = MSS_MAC_rx_packet();
\r
186 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
\r
189 /* Standard uIP loop taken from the uIP manual. */
\r
190 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
195 /* If the above function invocation resulted in data that
\r
196 should be sent out on the network, the global variable
\r
197 uip_len is set to a value > 0. */
\r
204 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
208 /* If the above function invocation resulted in data that
\r
209 should be sent out on the network, the global variable
\r
210 uip_len is set to a value > 0. */
\r
219 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
221 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
223 for( i = 0; i < UIP_CONNS; i++ )
\r
227 /* If the above function invocation resulted in data that
\r
228 should be sent out on the network, the global variable
\r
229 uip_len is set to a value > 0. */
\r
238 /* Call the ARP timer function every 10 seconds. */
\r
239 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
241 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
245 if( ulUIP_Events == pdFALSE )
\r
247 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
248 ulUIP_Events |= ulNewEvent;
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvSetMACAddress( void )
\r
256 struct uip_eth_addr xAddr;
\r
258 /* Configure the MAC address in the uIP stack. */
\r
259 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
260 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
261 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
262 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
263 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
264 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
265 uip_setethaddr( xAddr );
\r
267 /*-----------------------------------------------------------*/
\r
269 void vApplicationProcessFormInput( char *pcInputString )
\r
273 /* Only interested in processing form input if this is the IO page. */
\r
274 c = strstr( pcInputString, "io.shtml" );
\r
278 /* Is there a command in the string? */
\r
279 c = strstr( pcInputString, "?" );
\r
282 /* Turn the LED's on or off in accordance with the check box status. */
\r
283 if( strstr( c, "LED0=1" ) != NULL )
\r
285 /* Turn the LEDs on. */
\r
286 vParTestSetLED( 7, 1 );
\r
287 vParTestSetLED( 8, 1 );
\r
288 vParTestSetLED( 9, 1 );
\r
289 vParTestSetLED( 10, 1 );
\r
293 /* Turn the LEDs off. */
\r
294 vParTestSetLED( 7, 0 );
\r
295 vParTestSetLED( 8, 0 );
\r
296 vParTestSetLED( 9, 0 );
\r
297 vParTestSetLED( 10, 0 );
\r
302 /* Commands to turn LEDs off are not always explicit. */
\r
303 vParTestSetLED( 7, 0 );
\r
304 vParTestSetLED( 8, 0 );
\r
305 vParTestSetLED( 9, 0 );
\r
306 vParTestSetLED( 10, 0 );
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvInitialise_uIP( void )
\r
314 uip_ipaddr_t xIPAddr;
\r
315 xTimerHandle xARPTimer, xPeriodicTimer;
\r
318 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
319 uip_sethostaddr( &xIPAddr );
\r
320 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
321 uip_setnetmask( &xIPAddr );
\r
322 prvSetMACAddress();
\r
325 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
326 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
328 /* Create and start the uIP timers. */
\r
329 xARPTimer = xTimerCreate( ( const signed char * const ) "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
330 ( 500 / portTICK_RATE_MS ), /* Timer period. */
\r
331 pdTRUE, /* Autor-reload. */
\r
332 ( void * ) uipARP_TIMER,
\r
333 prvUIPTimerCallback
\r
336 xPeriodicTimer = xTimerCreate( ( const signed char * const ) "PeriodicTimer",
\r
337 ( 5000 / portTICK_RATE_MS ),
\r
338 pdTRUE, /* Autor-reload. */
\r
339 ( void * ) uipPERIODIC_TIMER,
\r
340 prvUIPTimerCallback
\r
343 configASSERT( xARPTimer );
\r
344 configASSERT( xPeriodicTimer );
\r
346 xTimerStart( xARPTimer, portMAX_DELAY );
\r
347 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
349 /*-----------------------------------------------------------*/
\r
351 static void prvEMACEventListener( unsigned long ulISREvents )
\r
353 long lHigherPriorityTaskWoken = pdFALSE;
\r
354 unsigned long ulUIPEvents = 0UL;
\r
356 configASSERT( xEMACEventQueue );
\r
358 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )
\r
361 MSS_MAC_TxBufferCompleted();
\r
364 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )
\r
367 /* Wake the uIP task as new data has arrived. */
\r
368 ulUIPEvents |= uipETHERNET_RX_EVENT;
\r
371 if( ulUIPEvents != 0UL )
\r
373 xQueueSendFromISR( xEMACEventQueue, &ulUIPEvents, &lHigherPriorityTaskWoken );
\r
376 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
\r
378 /*-----------------------------------------------------------*/
\r
380 static void prvInitEmac( void )
\r
382 const unsigned char ucPHYAddress = 1;
\r
384 MSS_MAC_init( ucPHYAddress );
\r
386 MSS_MAC_set_callback( prvEMACEventListener );
\r
388 /* Setup the EMAC and the NVIC for MAC interrupts. */
\r
389 NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
390 NVIC_EnableIRQ( EthernetMAC_IRQn );
\r
392 /*-----------------------------------------------------------*/
\r
394 void vEMACWrite( void )
\r
396 const long lMaxAttempts = 10;
\r
398 const portTickType xShortDelay = ( 10 / portTICK_RATE_MS );
\r
400 for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )
\r
402 if( MSS_MAC_tx_packet( uip_len ) != 0 )
\r
409 vTaskDelay( xShortDelay );
\r
413 /*-----------------------------------------------------------*/
\r
415 long lEMACWaitForLink( void )
\r
417 long lReturn = pdFAIL;
\r
418 unsigned long ulStatus;
\r
420 ulStatus = MSS_MAC_link_status();
\r
421 if( ( ulStatus & ( unsigned long ) MSS_MAC_LINK_STATUS_LINK ) != 0UL )
\r
428 /*-----------------------------------------------------------*/
\r
430 static void prvUIPTimerCallback( xTimerHandle xTimer )
\r
432 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
433 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
435 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
437 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
439 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
442 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
445 default : /* Should not get here. */
\r
449 /*-----------------------------------------------------------*/
\r