2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Standard includes. */
\r
32 /* Scheduler includes. */
\r
33 #include "FreeRTOS.h"
\r
39 #include "net/uip.h"
\r
40 #include "net/uip_arp.h"
\r
41 #include "apps/httpd/httpd.h"
\r
42 #include "sys/timer.h"
\r
43 #include "net/clock-arch.h"
\r
45 /* Demo includes. */
\r
46 #include "ParTest.h"
\r
48 /* Hardware driver includes. */
\r
49 #include "mss_ethernet_mac_regs.h"
\r
50 #include "mss_ethernet_mac.h"
\r
52 /* The buffer used by the uIP stack to both receive and send. In this case,
\r
53 because the Ethernet driver has been modified to be zero copy - the uip_buf
\r
54 variable is just a pointer to an Ethernet buffer, and not a buffer in its own
\r
56 extern unsigned char *uip_buf;
\r
58 /* The ARP timer and the periodic timer share a callback function, so the
\r
59 respective timer IDs are used to determine which timer actually expired. These
\r
60 constants are assigned to the timer IDs. */
\r
61 #define uipARP_TIMER 0
\r
62 #define uipPERIODIC_TIMER 1
\r
64 /* The length of the queue used to send events from timers or the Ethernet
\r
65 driver to the uIP stack. */
\r
66 #define uipEVENT_QUEUE_LENGTH 10
\r
68 /* A block time of zero simply means "don't block". */
\r
69 #define uipDONT_BLOCK 0UL
\r
71 /* How long to wait before attempting to connect the MAC again. */
\r
72 #define uipINIT_WAIT ( 100 / portTICK_PERIOD_MS )
\r
74 /* Shortcut to the header within the Rx buffer. */
\r
75 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
77 /* Standard constant. */
\r
78 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
80 /*-----------------------------------------------------------*/
\r
83 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
85 static void prvSetMACAddress( void );
\r
88 * Perform any uIP initialisation required to ready the stack for http
\r
91 static void prvInitialise_uIP( void );
\r
94 * Handles Ethernet interrupt events.
\r
96 static void prvEMACEventListener( unsigned long ulISREvents );
\r
99 * The callback function that is assigned to both the periodic timer and the
\r
102 static void prvUIPTimerCallback( TimerHandle_t xTimer );
\r
105 * Initialise the MAC hardware.
\r
107 static void prvInitEmac( void );
\r
110 * Write data to the Ethener. Note that this actually writes data twice for the
\r
111 * to get around delayed ack issues when communicating with a non real-time
\r
112 * peer (for example, a Windows machine).
\r
114 void vEMACWrite( void );
\r
117 * Port functions required by the uIP stack.
\r
119 clock_time_t clock_time( void );
\r
121 /*-----------------------------------------------------------*/
\r
123 /* The queue used to send TCP/IP events to the uIP stack. */
\r
124 QueueHandle_t xEMACEventQueue = NULL;
\r
126 /*-----------------------------------------------------------*/
\r
128 clock_time_t clock_time( void )
\r
130 return xTaskGetTickCount();
\r
132 /*-----------------------------------------------------------*/
\r
134 void vuIP_Task( void *pvParameters )
\r
137 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;
\r
138 long lPacketLength;
\r
140 /* Just to prevent compiler warnings about the unused parameter. */
\r
141 ( void ) pvParameters;
\r
143 /* Initialise the uIP stack, configuring for web server usage. */
\r
144 prvInitialise_uIP();
\r
146 /* Initialise the MAC and PHY. */
\r
151 /* Is there received data ready to be processed? */
\r
152 lPacketLength = MSS_MAC_rx_packet();
\r
154 /* Statements to be executed if data has been received on the Ethernet. */
\r
155 if( ( lPacketLength > 0 ) && ( uip_buf != NULL ) )
\r
157 uip_len = ( u16_t ) lPacketLength;
\r
159 /* Standard uIP loop taken from the uIP manual. */
\r
160 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
165 /* If the above function invocation resulted in data that
\r
166 should be sent out on the network, the global variable
\r
167 uip_len is set to a value > 0. */
\r
174 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
178 /* If the above function invocation resulted in data that
\r
179 should be sent out on the network, the global variable
\r
180 uip_len is set to a value > 0. */
\r
189 /* Clear the RX event latched in ulUIP_Events - if one was latched. */
\r
190 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
193 /* Statements to be executed if the TCP/IP period timer has expired. */
\r
194 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
196 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
198 if( uip_buf != NULL )
\r
200 for( i = 0; i < UIP_CONNS; i++ )
\r
204 /* If the above function invocation resulted in data that
\r
205 should be sent out on the network, the global variable
\r
206 uip_len is set to a value > 0. */
\r
216 /* Statements to be executed if the ARP timer has expired. */
\r
217 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
219 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
223 /* If all latched events have been cleared - block until another event
\r
225 if( ulUIP_Events == pdFALSE )
\r
227 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
228 ulUIP_Events |= ulNewEvent;
\r
232 /*-----------------------------------------------------------*/
\r
234 static void prvSetMACAddress( void )
\r
236 struct uip_eth_addr xAddr;
\r
238 /* Configure the MAC address in the uIP stack. */
\r
239 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
240 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
241 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
242 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
243 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
244 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
245 uip_setethaddr( xAddr );
\r
247 /*-----------------------------------------------------------*/
\r
249 static void prvInitialise_uIP( void )
\r
251 uip_ipaddr_t xIPAddr;
\r
252 TimerHandle_t xARPTimer, xPeriodicTimer;
\r
255 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
256 uip_sethostaddr( &xIPAddr );
\r
257 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
258 uip_setnetmask( &xIPAddr );
\r
259 prvSetMACAddress();
\r
262 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
263 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
265 /* Create and start the uIP timers. */
\r
266 xARPTimer = xTimerCreate( "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
267 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */
\r
268 pdTRUE, /* Autor-reload. */
\r
269 ( void * ) uipARP_TIMER,
\r
270 prvUIPTimerCallback
\r
273 xPeriodicTimer = xTimerCreate( "PeriodicTimer",
\r
274 ( 500UL / portTICK_PERIOD_MS ),
\r
275 pdTRUE, /* Autor-reload. */
\r
276 ( void * ) uipPERIODIC_TIMER,
\r
277 prvUIPTimerCallback
\r
280 /* Sanity check that the timers were indeed created. */
\r
281 configASSERT( xARPTimer );
\r
282 configASSERT( xPeriodicTimer );
\r
284 /* These commands will block indefinitely until they succeed, so there is
\r
285 no point in checking their return values. */
\r
286 xTimerStart( xARPTimer, portMAX_DELAY );
\r
287 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
289 /*-----------------------------------------------------------*/
\r
291 static void prvEMACEventListener( unsigned long ulISREvents )
\r
293 long lHigherPriorityTaskWoken = pdFALSE;
\r
294 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;
\r
296 /* Sanity check that the event queue was indeed created. */
\r
297 configASSERT( xEMACEventQueue );
\r
299 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )
\r
301 /* An Ethernet Tx event has occurred. */
\r
302 MSS_MAC_FreeTxBuffers();
\r
305 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )
\r
307 /* An Ethernet Rx event has occurred. */
\r
308 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );
\r
311 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
\r
313 /*-----------------------------------------------------------*/
\r
315 static void prvInitEmac( void )
\r
317 const unsigned char ucPHYAddress = 1;
\r
319 /* Initialise the MAC and PHY hardware. */
\r
320 MSS_MAC_init( ucPHYAddress );
\r
322 /* Register the event listener. The Ethernet interrupt handler will call
\r
323 this listener whenever an Rx or a Tx interrupt occurs. */
\r
324 MSS_MAC_set_callback( ( MSS_MAC_callback_t ) prvEMACEventListener );
\r
326 /* Setup the EMAC and the NVIC for MAC interrupts. */
\r
327 NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
328 NVIC_EnableIRQ( EthernetMAC_IRQn );
\r
330 /*-----------------------------------------------------------*/
\r
332 void vEMACWrite( void )
\r
334 const long lMaxAttempts = 10;
\r
336 const TickType_t xShortDelay = ( 5 / portTICK_PERIOD_MS );
\r
338 /* Try to send data to the Ethernet. Keep trying for a while if data cannot
\r
339 be sent immediately. Note that this will actually cause the data to be sent
\r
340 twice to get around delayed ACK problems when communicating with non real-
\r
341 time TCP/IP stacks (such as a Windows machine). */
\r
342 for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )
\r
344 if( MSS_MAC_tx_packet( uip_len ) != 0 )
\r
350 vTaskDelay( xShortDelay );
\r
354 /*-----------------------------------------------------------*/
\r
356 static void prvUIPTimerCallback( TimerHandle_t xTimer )
\r
358 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
359 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
361 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
362 block. As this callback is assigned to both the ARP and Periodic timers, the
\r
363 first thing to do is ascertain which timer it was that actually expired. */
\r
364 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
366 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
369 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
372 default : /* Should not get here. */
\r
376 /*-----------------------------------------------------------*/
\r
378 void vApplicationProcessFormInput( char *pcInputString )
\r
382 /* Only interested in processing form input if this is the IO page. */
\r
383 c = strstr( pcInputString, "io.shtml" );
\r
387 /* Is there a command in the string? */
\r
388 c = strstr( pcInputString, "?" );
\r
391 /* Turn the LED's on or off in accordance with the check box status. */
\r
392 if( strstr( c, "LED0=1" ) != NULL )
\r
394 /* Turn the LEDs on. */
\r
395 vParTestSetLED( 3, 1 );
\r
396 vParTestSetLED( 4, 1 );
\r
400 /* Turn the LEDs off. */
\r
401 vParTestSetLED( 3, 0 );
\r
402 vParTestSetLED( 4, 0 );
\r
407 /* Commands to turn LEDs off are not always explicit. */
\r
408 vParTestSetLED( 3, 0 );
\r
409 vParTestSetLED( 4, 0 );
\r
413 /*-----------------------------------------------------------*/
\r