2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * Very simple task that responds with a single WEB page to http requests.
\r
99 * The WEB page displays task and system status. A semaphore is used to
\r
100 * wake the task when there is processing to perform as determined by the
\r
101 * interrupts generated by the Ethernet interface.
\r
104 /* Standard includes. */
\r
105 #include <string.h>
\r
108 /* Tern includes. */
\r
109 #include "utils\system_common.h"
\r
110 #include "i2chip_hw.h"
\r
111 #include "socket.h"
\r
113 /* FreeRTOS.org includes. */
\r
114 #include <FreeRTOS.h>
\r
116 #include <semphr.h>
\r
118 /* The standard http port on which we are going to listen. */
\r
119 #define httpPORT 80
\r
121 #define httpTX_WAIT 2
\r
123 /* Network address configuration. */
\r
124 const unsigned char ucMacAddress[] = { 12, 128, 12, 34, 56, 78 };
\r
125 const unsigned char ucGatewayAddress[] = { 192, 168, 2, 1 };
\r
126 const unsigned char ucIPAddress[] = { 172, 25, 218, 210 };
\r
127 const unsigned char ucSubnetMask[] = { 255, 255, 255, 0 };
\r
129 /* The number of sockets this task is going to handle. */
\r
130 #define httpSOCKET_NUM 3
\r
131 unsigned char ucConnection[ httpSOCKET_NUM ];
\r
133 /* The maximum data buffer size we can handle. */
\r
134 #define httpSOCKET_BUFFER_SIZE 2048
\r
136 /* Standard HTTP response. */
\r
137 #define httpOUTPUT_OK "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
\r
139 /* Hard coded HTML components. Other data is generated dynamically. */
\r
140 #define HTML_OUTPUT_BEGIN "\
\r
141 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
\r
142 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
\r
143 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
\r
144 <HR>Task status table:\r\n\
\r
145 <p><font face=\"courier\"><pre>Task State Priority Stack #<br>\
\r
146 ************************************************<br>"
\r
148 #define HTML_OUTPUT_END "\
\r
149 </font></BODY></HTML>"
\r
151 /*-----------------------------------------------------------*/
\r
154 * Initialise the data structures used to hold the socket status.
\r
156 static void prvHTTPInit( void );
\r
159 * Setup the Ethernet interface with the network addressing information.
\r
161 static void prvNetifInit( void );
\r
164 * Generate the dynamic components of the served WEB page and transmit the
\r
165 * entire page through the socket.
\r
167 static void prvTransmitHTTP( unsigned char socket );
\r
168 /*-----------------------------------------------------------*/
\r
170 /* This variable is simply incremented by the idle task hook so the number of
\r
171 iterations the idle task has performed can be displayed as part of the served
\r
173 unsigned long ulIdleLoops = 0UL;
\r
175 /* Data buffer shared by sockets. */
\r
176 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
\r
178 /* The semaphore used by the Ethernet ISR to signal that the task should wake
\r
179 and process whatever caused the interrupt. */
\r
180 SemaphoreHandle_t xTCPSemaphore = NULL;
\r
182 /*-----------------------------------------------------------*/
\r
183 void vHTTPTask( void * pvParameters )
\r
186 unsigned char ucState;
\r
188 ( void ) pvParameters;
\r
190 /* Create the semaphore used to communicate between this task and the
\r
192 vSemaphoreCreateBinary( xTCPSemaphore );
\r
194 /* Make sure everything is setup before we start. */
\r
200 /* Wait until the ISR tells us there is something to do. */
\r
201 xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
\r
203 /* Check each socket. */
\r
204 for( i = 0; i < httpSOCKET_NUM; i++ )
\r
206 ucState = select( i, SEL_CONTROL );
\r
210 case SOCK_ESTABLISHED : /* new connection established. */
\r
212 if( ( sLen = select( i, SEL_RECV ) ) > 0 )
\r
214 if( sLen > httpSOCKET_BUFFER_SIZE )
\r
216 sLen = httpSOCKET_BUFFER_SIZE;
\r
221 sLen = recv( i, ucSocketBuffer, sLen );
\r
223 if( ucConnection[ i ] == 1 )
\r
225 /* This is our first time processing a HTTP
\r
226 request on this connection. */
\r
227 prvTransmitHTTP( i );
\r
228 ucConnection[i] = 0;
\r
234 case SOCK_CLOSE_WAIT :
\r
241 ucConnection[i] = 1;
\r
242 socket( i, SOCK_STREAM, 80, 0x00 );
\r
243 NBlisten( i ); /* reinitialize socket. */
\r
249 /*-----------------------------------------------------------*/
\r
251 static void prvHTTPInit( void )
\r
253 unsigned char ucIndex;
\r
255 /* There are 4 total sockets available; we will claim 3 for HTTP. */
\r
256 for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
\r
258 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
\r
259 NBlisten( ucIndex );
\r
260 ucConnection[ ucIndex ] = 1;
\r
263 /*-----------------------------------------------------------*/
\r
265 static void prvNetifInit( void )
\r
270 setMACAddr( ( unsigned char * ) ucMacAddress );
\r
271 setgateway( ( unsigned char * ) ucGatewayAddress );
\r
272 setsubmask( ( unsigned char * ) ucSubnetMask );
\r
273 setIP( ( unsigned char * ) ucIPAddress );
\r
275 /* See definition of 'sysinit' in socket.c
\r
276 - 8 KB transmit buffer, and 8 KB receive buffer available. These buffers
\r
277 are shared by all 4 channels.
\r
278 - (0x55, 0x55) configures the send and receive buffers at
\r
279 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
\r
280 sysinit( 0x55, 0x55 );
\r
282 /*-----------------------------------------------------------*/
\r
284 static void prvTransmitHTTP(unsigned char socket)
\r
286 extern short usCheckStatus;
\r
288 /* Send the http and html headers. */
\r
289 send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
\r
290 send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
\r
292 /* Generate then send the table showing the status of each task. */
\r
293 vTaskList( ( char * ) ucSocketBuffer );
\r
294 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
296 /* Send the number of times the idle task has looped. */
\r
297 sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
\r
298 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
300 /* Send the tick count. */
\r
301 sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
\r
302 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
304 /* Show a message indicating whether or not the check task has discovered
\r
305 an error in any of the standard demo tasks. */
\r
306 if( usCheckStatus == 0 )
\r
308 sprintf( ucSocketBuffer, "No errors detected." );
\r
309 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
313 sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
\r
314 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
\r
317 /* Finish the page off. */
\r
318 send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
\r
320 /* Must make sure the data is gone before closing the socket. */
\r
321 while( !tx_empty( socket ) )
\r
323 vTaskDelay( httpTX_WAIT );
\r
327 /*-----------------------------------------------------------*/
\r
329 void vApplicationIdleHook( void )
\r