2 FreeRTOS V9.0.0 - Copyright (C) 2016 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 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* FreeRTOS includes. */
\r
71 #include "FreeRTOS.h"
\r
74 /* FreeRTOS+CLI includes. */
\r
75 #include "FreeRTOS_CLI.h"
\r
78 * Writes trace data to a disk file when the trace recording is stopped.
\r
79 * This function will simply overwrite any trace files that already exist.
\r
81 static void prvSaveTraceFile( void );
\r
84 * Defines a command that returns a table showing the state of each task at the
\r
85 * time the command is called.
\r
87 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
90 * Defines a command that returns a table showing how much time each task has
\r
91 * spent in the Running state.
\r
93 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
96 * Defines a command that expects exactly three parameters. Each of the three
\r
97 * parameter are echoed back one at a time.
\r
99 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
102 * Defines a command that can take a variable number of parameters. Each
\r
103 * parameter is echoes back one at a time.
\r
105 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
108 * Defines a command that starts/stops events being recorded for offline viewing
\r
109 * in FreeRTOS+Trace.
\r
111 static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
113 /* Structure that defines the "run-time-stats" command line command. */
\r
114 static const CLI_Command_Definition_t xRunTimeStats =
\r
116 "run-time-stats", /* The command string to type. */
\r
117 "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n\r\n",
\r
118 prvRunTimeStatsCommand, /* The function to run. */
\r
119 0 /* No parameters are expected. */
\r
122 /* Structure that defines the "task-stats" command line command. */
\r
123 static const CLI_Command_Definition_t xTaskStats =
\r
125 "task-stats", /* The command string to type. */
\r
126 "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n\r\n",
\r
127 prvTaskStatsCommand, /* The function to run. */
\r
128 0 /* No parameters are expected. */
\r
131 /* Structure that defines the "echo_3_parameters" command line command. This
\r
132 takes exactly three parameters that the command simply echos back one at a
\r
134 static const CLI_Command_Definition_t xThreeParameterEcho =
\r
136 "echo_3_parameters",
\r
137 "\r\necho_3_parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n\r\n",
\r
138 prvThreeParameterEchoCommand, /* The function to run. */
\r
139 3 /* Three parameters are expected, which can take any value. */
\r
142 /* Structure that defines the "echo_parameters" command line command. This
\r
143 takes a variable number of parameters that the command simply echos back one at
\r
145 static const CLI_Command_Definition_t xParameterEcho =
\r
148 "\r\necho_parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n\r\n",
\r
149 prvParameterEchoCommand, /* The function to run. */
\r
150 -1 /* The user can enter any number of commands. */
\r
153 /* Structure that defines the "trace" command line command. This takes a single
\r
154 parameter, which can be either "start" or "stop". */
\r
155 static const CLI_Command_Definition_t xStartTrace =
\r
158 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n\r\n",
\r
159 prvStartStopTraceCommand, /* The function to run. */
\r
160 1 /* One parameter is expected. Valid values are "start" and "stop". */
\r
163 /*-----------------------------------------------------------*/
\r
165 void vRegisterCLICommands( void )
\r
167 /* Register all the command line commands defined immediately above. */
\r
168 FreeRTOS_CLIRegisterCommand( &xTaskStats );
\r
169 FreeRTOS_CLIRegisterCommand( &xRunTimeStats );
\r
170 FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );
\r
171 FreeRTOS_CLIRegisterCommand( &xParameterEcho );
\r
172 FreeRTOS_CLIRegisterCommand( &xStartTrace );
\r
174 /*-----------------------------------------------------------*/
\r
176 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
178 const char *const pcHeader = "Task State Priority Stack #\r\n************************************************\r\n";
\r
180 /* Remove compile time warnings about unused parameters, and check the
\r
181 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
182 write buffer length is adequate, so does not check for buffer overflows. */
\r
183 ( void ) pcCommandString;
\r
184 ( void ) xWriteBufferLen;
\r
185 configASSERT( pcWriteBuffer );
\r
187 /* Generate a table of task stats. */
\r
188 strcpy( pcWriteBuffer, pcHeader );
\r
189 vTaskList( pcWriteBuffer + strlen( pcHeader ) );
\r
191 /* There is no more data to return after this single string, so return
\r
195 /*-----------------------------------------------------------*/
\r
197 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
199 const char * const pcHeader = "Task Abs Time % Time\r\n****************************************\r\n";
\r
201 /* Remove compile time warnings about unused parameters, and check the
\r
202 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
203 write buffer length is adequate, so does not check for buffer overflows. */
\r
204 ( void ) pcCommandString;
\r
205 ( void ) xWriteBufferLen;
\r
206 configASSERT( pcWriteBuffer );
\r
208 /* Generate a table of task stats. */
\r
209 strcpy( pcWriteBuffer, pcHeader );
\r
210 vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );
\r
212 /* There is no more data to return after this single string, so return
\r
216 /*-----------------------------------------------------------*/
\r
218 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
220 const char *pcParameter;
\r
221 BaseType_t lParameterStringLength, xReturn;
\r
222 static BaseType_t lParameterNumber = 0;
\r
224 /* Remove compile time warnings about unused parameters, and check the
\r
225 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
226 write buffer length is adequate, so does not check for buffer overflows. */
\r
227 ( void ) pcCommandString;
\r
228 ( void ) xWriteBufferLen;
\r
229 configASSERT( pcWriteBuffer );
\r
231 if( lParameterNumber == 0 )
\r
233 /* The first time the function is called after the command has been
\r
234 entered just a header string is returned. */
\r
235 sprintf( pcWriteBuffer, "The three parameters were:\r\n" );
\r
237 /* Next time the function is called the first parameter will be echoed
\r
239 lParameterNumber = 1L;
\r
241 /* There is more data to be returned as no parameters have been echoed
\r
247 /* Obtain the parameter string. */
\r
248 pcParameter = FreeRTOS_CLIGetParameter
\r
250 pcCommandString, /* The command string itself. */
\r
251 lParameterNumber, /* Return the next parameter. */
\r
252 &lParameterStringLength /* Store the parameter string length. */
\r
255 /* Sanity check something was returned. */
\r
256 configASSERT( pcParameter );
\r
258 /* Return the parameter string. */
\r
259 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
260 sprintf( pcWriteBuffer, "%d: ", lParameterNumber );
\r
261 strncat( pcWriteBuffer, pcParameter, lParameterStringLength );
\r
262 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
264 /* If this is the last of the three parameters then there are no more
\r
265 strings to return after this one. */
\r
266 if( lParameterNumber == 3L )
\r
268 /* If this is the last of the three parameters then there are no more
\r
269 strings to return after this one. */
\r
271 lParameterNumber = 0L;
\r
275 /* There are more parameters to return after this one. */
\r
277 lParameterNumber++;
\r
283 /*-----------------------------------------------------------*/
\r
285 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
287 const char *pcParameter;
\r
288 BaseType_t lParameterStringLength, xReturn;
\r
289 static BaseType_t lParameterNumber = 0;
\r
291 /* Remove compile time warnings about unused parameters, and check the
\r
292 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
293 write buffer length is adequate, so does not check for buffer overflows. */
\r
294 ( void ) pcCommandString;
\r
295 ( void ) xWriteBufferLen;
\r
296 configASSERT( pcWriteBuffer );
\r
298 if( lParameterNumber == 0 )
\r
300 /* The first time the function is called after the command has been
\r
301 entered just a header string is returned. */
\r
302 sprintf( pcWriteBuffer, "The parameters were:\r\n" );
\r
304 /* Next time the function is called the first parameter will be echoed
\r
306 lParameterNumber = 1L;
\r
308 /* There is more data to be returned as no parameters have been echoed
\r
314 /* Obtain the parameter string. */
\r
315 pcParameter = FreeRTOS_CLIGetParameter
\r
317 pcCommandString, /* The command string itself. */
\r
318 lParameterNumber, /* Return the next parameter. */
\r
319 &lParameterStringLength /* Store the parameter string length. */
\r
322 if( pcParameter != NULL )
\r
324 /* Return the parameter string. */
\r
325 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
326 sprintf( pcWriteBuffer, "%d: ", lParameterNumber );
\r
327 strncat( pcWriteBuffer, pcParameter, lParameterStringLength );
\r
328 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
330 /* There might be more parameters to return after this one. */
\r
332 lParameterNumber++;
\r
336 /* No more parameters were found. Make sure the write buffer does
\r
337 not contain a valid string. */
\r
338 pcWriteBuffer[ 0 ] = 0x00;
\r
340 /* No more data to return. */
\r
343 /* Start over the next time this command is executed. */
\r
344 lParameterNumber = 0;
\r
350 /*-----------------------------------------------------------*/
\r
352 static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
354 const char *pcParameter;
\r
355 BaseType_t lParameterStringLength;
\r
357 /* Remove compile time warnings about unused parameters, and check the
\r
358 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
359 write buffer length is adequate, so does not check for buffer overflows. */
\r
360 ( void ) pcCommandString;
\r
361 ( void ) xWriteBufferLen;
\r
362 configASSERT( pcWriteBuffer );
\r
364 /* Obtain the parameter string. */
\r
365 pcParameter = FreeRTOS_CLIGetParameter
\r
367 pcCommandString, /* The command string itself. */
\r
368 1, /* Return the first parameter. */
\r
369 &lParameterStringLength /* Store the parameter string length. */
\r
372 /* Sanity check something was returned. */
\r
373 configASSERT( pcParameter );
\r
375 /* There are only two valid parameter values. */
\r
376 if( strncmp( pcParameter, "start", strlen( "start" ) ) == 0 )
\r
378 /* Start or restart the trace. */
\r
383 sprintf( pcWriteBuffer, "Trace recording (re)started.\r\n" );
\r
385 else if( strncmp( pcParameter, "stop", strlen( "stop" ) ) == 0 )
\r
387 /* End the trace, if one is running. */
\r
389 sprintf( pcWriteBuffer, "Stopping trace recording and dumping log to disk.\r\n" );
\r
390 prvSaveTraceFile();
\r
394 sprintf( pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );
\r
397 /* There is no more data to return after this single string, so return
\r
401 /*-----------------------------------------------------------*/
\r
403 static void prvSaveTraceFile( void )
\r
405 FILE* pxOutputFile;
\r
407 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
409 if( pxOutputFile != NULL )
\r
411 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
412 fclose( pxOutputFile );
\r
413 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
417 printf( "\r\nFailed to create trace dump file\r\n" );
\r