2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* Standard includes. */
\r
73 /* Scheduler includes. */
\r
74 #include "FreeRTOS.h"
\r
80 #include "net/uip.h"
\r
81 #include "net/uip_arp.h"
\r
82 #include "apps/httpd/httpd.h"
\r
83 #include "sys/timer.h"
\r
84 #include "net/clock-arch.h"
\r
86 /* Demo includes. */
\r
87 #include "ParTest.h"
\r
89 /* Hardware driver includes. */
\r
90 #include "mss_ethernet_mac_regs.h"
\r
91 #include "mss_ethernet_mac.h"
\r
93 /* The buffer used by the uIP stack to both receive and send. In this case,
\r
94 because the Ethernet driver has been modified to be zero copy - the uip_buf
\r
95 variable is just a pointer to an Ethernet buffer, and not a buffer in its own
\r
97 extern unsigned char *uip_buf;
\r
99 /* The ARP timer and the periodic timer share a callback function, so the
\r
100 respective timer IDs are used to determine which timer actually expired. These
\r
101 constants are assigned to the timer IDs. */
\r
102 #define uipARP_TIMER 0
\r
103 #define uipPERIODIC_TIMER 1
\r
105 /* The length of the queue used to send events from timers or the Ethernet
\r
106 driver to the uIP stack. */
\r
107 #define uipEVENT_QUEUE_LENGTH 10
\r
109 /* A block time of zero simply means "don't block". */
\r
110 #define uipDONT_BLOCK 0UL
\r
112 /* How long to wait before attempting to connect the MAC again. */
\r
113 #define uipINIT_WAIT ( 100 / portTICK_PERIOD_MS )
\r
115 /* Shortcut to the header within the Rx buffer. */
\r
116 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
118 /* Standard constant. */
\r
119 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
121 /*-----------------------------------------------------------*/
\r
124 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
126 static void prvSetMACAddress( void );
\r
129 * Perform any uIP initialisation required to ready the stack for http
\r
132 static void prvInitialise_uIP( void );
\r
135 * Handles Ethernet interrupt events.
\r
137 static void prvEMACEventListener( unsigned long ulISREvents );
\r
140 * The callback function that is assigned to both the periodic timer and the
\r
143 static void prvUIPTimerCallback( TimerHandle_t xTimer );
\r
146 * Initialise the MAC hardware.
\r
148 static void prvInitEmac( void );
\r
151 * Write data to the Ethener. Note that this actually writes data twice for the
\r
152 * to get around delayed ack issues when communicating with a non real-time
\r
153 * peer (for example, a Windows machine).
\r
155 void vEMACWrite( void );
\r
158 * Port functions required by the uIP stack.
\r
160 clock_time_t clock_time( void );
\r
162 /*-----------------------------------------------------------*/
\r
164 /* The queue used to send TCP/IP events to the uIP stack. */
\r
165 QueueHandle_t xEMACEventQueue = NULL;
\r
167 /*-----------------------------------------------------------*/
\r
169 clock_time_t clock_time( void )
\r
171 return xTaskGetTickCount();
\r
173 /*-----------------------------------------------------------*/
\r
175 void vuIP_Task( void *pvParameters )
\r
178 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;
\r
179 long lPacketLength;
\r
181 /* Just to prevent compiler warnings about the unused parameter. */
\r
182 ( void ) pvParameters;
\r
184 /* Initialise the uIP stack, configuring for web server usage. */
\r
185 prvInitialise_uIP();
\r
187 /* Initialise the MAC and PHY. */
\r
192 /* Is there received data ready to be processed? */
\r
193 lPacketLength = MSS_MAC_rx_packet();
\r
195 /* Statements to be executed if data has been received on the Ethernet. */
\r
196 if( ( lPacketLength > 0 ) && ( uip_buf != NULL ) )
\r
198 uip_len = ( u16_t ) lPacketLength;
\r
200 /* Standard uIP loop taken from the uIP manual. */
\r
201 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
206 /* If the above function invocation resulted in data that
\r
207 should be sent out on the network, the global variable
\r
208 uip_len is set to a value > 0. */
\r
215 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
219 /* If the above function invocation resulted in data that
\r
220 should be sent out on the network, the global variable
\r
221 uip_len is set to a value > 0. */
\r
230 /* Clear the RX event latched in ulUIP_Events - if one was latched. */
\r
231 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
234 /* Statements to be executed if the TCP/IP period timer has expired. */
\r
235 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
237 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
239 if( uip_buf != NULL )
\r
241 for( i = 0; i < UIP_CONNS; i++ )
\r
245 /* If the above function invocation resulted in data that
\r
246 should be sent out on the network, the global variable
\r
247 uip_len is set to a value > 0. */
\r
257 /* Statements to be executed if the ARP timer has expired. */
\r
258 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
260 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
264 /* If all latched events have been cleared - block until another event
\r
266 if( ulUIP_Events == pdFALSE )
\r
268 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
269 ulUIP_Events |= ulNewEvent;
\r
273 /*-----------------------------------------------------------*/
\r
275 static void prvSetMACAddress( void )
\r
277 struct uip_eth_addr xAddr;
\r
279 /* Configure the MAC address in the uIP stack. */
\r
280 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
281 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
282 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
283 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
284 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
285 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
286 uip_setethaddr( xAddr );
\r
288 /*-----------------------------------------------------------*/
\r
290 static void prvInitialise_uIP( void )
\r
292 uip_ipaddr_t xIPAddr;
\r
293 TimerHandle_t xARPTimer, xPeriodicTimer;
\r
296 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
297 uip_sethostaddr( &xIPAddr );
\r
298 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
299 uip_setnetmask( &xIPAddr );
\r
300 prvSetMACAddress();
\r
303 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
304 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
306 /* Create and start the uIP timers. */
\r
307 xARPTimer = xTimerCreate( "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
308 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */
\r
309 pdTRUE, /* Autor-reload. */
\r
310 ( void * ) uipARP_TIMER,
\r
311 prvUIPTimerCallback
\r
314 xPeriodicTimer = xTimerCreate( "PeriodicTimer",
\r
315 ( 500UL / portTICK_PERIOD_MS ),
\r
316 pdTRUE, /* Autor-reload. */
\r
317 ( void * ) uipPERIODIC_TIMER,
\r
318 prvUIPTimerCallback
\r
321 /* Sanity check that the timers were indeed created. */
\r
322 configASSERT( xARPTimer );
\r
323 configASSERT( xPeriodicTimer );
\r
325 /* These commands will block indefinitely until they succeed, so there is
\r
326 no point in checking their return values. */
\r
327 xTimerStart( xARPTimer, portMAX_DELAY );
\r
328 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
330 /*-----------------------------------------------------------*/
\r
332 static void prvEMACEventListener( unsigned long ulISREvents )
\r
334 long lHigherPriorityTaskWoken = pdFALSE;
\r
335 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;
\r
337 /* Sanity check that the event queue was indeed created. */
\r
338 configASSERT( xEMACEventQueue );
\r
340 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )
\r
342 /* An Ethernet Tx event has occurred. */
\r
343 MSS_MAC_FreeTxBuffers();
\r
346 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )
\r
348 /* An Ethernet Rx event has occurred. */
\r
349 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );
\r
352 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
\r
354 /*-----------------------------------------------------------*/
\r
356 static void prvInitEmac( void )
\r
358 const unsigned char ucPHYAddress = 1;
\r
360 /* Initialise the MAC and PHY hardware. */
\r
361 MSS_MAC_init( ucPHYAddress );
\r
363 /* Register the event listener. The Ethernet interrupt handler will call
\r
364 this listener whenever an Rx or a Tx interrupt occurs. */
\r
365 MSS_MAC_set_callback( ( MSS_MAC_callback_t ) prvEMACEventListener );
\r
367 /* Setup the EMAC and the NVIC for MAC interrupts. */
\r
368 NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
369 NVIC_EnableIRQ( EthernetMAC_IRQn );
\r
371 /*-----------------------------------------------------------*/
\r
373 void vEMACWrite( void )
\r
375 const long lMaxAttempts = 10;
\r
377 const TickType_t xShortDelay = ( 5 / portTICK_PERIOD_MS );
\r
379 /* Try to send data to the Ethernet. Keep trying for a while if data cannot
\r
380 be sent immediately. Note that this will actually cause the data to be sent
\r
381 twice to get around delayed ACK problems when communicating with non real-
\r
382 time TCP/IP stacks (such as a Windows machine). */
\r
383 for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )
\r
385 if( MSS_MAC_tx_packet( uip_len ) != 0 )
\r
391 vTaskDelay( xShortDelay );
\r
395 /*-----------------------------------------------------------*/
\r
397 static void prvUIPTimerCallback( TimerHandle_t xTimer )
\r
399 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
400 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
402 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
403 block. As this callback is assigned to both the ARP and Periodic timers, the
\r
404 first thing to do is ascertain which timer it was that actually expired. */
\r
405 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
407 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
410 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
413 default : /* Should not get here. */
\r
417 /*-----------------------------------------------------------*/
\r
419 void vApplicationProcessFormInput( char *pcInputString )
\r
423 /* Only interested in processing form input if this is the IO page. */
\r
424 c = strstr( pcInputString, "io.shtml" );
\r
428 /* Is there a command in the string? */
\r
429 c = strstr( pcInputString, "?" );
\r
432 /* Turn the LED's on or off in accordance with the check box status. */
\r
433 if( strstr( c, "LED0=1" ) != NULL )
\r
435 /* Turn the LEDs on. */
\r
436 vParTestSetLED( 3, 1 );
\r
437 vParTestSetLED( 4, 1 );
\r
441 /* Turn the LEDs off. */
\r
442 vParTestSetLED( 3, 0 );
\r
443 vParTestSetLED( 4, 0 );
\r
448 /* Commands to turn LEDs off are not always explicit. */
\r
449 vParTestSetLED( 3, 0 );
\r
450 vParTestSetLED( 4, 0 );
\r
454 /*-----------------------------------------------------------*/
\r