2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* FreeRTOS includes. */
\r
66 #include "FreeRTOS.h"
\r
69 /* FreeRTOS+CLI includes. */
\r
70 #include "FreeRTOS_CLI.h"
\r
72 /* FreeRTOS+Trace includes. */
\r
73 #include "trcUser.h"
\r
76 * Writes trace data to a disk file when the trace recording is stopped.
\r
77 * This function will simply overwrite any trace files that already exist.
\r
79 static void prvSaveTraceFile( void );
\r
82 * Defines a command that returns a table showing the state of each task at the
\r
83 * time the command is called.
\r
85 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
88 * Defines a command that returns a table showing how much time each task has
\r
89 * spent in the Running state.
\r
91 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
94 * Defines a command that expects exactly three parameters. Each of the three
\r
95 * parameter are echoed back one at a time.
\r
97 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
100 * Defines a command that can take a variable number of parameters. Each
\r
101 * parameter is echoes back one at a time.
\r
103 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
106 * Defines a command that starts/stops events being recorded for offline viewing
\r
107 * in FreeRTOS+Trace.
\r
109 static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
111 /* Structure that defines the "run-time-stats" command line command. */
\r
112 static const CLI_Command_Definition_t xRunTimeStats =
\r
114 ( const int8_t * const ) "run-time-stats", /* The command string to type. */
\r
115 ( const int8_t * const ) "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n\r\n",
\r
116 prvRunTimeStatsCommand, /* The function to run. */
\r
117 0 /* No parameters are expected. */
\r
120 /* Structure that defines the "task-stats" command line command. */
\r
121 static const CLI_Command_Definition_t xTaskStats =
\r
123 ( const int8_t * const ) "task-stats", /* The command string to type. */
\r
124 ( const int8_t * const ) "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n\r\n",
\r
125 prvTaskStatsCommand, /* The function to run. */
\r
126 0 /* No parameters are expected. */
\r
129 /* Structure that defines the "echo_3_parameters" command line command. This
\r
130 takes exactly three parameters that the command simply echos back one at a
\r
132 static const CLI_Command_Definition_t xThreeParameterEcho =
\r
134 ( const int8_t * const ) "echo_3_parameters",
\r
135 ( const int8_t * const ) "\r\necho_3_parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n\r\n",
\r
136 prvThreeParameterEchoCommand, /* The function to run. */
\r
137 3 /* Three parameters are expected, which can take any value. */
\r
140 /* Structure that defines the "echo_parameters" command line command. This
\r
141 takes a variable number of parameters that the command simply echos back one at
\r
143 static const CLI_Command_Definition_t xParameterEcho =
\r
145 ( const int8_t * const ) "echo_parameters",
\r
146 ( const int8_t * const ) "\r\necho_parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n\r\n",
\r
147 prvParameterEchoCommand, /* The function to run. */
\r
148 -1 /* The user can enter any number of commands. */
\r
151 /* Structure that defines the "trace" command line command. This takes a single
\r
152 parameter, which can be either "start" or "stop". */
\r
153 static const CLI_Command_Definition_t xStartTrace =
\r
155 ( const int8_t * const ) "trace",
\r
156 ( const int8_t * const ) "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n\r\n",
\r
157 prvStartStopTraceCommand, /* The function to run. */
\r
158 1 /* One parameter is expected. Valid values are "start" and "stop". */
\r
161 /*-----------------------------------------------------------*/
\r
163 void vRegisterCLICommands( void )
\r
165 /* Register all the command line commands defined immediately above. */
\r
166 FreeRTOS_CLIRegisterCommand( &xTaskStats );
\r
167 FreeRTOS_CLIRegisterCommand( &xRunTimeStats );
\r
168 FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );
\r
169 FreeRTOS_CLIRegisterCommand( &xParameterEcho );
\r
170 FreeRTOS_CLIRegisterCommand( &xStartTrace );
\r
172 /*-----------------------------------------------------------*/
\r
174 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
176 const int8_t *const pcHeader = ( int8_t * ) "Task State Priority Stack #\r\n************************************************\r\n";
\r
178 /* Remove compile time warnings about unused parameters, and check the
\r
179 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
180 write buffer length is adequate, so does not check for buffer overflows. */
\r
181 ( void ) pcCommandString;
\r
182 ( void ) xWriteBufferLen;
\r
183 configASSERT( pcWriteBuffer );
\r
185 /* Generate a table of task stats. */
\r
186 strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );
\r
187 vTaskList( pcWriteBuffer + strlen( ( char * ) pcHeader ) );
\r
189 /* There is no more data to return after this single string, so return
\r
193 /*-----------------------------------------------------------*/
\r
195 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
197 const int8_t * const pcHeader = ( int8_t * ) "Task Abs Time % Time\r\n****************************************\r\n";
\r
199 /* Remove compile time warnings about unused parameters, and check the
\r
200 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
201 write buffer length is adequate, so does not check for buffer overflows. */
\r
202 ( void ) pcCommandString;
\r
203 ( void ) xWriteBufferLen;
\r
204 configASSERT( pcWriteBuffer );
\r
206 /* Generate a table of task stats. */
\r
207 strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );
\r
208 vTaskGetRunTimeStats( pcWriteBuffer + strlen( ( char * ) pcHeader ) );
\r
210 /* There is no more data to return after this single string, so return
\r
214 /*-----------------------------------------------------------*/
\r
216 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
218 int8_t *pcParameter;
\r
219 portBASE_TYPE lParameterStringLength, xReturn;
\r
220 static portBASE_TYPE lParameterNumber = 0;
\r
222 /* Remove compile time warnings about unused parameters, and check the
\r
223 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
224 write buffer length is adequate, so does not check for buffer overflows. */
\r
225 ( void ) pcCommandString;
\r
226 ( void ) xWriteBufferLen;
\r
227 configASSERT( pcWriteBuffer );
\r
229 if( lParameterNumber == 0 )
\r
231 /* The first time the function is called after the command has been
\r
232 entered just a header string is returned. */
\r
233 sprintf( ( char * ) pcWriteBuffer, "The three parameters were:\r\n" );
\r
235 /* Next time the function is called the first parameter will be echoed
\r
237 lParameterNumber = 1L;
\r
239 /* There is more data to be returned as no parameters have been echoed
\r
245 /* Obtain the parameter string. */
\r
246 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
248 pcCommandString, /* The command string itself. */
\r
249 lParameterNumber, /* Return the next parameter. */
\r
250 &lParameterStringLength /* Store the parameter string length. */
\r
253 /* Sanity check something was returned. */
\r
254 configASSERT( pcParameter );
\r
256 /* Return the parameter string. */
\r
257 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
258 sprintf( ( char * ) pcWriteBuffer, "%d: ", lParameterNumber );
\r
259 strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, lParameterStringLength );
\r
260 strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
262 /* If this is the last of the three parameters then there are no more
\r
263 strings to return after this one. */
\r
264 if( lParameterNumber == 3L )
\r
266 /* If this is the last of the three parameters then there are no more
\r
267 strings to return after this one. */
\r
269 lParameterNumber = 0L;
\r
273 /* There are more parameters to return after this one. */
\r
275 lParameterNumber++;
\r
281 /*-----------------------------------------------------------*/
\r
283 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
285 int8_t *pcParameter;
\r
286 portBASE_TYPE lParameterStringLength, xReturn;
\r
287 static portBASE_TYPE lParameterNumber = 0;
\r
289 /* Remove compile time warnings about unused parameters, and check the
\r
290 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
291 write buffer length is adequate, so does not check for buffer overflows. */
\r
292 ( void ) pcCommandString;
\r
293 ( void ) xWriteBufferLen;
\r
294 configASSERT( pcWriteBuffer );
\r
296 if( lParameterNumber == 0 )
\r
298 /* The first time the function is called after the command has been
\r
299 entered just a header string is returned. */
\r
300 sprintf( ( char * ) pcWriteBuffer, "The parameters were:\r\n" );
\r
302 /* Next time the function is called the first parameter will be echoed
\r
304 lParameterNumber = 1L;
\r
306 /* There is more data to be returned as no parameters have been echoed
\r
312 /* Obtain the parameter string. */
\r
313 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
315 pcCommandString, /* The command string itself. */
\r
316 lParameterNumber, /* Return the next parameter. */
\r
317 &lParameterStringLength /* Store the parameter string length. */
\r
320 if( pcParameter != NULL )
\r
322 /* Return the parameter string. */
\r
323 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
324 sprintf( ( char * ) pcWriteBuffer, "%d: ", lParameterNumber );
\r
325 strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, lParameterStringLength );
\r
326 strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
328 /* There might be more parameters to return after this one. */
\r
330 lParameterNumber++;
\r
334 /* No more parameters were found. Make sure the write buffer does
\r
335 not contain a valid string. */
\r
336 pcWriteBuffer[ 0 ] = 0x00;
\r
338 /* No more data to return. */
\r
341 /* Start over the next time this command is executed. */
\r
342 lParameterNumber = 0;
\r
348 /*-----------------------------------------------------------*/
\r
350 static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
352 int8_t *pcParameter;
\r
353 portBASE_TYPE lParameterStringLength;
\r
355 /* Remove compile time warnings about unused parameters, and check the
\r
356 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
357 write buffer length is adequate, so does not check for buffer overflows. */
\r
358 ( void ) pcCommandString;
\r
359 ( void ) xWriteBufferLen;
\r
360 configASSERT( pcWriteBuffer );
\r
362 /* Obtain the parameter string. */
\r
363 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
365 pcCommandString, /* The command string itself. */
\r
366 1, /* Return the first parameter. */
\r
367 &lParameterStringLength /* Store the parameter string length. */
\r
370 /* Sanity check something was returned. */
\r
371 configASSERT( pcParameter );
\r
373 /* There are only two valid parameter values. */
\r
374 if( strncmp( ( const char * ) pcParameter, "start", strlen( "start" ) ) == 0 )
\r
376 /* Start or restart the trace. */
\r
381 sprintf( ( char * ) pcWriteBuffer, "Trace recording (re)started.\r\n" );
\r
383 else if( strncmp( ( const char * ) pcParameter, "stop", strlen( "stop" ) ) == 0 )
\r
385 /* End the trace, if one is running. */
\r
387 sprintf( ( char * ) pcWriteBuffer, "Stopping trace recording and dumping log to disk.\r\n" );
\r
388 prvSaveTraceFile();
\r
392 sprintf( ( char * ) pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );
\r
395 /* There is no more data to return after this single string, so return
\r
399 /*-----------------------------------------------------------*/
\r
401 static void prvSaveTraceFile( void )
\r
403 FILE* pxOutputFile;
\r
405 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
407 if( pxOutputFile != NULL )
\r
409 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
410 fclose( pxOutputFile );
\r
411 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
415 printf( "\r\nFailed to create trace dump file\r\n" );
\r