2 * FreeRTOS Kernel V10.3.1
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
28 /* Standard includes. */
31 /* Scheduler includes. */
39 #include "net/uip_arp.h"
40 #include "apps/httpd/httpd.h"
41 #include "sys/timer.h"
42 #include "net/clock-arch.h"
48 /*-----------------------------------------------------------*/
50 /* How long to wait before attempting to connect the MAC again. */
51 #define uipINIT_WAIT ( 100 / portTICK_PERIOD_MS )
53 /* Shortcut to the header within the Rx buffer. */
54 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
56 /* Standard constant. */
57 #define uipTOTAL_FRAME_HEADER_SIZE 54
59 /* The ARP timer and the periodic timer share a callback function, so the
60 respective timer IDs are used to determine which timer actually expired. These
61 constants are assigned to the timer IDs. */
62 #define uipARP_TIMER 0
63 #define uipPERIODIC_TIMER 1
65 /* A block time of zero ticks simply means, "don't block". */
66 #define uipDONT_BLOCK 0UL
68 /*-----------------------------------------------------------*/
71 * Setup the MAC address in the MAC itself, and in the uIP stack.
73 static void prvSetMACAddress( void );
76 * Perform any uIP initialisation necessary.
78 static void prvInitialise_uIP( void );
81 * The callback function that is assigned to both the periodic timer and the
84 static void prvUIPTimerCallback( TimerHandle_t xTimer );
87 * Port functions required by the uIP stack.
89 clock_time_t clock_time( void );
91 /*-----------------------------------------------------------*/
93 /* The queue used to send TCP/IP events to the uIP stack. */
94 QueueHandle_t xEMACEventQueue = NULL;
96 /*-----------------------------------------------------------*/
98 clock_time_t clock_time( void )
100 return xTaskGetTickCount();
102 /*-----------------------------------------------------------*/
104 void vuIP_Task( void *pvParameters )
107 unsigned long ulNewEvent = 0UL;
108 unsigned long ulUIP_Events = 0UL;
110 ( void ) pvParameters;
112 /* Initialise the uIP stack. */
115 /* Initialise the MAC. */
118 while( lEMACWaitForLink() != pdPASS )
120 vTaskDelay( uipINIT_WAIT );
125 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )
127 /* Is there received data ready to be processed? */
128 uip_len = ( unsigned short ) ulEMACRead();
130 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
132 /* Standard uIP loop taken from the uIP manual. */
133 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
138 /* If the above function invocation resulted in data that
139 should be sent out on the network, the global variable
140 uip_len is set to a value > 0. */
147 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
151 /* If the above function invocation resulted in data that
152 should be sent out on the network, the global variable
153 uip_len is set to a value > 0. */
162 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
166 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
168 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
170 for( i = 0; i < UIP_CONNS; i++ )
174 /* If the above function invocation resulted in data that
175 should be sent out on the network, the global variable
176 uip_len is set to a value > 0. */
185 /* Call the ARP timer function every 10 seconds. */
186 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
188 ulUIP_Events &= ~uipARP_TIMER_EVENT;
192 if( ulUIP_Events == pdFALSE )
194 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
195 ulUIP_Events |= ulNewEvent;
199 /*-----------------------------------------------------------*/
201 static void prvInitialise_uIP( void )
203 TimerHandle_t xARPTimer, xPeriodicTimer;
204 uip_ipaddr_t xIPAddr;
205 const unsigned long ul_uIPEventQueueLength = 10UL;
207 /* Initialise the uIP stack. */
209 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
210 uip_sethostaddr( &xIPAddr );
211 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
212 uip_setnetmask( &xIPAddr );
216 /* Create the queue used to sent TCP/IP events to the uIP stack. */
217 xEMACEventQueue = xQueueCreate( ul_uIPEventQueueLength, sizeof( unsigned long ) );
219 /* Create and start the uIP timers. */
220 xARPTimer = xTimerCreate( "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
221 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */
222 pdTRUE, /* Autor-reload. */
223 ( void * ) uipARP_TIMER,
227 xPeriodicTimer = xTimerCreate( "PeriodicTimer",
228 ( 50 / portTICK_PERIOD_MS ),
229 pdTRUE, /* Autor-reload. */
230 ( void * ) uipPERIODIC_TIMER,
234 configASSERT( xARPTimer );
235 configASSERT( xPeriodicTimer );
237 xTimerStart( xARPTimer, portMAX_DELAY );
238 xTimerStart( xPeriodicTimer, portMAX_DELAY );
240 /*-----------------------------------------------------------*/
242 static void prvUIPTimerCallback( TimerHandle_t xTimer )
244 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
245 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
247 /* This is a time callback, so calls to xQueueSend() must not attempt to
249 switch( ( int ) pvTimerGetTimerID( xTimer ) )
251 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
254 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
257 default : /* Should not get here. */
261 /*-----------------------------------------------------------*/
263 static void prvSetMACAddress( void )
265 struct uip_eth_addr xAddr;
267 /* Configure the MAC address in the uIP stack. */
268 xAddr.addr[ 0 ] = configMAC_ADDR0;
269 xAddr.addr[ 1 ] = configMAC_ADDR1;
270 xAddr.addr[ 2 ] = configMAC_ADDR2;
271 xAddr.addr[ 3 ] = configMAC_ADDR3;
272 xAddr.addr[ 4 ] = configMAC_ADDR4;
273 xAddr.addr[ 5 ] = configMAC_ADDR5;
274 uip_setethaddr( xAddr );
276 /*-----------------------------------------------------------*/
278 void vApplicationProcessFormInput( char *pcInputString )
282 /* Only interested in processing form input if this is the IO page. */
283 c = strstr( pcInputString, "io.shtml" );
287 /* Is there a command in the string? */
288 c = strstr( pcInputString, "?" );
291 /* Turn the LED's on or off in accordance with the check box status. */
292 if( strstr( c, "LED0=1" ) != NULL )
294 /* Turn the LEDs on. */
295 vParTestSetLED( 7, 1 );
296 vParTestSetLED( 8, 1 );
297 vParTestSetLED( 9, 1 );
298 vParTestSetLED( 10, 1 );
302 /* Turn the LEDs off. */
303 vParTestSetLED( 7, 0 );
304 vParTestSetLED( 8, 0 );
305 vParTestSetLED( 9, 0 );
306 vParTestSetLED( 10, 0 );
311 /* Commands to turn LEDs off are not always explicit. */
312 vParTestSetLED( 7, 0 );
313 vParTestSetLED( 8, 0 );
314 vParTestSetLED( 9, 0 );
315 vParTestSetLED( 10, 0 );