2 FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Very simple task that responds with a single WEB page to http requests.
\r
57 * The WEB page displays task and system status. A semaphore is used to
\r
58 * wake the task when there is processing to perform as determined by the
\r
59 * interrupts generated by the Ethernet interface.
\r
62 /* Standard includes. */
\r
66 /* Tern includes. */
\r
67 #include "utils\system_common.h"
\r
68 #include "i2chip_hw.h"
\r
71 /* FreeRTOS.org includes. */
\r
72 #include <FreeRTOS.h>
\r
76 /* The standard http port on which we are going to listen. */
\r
79 #define httpTX_WAIT 2
\r
81 /* Network address configuration. */
\r
82 const unsigned char ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
83 const unsigned char ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
84 const unsigned char ucIPAddress[] = { 172, 25, 218, 210 };
\r
85 const unsigned char ucSubnetMask[] = { 255, 255, 255, 0 };
\r
87 /* The number of sockets this task is going to handle. */
\r
88 #define httpSOCKET_NUM 3
\r
89 unsigned char ucConnection[ httpSOCKET_NUM ];
\r
91 /* The maximum data buffer size we can handle. */
\r
92 #define httpSOCKET_BUFFER_SIZE 2048
\r
94 /* Standard HTTP response. */
\r
95 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
97 /* Hard coded HTML components. Other data is generated dynamically. */
\r
98 #define HTML_OUTPUT_BEGIN "\
\r
99 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
100 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
101 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
102 <HR>Task status table:\r\n\
\r
103 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
104 ************************************************<br>"
\r
106 #define HTML_OUTPUT_END "\
\r
107 </font></BODY></HTML>"
\r
109 /*-----------------------------------------------------------*/
\r
112 * Initialise the data structures used to hold the socket status.
\r
114 static void prvHTTPInit( void );
\r
117 * Setup the Ethernet interface with the network addressing information.
\r
119 static void prvNetifInit( void );
\r
122 * Generate the dynamic components of the served WEB page and transmit the
\r
123 * entire page through the socket.
\r
125 static void prvTransmitHTTP( unsigned char socket );
\r
126 /*-----------------------------------------------------------*/
\r
128 /* This variable is simply incremented by the idle task hook so the number of
\r
129 iterations the idle task has performed can be displayed as part of the served
\r
131 unsigned long ulIdleLoops = 0UL;
\r
133 /* Data buffer shared by sockets. */
\r
134 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
136 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
137 and process whatever caused the interrupt. */
\r
138 xSemaphoreHandle xTCPSemaphore = NULL;
\r
140 /*-----------------------------------------------------------*/
\r
141 void vHTTPTask( void * pvParameters )
\r
144 unsigned char ucState;
\r
146 ( void ) pvParameters;
\r
148 /* Create the semaphore used to communicate between this task and the
\r
150 vSemaphoreCreateBinary( xTCPSemaphore );
\r
152 /* Make sure everything is setup before we start. */
\r
158 /* Wait until the ISR tells us there is something to do. */
\r
159 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
161 /* Check each socket. */
\r
162 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
164 ucState = select( i, SEL_CONTROL );
\r
168 case SOCK_ESTABLISHED : /* new connection established. */
\r
170 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
172 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
174 sLen = httpSOCKET_BUFFER_SIZE;
\r
179 sLen = recv( i, ucSocketBuffer, sLen );
\r
181 if( ucConnection[ i ] == 1 )
\r
183 /* This is our first time processing a HTTP
\r
184 request on this connection. */
\r
185 prvTransmitHTTP( i );
\r
186 ucConnection[i] = 0;
\r
192 case SOCK_CLOSE_WAIT :
\r
199 ucConnection[i] = 1;
\r
200 socket( i, SOCK_STREAM, 80, 0x00 );
\r
201 NBlisten( i ); /* reinitialize socket. */
\r
207 /*-----------------------------------------------------------*/
\r
209 static void prvHTTPInit( void )
\r
211 unsigned char ucIndex;
\r
213 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
214 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
216 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
217 NBlisten( ucIndex );
\r
218 ucConnection[ ucIndex ] = 1;
\r
221 /*-----------------------------------------------------------*/
\r
223 static void prvNetifInit( void )
\r
228 setMACAddr( ( unsigned char * ) ucMacAddress );
\r
229 setgateway( ( unsigned char * ) ucGatewayAddress );
\r
230 setsubmask( ( unsigned char * ) ucSubnetMask );
\r
231 setIP( ( unsigned char * ) ucIPAddress );
\r
233 /* See definition of 'sysinit' in socket.c
\r
234 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
235 are shared by all 4 channels.
\r
236 - (0x55, 0x55) configures the send and receive buffers at
\r
237 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
238 sysinit( 0x55, 0x55 );
\r
240 /*-----------------------------------------------------------*/
\r
242 static void prvTransmitHTTP(unsigned char socket)
\r
244 extern short usCheckStatus;
\r
246 /* Send the http and html headers. */
\r
247 send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
248 send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
250 /* Generate then send the table showing the status of each task. */
\r
251 vTaskList( ucSocketBuffer );
\r
252 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
254 /* Send the number of times the idle task has looped. */
\r
255 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
256 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
258 /* Send the tick count. */
\r
259 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
260 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
262 /* Show a message indicating whether or not the check task has discovered
\r
263 an error in any of the standard demo tasks. */
\r
264 if( usCheckStatus == 0 )
\r
266 sprintf( ucSocketBuffer, "No errors detected." );
\r
267 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
271 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
272 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
275 /* Finish the page off. */
\r
276 send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
278 /* Must make sure the data is gone before closing the socket. */
\r
279 while( !tx_empty( socket ) )
\r
281 vTaskDelay( httpTX_WAIT );
\r
285 /*-----------------------------------------------------------*/
\r
287 void vApplicationIdleHook( void )
\r