2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * Very simple task that responds with a single WEB page to http requests.
\r
69 * The WEB page displays task and system status. A semaphore is used to
\r
70 * wake the task when there is processing to perform as determined by the
\r
71 * interrupts generated by the Ethernet interface.
\r
74 /* Standard includes. */
\r
78 /* Tern includes. */
\r
79 #include "utils\system_common.h"
\r
80 #include "i2chip_hw.h"
\r
83 /* FreeRTOS.org includes. */
\r
84 #include <FreeRTOS.h>
\r
88 /* The standard http port on which we are going to listen. */
\r
91 #define httpTX_WAIT 2
\r
93 /* Network address configuration. */
\r
94 const unsigned char ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
95 const unsigned char ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
96 const unsigned char ucIPAddress[] = { 172, 25, 218, 210 };
\r
97 const unsigned char ucSubnetMask[] = { 255, 255, 255, 0 };
\r
99 /* The number of sockets this task is going to handle. */
\r
100 #define httpSOCKET_NUM 3
\r
101 unsigned char ucConnection[ httpSOCKET_NUM ];
\r
103 /* The maximum data buffer size we can handle. */
\r
104 #define httpSOCKET_BUFFER_SIZE 2048
\r
106 /* Standard HTTP response. */
\r
107 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
109 /* Hard coded HTML components. Other data is generated dynamically. */
\r
110 #define HTML_OUTPUT_BEGIN "\
\r
111 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
112 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
113 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
114 <HR>Task status table:\r\n\
\r
115 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
116 ************************************************<br>"
\r
118 #define HTML_OUTPUT_END "\
\r
119 </font></BODY></HTML>"
\r
121 /*-----------------------------------------------------------*/
\r
124 * Initialise the data structures used to hold the socket status.
\r
126 static void prvHTTPInit( void );
\r
129 * Setup the Ethernet interface with the network addressing information.
\r
131 static void prvNetifInit( void );
\r
134 * Generate the dynamic components of the served WEB page and transmit the
\r
135 * entire page through the socket.
\r
137 static void prvTransmitHTTP( unsigned char socket );
\r
138 /*-----------------------------------------------------------*/
\r
140 /* This variable is simply incremented by the idle task hook so the number of
\r
141 iterations the idle task has performed can be displayed as part of the served
\r
143 unsigned long ulIdleLoops = 0UL;
\r
145 /* Data buffer shared by sockets. */
\r
146 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
148 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
149 and process whatever caused the interrupt. */
\r
150 SemaphoreHandle_t xTCPSemaphore = NULL;
\r
152 /*-----------------------------------------------------------*/
\r
153 void vHTTPTask( void * pvParameters )
\r
156 unsigned char ucState;
\r
158 ( void ) pvParameters;
\r
160 /* Create the semaphore used to communicate between this task and the
\r
162 vSemaphoreCreateBinary( xTCPSemaphore );
\r
164 /* Make sure everything is setup before we start. */
\r
170 /* Wait until the ISR tells us there is something to do. */
\r
171 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
173 /* Check each socket. */
\r
174 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
176 ucState = select( i, SEL_CONTROL );
\r
180 case SOCK_ESTABLISHED : /* new connection established. */
\r
182 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
184 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
186 sLen = httpSOCKET_BUFFER_SIZE;
\r
191 sLen = recv( i, ucSocketBuffer, sLen );
\r
193 if( ucConnection[ i ] == 1 )
\r
195 /* This is our first time processing a HTTP
\r
196 request on this connection. */
\r
197 prvTransmitHTTP( i );
\r
198 ucConnection[i] = 0;
\r
204 case SOCK_CLOSE_WAIT :
\r
211 ucConnection[i] = 1;
\r
212 socket( i, SOCK_STREAM, 80, 0x00 );
\r
213 NBlisten( i ); /* reinitialize socket. */
\r
219 /*-----------------------------------------------------------*/
\r
221 static void prvHTTPInit( void )
\r
223 unsigned char ucIndex;
\r
225 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
226 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
228 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
229 NBlisten( ucIndex );
\r
230 ucConnection[ ucIndex ] = 1;
\r
233 /*-----------------------------------------------------------*/
\r
235 static void prvNetifInit( void )
\r
240 setMACAddr( ( unsigned char * ) ucMacAddress );
\r
241 setgateway( ( unsigned char * ) ucGatewayAddress );
\r
242 setsubmask( ( unsigned char * ) ucSubnetMask );
\r
243 setIP( ( unsigned char * ) ucIPAddress );
\r
245 /* See definition of 'sysinit' in socket.c
\r
246 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
247 are shared by all 4 channels.
\r
248 - (0x55, 0x55) configures the send and receive buffers at
\r
249 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
250 sysinit( 0x55, 0x55 );
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvTransmitHTTP(unsigned char socket)
\r
256 extern short usCheckStatus;
\r
258 /* Send the http and html headers. */
\r
259 send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
260 send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
262 /* Generate then send the table showing the status of each task. */
\r
263 vTaskList( ( char * ) ucSocketBuffer );
\r
264 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
266 /* Send the number of times the idle task has looped. */
\r
267 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
268 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
270 /* Send the tick count. */
\r
271 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
272 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
274 /* Show a message indicating whether or not the check task has discovered
\r
275 an error in any of the standard demo tasks. */
\r
276 if( usCheckStatus == 0 )
\r
278 sprintf( ucSocketBuffer, "No errors detected." );
\r
279 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
283 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
284 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
287 /* Finish the page off. */
\r
288 send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
290 /* Must make sure the data is gone before closing the socket. */
\r
291 while( !tx_empty( socket ) )
\r
293 vTaskDelay( httpTX_WAIT );
\r
297 /*-----------------------------------------------------------*/
\r
299 void vApplicationIdleHook( void )
\r