2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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
27 /* Standard includes. */
\r
30 /* Scheduler includes. */
\r
31 #include "FreeRTOS.h"
\r
35 #include "lcd_message.h"
\r
38 #include "hw_types.h"
\r
41 #include "uip_arp.h"
\r
44 #include "clock-arch.h"
\r
45 #include "hw_ethernet.h"
\r
46 #include "ethernet.h"
\r
47 #include "hw_memmap.h"
\r
48 #include "lmi_flash.h"
\r
51 /* Demo includes. */
\r
53 #include "partest.h"
\r
55 /*-----------------------------------------------------------*/
\r
57 /* IP address configuration. */
\r
58 #define uipIP_ADDR0 192
\r
59 #define uipIP_ADDR1 168
\r
60 #define uipIP_ADDR2 0
\r
61 #define uipIP_ADDR3 201
\r
63 /* Netmask configuration. */
\r
64 #define uipNETMASK_0 255
\r
65 #define uipNETMASK_1 255
\r
66 #define uipNETMASK_2 255
\r
67 #define uipNETMASK_3 0
\r
69 /* How long to wait before attempting to connect the MAC again. */
\r
70 #define uipINIT_WAIT 100
\r
72 /* Shortcut to the header within the Rx buffer. */
\r
73 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
75 /* Standard constant. */
\r
76 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
78 /*-----------------------------------------------------------*/
\r
81 * Send the uIP buffer to the MAC.
\r
83 static void prvENET_Send(void);
\r
86 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
88 static void prvSetMACAddress( void );
\r
91 * Port functions required by the uIP stack.
\r
93 void clock_init( void );
\r
94 clock_time_t clock_time( void );
\r
96 /*-----------------------------------------------------------*/
\r
98 /* The semaphore used by the ISR to wake the uIP task. */
\r
99 extern SemaphoreHandle_t xEMACSemaphore;
\r
101 /*-----------------------------------------------------------*/
\r
103 void clock_init(void)
\r
105 /* This is done when the scheduler starts. */
\r
107 /*-----------------------------------------------------------*/
\r
109 clock_time_t clock_time( void )
\r
111 return xTaskGetTickCount();
\r
115 void vuIP_Task( void *pvParameters )
\r
118 uip_ipaddr_t xIPAddr;
\r
119 struct timer periodic_timer, arp_timer;
\r
120 extern void ( vEMAC_ISR )( void );
\r
122 /* Enable/Reset the Ethernet Controller */
\r
123 SysCtlPeripheralEnable( SYSCTL_PERIPH_ETH );
\r
124 SysCtlPeripheralReset( SYSCTL_PERIPH_ETH );
\r
126 /* Create the semaphore used by the ISR to wake this task. */
\r
127 vSemaphoreCreateBinary( xEMACSemaphore );
\r
129 /* Initialise the uIP stack. */
\r
130 timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
\r
131 timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
\r
133 uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
\r
134 uip_sethostaddr( xIPAddr );
\r
135 uip_ipaddr( xIPAddr, uipNETMASK_0, uipNETMASK_1, uipNETMASK_2, uipNETMASK_3 );
\r
136 uip_setnetmask( xIPAddr );
\r
139 while( vInitEMAC() != pdPASS )
\r
141 vTaskDelay( uipINIT_WAIT );
\r
143 prvSetMACAddress();
\r
148 /* Is there received data ready to be processed? */
\r
149 uip_len = uiGetEMACRxData( uip_buf );
\r
153 /* Standard uIP loop taken from the uIP manual. */
\r
155 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
160 /* If the above function invocation resulted in data that
\r
161 should be sent out on the network, the global variable
\r
162 uip_len is set to a value > 0. */
\r
169 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
173 /* If the above function invocation resulted in data that
\r
174 should be sent out on the network, the global variable
\r
175 uip_len is set to a value > 0. */
\r
184 if( timer_expired( &periodic_timer ) )
\r
186 timer_reset( &periodic_timer );
\r
187 for( i = 0; i < UIP_CONNS; i++ )
\r
191 /* If the above function invocation resulted in data that
\r
192 should be sent out on the network, the global variable
\r
193 uip_len is set to a value > 0. */
\r
201 /* Call the ARP timer function every 10 seconds. */
\r
202 if( timer_expired( &arp_timer ) )
\r
204 timer_reset( &arp_timer );
\r
210 /* We did not receive a packet, and there was no periodic
\r
211 processing to perform. Block for a fixed period. If a packet
\r
212 is received during this period we will be woken by the ISR
\r
213 giving us the Semaphore. */
\r
214 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );
\r
219 /*-----------------------------------------------------------*/
\r
221 static void prvENET_Send(void)
\r
224 vIncrementTxLength( uip_len );
\r
225 vSendBufferToMAC();
\r
227 vIncrementTxLength( uip_len );
\r
228 vSendBufferToMAC();
\r
230 /*-----------------------------------------------------------*/
\r
232 static void prvSetMACAddress( void )
\r
234 unsigned long ulUser0, ulUser1;
\r
235 unsigned char pucMACArray[8];
\r
236 struct uip_eth_addr xAddr;
\r
238 /* Get the device MAC address from flash */
\r
239 FlashUserGet(&ulUser0, &ulUser1);
\r
241 /* Convert the MAC address from flash into sequence of bytes. */
\r
242 pucMACArray[0] = ((ulUser0 >> 0) & 0xff);
\r
243 pucMACArray[1] = ((ulUser0 >> 8) & 0xff);
\r
244 pucMACArray[2] = ((ulUser0 >> 16) & 0xff);
\r
245 pucMACArray[3] = ((ulUser1 >> 0) & 0xff);
\r
246 pucMACArray[4] = ((ulUser1 >> 8) & 0xff);
\r
247 pucMACArray[5] = ((ulUser1 >> 16) & 0xff);
\r
249 /* Program the MAC address. */
\r
250 EthernetMACAddrSet(ETH_BASE, pucMACArray);
\r
252 xAddr.addr[ 0 ] = pucMACArray[0];
\r
253 xAddr.addr[ 1 ] = pucMACArray[1];
\r
254 xAddr.addr[ 2 ] = pucMACArray[2];
\r
255 xAddr.addr[ 3 ] = pucMACArray[3];
\r
256 xAddr.addr[ 4 ] = pucMACArray[4];
\r
257 xAddr.addr[ 5 ] = pucMACArray[5];
\r
258 uip_setethaddr( xAddr );
\r
260 /*-----------------------------------------------------------*/
\r
262 void vApplicationProcessFormInput( char *pcInputString, portBASE_TYPE xInputLength )
\r
265 static char cMessageForDisplay[ 32 ];
\r
266 extern QueueHandle_t xOLEDQueue;
\r
267 xOLEDMessage xOLEDMessage;
\r
269 /* Process the form input sent by the IO page of the served HTML. */
\r
271 c = strstr( pcInputString, "?" );
\r
275 /* Turn LED's on or off in accordance with the check box status. */
\r
276 if( strstr( c, "LED0=1" ) != NULL )
\r
278 vParTestSetLED( 0, 1 );
\r
282 vParTestSetLED( 0, 0 );
\r
285 /* Find the start of the text to be displayed on the LCD. */
\r
286 pcText = strstr( c, "LCD=" );
\r
287 pcText += strlen( "LCD=" );
\r
289 /* Terminate the file name for further processing within uIP. */
\r
292 /* Terminate the LCD string. */
\r
293 c = strstr( pcText, " " );
\r
299 /* Add required spaces. */
\r
300 while( ( c = strstr( pcText, "+" ) ) != NULL )
\r
305 /* Write the message to the LCD. */
\r
306 strcpy( cMessageForDisplay, pcText );
\r
307 xOLEDMessage.pcMessage = cMessageForDisplay;
\r
308 xQueueSend( xOLEDQueue, &xOLEDMessage, portMAX_DELAY );
\r