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
28 /* Standard includes. */
\r
31 /* Scheduler includes. */
\r
32 #include "FreeRTOS.h"
\r
37 #include "hw_types.h"
\r
39 #include "uip_arp.h"
\r
42 #include "clock-arch.h"
\r
43 #include "hw_ethernet.h"
\r
44 #include "ethernet.h"
\r
45 #include "hw_memmap.h"
\r
46 #include "lmi_flash.h"
\r
49 /* Demo includes. */
\r
51 #include "partest.h"
\r
52 #include "lcd_message.h"
\r
56 clock_time_t interval;
\r
60 /*-----------------------------------------------------------*/
\r
62 /* IP address configuration. */
\r
63 #define uipIP_ADDR0 172
\r
64 #define uipIP_ADDR1 25
\r
65 #define uipIP_ADDR2 218
\r
66 #define uipIP_ADDR3 19
\r
68 /* How long to wait before attempting to connect the MAC again. */
\r
69 #define uipINIT_WAIT 100
\r
71 /* Shortcut to the header within the Rx buffer. */
\r
72 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
74 /* Standard constant. */
\r
75 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
77 /*-----------------------------------------------------------*/
\r
80 * Send the uIP buffer to the MAC.
\r
82 static void prvENET_Send(void);
\r
85 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
87 static void prvSetMACAddress( void );
\r
90 * Port functions required by the uIP stack.
\r
92 void clock_init( void );
\r
93 clock_time_t clock_time( void );
\r
95 /*-----------------------------------------------------------*/
\r
97 /* The semaphore used by the ISR to wake the uIP task. */
\r
98 extern SemaphoreHandle_t xEMACSemaphore;
\r
100 /*-----------------------------------------------------------*/
\r
102 void clock_init(void)
\r
104 /* This is done when the scheduler starts. */
\r
106 /*-----------------------------------------------------------*/
\r
108 /* Define clock functions here to avoid header file name clash between uIP
\r
109 and the Luminary Micro driver library. */
\r
110 clock_time_t clock_time( void )
\r
112 return xTaskGetTickCount();
\r
114 extern void timer_set(struct timer *t, clock_time_t interval);
\r
115 extern int timer_expired(struct timer *t);
\r
116 extern void timer_reset(struct timer *t);
\r
121 void vuIP_Task( void *pvParameters )
\r
124 uip_ipaddr_t xIPAddr;
\r
125 struct timer periodic_timer, arp_timer;
\r
126 extern void ( vEMAC_ISR )( void );
\r
128 /* Enable/Reset the Ethernet Controller */
\r
129 SysCtlPeripheralEnable( SYSCTL_PERIPH_ETH );
\r
130 SysCtlPeripheralReset( SYSCTL_PERIPH_ETH );
\r
132 /* Create the semaphore used by the ISR to wake this task. */
\r
133 vSemaphoreCreateBinary( xEMACSemaphore );
\r
135 /* Initialise the uIP stack. */
\r
136 timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
\r
137 timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
\r
139 uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
\r
140 uip_sethostaddr( xIPAddr );
\r
143 while( vInitEMAC() != pdPASS )
\r
145 vTaskDelay( uipINIT_WAIT );
\r
147 prvSetMACAddress();
\r
152 /* Is there received data ready to be processed? */
\r
153 uip_len = uiGetEMACRxData( uip_buf );
\r
157 /* 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 if( timer_expired( &periodic_timer ) )
\r
190 timer_reset( &periodic_timer );
\r
191 for( i = 0; i < UIP_CONNS; i++ )
\r
195 /* If the above function invocation resulted in data that
\r
196 should be sent out on the network, the global variable
\r
197 uip_len is set to a value > 0. */
\r
205 /* Call the ARP timer function every 10 seconds. */
\r
206 if( timer_expired( &arp_timer ) )
\r
208 timer_reset( &arp_timer );
\r
214 /* We did not receive a packet, and there was no periodic
\r
215 processing to perform. Block for a fixed period. If a packet
\r
216 is received during this period we will be woken by the ISR
\r
217 giving us the Semaphore. */
\r
218 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );
\r
223 /*-----------------------------------------------------------*/
\r
225 static void prvENET_Send(void)
\r
228 vIncrementTxLength( uip_len );
\r
229 vSendBufferToMAC();
\r
231 /*-----------------------------------------------------------*/
\r
233 static void prvSetMACAddress( void )
\r
235 unsigned long ulUser0, ulUser1;
\r
236 unsigned char pucMACArray[8];
\r
237 struct uip_eth_addr xAddr;
\r
239 /* Get the device MAC address from flash */
\r
240 FlashUserGet(&ulUser0, &ulUser1);
\r
242 /* Convert the MAC address from flash into sequence of bytes. */
\r
243 pucMACArray[0] = ((ulUser0 >> 0) & 0xff);
\r
244 pucMACArray[1] = ((ulUser0 >> 8) & 0xff);
\r
245 pucMACArray[2] = ((ulUser0 >> 16) & 0xff);
\r
246 pucMACArray[3] = ((ulUser1 >> 0) & 0xff);
\r
247 pucMACArray[4] = ((ulUser1 >> 8) & 0xff);
\r
248 pucMACArray[5] = ((ulUser1 >> 16) & 0xff);
\r
250 /* Program the MAC address. */
\r
251 EthernetMACAddrSet(ETH_BASE, pucMACArray);
\r
253 xAddr.addr[ 0 ] = pucMACArray[0];
\r
254 xAddr.addr[ 1 ] = pucMACArray[1];
\r
255 xAddr.addr[ 2 ] = pucMACArray[2];
\r
256 xAddr.addr[ 3 ] = pucMACArray[3];
\r
257 xAddr.addr[ 4 ] = pucMACArray[4];
\r
258 xAddr.addr[ 5 ] = pucMACArray[5];
\r
259 uip_setethaddr( xAddr );
\r
261 /*-----------------------------------------------------------*/
\r
263 void vApplicationProcessFormInput( char *pcInputString, portBASE_TYPE xInputLength )
\r
266 static char cMessageForDisplay[ 32 ];
\r
267 extern QueueHandle_t xOLEDQueue;
\r
268 xOLEDMessage xOLEDMessage;
\r
270 /* Process the form input sent by the IO page of the served HTML. */
\r
272 c = strstr( pcInputString, "?" );
\r
276 /* Turn LED's on or off in accordance with the check box status. */
\r
277 if( strstr( c, "LED0=1" ) != NULL )
\r
279 vParTestSetLED( 0, 1 );
\r
283 vParTestSetLED( 0, 0 );
\r
286 /* Find the start of the text to be displayed on the LCD. */
\r
287 pcText = strstr( c, "LCD=" );
\r
288 pcText += strlen( "LCD=" );
\r
290 /* Terminate the file name for further processing within uIP. */
\r
293 /* Terminate the LCD string. */
\r
294 c = strstr( pcText, " " );
\r
300 /* Add required spaces. */
\r
301 while( ( c = strstr( pcText, "+" ) ) != NULL )
\r
306 /* Write the message to the LCD. */
\r
307 strcpy( cMessageForDisplay, pcText );
\r
308 xOLEDMessage.pcMessage = ( signed char * ) cMessageForDisplay;
\r
309 xQueueSend( xOLEDQueue, &xOLEDMessage, portMAX_DELAY );
\r