2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * Very simple task that responds with a single WEB page to http requests.
\r
70 * The WEB page displays task and system status. A semaphore is used to
\r
71 * wake the task when there is processing to perform as determined by the
\r
72 * interrupts generated by the Ethernet interface.
\r
75 /* Standard includes. */
\r
79 /* Tern includes. */
\r
80 #include "utils\system_common.h"
\r
81 #include "i2chip_hw.h"
\r
84 /* FreeRTOS.org includes. */
\r
85 #include <FreeRTOS.h>
\r
89 /* The standard http port on which we are going to listen. */
\r
92 #define httpTX_WAIT 2
\r
94 /* Network address configuration. */
\r
95 const unsigned char ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
96 const unsigned char ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
97 const unsigned char ucIPAddress[] = { 172, 25, 218, 210 };
\r
98 const unsigned char ucSubnetMask[] = { 255, 255, 255, 0 };
\r
100 /* The number of sockets this task is going to handle. */
\r
101 #define httpSOCKET_NUM 3
\r
102 unsigned char ucConnection[ httpSOCKET_NUM ];
\r
104 /* The maximum data buffer size we can handle. */
\r
105 #define httpSOCKET_BUFFER_SIZE 2048
\r
107 /* Standard HTTP response. */
\r
108 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
110 /* Hard coded HTML components. Other data is generated dynamically. */
\r
111 #define HTML_OUTPUT_BEGIN "\
\r
112 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
113 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
114 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
115 <HR>Task status table:\r\n\
\r
116 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
117 ************************************************<br>"
\r
119 #define HTML_OUTPUT_END "\
\r
120 </font></BODY></HTML>"
\r
122 /*-----------------------------------------------------------*/
\r
125 * Initialise the data structures used to hold the socket status.
\r
127 static void prvHTTPInit( void );
\r
130 * Setup the Ethernet interface with the network addressing information.
\r
132 static void prvNetifInit( void );
\r
135 * Generate the dynamic components of the served WEB page and transmit the
\r
136 * entire page through the socket.
\r
138 static void prvTransmitHTTP( unsigned char socket );
\r
139 /*-----------------------------------------------------------*/
\r
141 /* This variable is simply incremented by the idle task hook so the number of
\r
142 iterations the idle task has performed can be displayed as part of the served
\r
144 unsigned long ulIdleLoops = 0UL;
\r
146 /* Data buffer shared by sockets. */
\r
147 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
149 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
150 and process whatever caused the interrupt. */
\r
151 xSemaphoreHandle xTCPSemaphore = NULL;
\r
153 /*-----------------------------------------------------------*/
\r
154 void vHTTPTask( void * pvParameters )
\r
157 unsigned char ucState;
\r
159 ( void ) pvParameters;
\r
161 /* Create the semaphore used to communicate between this task and the
\r
163 vSemaphoreCreateBinary( xTCPSemaphore );
\r
165 /* Make sure everything is setup before we start. */
\r
171 /* Wait until the ISR tells us there is something to do. */
\r
172 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
174 /* Check each socket. */
\r
175 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
177 ucState = select( i, SEL_CONTROL );
\r
181 case SOCK_ESTABLISHED : /* new connection established. */
\r
183 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
185 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
187 sLen = httpSOCKET_BUFFER_SIZE;
\r
192 sLen = recv( i, ucSocketBuffer, sLen );
\r
194 if( ucConnection[ i ] == 1 )
\r
196 /* This is our first time processing a HTTP
\r
197 request on this connection. */
\r
198 prvTransmitHTTP( i );
\r
199 ucConnection[i] = 0;
\r
205 case SOCK_CLOSE_WAIT :
\r
212 ucConnection[i] = 1;
\r
213 socket( i, SOCK_STREAM, 80, 0x00 );
\r
214 NBlisten( i ); /* reinitialize socket. */
\r
220 /*-----------------------------------------------------------*/
\r
222 static void prvHTTPInit( void )
\r
224 unsigned char ucIndex;
\r
226 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
227 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
229 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
230 NBlisten( ucIndex );
\r
231 ucConnection[ ucIndex ] = 1;
\r
234 /*-----------------------------------------------------------*/
\r
236 static void prvNetifInit( void )
\r
241 setMACAddr( ( unsigned char * ) ucMacAddress );
\r
242 setgateway( ( unsigned char * ) ucGatewayAddress );
\r
243 setsubmask( ( unsigned char * ) ucSubnetMask );
\r
244 setIP( ( unsigned char * ) ucIPAddress );
\r
246 /* See definition of 'sysinit' in socket.c
\r
247 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
248 are shared by all 4 channels.
\r
249 - (0x55, 0x55) configures the send and receive buffers at
\r
250 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
251 sysinit( 0x55, 0x55 );
\r
253 /*-----------------------------------------------------------*/
\r
255 static void prvTransmitHTTP(unsigned char socket)
\r
257 extern short usCheckStatus;
\r
259 /* Send the http and html headers. */
\r
260 send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
261 send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
263 /* Generate then send the table showing the status of each task. */
\r
264 vTaskList( ucSocketBuffer );
\r
265 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
267 /* Send the number of times the idle task has looped. */
\r
268 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
269 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
271 /* Send the tick count. */
\r
272 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
273 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
275 /* Show a message indicating whether or not the check task has discovered
\r
276 an error in any of the standard demo tasks. */
\r
277 if( usCheckStatus == 0 )
\r
279 sprintf( ucSocketBuffer, "No errors detected." );
\r
280 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
284 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
285 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
288 /* Finish the page off. */
\r
289 send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
291 /* Must make sure the data is gone before closing the socket. */
\r
292 while( !tx_empty( socket ) )
\r
294 vTaskDelay( httpTX_WAIT );
\r
298 /*-----------------------------------------------------------*/
\r
300 void vApplicationIdleHook( void )
\r