2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Very simple task that responds with a single WEB page to http requests.
\r
68 * The WEB page displays task and system status. A semaphore is used to
\r
69 * wake the task when there is processing to perform as determined by the
\r
70 * interrupts generated by the Ethernet interface.
\r
73 /* Standard includes. */
\r
77 /* Tern includes. */
\r
78 #include "utils\system_common.h"
\r
79 #include "i2chip_hw.h"
\r
82 /* FreeRTOS.org includes. */
\r
83 #include <FreeRTOS.h>
\r
87 /* The standard http port on which we are going to listen. */
\r
90 #define httpTX_WAIT 2
\r
92 /* Network address configuration. */
\r
93 const unsigned char ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
94 const unsigned char ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
95 const unsigned char ucIPAddress[] = { 172, 25, 218, 210 };
\r
96 const unsigned char ucSubnetMask[] = { 255, 255, 255, 0 };
\r
98 /* The number of sockets this task is going to handle. */
\r
99 #define httpSOCKET_NUM 3
\r
100 unsigned char ucConnection[ httpSOCKET_NUM ];
\r
102 /* The maximum data buffer size we can handle. */
\r
103 #define httpSOCKET_BUFFER_SIZE 2048
\r
105 /* Standard HTTP response. */
\r
106 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
108 /* Hard coded HTML components. Other data is generated dynamically. */
\r
109 #define HTML_OUTPUT_BEGIN "\
\r
110 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
111 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
112 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
113 <HR>Task status table:\r\n\
\r
114 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
115 ************************************************<br>"
\r
117 #define HTML_OUTPUT_END "\
\r
118 </font></BODY></HTML>"
\r
120 /*-----------------------------------------------------------*/
\r
123 * Initialise the data structures used to hold the socket status.
\r
125 static void prvHTTPInit( void );
\r
128 * Setup the Ethernet interface with the network addressing information.
\r
130 static void prvNetifInit( void );
\r
133 * Generate the dynamic components of the served WEB page and transmit the
\r
134 * entire page through the socket.
\r
136 static void prvTransmitHTTP( unsigned char socket );
\r
137 /*-----------------------------------------------------------*/
\r
139 /* This variable is simply incremented by the idle task hook so the number of
\r
140 iterations the idle task has performed can be displayed as part of the served
\r
142 unsigned long ulIdleLoops = 0UL;
\r
144 /* Data buffer shared by sockets. */
\r
145 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
147 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
148 and process whatever caused the interrupt. */
\r
149 xSemaphoreHandle xTCPSemaphore = NULL;
\r
151 /*-----------------------------------------------------------*/
\r
152 void vHTTPTask( void * pvParameters )
\r
155 unsigned char ucState;
\r
157 ( void ) pvParameters;
\r
159 /* Create the semaphore used to communicate between this task and the
\r
161 vSemaphoreCreateBinary( xTCPSemaphore );
\r
163 /* Make sure everything is setup before we start. */
\r
169 /* Wait until the ISR tells us there is something to do. */
\r
170 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
172 /* Check each socket. */
\r
173 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
175 ucState = select( i, SEL_CONTROL );
\r
179 case SOCK_ESTABLISHED : /* new connection established. */
\r
181 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
183 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
185 sLen = httpSOCKET_BUFFER_SIZE;
\r
190 sLen = recv( i, ucSocketBuffer, sLen );
\r
192 if( ucConnection[ i ] == 1 )
\r
194 /* This is our first time processing a HTTP
\r
195 request on this connection. */
\r
196 prvTransmitHTTP( i );
\r
197 ucConnection[i] = 0;
\r
203 case SOCK_CLOSE_WAIT :
\r
210 ucConnection[i] = 1;
\r
211 socket( i, SOCK_STREAM, 80, 0x00 );
\r
212 NBlisten( i ); /* reinitialize socket. */
\r
218 /*-----------------------------------------------------------*/
\r
220 static void prvHTTPInit( void )
\r
222 unsigned char ucIndex;
\r
224 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
225 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
227 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
228 NBlisten( ucIndex );
\r
229 ucConnection[ ucIndex ] = 1;
\r
232 /*-----------------------------------------------------------*/
\r
234 static void prvNetifInit( void )
\r
239 setMACAddr( ( unsigned char * ) ucMacAddress );
\r
240 setgateway( ( unsigned char * ) ucGatewayAddress );
\r
241 setsubmask( ( unsigned char * ) ucSubnetMask );
\r
242 setIP( ( unsigned char * ) ucIPAddress );
\r
244 /* See definition of 'sysinit' in socket.c
\r
245 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
246 are shared by all 4 channels.
\r
247 - (0x55, 0x55) configures the send and receive buffers at
\r
248 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
249 sysinit( 0x55, 0x55 );
\r
251 /*-----------------------------------------------------------*/
\r
253 static void prvTransmitHTTP(unsigned char socket)
\r
255 extern short usCheckStatus;
\r
257 /* Send the http and html headers. */
\r
258 send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
259 send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
261 /* Generate then send the table showing the status of each task. */
\r
262 vTaskList( ucSocketBuffer );
\r
263 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
265 /* Send the number of times the idle task has looped. */
\r
266 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
267 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
269 /* Send the tick count. */
\r
270 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
271 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
273 /* Show a message indicating whether or not the check task has discovered
\r
274 an error in any of the standard demo tasks. */
\r
275 if( usCheckStatus == 0 )
\r
277 sprintf( ucSocketBuffer, "No errors detected." );
\r
278 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
282 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
283 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
286 /* Finish the page off. */
\r
287 send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
289 /* Must make sure the data is gone before closing the socket. */
\r
290 while( !tx_empty( socket ) )
\r
292 vTaskDelay( httpTX_WAIT );
\r
296 /*-----------------------------------------------------------*/
\r
298 void vApplicationIdleHook( void )
\r