2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 *******************************************************************************
\r
31 * -NOTE- The Win32 port is a simulation (or is that emulation?) only! Do not
\r
32 * expect to get real time behaviour from the Win32 port or this demo
\r
33 * application. It is provided as a convenient development and demonstration
\r
34 * test bed only. This was tested using Windows XP on a dual core laptop.
\r
36 * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
\r
37 * - http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
\r
38 * - Note that the above linked page describes the simulator environment. It
\r
39 * - is not the correct page to view for information on using this lwIP demo.
\r
40 *******************************************************************************
\r
42 * This project demonstrates use of the lwIP stack. The lwIP raw API is
\r
43 * demonstrated by a simple http server that comes as part of the lwIP
\r
44 * distribution - and executes in the tcpip task. The lwIP sockets API
\r
45 * is demonstrated by a simple command line interpreter interface, which
\r
46 * executes in its own task.
\r
48 * Both the http and command line server can be used to view task stats, and
\r
49 * run time stats. Task stats give a snapshot of the state of each task in
\r
50 * the system. Run time stats show how much processing time has been allocated
\r
51 * to each task. A few of the standard demo tasks are created, just to ensure
\r
52 * there is some data to be viewed.
\r
54 * Finally, a check timer is created. The check timer is a software timer that
\r
55 * inspects the few standard demo tasks that are created to ensure they are
\r
56 * executing as expected. It maintains a status string that can be viewed on
\r
57 * the "task stats" page served by the web server.
\r
59 * More information about this demo, including details of how to set up the
\r
60 * network interface, and the command line commands that are available, is
\r
61 * available on the documentation page for this demo on the
\r
62 * http://www.FreeRTOS.org web site.
\r
67 /* Standard includes. */
\r
70 /* Kernel includes. */
\r
71 #include <FreeRTOS.h>
\r
75 /* Standard demo includes. */
\r
76 #include "GenQTest.h"
\r
78 /* lwIP includes. */
\r
79 #include "lwip/tcpip.h"
\r
80 #include "lwIP_Apps.h"
\r
82 /* Utils includes. */
\r
83 #include "CommandInterpreter.h"
\r
85 /* Priorities at which the tasks are created. */
\r
86 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
88 /* The period at which the check timer will expire, in ms, provided no errors
\r
89 have been reported by any of the standard demo tasks. ms are converted to the
\r
90 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
91 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
93 /* Check timer callback function. */
\r
94 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
96 /* Defined in lwIPApps.c. */
\r
97 extern void lwIPAppsInit( void *pvArguments );
\r
99 /* Callbacks to handle the command line commands defined by the xTaskStats and
\r
100 xRunTimeStats command definitions respectively. These functions are not
\r
101 necessarily reentrant! They must be used from one task only - or at least by
\r
102 only one task at a time. */
\r
103 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );
\r
104 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );
\r
106 /* The string that latches the current demo status. */
\r
107 static char *pcStatusMessage = "All tasks running without error";
\r
109 /* Variables used in the creation of the run time stats time base. Run time
\r
110 stats record how much time each task spends in the Running state. */
\r
111 long long llInitialRunTimeCounterValue = 0LL, llRunTimeStatsDivisor = 0LL;
\r
113 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
115 static TimerHandle_t xCheckTimer = NULL;
\r
117 /* Structure that defines the "run-time-stats" command line command. */
\r
118 static const xCommandLineInput xRunTimeStats =
\r
121 "run-time-stats: Displays a table showing how much processing time each FreeRTOS task has used\r\n",
\r
122 prvRunTimeStatsCommand,
\r
126 /* Structure that defines the "task-stats" command line command. */
\r
127 static const xCommandLineInput xTaskStats =
\r
130 "task-stats: Displays a table showing the state of each FreeRTOS task\r\n",
\r
131 prvTaskStatsCommand,
\r
135 /*-----------------------------------------------------------*/
\r
139 const unsigned long ulLongTime_ms = 1000UL;
\r
141 /* This call creates the TCP/IP thread. */
\r
142 tcpip_init( lwIPAppsInit, NULL );
\r
144 /* Create and start the check timer, as described at the top of this file. */
\r
145 xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */
\r
146 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
147 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
148 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
149 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
152 /* Sanity check that the timer was created. */
\r
153 configASSERT( xCheckTimer );
\r
155 /* Start the check timer. */
\r
156 xTimerStart( xCheckTimer, 0UL );
\r
158 /* Create a few standard demo tasks, just so there are tasks running to
\r
159 view on the web server and via the command line command interpreter. */
\r
160 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
162 /* Register two command line commands to show task stats and run time stats
\r
164 xCmdIntRegisterCommand( &xTaskStats );
\r
165 xCmdIntRegisterCommand( &xRunTimeStats );
\r
167 /* Start the scheduler itself. */
\r
168 vTaskStartScheduler();
\r
170 /* This line should never be reached. If it does execute then there was
\r
171 insufficient FreeRTOS heap memory available for the idle and/or timer
\r
172 tasks to be created. */
\r
175 Sleep( ulLongTime_ms );
\r
178 /*-----------------------------------------------------------*/
\r
180 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
182 /* The parameter is not used in this case. */
\r
185 /* Check the standard demo tasks are running without error. Latch the
\r
186 latest reported error in the pcStatusMessage character pointer. The latched
\r
187 string can be viewed using the embedded web server and the command line
\r
188 interpreter. This project is really to demonstrate the lwIP stack - so very
\r
189 few tasks are created - and those that are created are created purely so
\r
190 there is something to view. */
\r
191 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
193 pcStatusMessage = "Error: The GenQueue test reported an error.";
\r
196 /*-----------------------------------------------------------*/
\r
198 void vApplicationIdleHook( void )
\r
200 const unsigned long ulMSToSleep = 5;
\r
202 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
203 tasks waiting to be terminated by the idle task. */
\r
204 Sleep( ulMSToSleep );
\r
206 /*-----------------------------------------------------------*/
\r
208 void vApplicationMallocFailedHook( void )
\r
210 const unsigned long ulLongSleep = 1000UL;
\r
212 /* Can be implemented if required, but probably not required in this
\r
213 environment and running this demo. */
\r
214 taskDISABLE_INTERRUPTS();
\r
217 Sleep( ulLongSleep );
\r
220 /*-----------------------------------------------------------*/
\r
222 void vApplicationStackOverflowHook( void )
\r
224 const unsigned long ulLongSleep = 1000UL;
\r
226 /* Can be implemented if required, but probably not required in this
\r
227 environment and running this demo. */
\r
228 taskDISABLE_INTERRUPTS();
\r
231 Sleep( ulLongSleep );
\r
234 /*-----------------------------------------------------------*/
\r
236 void vAssertCalled( void )
\r
238 const unsigned long ulLongSleep = 1000UL;
\r
240 taskDISABLE_INTERRUPTS();
\r
243 Sleep( ulLongSleep );
\r
246 /*-----------------------------------------------------------*/
\r
248 char *pcMainGetTaskStatusMessage( void )
\r
250 return pcStatusMessage;
\r
252 /*-----------------------------------------------------------*/
\r
254 void vMainConfigureTimerForRunTimeStats( void )
\r
256 LARGE_INTEGER liPerformanceCounterFrequency, liInitialRunTimeValue;
\r
258 /* Initialise the variables used to create the run time stats time base.
\r
259 Run time stats record how much time each task spends in the Running
\r
262 if( QueryPerformanceFrequency( &liPerformanceCounterFrequency ) == 0 )
\r
264 llRunTimeStatsDivisor = 1;
\r
268 /* How many times does the performance counter increment in 10ms? */
\r
269 llRunTimeStatsDivisor = liPerformanceCounterFrequency.QuadPart / 1000LL;
\r
271 /* What is the performance counter value now, this will be subtracted
\r
272 from readings taken at run time. */
\r
273 QueryPerformanceCounter( &liInitialRunTimeValue );
\r
274 llInitialRunTimeCounterValue = liInitialRunTimeValue.QuadPart;
\r
277 /*-----------------------------------------------------------*/
\r
279 unsigned long ulMainGetRunTimeCounterValue( void )
\r
281 LARGE_INTEGER liCurrentCount;
\r
282 unsigned long ulReturn;
\r
284 /* What is the performance counter value now? */
\r
285 QueryPerformanceCounter( &liCurrentCount );
\r
287 /* Subtract the performance counter value reading taken when the
\r
288 application started to get a count from that reference point, then
\r
289 scale to a 32 bit number. */
\r
290 ulReturn = ( unsigned long ) ( ( liCurrentCount.QuadPart - llInitialRunTimeCounterValue ) / llRunTimeStatsDivisor );
\r
294 /*-----------------------------------------------------------*/
\r
296 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString )
\r
298 const char *const pcHeader = "Task State Priority Stack #\r\n************************************************\r\n";
\r
300 configASSERT( pcWriteBuffer );
\r
302 /* This function assumes the buffer length is adequate and does not look
\r
304 ( void ) xWriteBufferLen;
\r
305 ( void ) pcCommandString;
\r
307 /* Generate a table of task stats. */
\r
308 strcpy( pcWriteBuffer, pcHeader );
\r
309 vTaskList( pcWriteBuffer + strlen( pcHeader ) );
\r
311 /* There is no more data to return after this single string, so return
\r
315 /*-----------------------------------------------------------*/
\r
317 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString )
\r
319 const char * const pcHeader = "Task Abs Time % Time\r\n****************************************\r\n";
\r
321 configASSERT( pcWriteBuffer );
\r
323 /* This function assumes the buffer length is adequate and does not look
\r
325 ( void ) xWriteBufferLen;
\r
326 ( void ) pcCommandString;
\r
328 /* Generate a table of task stats. */
\r
329 strcpy( pcWriteBuffer, pcHeader );
\r
330 vTaskGetRunTimeStats( ( char * ) pcWriteBuffer + strlen( pcHeader ) );
\r
332 /* There is no more data to return after this single string, so return
\r