2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard includes. */
\r
31 /* Scheduler includes. */
\r
32 #include "FreeRTOS.h"
\r
38 #include "net/uip.h"
\r
39 #include "net/uip_arp.h"
\r
40 #include "apps/httpd/httpd.h"
\r
41 #include "sys/timer.h"
\r
42 #include "net/clock-arch.h"
\r
44 /* Demo includes. */
\r
45 #include "ParTest.h"
\r
47 /* Hardware driver includes. */
\r
48 #include "mss_ethernet_mac_regs.h"
\r
49 #include "mss_ethernet_mac.h"
\r
51 /* The buffer used by the uIP stack to both receive and send. In this case,
\r
52 because the Ethernet driver has been modified to be zero copy - the uip_buf
\r
53 variable is just a pointer to an Ethernet buffer, and not a buffer in its own
\r
55 extern unsigned char *uip_buf;
\r
57 /* The ARP timer and the periodic timer share a callback function, so the
\r
58 respective timer IDs are used to determine which timer actually expired. These
\r
59 constants are assigned to the timer IDs. */
\r
60 #define uipARP_TIMER 0
\r
61 #define uipPERIODIC_TIMER 1
\r
63 /* The length of the queue used to send events from timers or the Ethernet
\r
64 driver to the uIP stack. */
\r
65 #define uipEVENT_QUEUE_LENGTH 10
\r
67 /* A block time of zero simply means "don't block". */
\r
68 #define uipDONT_BLOCK 0UL
\r
70 /* How long to wait before attempting to connect the MAC again. */
\r
71 #define uipINIT_WAIT ( 100 / portTICK_PERIOD_MS )
\r
73 /* Shortcut to the header within the Rx buffer. */
\r
74 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
76 /* Standard constant. */
\r
77 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
79 /*-----------------------------------------------------------*/
\r
82 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
84 static void prvSetMACAddress( void );
\r
87 * Perform any uIP initialisation required to ready the stack for http
\r
90 static void prvInitialise_uIP( void );
\r
93 * Handles Ethernet interrupt events.
\r
95 static void prvEMACEventListener( unsigned long ulISREvents );
\r
98 * The callback function that is assigned to both the periodic timer and the
\r
101 static void prvUIPTimerCallback( TimerHandle_t xTimer );
\r
104 * Initialise the MAC hardware.
\r
106 static void prvInitEmac( void );
\r
109 * Write data to the Ethener. Note that this actually writes data twice for the
\r
110 * to get around delayed ack issues when communicating with a non real-time
\r
111 * peer (for example, a Windows machine).
\r
113 void vEMACWrite( void );
\r
116 * Port functions required by the uIP stack.
\r
118 clock_time_t clock_time( void );
\r
120 /*-----------------------------------------------------------*/
\r
122 /* The queue used to send TCP/IP events to the uIP stack. */
\r
123 QueueHandle_t xEMACEventQueue = NULL;
\r
125 /*-----------------------------------------------------------*/
\r
127 clock_time_t clock_time( void )
\r
129 return xTaskGetTickCount();
\r
131 /*-----------------------------------------------------------*/
\r
133 void vuIP_Task( void *pvParameters )
\r
136 unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;
\r
137 long lPacketLength;
\r
139 /* Just to prevent compiler warnings about the unused parameter. */
\r
140 ( void ) pvParameters;
\r
142 /* Initialise the uIP stack, configuring for web server usage. */
\r
143 prvInitialise_uIP();
\r
145 /* Initialise the MAC and PHY. */
\r
150 /* Is there received data ready to be processed? */
\r
151 lPacketLength = MSS_MAC_rx_packet();
\r
153 /* Statements to be executed if data has been received on the Ethernet. */
\r
154 if( ( lPacketLength > 0 ) && ( uip_buf != NULL ) )
\r
156 uip_len = ( u16_t ) lPacketLength;
\r
158 /* Standard uIP loop taken from the uIP manual. */
\r
159 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
164 /* If the above function invocation resulted in data that
\r
165 should be sent out on the network, the global variable
\r
166 uip_len is set to a value > 0. */
\r
173 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
177 /* If the above function invocation resulted in data that
\r
178 should be sent out on the network, the global variable
\r
179 uip_len is set to a value > 0. */
\r
188 /* Clear the RX event latched in ulUIP_Events - if one was latched. */
\r
189 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
192 /* Statements to be executed if the TCP/IP period timer has expired. */
\r
193 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
195 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
197 if( uip_buf != NULL )
\r
199 for( i = 0; i < UIP_CONNS; i++ )
\r
203 /* If the above function invocation resulted in data that
\r
204 should be sent out on the network, the global variable
\r
205 uip_len is set to a value > 0. */
\r
215 /* Statements to be executed if the ARP timer has expired. */
\r
216 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
218 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
222 /* If all latched events have been cleared - block until another event
\r
224 if( ulUIP_Events == pdFALSE )
\r
226 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
227 ulUIP_Events |= ulNewEvent;
\r
231 /*-----------------------------------------------------------*/
\r
233 static void prvSetMACAddress( void )
\r
235 struct uip_eth_addr xAddr;
\r
237 /* Configure the MAC address in the uIP stack. */
\r
238 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
239 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
240 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
241 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
242 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
243 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
244 uip_setethaddr( xAddr );
\r
246 /*-----------------------------------------------------------*/
\r
248 static void prvInitialise_uIP( void )
\r
250 uip_ipaddr_t xIPAddr;
\r
251 TimerHandle_t xARPTimer, xPeriodicTimer;
\r
254 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
255 uip_sethostaddr( &xIPAddr );
\r
256 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
257 uip_setnetmask( &xIPAddr );
\r
258 prvSetMACAddress();
\r
261 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
262 xEMACEventQueue = xQueueCreate( uipEVENT_QUEUE_LENGTH, sizeof( unsigned long ) );
\r
264 /* Create and start the uIP timers. */
\r
265 xARPTimer = xTimerCreate( "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
266 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */
\r
267 pdTRUE, /* Autor-reload. */
\r
268 ( void * ) uipARP_TIMER,
\r
269 prvUIPTimerCallback
\r
272 xPeriodicTimer = xTimerCreate( "PeriodicTimer",
\r
273 ( 500UL / portTICK_PERIOD_MS ),
\r
274 pdTRUE, /* Autor-reload. */
\r
275 ( void * ) uipPERIODIC_TIMER,
\r
276 prvUIPTimerCallback
\r
279 /* Sanity check that the timers were indeed created. */
\r
280 configASSERT( xARPTimer );
\r
281 configASSERT( xPeriodicTimer );
\r
283 /* These commands will block indefinitely until they succeed, so there is
\r
284 no point in checking their return values. */
\r
285 xTimerStart( xARPTimer, portMAX_DELAY );
\r
286 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
288 /*-----------------------------------------------------------*/
\r
290 static void prvEMACEventListener( unsigned long ulISREvents )
\r
292 long lHigherPriorityTaskWoken = pdFALSE;
\r
293 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;
\r
295 /* Sanity check that the event queue was indeed created. */
\r
296 configASSERT( xEMACEventQueue );
\r
298 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_SEND ) != 0UL )
\r
300 /* An Ethernet Tx event has occurred. */
\r
301 MSS_MAC_FreeTxBuffers();
\r
304 if( ( ulISREvents & MSS_MAC_EVENT_PACKET_RECEIVED ) != 0UL )
\r
306 /* An Ethernet Rx event has occurred. */
\r
307 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );
\r
310 portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
\r
312 /*-----------------------------------------------------------*/
\r
314 static void prvInitEmac( void )
\r
316 const unsigned char ucPHYAddress = 1;
\r
318 /* Initialise the MAC and PHY hardware. */
\r
319 MSS_MAC_init( ucPHYAddress );
\r
321 /* Register the event listener. The Ethernet interrupt handler will call
\r
322 this listener whenever an Rx or a Tx interrupt occurs. */
\r
323 MSS_MAC_set_callback( ( MSS_MAC_callback_t ) prvEMACEventListener );
\r
325 /* Setup the EMAC and the NVIC for MAC interrupts. */
\r
326 NVIC_SetPriority( EthernetMAC_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
327 NVIC_EnableIRQ( EthernetMAC_IRQn );
\r
329 /*-----------------------------------------------------------*/
\r
331 void vEMACWrite( void )
\r
333 const long lMaxAttempts = 10;
\r
335 const TickType_t xShortDelay = ( 5 / portTICK_PERIOD_MS );
\r
337 /* Try to send data to the Ethernet. Keep trying for a while if data cannot
\r
338 be sent immediately. Note that this will actually cause the data to be sent
\r
339 twice to get around delayed ACK problems when communicating with non real-
\r
340 time TCP/IP stacks (such as a Windows machine). */
\r
341 for( lAttempt = 0; lAttempt < lMaxAttempts; lAttempt++ )
\r
343 if( MSS_MAC_tx_packet( uip_len ) != 0 )
\r
349 vTaskDelay( xShortDelay );
\r
353 /*-----------------------------------------------------------*/
\r
355 static void prvUIPTimerCallback( TimerHandle_t xTimer )
\r
357 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
358 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
360 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
361 block. As this callback is assigned to both the ARP and Periodic timers, the
\r
362 first thing to do is ascertain which timer it was that actually expired. */
\r
363 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
365 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
368 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
371 default : /* Should not get here. */
\r
375 /*-----------------------------------------------------------*/
\r
377 void vApplicationProcessFormInput( char *pcInputString )
\r
381 /* Only interested in processing form input if this is the IO page. */
\r
382 c = strstr( pcInputString, "io.shtml" );
\r
386 /* Is there a command in the string? */
\r
387 c = strstr( pcInputString, "?" );
\r
390 /* Turn the LED's on or off in accordance with the check box status. */
\r
391 if( strstr( c, "LED0=1" ) != NULL )
\r
393 /* Turn the LEDs on. */
\r
394 vParTestSetLED( 3, 1 );
\r
395 vParTestSetLED( 4, 1 );
\r
399 /* Turn the LEDs off. */
\r
400 vParTestSetLED( 3, 0 );
\r
401 vParTestSetLED( 4, 0 );
\r
406 /* Commands to turn LEDs off are not always explicit. */
\r
407 vParTestSetLED( 3, 0 );
\r
408 vParTestSetLED( 4, 0 );
\r
412 /*-----------------------------------------------------------*/
\r