2 FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* Standard includes. */
\r
73 /* FreeRTOS includes. */
\r
74 #include "FreeRTOS.h"
\r
78 /* lwIP core includes */
\r
79 #include "lwip/opt.h"
\r
80 #include "lwip/tcpip.h"
\r
81 #include "lwip/inet.h"
\r
82 #include "lwip/dhcp.h"
\r
84 /* applications includes */
\r
85 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"
\r
87 /* include the port-dependent configuration */
\r
88 #include "lwipcfg_msvc.h"
\r
90 /* Dimensions the cTxBuffer array - which is itself used to hold replies from
\r
91 command line commands. cTxBuffer is a shared buffer, so protected by the
\r
92 xTxBufferMutex mutex. */
\r
93 #define lwipappsTX_BUFFER_SIZE 1024
\r
95 /* The maximum time to block waiting to obtain the xTxBufferMutex to become
\r
97 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ( 100 / portTICK_RATE_MS )
\r
99 /* Definitions of the various SSI callback functions within the pccSSITags
\r
100 array. If pccSSITags is updated, then these definitions must also be updated. */
\r
101 #define ssiTASK_STATS_INDEX 0
\r
102 #define ssiRUN_TIME_STATS_INDEX 1
\r
104 /*-----------------------------------------------------------*/
\r
107 * The function that implements the lwIP based sockets command interpreter
\r
110 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );
\r
113 * The SSI handler callback function passed to lwIP.
\r
115 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );
\r
117 /*-----------------------------------------------------------*/
\r
119 /* The SSI strings that are embedded in the served html files. If this array
\r
120 is changed, then the index position defined by the #defines such as
\r
121 ssiTASK_STATS_INDEX above must also be updated. */
\r
122 static const char *pccSSITags[] =
\r
128 /* Semaphore used to guard the Tx buffer. */
\r
129 static xSemaphoreHandle xTxBufferMutex = NULL;
\r
131 /* The Tx buffer itself. This is used to hold the text generated by the
\r
132 execution of command line commands, and (hopefully) the execution of
\r
133 server side include callbacks. It is a shared buffer so protected by the
\r
134 xTxBufferMutex mutex. pcLwipAppsBlockingGetTxBuffer() and
\r
135 vLwipAppsReleaseTxBuffer() are provided to obtain and release the
\r
136 xTxBufferMutex respectively. pcLwipAppsBlockingGetTxBuffer() must be used with
\r
137 caution as it has the potential to block. */
\r
138 static signed char cTxBuffer[ lwipappsTX_BUFFER_SIZE ];
\r
140 /*-----------------------------------------------------------*/
\r
142 void vStatusCallback( struct netif *pxNetIf )
\r
144 char pcMessage[20];
\r
145 struct in_addr* pxIPAddress;
\r
147 if( netif_is_up( pxNetIf ) != 0 )
\r
149 strcpy( pcMessage, "IP=" );
\r
150 pxIPAddress = ( struct in_addr* ) &( pxNetIf->ip_addr );
\r
151 strcat( pcMessage, inet_ntoa( ( *pxIPAddress ) ) );
\r
152 xil_printf( pcMessage );
\r
156 xil_printf( "Network is down" );
\r
160 /* Called from the TCP/IP thread. */
\r
161 void lwIPAppsInit( void *pvArgument )
\r
163 ip_addr_t xIPAddr, xNetMask, xGateway;
\r
164 extern err_t xemacpsif_init( struct netif *netif );
\r
165 extern void xemacif_input_thread( void *netif );
\r
166 static struct netif xNetIf;
\r
168 ( void ) pvArgument;
\r
170 /* Set up the network interface. */
\r
171 ip_addr_set_zero( &xGateway );
\r
172 ip_addr_set_zero( &xIPAddr );
\r
173 ip_addr_set_zero( &xNetMask );
\r
175 LWIP_PORT_INIT_GW(&xGateway);
\r
176 LWIP_PORT_INIT_IPADDR( &xIPAddr );
\r
177 LWIP_PORT_INIT_NETMASK(&xNetMask);
\r
179 /* Set mac address */
\r
180 xNetIf.hwaddr_len = 6;
\r
181 xNetIf.hwaddr[ 0 ] = configMAC_ADDR0;
\r
182 xNetIf.hwaddr[ 1 ] = configMAC_ADDR1;
\r
183 xNetIf.hwaddr[ 2 ] = configMAC_ADDR2;
\r
184 xNetIf.hwaddr[ 3 ] = configMAC_ADDR3;
\r
185 xNetIf.hwaddr[ 4 ] = configMAC_ADDR4;
\r
186 xNetIf.hwaddr[ 5 ] = configMAC_ADDR5;
\r
188 netif_set_default( netif_add( &xNetIf, &xIPAddr, &xNetMask, &xGateway, ( void * ) XPAR_XEMACPS_0_BASEADDR, xemacpsif_init, tcpip_input ) );
\r
189 netif_set_status_callback( &xNetIf, vStatusCallback );
\r
192 dhcp_start( &xNetIf );
\r
196 netif_set_up( &xNetIf );
\r
200 /* Install the server side include handler. */
\r
201 http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );
\r
203 /* Create the mutex used to ensure mutual exclusive access to the Tx
\r
205 xTxBufferMutex = xSemaphoreCreateMutex();
\r
206 configASSERT( xTxBufferMutex );
\r
208 /* Create the httpd server from the standard lwIP code. This demonstrates
\r
209 use of the lwIP raw API. */
\r
212 sys_thread_new( "lwIP_In", xemacif_input_thread, &xNetIf, configMINIMAL_STACK_SIZE, configMAC_INPUT_TASK_PRIORITY );
\r
214 /* Create the FreeRTOS defined basic command server. This demonstrates use
\r
215 of the lwIP sockets API. */
\r
216 xTaskCreate( vBasicSocketsCommandInterpreterTask, "CmdInt", configMINIMAL_STACK_SIZE * 5, NULL, configCLI_TASK_PRIORITY, NULL );
\r
218 /*-----------------------------------------------------------*/
\r
220 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )
\r
222 static unsigned int uiUpdateCount = 0;
\r
223 static char cUpdateString[ 200 ];
\r
224 extern char *pcMainGetTaskStatusMessage( void );
\r
226 /* Unused parameter. */
\r
227 ( void ) iBufferLength;
\r
229 /* The SSI handler function that generates text depending on the index of
\r
230 the SSI tag encountered. */
\r
234 case ssiTASK_STATS_INDEX :
\r
235 vTaskList( pcBuffer );
\r
238 case ssiRUN_TIME_STATS_INDEX :
\r
239 vTaskGetRunTimeStats( pcBuffer );
\r
243 /* Include a count of the number of times an SSI function has been executed
\r
244 in the returned string. */
\r
246 sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );
\r
247 strcat( pcBuffer, cUpdateString );
\r
249 return strlen( pcBuffer );
\r
251 /*-----------------------------------------------------------*/
\r
253 signed char *pcLwipAppsBlockingGetTxBuffer( void )
\r
255 signed char *pcReturn;
\r
257 /* Attempt to obtain the semaphore that guards the Tx buffer. */
\r
258 if( xSemaphoreTakeRecursive( xTxBufferMutex, lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ) == pdFAIL )
\r
260 /* The semaphore could not be obtained before timing out. */
\r
265 /* The semaphore was obtained successfully. Return a pointer to the
\r
267 pcReturn = cTxBuffer;
\r
272 /*-----------------------------------------------------------*/
\r
274 void vLwipAppsReleaseTxBuffer( void )
\r
276 /* Finished with the Tx buffer. Return the mutex. */
\r
277 xSemaphoreGiveRecursive( xTxBufferMutex );
\r