2 * FreeRTOS Kernel V10.2.1
\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
43 #include "r_ether.h"
\r
45 /* Demo includes. */
\r
46 #include "ParTest.h"
\r
48 /*-----------------------------------------------------------*/
\r
50 /* How long to wait before attempting to connect the MAC again. */
\r
51 #define uipINIT_WAIT ( 100 / portTICK_PERIOD_MS )
\r
53 /* Shortcut to the header within the Rx buffer. */
\r
54 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
56 /* Standard constant. */
\r
57 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
59 /* The ARP timer and the periodic timer share a callback function, so the
\r
60 respective timer IDs are used to determine which timer actually expired. These
\r
61 constants are assigned to the timer IDs. */
\r
62 #define uipARP_TIMER 0
\r
63 #define uipPERIODIC_TIMER 1
\r
65 /* A block time of zero ticks simply means, "don't block". */
\r
66 #define uipDONT_BLOCK 0UL
\r
68 /*-----------------------------------------------------------*/
\r
71 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
73 static void prvSetMACAddress( void );
\r
76 * Perform any uIP initialisation necessary.
\r
78 static void prvInitialise_uIP( void );
\r
81 * The callback function that is assigned to both the periodic timer and the
\r
84 static void prvUIPTimerCallback( TimerHandle_t xTimer );
\r
87 * Port functions required by the uIP stack.
\r
89 clock_time_t clock_time( void );
\r
91 /*-----------------------------------------------------------*/
\r
93 /* The queue used to send TCP/IP events to the uIP stack. */
\r
94 QueueHandle_t xEMACEventQueue = NULL;
\r
96 /*-----------------------------------------------------------*/
\r
98 clock_time_t clock_time( void )
\r
100 return xTaskGetTickCount();
\r
102 /*-----------------------------------------------------------*/
\r
104 void vuIP_Task( void *pvParameters )
\r
107 unsigned long ulNewEvent = 0UL;
\r
108 unsigned long ulUIP_Events = 0UL;
\r
110 ( void ) pvParameters;
\r
112 /* Initialise the uIP stack. */
\r
113 prvInitialise_uIP();
\r
115 /* Initialise the MAC. */
\r
118 while( lEMACWaitForLink() != pdPASS )
\r
120 vTaskDelay( uipINIT_WAIT );
\r
125 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )
\r
127 /* Is there received data ready to be processed? */
\r
128 uip_len = ( unsigned short ) ulEMACRead();
\r
130 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
\r
132 /* Standard uIP loop taken from the uIP manual. */
\r
133 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
138 /* If the above function invocation resulted in data that
\r
139 should be sent out on the network, the global variable
\r
140 uip_len is set to a value > 0. */
\r
147 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
151 /* If the above function invocation resulted in data that
\r
152 should be sent out on the network, the global variable
\r
153 uip_len is set to a value > 0. */
\r
162 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
\r
166 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
\r
168 ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
\r
170 for( i = 0; i < UIP_CONNS; i++ )
\r
174 /* If the above function invocation resulted in data that
\r
175 should be sent out on the network, the global variable
\r
176 uip_len is set to a value > 0. */
\r
185 /* Call the ARP timer function every 10 seconds. */
\r
186 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
\r
188 ulUIP_Events &= ~uipARP_TIMER_EVENT;
\r
192 if( ulUIP_Events == pdFALSE )
\r
194 xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
\r
195 ulUIP_Events |= ulNewEvent;
\r
199 /*-----------------------------------------------------------*/
\r
201 static void prvInitialise_uIP( void )
\r
203 TimerHandle_t xARPTimer, xPeriodicTimer;
\r
204 uip_ipaddr_t xIPAddr;
\r
205 const unsigned long ul_uIPEventQueueLength = 10UL;
\r
207 /* Initialise the uIP stack. */
\r
209 uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
210 uip_sethostaddr( &xIPAddr );
\r
211 uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
\r
212 uip_setnetmask( &xIPAddr );
\r
213 prvSetMACAddress();
\r
216 /* Create the queue used to sent TCP/IP events to the uIP stack. */
\r
217 xEMACEventQueue = xQueueCreate( ul_uIPEventQueueLength, sizeof( unsigned long ) );
\r
219 /* Create and start the uIP timers. */
\r
220 xARPTimer = xTimerCreate( "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
\r
221 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */
\r
222 pdTRUE, /* Autor-reload. */
\r
223 ( void * ) uipARP_TIMER,
\r
224 prvUIPTimerCallback
\r
227 xPeriodicTimer = xTimerCreate( "PeriodicTimer",
\r
228 ( 50 / portTICK_PERIOD_MS ),
\r
229 pdTRUE, /* Autor-reload. */
\r
230 ( void * ) uipPERIODIC_TIMER,
\r
231 prvUIPTimerCallback
\r
234 configASSERT( xARPTimer );
\r
235 configASSERT( xPeriodicTimer );
\r
237 xTimerStart( xARPTimer, portMAX_DELAY );
\r
238 xTimerStart( xPeriodicTimer, portMAX_DELAY );
\r
240 /*-----------------------------------------------------------*/
\r
242 static void prvUIPTimerCallback( TimerHandle_t xTimer )
\r
244 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
\r
245 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
\r
247 /* This is a time callback, so calls to xQueueSend() must not attempt to
\r
249 switch( ( int ) pvTimerGetTimerID( xTimer ) )
\r
251 case uipARP_TIMER : xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
\r
254 case uipPERIODIC_TIMER : xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
\r
257 default : /* Should not get here. */
\r
261 /*-----------------------------------------------------------*/
\r
263 static void prvSetMACAddress( void )
\r
265 struct uip_eth_addr xAddr;
\r
267 /* Configure the MAC address in the uIP stack. */
\r
268 xAddr.addr[ 0 ] = configMAC_ADDR0;
\r
269 xAddr.addr[ 1 ] = configMAC_ADDR1;
\r
270 xAddr.addr[ 2 ] = configMAC_ADDR2;
\r
271 xAddr.addr[ 3 ] = configMAC_ADDR3;
\r
272 xAddr.addr[ 4 ] = configMAC_ADDR4;
\r
273 xAddr.addr[ 5 ] = configMAC_ADDR5;
\r
274 uip_setethaddr( xAddr );
\r
276 /*-----------------------------------------------------------*/
\r
278 void vApplicationProcessFormInput( char *pcInputString )
\r
282 /* Only interested in processing form input if this is the IO page. */
\r
283 c = strstr( pcInputString, "io.shtml" );
\r
287 /* Is there a command in the string? */
\r
288 c = strstr( pcInputString, "?" );
\r
291 /* Turn the LED's on or off in accordance with the check box status. */
\r
292 if( strstr( c, "LED0=1" ) != NULL )
\r
294 /* Turn the LEDs on. */
\r
295 vParTestSetLED( 7, 1 );
\r
296 vParTestSetLED( 8, 1 );
\r
297 vParTestSetLED( 9, 1 );
\r
298 vParTestSetLED( 10, 1 );
\r
302 /* Turn the LEDs off. */
\r
303 vParTestSetLED( 7, 0 );
\r
304 vParTestSetLED( 8, 0 );
\r
305 vParTestSetLED( 9, 0 );
\r
306 vParTestSetLED( 10, 0 );
\r
311 /* Commands to turn LEDs off are not always explicit. */
\r
312 vParTestSetLED( 7, 0 );
\r
313 vParTestSetLED( 8, 0 );
\r
314 vParTestSetLED( 9, 0 );
\r
315 vParTestSetLED( 10, 0 );
\r