2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 *******************************************************************************
\r
77 * -NOTE- The Win32 port is a simulation (or is that emulation?) only! Do not
\r
78 * expect to get real time behaviour from the Win32 port or this demo
\r
79 * application. It is provided as a convenient development and demonstration
\r
80 * test bed only. This was tested using Windows XP on a dual core laptop.
\r
82 * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
\r
83 * - http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
\r
84 * - Note that the above linked page describes the simulator environment. It
\r
85 * - is not the correct page to view for information on using this lwIP demo.
\r
86 *******************************************************************************
\r
88 * This project demonstrates use of the lwIP stack. The lwIP raw API is
\r
89 * demonstrated by a simple http server that comes as part of the lwIP
\r
90 * distribution - and executes in the tcpip task. The lwIP sockets API
\r
91 * is demonstrated by a simple command line interpreter interface, which
\r
92 * executes in its own task.
\r
94 * Both the http and command line server can be used to view task stats, and
\r
95 * run time stats. Task stats give a snapshot of the state of each task in
\r
96 * the system. Run time stats show how much processing time has been allocated
\r
97 * to each task. A few of the standard demo tasks are created, just to ensure
\r
98 * there is some data to be viewed.
\r
100 * Finally, a check timer is created. The check timer is a software timer that
\r
101 * inspects the few standard demo tasks that are created to ensure they are
\r
102 * executing as expected. It maintains a status string that can be viewed on
\r
103 * the "task stats" page served by the web server.
\r
105 * More information about this demo, including details of how to set up the
\r
106 * network interface, and the command line commands that are available, is
\r
107 * available on the documentation page for this demo on the
\r
108 * http://www.FreeRTOS.org web site.
\r
113 /* Standard includes. */
\r
116 /* Kernel includes. */
\r
117 #include <FreeRTOS.h>
\r
119 #include "timers.h"
\r
121 /* Standard demo includes. */
\r
122 #include "GenQTest.h"
\r
124 /* lwIP includes. */
\r
125 #include "lwip/tcpip.h"
\r
126 #include "lwIP_Apps.h"
\r
128 /* Utils includes. */
\r
129 #include "CommandInterpreter.h"
\r
131 /* Priorities at which the tasks are created. */
\r
132 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
134 /* The period at which the check timer will expire, in ms, provided no errors
\r
135 have been reported by any of the standard demo tasks. ms are converted to the
\r
136 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
137 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
139 /* Check timer callback function. */
\r
140 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
142 /* Defined in lwIPApps.c. */
\r
143 extern void lwIPAppsInit( void *pvArguments );
\r
145 /* Callbacks to handle the command line commands defined by the xTaskStats and
\r
146 xRunTimeStats command definitions respectively. These functions are not
\r
147 necessarily reentrant! They must be used from one task only - or at least by
\r
148 only one task at a time. */
\r
149 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );
\r
150 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );
\r
152 /* The string that latches the current demo status. */
\r
153 static char *pcStatusMessage = "All tasks running without error";
\r
155 /* Variables used in the creation of the run time stats time base. Run time
\r
156 stats record how much time each task spends in the Running state. */
\r
157 long long llInitialRunTimeCounterValue = 0LL, llRunTimeStatsDivisor = 0LL;
\r
159 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
161 static TimerHandle_t xCheckTimer = NULL;
\r
163 /* Structure that defines the "run-time-stats" command line command. */
\r
164 static const xCommandLineInput xRunTimeStats =
\r
167 "run-time-stats: Displays a table showing how much processing time each FreeRTOS task has used\r\n",
\r
168 prvRunTimeStatsCommand,
\r
172 /* Structure that defines the "task-stats" command line command. */
\r
173 static const xCommandLineInput xTaskStats =
\r
176 "task-stats: Displays a table showing the state of each FreeRTOS task\r\n",
\r
177 prvTaskStatsCommand,
\r
181 /*-----------------------------------------------------------*/
\r
185 const unsigned long ulLongTime_ms = 1000UL;
\r
187 /* This call creates the TCP/IP thread. */
\r
188 tcpip_init( lwIPAppsInit, NULL );
\r
190 /* Create and start the check timer, as described at the top of this file. */
\r
191 xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */
\r
192 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
193 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
194 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
195 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
198 /* Sanity check that the timer was created. */
\r
199 configASSERT( xCheckTimer );
\r
201 /* Start the check timer. */
\r
202 xTimerStart( xCheckTimer, 0UL );
\r
204 /* Create a few standard demo tasks, just so there are tasks running to
\r
205 view on the web server and via the command line command interpreter. */
\r
206 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
208 /* Register two command line commands to show task stats and run time stats
\r
210 xCmdIntRegisterCommand( &xTaskStats );
\r
211 xCmdIntRegisterCommand( &xRunTimeStats );
\r
213 /* Start the scheduler itself. */
\r
214 vTaskStartScheduler();
\r
216 /* This line should never be reached. If it does execute then there was
\r
217 insufficient FreeRTOS heap memory available for the idle and/or timer
\r
218 tasks to be created. */
\r
221 Sleep( ulLongTime_ms );
\r
224 /*-----------------------------------------------------------*/
\r
226 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
228 /* The parameter is not used in this case. */
\r
231 /* Check the standard demo tasks are running without error. Latch the
\r
232 latest reported error in the pcStatusMessage character pointer. The latched
\r
233 string can be viewed using the embedded web server and the command line
\r
234 interpreter. This project is really to demonstrate the lwIP stack - so very
\r
235 few tasks are created - and those that are created are created purely so
\r
236 there is something to view. */
\r
237 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
239 pcStatusMessage = "Error: The GenQueue test reported an error.";
\r
242 /*-----------------------------------------------------------*/
\r
244 void vApplicationIdleHook( void )
\r
246 const unsigned long ulMSToSleep = 5;
\r
248 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
249 tasks waiting to be terminated by the idle task. */
\r
250 Sleep( ulMSToSleep );
\r
252 /*-----------------------------------------------------------*/
\r
254 void vApplicationMallocFailedHook( void )
\r
256 const unsigned long ulLongSleep = 1000UL;
\r
258 /* Can be implemented if required, but probably not required in this
\r
259 environment and running this demo. */
\r
260 taskDISABLE_INTERRUPTS();
\r
263 Sleep( ulLongSleep );
\r
266 /*-----------------------------------------------------------*/
\r
268 void vApplicationStackOverflowHook( void )
\r
270 const unsigned long ulLongSleep = 1000UL;
\r
272 /* Can be implemented if required, but probably not required in this
\r
273 environment and running this demo. */
\r
274 taskDISABLE_INTERRUPTS();
\r
277 Sleep( ulLongSleep );
\r
280 /*-----------------------------------------------------------*/
\r
282 void vAssertCalled( void )
\r
284 const unsigned long ulLongSleep = 1000UL;
\r
286 taskDISABLE_INTERRUPTS();
\r
289 Sleep( ulLongSleep );
\r
292 /*-----------------------------------------------------------*/
\r
294 char *pcMainGetTaskStatusMessage( void )
\r
296 return pcStatusMessage;
\r
298 /*-----------------------------------------------------------*/
\r
300 void vMainConfigureTimerForRunTimeStats( void )
\r
302 LARGE_INTEGER liPerformanceCounterFrequency, liInitialRunTimeValue;
\r
304 /* Initialise the variables used to create the run time stats time base.
\r
305 Run time stats record how much time each task spends in the Running
\r
308 if( QueryPerformanceFrequency( &liPerformanceCounterFrequency ) == 0 )
\r
310 llRunTimeStatsDivisor = 1;
\r
314 /* How many times does the performance counter increment in 10ms? */
\r
315 llRunTimeStatsDivisor = liPerformanceCounterFrequency.QuadPart / 1000LL;
\r
317 /* What is the performance counter value now, this will be subtracted
\r
318 from readings taken at run time. */
\r
319 QueryPerformanceCounter( &liInitialRunTimeValue );
\r
320 llInitialRunTimeCounterValue = liInitialRunTimeValue.QuadPart;
\r
323 /*-----------------------------------------------------------*/
\r
325 unsigned long ulMainGetRunTimeCounterValue( void )
\r
327 LARGE_INTEGER liCurrentCount;
\r
328 unsigned long ulReturn;
\r
330 /* What is the performance counter value now? */
\r
331 QueryPerformanceCounter( &liCurrentCount );
\r
333 /* Subtract the performance counter value reading taken when the
\r
334 application started to get a count from that reference point, then
\r
335 scale to a 32 bit number. */
\r
336 ulReturn = ( unsigned long ) ( ( liCurrentCount.QuadPart - llInitialRunTimeCounterValue ) / llRunTimeStatsDivisor );
\r
340 /*-----------------------------------------------------------*/
\r
342 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString )
\r
344 const char *const pcHeader = "Task State Priority Stack #\r\n************************************************\r\n";
\r
346 configASSERT( pcWriteBuffer );
\r
348 /* This function assumes the buffer length is adequate and does not look
\r
350 ( void ) xWriteBufferLen;
\r
351 ( void ) pcCommandString;
\r
353 /* Generate a table of task stats. */
\r
354 strcpy( pcWriteBuffer, pcHeader );
\r
355 vTaskList( pcWriteBuffer + strlen( pcHeader ) );
\r
357 /* There is no more data to return after this single string, so return
\r
361 /*-----------------------------------------------------------*/
\r
363 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString )
\r
365 const char * const pcHeader = "Task Abs Time % Time\r\n****************************************\r\n";
\r
367 configASSERT( pcWriteBuffer );
\r
369 /* This function assumes the buffer length is adequate and does not look
\r
371 ( void ) xWriteBufferLen;
\r
372 ( void ) pcCommandString;
\r
374 /* Generate a table of task stats. */
\r
375 strcpy( pcWriteBuffer, pcHeader );
\r
376 vTaskGetRunTimeStats( ( char * ) pcWriteBuffer + strlen( pcHeader ) );
\r
378 /* There is no more data to return after this single string, so return
\r