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