2 FreeRTOS V8.0.0:rc1 - 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 distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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
66 /* FreeRTOS includes. */
\r
67 #include "FreeRTOS.h"
\r
70 /* FreeRTOS+CLI includes. */
\r
71 #include "FreeRTOS_CLI.h"
\r
73 /* FreeRTOS+Trace includes. */
\r
74 #include "trcUser.h"
\r
77 * Writes trace data to a disk file when the trace recording is stopped.
\r
78 * This function will simply overwrite any trace files that already exist.
\r
80 static void prvSaveTraceFile( void );
\r
83 * Defines a command that returns a table showing the state of each task at the
\r
84 * time the command is called.
\r
86 static portBASE_TYPE prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
89 * Defines a command that returns a table showing how much time each task has
\r
90 * spent in the Running state.
\r
92 static portBASE_TYPE prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
95 * Defines a command that expects exactly three parameters. Each of the three
\r
96 * parameter are echoed back one at a time.
\r
98 static portBASE_TYPE prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
101 * Defines a command that can take a variable number of parameters. Each
\r
102 * parameter is echoes back one at a time.
\r
104 static portBASE_TYPE prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
107 * Defines a command that starts/stops events being recorded for offline viewing
\r
108 * in FreeRTOS+Trace.
\r
110 static portBASE_TYPE prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
112 /* Structure that defines the "run-time-stats" command line command. */
\r
113 static const CLI_Command_Definition_t xRunTimeStats =
\r
115 "run-time-stats", /* The command string to type. */
\r
116 "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n\r\n",
\r
117 prvRunTimeStatsCommand, /* The function to run. */
\r
118 0 /* No parameters are expected. */
\r
121 /* Structure that defines the "task-stats" command line command. */
\r
122 static const CLI_Command_Definition_t xTaskStats =
\r
124 "task-stats", /* The command string to type. */
\r
125 "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n\r\n",
\r
126 prvTaskStatsCommand, /* The function to run. */
\r
127 0 /* No parameters are expected. */
\r
130 /* Structure that defines the "echo_3_parameters" command line command. This
\r
131 takes exactly three parameters that the command simply echos back one at a
\r
133 static const CLI_Command_Definition_t xThreeParameterEcho =
\r
135 "echo_3_parameters",
\r
136 "\r\necho_3_parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n\r\n",
\r
137 prvThreeParameterEchoCommand, /* The function to run. */
\r
138 3 /* Three parameters are expected, which can take any value. */
\r
141 /* Structure that defines the "echo_parameters" command line command. This
\r
142 takes a variable number of parameters that the command simply echos back one at
\r
144 static const CLI_Command_Definition_t xParameterEcho =
\r
147 "\r\necho_parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n\r\n",
\r
148 prvParameterEchoCommand, /* The function to run. */
\r
149 -1 /* The user can enter any number of commands. */
\r
152 /* Structure that defines the "trace" command line command. This takes a single
\r
153 parameter, which can be either "start" or "stop". */
\r
154 static const CLI_Command_Definition_t xStartTrace =
\r
157 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n\r\n",
\r
158 prvStartStopTraceCommand, /* The function to run. */
\r
159 1 /* One parameter is expected. Valid values are "start" and "stop". */
\r
162 /*-----------------------------------------------------------*/
\r
164 void vRegisterCLICommands( void )
\r
166 /* Register all the command line commands defined immediately above. */
\r
167 FreeRTOS_CLIRegisterCommand( &xTaskStats );
\r
168 FreeRTOS_CLIRegisterCommand( &xRunTimeStats );
\r
169 FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );
\r
170 FreeRTOS_CLIRegisterCommand( &xParameterEcho );
\r
171 FreeRTOS_CLIRegisterCommand( &xStartTrace );
\r
173 /*-----------------------------------------------------------*/
\r
175 static portBASE_TYPE prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
177 const char *const pcHeader = "Task State Priority Stack #\r\n************************************************\r\n";
\r
179 /* Remove compile time warnings about unused parameters, and check the
\r
180 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
181 write buffer length is adequate, so does not check for buffer overflows. */
\r
182 ( void ) pcCommandString;
\r
183 ( void ) xWriteBufferLen;
\r
184 configASSERT( pcWriteBuffer );
\r
186 /* Generate a table of task stats. */
\r
187 strcpy( pcWriteBuffer, pcHeader );
\r
188 vTaskList( pcWriteBuffer + strlen( pcHeader ) );
\r
190 /* There is no more data to return after this single string, so return
\r
194 /*-----------------------------------------------------------*/
\r
196 static portBASE_TYPE prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
198 const char * const pcHeader = "Task Abs Time % Time\r\n****************************************\r\n";
\r
200 /* Remove compile time warnings about unused parameters, and check the
\r
201 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
202 write buffer length is adequate, so does not check for buffer overflows. */
\r
203 ( void ) pcCommandString;
\r
204 ( void ) xWriteBufferLen;
\r
205 configASSERT( pcWriteBuffer );
\r
207 /* Generate a table of task stats. */
\r
208 strcpy( pcWriteBuffer, pcHeader );
\r
209 vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );
\r
211 /* There is no more data to return after this single string, so return
\r
215 /*-----------------------------------------------------------*/
\r
217 static portBASE_TYPE prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
219 const char *pcParameter;
\r
220 portBASE_TYPE lParameterStringLength, xReturn;
\r
221 static portBASE_TYPE lParameterNumber = 0;
\r
223 /* Remove compile time warnings about unused parameters, and check the
\r
224 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
225 write buffer length is adequate, so does not check for buffer overflows. */
\r
226 ( void ) pcCommandString;
\r
227 ( void ) xWriteBufferLen;
\r
228 configASSERT( pcWriteBuffer );
\r
230 if( lParameterNumber == 0 )
\r
232 /* The first time the function is called after the command has been
\r
233 entered just a header string is returned. */
\r
234 sprintf( pcWriteBuffer, "The three parameters were:\r\n" );
\r
236 /* Next time the function is called the first parameter will be echoed
\r
238 lParameterNumber = 1L;
\r
240 /* There is more data to be returned as no parameters have been echoed
\r
246 /* Obtain the parameter string. */
\r
247 pcParameter = FreeRTOS_CLIGetParameter
\r
249 pcCommandString, /* The command string itself. */
\r
250 lParameterNumber, /* Return the next parameter. */
\r
251 &lParameterStringLength /* Store the parameter string length. */
\r
254 /* Sanity check something was returned. */
\r
255 configASSERT( pcParameter );
\r
257 /* Return the parameter string. */
\r
258 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
259 sprintf( pcWriteBuffer, "%d: ", lParameterNumber );
\r
260 strncat( pcWriteBuffer, pcParameter, lParameterStringLength );
\r
261 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
263 /* If this is the last of the three parameters then there are no more
\r
264 strings to return after this one. */
\r
265 if( lParameterNumber == 3L )
\r
267 /* If this is the last of the three parameters then there are no more
\r
268 strings to return after this one. */
\r
270 lParameterNumber = 0L;
\r
274 /* There are more parameters to return after this one. */
\r
276 lParameterNumber++;
\r
282 /*-----------------------------------------------------------*/
\r
284 static portBASE_TYPE prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
286 const char *pcParameter;
\r
287 portBASE_TYPE lParameterStringLength, xReturn;
\r
288 static portBASE_TYPE lParameterNumber = 0;
\r
290 /* Remove compile time warnings about unused parameters, and check the
\r
291 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
292 write buffer length is adequate, so does not check for buffer overflows. */
\r
293 ( void ) pcCommandString;
\r
294 ( void ) xWriteBufferLen;
\r
295 configASSERT( pcWriteBuffer );
\r
297 if( lParameterNumber == 0 )
\r
299 /* The first time the function is called after the command has been
\r
300 entered just a header string is returned. */
\r
301 sprintf( pcWriteBuffer, "The parameters were:\r\n" );
\r
303 /* Next time the function is called the first parameter will be echoed
\r
305 lParameterNumber = 1L;
\r
307 /* There is more data to be returned as no parameters have been echoed
\r
313 /* Obtain the parameter string. */
\r
314 pcParameter = FreeRTOS_CLIGetParameter
\r
316 pcCommandString, /* The command string itself. */
\r
317 lParameterNumber, /* Return the next parameter. */
\r
318 &lParameterStringLength /* Store the parameter string length. */
\r
321 if( pcParameter != NULL )
\r
323 /* Return the parameter string. */
\r
324 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
325 sprintf( pcWriteBuffer, "%d: ", lParameterNumber );
\r
326 strncat( pcWriteBuffer, pcParameter, lParameterStringLength );
\r
327 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
329 /* There might be more parameters to return after this one. */
\r
331 lParameterNumber++;
\r
335 /* No more parameters were found. Make sure the write buffer does
\r
336 not contain a valid string. */
\r
337 pcWriteBuffer[ 0 ] = 0x00;
\r
339 /* No more data to return. */
\r
342 /* Start over the next time this command is executed. */
\r
343 lParameterNumber = 0;
\r
349 /*-----------------------------------------------------------*/
\r
351 static portBASE_TYPE prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
353 const char *pcParameter;
\r
354 portBASE_TYPE lParameterStringLength;
\r
356 /* Remove compile time warnings about unused parameters, and check the
\r
357 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
358 write buffer length is adequate, so does not check for buffer overflows. */
\r
359 ( void ) pcCommandString;
\r
360 ( void ) xWriteBufferLen;
\r
361 configASSERT( pcWriteBuffer );
\r
363 /* Obtain the parameter string. */
\r
364 pcParameter = FreeRTOS_CLIGetParameter
\r
366 pcCommandString, /* The command string itself. */
\r
367 1, /* Return the first parameter. */
\r
368 &lParameterStringLength /* Store the parameter string length. */
\r
371 /* Sanity check something was returned. */
\r
372 configASSERT( pcParameter );
\r
374 /* There are only two valid parameter values. */
\r
375 if( strncmp( pcParameter, "start", strlen( "start" ) ) == 0 )
\r
377 /* Start or restart the trace. */
\r
382 sprintf( pcWriteBuffer, "Trace recording (re)started.\r\n" );
\r
384 else if( strncmp( pcParameter, "stop", strlen( "stop" ) ) == 0 )
\r
386 /* End the trace, if one is running. */
\r
388 sprintf( pcWriteBuffer, "Stopping trace recording and dumping log to disk.\r\n" );
\r
389 prvSaveTraceFile();
\r
393 sprintf( pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );
\r
396 /* There is no more data to return after this single string, so return
\r
400 /*-----------------------------------------------------------*/
\r
402 static void prvSaveTraceFile( void )
\r
404 FILE* pxOutputFile;
\r
406 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
408 if( pxOutputFile != NULL )
\r
410 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
411 fclose( pxOutputFile );
\r
412 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
416 printf( "\r\nFailed to create trace dump file\r\n" );
\r