]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/Sample-CLI-commands.c
b870852c25dd67d42f35c4fd4a8dc3e4ce229f69
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / Sample-CLI-commands.c
1 /*\r
2     FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
6     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
7 \r
8     ***************************************************************************\r
9      *                                                                       *\r
10      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
11      *    Complete, revised, and edited pdf reference manuals are also       *\r
12      *    available.                                                         *\r
13      *                                                                       *\r
14      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
15      *    ensuring you get running as quickly as possible and with an        *\r
16      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
17      *    the FreeRTOS project to continue with its mission of providing     *\r
18      *    professional grade, cross platform, de facto standard solutions    *\r
19      *    for microcontrollers - completely free of charge!                  *\r
20      *                                                                       *\r
21      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
22      *                                                                       *\r
23      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
24      *                                                                       *\r
25     ***************************************************************************\r
26 \r
27 \r
28     This file is part of the FreeRTOS distribution.\r
29 \r
30     FreeRTOS is free software; you can redistribute it and/or modify it under\r
31     the terms of the GNU General Public License (version 2) as published by the\r
32     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
33 \r
34     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
35     distribute a combined work that includes FreeRTOS without being obliged to\r
36     provide the source code for proprietary components outside of the FreeRTOS\r
37     kernel.\r
38 \r
39     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
40     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
41     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
42     details. You should have received a copy of the GNU General Public License\r
43     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
44     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
45     writing to Real Time Engineers Ltd., contact details for whom are available\r
46     on the FreeRTOS WEB site.\r
47 \r
48     1 tab == 4 spaces!\r
49 \r
50     ***************************************************************************\r
51      *                                                                       *\r
52      *    Having a problem?  Start by reading the FAQ "My application does   *\r
53      *    not run, what could be wrong?"                                     *\r
54      *                                                                       *\r
55      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
56      *                                                                       *\r
57     ***************************************************************************\r
58 \r
59 \r
60     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
61     license and Real Time Engineers Ltd. contact details.\r
62 \r
63     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
64     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
65     fully thread aware and reentrant UDP/IP stack.\r
66 \r
67     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
68     Integrity Systems, who sell the code with commercial support,\r
69     indemnification and middleware, under the OpenRTOS brand.\r
70 \r
71     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
72     engineered and independently SIL3 certified version for use in safety and\r
73     mission critical applications that require provable dependability.\r
74 */\r
75 \r
76  /******************************************************************************\r
77  *\r
78  * See the following URL for information on the commands defined in this file:\r
79  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Ethernet_Related_CLI_Commands.shtml\r
80  *\r
81  ******************************************************************************/\r
82 \r
83 \r
84 /* FreeRTOS includes. */\r
85 #include "FreeRTOS.h"\r
86 #include "task.h"\r
87 \r
88 /* Standard includes. */\r
89 #include <stdint.h>\r
90 #include <stdio.h>\r
91 #include <string.h>\r
92 \r
93 /* FreeRTOS+CLI includes. */\r
94 #include "FreeRTOS_CLI.h"\r
95 \r
96 #ifndef  configINCLUDE_TRACE_RELATED_CLI_COMMANDS\r
97         #define configINCLUDE_TRACE_RELATED_CLI_COMMANDS 0\r
98 #endif\r
99 \r
100 \r
101 /*\r
102  * Implements the run-time-stats command.\r
103  */\r
104 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
105 \r
106 /*\r
107  * Implements the task-stats command.\r
108  */\r
109 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
110 \r
111 /*\r
112  * Implements the echo-three-parameters command.\r
113  */\r
114 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
115 \r
116 /*\r
117  * Implements the echo-parameters command.\r
118  */\r
119 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
120 \r
121 /*\r
122  * Implements the "trace start" and "trace stop" commands;\r
123  */\r
124 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
125         static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
126 #endif\r
127 \r
128 /* Structure that defines the "run-time-stats" command line command.   This\r
129 generates a table that shows how much run time each task has */\r
130 static const CLI_Command_Definition_t xRunTimeStats =\r
131 {\r
132         ( const int8_t * const ) "run-time-stats", /* The command string to type. */\r
133         ( 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
134         prvRunTimeStatsCommand, /* The function to run. */\r
135         0 /* No parameters are expected. */\r
136 };\r
137 \r
138 /* Structure that defines the "task-stats" command line command.  This generates\r
139 a table that gives information on each task in the system. */\r
140 static const CLI_Command_Definition_t xTaskStats =\r
141 {\r
142         ( const int8_t * const ) "task-stats", /* The command string to type. */\r
143         ( const int8_t * const ) "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n",\r
144         prvTaskStatsCommand, /* The function to run. */\r
145         0 /* No parameters are expected. */\r
146 };\r
147 \r
148 /* Structure that defines the "echo_3_parameters" command line command.  This\r
149 takes exactly three parameters that the command simply echos back one at a\r
150 time. */\r
151 static const CLI_Command_Definition_t xThreeParameterEcho =\r
152 {\r
153         ( const int8_t * const ) "echo-3-parameters",\r
154         ( const int8_t * const ) "\r\necho-3-parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n",\r
155         prvThreeParameterEchoCommand, /* The function to run. */\r
156         3 /* Three parameters are expected, which can take any value. */\r
157 };\r
158 \r
159 /* Structure that defines the "echo_parameters" command line command.  This\r
160 takes a variable number of parameters that the command simply echos back one at\r
161 a time. */\r
162 static const CLI_Command_Definition_t xParameterEcho =\r
163 {\r
164         ( const int8_t * const ) "echo-parameters",\r
165         ( const int8_t * const ) "\r\necho-parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n",\r
166         prvParameterEchoCommand, /* The function to run. */\r
167         -1 /* The user can enter any number of commands. */\r
168 };\r
169 \r
170 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
171         /* Structure that defines the "trace" command line command.  This takes a single\r
172         parameter, which can be either "start" or "stop". */\r
173         static const CLI_Command_Definition_t xStartStopTrace =\r
174         {\r
175                 ( const int8_t * const ) "trace",\r
176                 ( const int8_t * const ) "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n",\r
177                 prvStartStopTraceCommand, /* The function to run. */\r
178                 1 /* One parameter is expected.  Valid values are "start" and "stop". */\r
179         };\r
180 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r
181 \r
182 /*-----------------------------------------------------------*/\r
183 \r
184 void vRegisterSampleCLICommands( void )\r
185 {\r
186         /* Register all the command line commands defined immediately above. */\r
187         FreeRTOS_CLIRegisterCommand( &xTaskStats );\r
188         FreeRTOS_CLIRegisterCommand( &xRunTimeStats );\r
189         FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );\r
190         FreeRTOS_CLIRegisterCommand( &xParameterEcho );\r
191 \r
192         #if( configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 )\r
193         {\r
194                 FreeRTOS_CLIRegisterCommand( & xStartStopTrace );\r
195         }\r
196         #endif\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
201 {\r
202 const int8_t *const pcHeader = ( int8_t * ) "Task          State  Priority  Stack       #\r\n************************************************\r\n";\r
203 \r
204         /* Remove compile time warnings about unused parameters, and check the\r
205         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
206         write buffer length is adequate, so does not check for buffer overflows. */\r
207         ( void ) pcCommandString;\r
208         ( void ) xWriteBufferLen;\r
209         configASSERT( pcWriteBuffer );\r
210 \r
211         /* Generate a table of task stats. */\r
212         strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );\r
213         vTaskList( pcWriteBuffer + strlen( ( char * ) pcHeader ) );\r
214 \r
215         /* There is no more data to return after this single string, so return\r
216         pdFALSE. */\r
217         return pdFALSE;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
222 {\r
223 const int8_t * const pcHeader = ( int8_t * ) "Task            Abs Time      % Time\r\n****************************************\r\n";\r
224 \r
225         /* Remove compile time warnings about unused parameters, and check the\r
226         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
227         write buffer length is adequate, so does not check for buffer overflows. */\r
228         ( void ) pcCommandString;\r
229         ( void ) xWriteBufferLen;\r
230         configASSERT( pcWriteBuffer );\r
231 \r
232         /* Generate a table of task stats. */\r
233         strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );\r
234         vTaskGetRunTimeStats( pcWriteBuffer + strlen( ( char * ) pcHeader ) );\r
235 \r
236         /* There is no more data to return after this single string, so return\r
237         pdFALSE. */\r
238         return pdFALSE;\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
243 {\r
244 int8_t *pcParameter;\r
245 portBASE_TYPE xParameterStringLength, xReturn;\r
246 static portBASE_TYPE lParameterNumber = 0;\r
247 \r
248         /* Remove compile time warnings about unused parameters, and check the\r
249         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
250         write buffer length is adequate, so does not check for buffer overflows. */\r
251         ( void ) pcCommandString;\r
252         ( void ) xWriteBufferLen;\r
253         configASSERT( pcWriteBuffer );\r
254 \r
255         if( lParameterNumber == 0 )\r
256         {\r
257                 /* The first time the function is called after the command has been\r
258                 entered just a header string is returned. */\r
259                 sprintf( ( char * ) pcWriteBuffer, "The three parameters were:\r\n" );\r
260 \r
261                 /* Next time the function is called the first parameter will be echoed\r
262                 back. */\r
263                 lParameterNumber = 1L;\r
264 \r
265                 /* There is more data to be returned as no parameters have been echoed\r
266                 back yet. */\r
267                 xReturn = pdPASS;\r
268         }\r
269         else\r
270         {\r
271                 /* Obtain the parameter string. */\r
272                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
273                                                                         (\r
274                                                                                 pcCommandString,                /* The command string itself. */\r
275                                                                                 lParameterNumber,               /* Return the next parameter. */\r
276                                                                                 &xParameterStringLength /* Store the parameter string length. */\r
277                                                                         );\r
278 \r
279                 /* Sanity check something was returned. */\r
280                 configASSERT( pcParameter );\r
281 \r
282                 /* Return the parameter string. */\r
283                 memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
284                 sprintf( ( char * ) pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
285                 strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, xParameterStringLength );\r
286                 strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
287 \r
288                 /* If this is the last of the three parameters then there are no more\r
289                 strings to return after this one. */\r
290                 if( lParameterNumber == 3L )\r
291                 {\r
292                         /* If this is the last of the three parameters then there are no more\r
293                         strings to return after this one. */\r
294                         xReturn = pdFALSE;\r
295                         lParameterNumber = 0L;\r
296                 }\r
297                 else\r
298                 {\r
299                         /* There are more parameters to return after this one. */\r
300                         xReturn = pdTRUE;\r
301                         lParameterNumber++;\r
302                 }\r
303         }\r
304 \r
305         return xReturn;\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
310 {\r
311 int8_t *pcParameter;\r
312 portBASE_TYPE xParameterStringLength, xReturn;\r
313 static portBASE_TYPE lParameterNumber = 0;\r
314 \r
315         /* Remove compile time warnings about unused parameters, and check the\r
316         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
317         write buffer length is adequate, so does not check for buffer overflows. */\r
318         ( void ) pcCommandString;\r
319         ( void ) xWriteBufferLen;\r
320         configASSERT( pcWriteBuffer );\r
321 \r
322         if( lParameterNumber == 0 )\r
323         {\r
324                 /* The first time the function is called after the command has been\r
325                 entered just a header string is returned. */\r
326                 sprintf( ( char * ) pcWriteBuffer, "The parameters were:\r\n" );\r
327 \r
328                 /* Next time the function is called the first parameter will be echoed\r
329                 back. */\r
330                 lParameterNumber = 1L;\r
331 \r
332                 /* There is more data to be returned as no parameters have been echoed\r
333                 back yet. */\r
334                 xReturn = pdPASS;\r
335         }\r
336         else\r
337         {\r
338                 /* Obtain the parameter string. */\r
339                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
340                                                                         (\r
341                                                                                 pcCommandString,                /* The command string itself. */\r
342                                                                                 lParameterNumber,               /* Return the next parameter. */\r
343                                                                                 &xParameterStringLength /* Store the parameter string length. */\r
344                                                                         );\r
345 \r
346                 if( pcParameter != NULL )\r
347                 {\r
348                         /* Return the parameter string. */\r
349                         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
350                         sprintf( ( char * ) pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
351                         strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, xParameterStringLength );\r
352                         strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
353 \r
354                         /* There might be more parameters to return after this one. */\r
355                         xReturn = pdTRUE;\r
356                         lParameterNumber++;\r
357                 }\r
358                 else\r
359                 {\r
360                         /* No more parameters were found.  Make sure the write buffer does\r
361                         not contain a valid string. */\r
362                         pcWriteBuffer[ 0 ] = 0x00;\r
363 \r
364                         /* No more data to return. */\r
365                         xReturn = pdFALSE;\r
366 \r
367                         /* Start over the next time this command is executed. */\r
368                         lParameterNumber = 0;\r
369                 }\r
370         }\r
371 \r
372         return xReturn;\r
373 }\r
374 /*-----------------------------------------------------------*/\r
375 \r
376 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
377 \r
378         static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
379         {\r
380         int8_t *pcParameter;\r
381         portBASE_TYPE lParameterStringLength;\r
382 \r
383                 /* Remove compile time warnings about unused parameters, and check the\r
384                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
385                 write buffer length is adequate, so does not check for buffer overflows. */\r
386                 ( void ) pcCommandString;\r
387                 ( void ) xWriteBufferLen;\r
388                 configASSERT( pcWriteBuffer );\r
389 \r
390                 /* Obtain the parameter string. */\r
391                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
392                                                                         (\r
393                                                                                 pcCommandString,                /* The command string itself. */\r
394                                                                                 1,                                              /* Return the first parameter. */\r
395                                                                                 &lParameterStringLength /* Store the parameter string length. */\r
396                                                                         );\r
397 \r
398                 /* Sanity check something was returned. */\r
399                 configASSERT( pcParameter );\r
400 \r
401                 /* There are only two valid parameter values. */\r
402                 if( strncmp( ( const char * ) pcParameter, "start", strlen( "start" ) ) == 0 )\r
403                 {\r
404                         /* Start or restart the trace. */\r
405                         vTraceStop();\r
406                         vTraceClear();\r
407                         vTraceStart();\r
408 \r
409                         sprintf( ( char * ) pcWriteBuffer, "Trace recording (re)started.\r\n" );\r
410                 }\r
411                 else if( strncmp( ( const char * ) pcParameter, "stop", strlen( "stop" ) ) == 0 )\r
412                 {\r
413                         /* End the trace, if one is running. */\r
414                         vTraceStop();\r
415                         sprintf( ( char * ) pcWriteBuffer, "Stopping trace recording.\r\n" );\r
416                 }\r
417                 else\r
418                 {\r
419                         sprintf( ( char * ) pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );\r
420                 }\r
421 \r
422                 /* There is no more data to return after this single string, so return\r
423                 pdFALSE. */\r
424                 return pdFALSE;\r
425         }\r
426 \r
427 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r