2 FreeRTOS V7.4.2 - 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 it can 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
75 /* FreeRTOS includes. */
\r
76 #include "FreeRTOS.h"
\r
79 /* FreeRTOS+CLI includes. */
\r
80 #include "FreeRTOS_CLI.h"
\r
82 /* FreeRTOS+Trace includes. */
\r
83 #include "trcUser.h"
\r
86 * Writes trace data to a disk file when the trace recording is stopped.
\r
87 * This function will simply overwrite any trace files that already exist.
\r
89 static void prvSaveTraceFile( void );
\r
92 * Defines a command that returns a table showing the state of each task at the
\r
93 * time the command is called.
\r
95 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
98 * Defines a command that returns a table showing how much time each task has
\r
99 * spent in the Running state.
\r
101 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
104 * Defines a command that expects exactly three parameters. Each of the three
\r
105 * parameter are echoed back one at a time.
\r
107 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
110 * Defines a command that can take a variable number of parameters. Each
\r
111 * parameter is echoes back one at a time.
\r
113 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
116 * Defines a command that starts/stops events being recorded for offline viewing
\r
117 * in FreeRTOS+Trace.
\r
119 static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
121 /* Structure that defines the "run-time-stats" command line command. */
\r
122 static const CLI_Command_Definition_t xRunTimeStats =
\r
124 ( const int8_t * const ) "run-time-stats", /* The command string to type. */
\r
125 ( 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
126 prvRunTimeStatsCommand, /* The function to run. */
\r
127 0 /* No parameters are expected. */
\r
130 /* Structure that defines the "task-stats" command line command. */
\r
131 static const CLI_Command_Definition_t xTaskStats =
\r
133 ( const int8_t * const ) "task-stats", /* The command string to type. */
\r
134 ( const int8_t * const ) "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n\r\n",
\r
135 prvTaskStatsCommand, /* The function to run. */
\r
136 0 /* No parameters are expected. */
\r
139 /* Structure that defines the "echo_3_parameters" command line command. This
\r
140 takes exactly three parameters that the command simply echos back one at a
\r
142 static const CLI_Command_Definition_t xThreeParameterEcho =
\r
144 ( const int8_t * const ) "echo_3_parameters",
\r
145 ( 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
146 prvThreeParameterEchoCommand, /* The function to run. */
\r
147 3 /* Three parameters are expected, which can take any value. */
\r
150 /* Structure that defines the "echo_parameters" command line command. This
\r
151 takes a variable number of parameters that the command simply echos back one at
\r
153 static const CLI_Command_Definition_t xParameterEcho =
\r
155 ( const int8_t * const ) "echo_parameters",
\r
156 ( const int8_t * const ) "\r\necho_parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n\r\n",
\r
157 prvParameterEchoCommand, /* The function to run. */
\r
158 -1 /* The user can enter any number of commands. */
\r
161 /* Structure that defines the "trace" command line command. This takes a single
\r
162 parameter, which can be either "start" or "stop". */
\r
163 static const CLI_Command_Definition_t xStartTrace =
\r
165 ( const int8_t * const ) "trace",
\r
166 ( 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
167 prvStartStopTraceCommand, /* The function to run. */
\r
168 1 /* One parameter is expected. Valid values are "start" and "stop". */
\r
171 /*-----------------------------------------------------------*/
\r
173 void vRegisterCLICommands( void )
\r
175 /* Register all the command line commands defined immediately above. */
\r
176 FreeRTOS_CLIRegisterCommand( &xTaskStats );
\r
177 FreeRTOS_CLIRegisterCommand( &xRunTimeStats );
\r
178 FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );
\r
179 FreeRTOS_CLIRegisterCommand( &xParameterEcho );
\r
180 FreeRTOS_CLIRegisterCommand( &xStartTrace );
\r
182 /*-----------------------------------------------------------*/
\r
184 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
186 const int8_t *const pcHeader = ( int8_t * ) "Task State Priority Stack #\r\n************************************************\r\n";
\r
188 /* Remove compile time warnings about unused parameters, and check the
\r
189 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
190 write buffer length is adequate, so does not check for buffer overflows. */
\r
191 ( void ) pcCommandString;
\r
192 ( void ) xWriteBufferLen;
\r
193 configASSERT( pcWriteBuffer );
\r
195 /* Generate a table of task stats. */
\r
196 strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );
\r
197 vTaskList( pcWriteBuffer + strlen( ( char * ) pcHeader ) );
\r
199 /* There is no more data to return after this single string, so return
\r
203 /*-----------------------------------------------------------*/
\r
205 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
207 const int8_t * const pcHeader = ( int8_t * ) "Task Abs Time % Time\r\n****************************************\r\n";
\r
209 /* Remove compile time warnings about unused parameters, and check the
\r
210 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
211 write buffer length is adequate, so does not check for buffer overflows. */
\r
212 ( void ) pcCommandString;
\r
213 ( void ) xWriteBufferLen;
\r
214 configASSERT( pcWriteBuffer );
\r
216 /* Generate a table of task stats. */
\r
217 strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );
\r
218 vTaskGetRunTimeStats( pcWriteBuffer + strlen( ( char * ) pcHeader ) );
\r
220 /* There is no more data to return after this single string, so return
\r
224 /*-----------------------------------------------------------*/
\r
226 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
228 int8_t *pcParameter;
\r
229 portBASE_TYPE lParameterStringLength, xReturn;
\r
230 static portBASE_TYPE lParameterNumber = 0;
\r
232 /* Remove compile time warnings about unused parameters, and check the
\r
233 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
234 write buffer length is adequate, so does not check for buffer overflows. */
\r
235 ( void ) pcCommandString;
\r
236 ( void ) xWriteBufferLen;
\r
237 configASSERT( pcWriteBuffer );
\r
239 if( lParameterNumber == 0 )
\r
241 /* The first time the function is called after the command has been
\r
242 entered just a header string is returned. */
\r
243 sprintf( ( char * ) pcWriteBuffer, "The three parameters were:\r\n" );
\r
245 /* Next time the function is called the first parameter will be echoed
\r
247 lParameterNumber = 1L;
\r
249 /* There is more data to be returned as no parameters have been echoed
\r
255 /* Obtain the parameter string. */
\r
256 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
258 pcCommandString, /* The command string itself. */
\r
259 lParameterNumber, /* Return the next parameter. */
\r
260 &lParameterStringLength /* Store the parameter string length. */
\r
263 /* Sanity check something was returned. */
\r
264 configASSERT( pcParameter );
\r
266 /* Return the parameter string. */
\r
267 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
268 sprintf( ( char * ) pcWriteBuffer, "%d: ", lParameterNumber );
\r
269 strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, lParameterStringLength );
\r
270 strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
272 /* If this is the last of the three parameters then there are no more
\r
273 strings to return after this one. */
\r
274 if( lParameterNumber == 3L )
\r
276 /* If this is the last of the three parameters then there are no more
\r
277 strings to return after this one. */
\r
279 lParameterNumber = 0L;
\r
283 /* There are more parameters to return after this one. */
\r
285 lParameterNumber++;
\r
291 /*-----------------------------------------------------------*/
\r
293 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
295 int8_t *pcParameter;
\r
296 portBASE_TYPE lParameterStringLength, xReturn;
\r
297 static portBASE_TYPE lParameterNumber = 0;
\r
299 /* Remove compile time warnings about unused parameters, and check the
\r
300 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
301 write buffer length is adequate, so does not check for buffer overflows. */
\r
302 ( void ) pcCommandString;
\r
303 ( void ) xWriteBufferLen;
\r
304 configASSERT( pcWriteBuffer );
\r
306 if( lParameterNumber == 0 )
\r
308 /* The first time the function is called after the command has been
\r
309 entered just a header string is returned. */
\r
310 sprintf( ( char * ) pcWriteBuffer, "The parameters were:\r\n" );
\r
312 /* Next time the function is called the first parameter will be echoed
\r
314 lParameterNumber = 1L;
\r
316 /* There is more data to be returned as no parameters have been echoed
\r
322 /* Obtain the parameter string. */
\r
323 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
325 pcCommandString, /* The command string itself. */
\r
326 lParameterNumber, /* Return the next parameter. */
\r
327 &lParameterStringLength /* Store the parameter string length. */
\r
330 if( pcParameter != NULL )
\r
332 /* Return the parameter string. */
\r
333 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
334 sprintf( ( char * ) pcWriteBuffer, "%d: ", lParameterNumber );
\r
335 strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, lParameterStringLength );
\r
336 strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
338 /* There might be more parameters to return after this one. */
\r
340 lParameterNumber++;
\r
344 /* No more parameters were found. Make sure the write buffer does
\r
345 not contain a valid string. */
\r
346 pcWriteBuffer[ 0 ] = 0x00;
\r
348 /* No more data to return. */
\r
351 /* Start over the next time this command is executed. */
\r
352 lParameterNumber = 0;
\r
358 /*-----------------------------------------------------------*/
\r
360 static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
362 int8_t *pcParameter;
\r
363 portBASE_TYPE lParameterStringLength;
\r
365 /* Remove compile time warnings about unused parameters, and check the
\r
366 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
367 write buffer length is adequate, so does not check for buffer overflows. */
\r
368 ( void ) pcCommandString;
\r
369 ( void ) xWriteBufferLen;
\r
370 configASSERT( pcWriteBuffer );
\r
372 /* Obtain the parameter string. */
\r
373 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
375 pcCommandString, /* The command string itself. */
\r
376 1, /* Return the first parameter. */
\r
377 &lParameterStringLength /* Store the parameter string length. */
\r
380 /* Sanity check something was returned. */
\r
381 configASSERT( pcParameter );
\r
383 /* There are only two valid parameter values. */
\r
384 if( strncmp( ( const char * ) pcParameter, "start", strlen( "start" ) ) == 0 )
\r
386 /* Start or restart the trace. */
\r
391 sprintf( ( char * ) pcWriteBuffer, "Trace recording (re)started.\r\n" );
\r
393 else if( strncmp( ( const char * ) pcParameter, "stop", strlen( "stop" ) ) == 0 )
\r
395 /* End the trace, if one is running. */
\r
397 sprintf( ( char * ) pcWriteBuffer, "Stopping trace recording and dumping log to disk.\r\n" );
\r
398 prvSaveTraceFile();
\r
402 sprintf( ( char * ) pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );
\r
405 /* There is no more data to return after this single string, so return
\r
409 /*-----------------------------------------------------------*/
\r
411 static void prvSaveTraceFile( void )
\r
413 FILE* pxOutputFile;
\r
415 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
417 if( pxOutputFile != NULL )
\r
419 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
420 fclose( pxOutputFile );
\r
421 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
425 printf( "\r\nFailed to create trace dump file\r\n" );
\r