]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/Source/Full-Demo/Sample-CLI-commands.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / CORTEX_A9_RZ_R7S72100_IAR_DS-5 / Source / Full-Demo / Sample-CLI-commands.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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
32 \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
36     kernel.\r
37 \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 itcan 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
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \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
65 \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
69 \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
73 */\r
74 \r
75  /******************************************************************************\r
76  *\r
77  * See the following URL for information on the commands defined in this file:\r
78  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/Ethernet_Related_CLI_Commands.shtml\r
79  *\r
80  ******************************************************************************/\r
81 \r
82 \r
83 /* FreeRTOS includes. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 \r
87 /* Standard includes. */\r
88 #include <stdint.h>\r
89 #include <stdio.h>\r
90 #include <string.h>\r
91 \r
92 /* FreeRTOS+CLI includes. */\r
93 #include "FreeRTOS_CLI.h"\r
94 \r
95 #ifndef  configINCLUDE_TRACE_RELATED_CLI_COMMANDS\r
96         #define configINCLUDE_TRACE_RELATED_CLI_COMMANDS 0\r
97 #endif\r
98 \r
99 \r
100 /*\r
101  * Implements the run-time-stats command.\r
102  */\r
103 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
104 \r
105 /*\r
106  * Implements the task-stats command.\r
107  */\r
108 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
109 \r
110 /*\r
111  * Implements the echo-three-parameters command.\r
112  */\r
113 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
114 \r
115 /*\r
116  * Implements the echo-parameters command.\r
117  */\r
118 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
119 \r
120 /*\r
121  * Registers the CLI commands defined within this file with the FreeRTOS+CLI\r
122  * command line interface.\r
123  */\r
124 void vRegisterSampleCLICommands( void );\r
125 \r
126 /*\r
127  * Implements the "trace start" and "trace stop" commands;\r
128  */\r
129 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
130         static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
131 #endif\r
132 \r
133 /* Structure that defines the "run-time-stats" command line command.   This\r
134 generates a table that shows how much run time each task has */\r
135 static const CLI_Command_Definition_t xRunTimeStats =\r
136 {\r
137         "run-time-stats", /* The command string to type. */\r
138         "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n",\r
139         prvRunTimeStatsCommand, /* The function to run. */\r
140         0 /* No parameters are expected. */\r
141 };\r
142 \r
143 /* Structure that defines the "task-stats" command line command.  This generates\r
144 a table that gives information on each task in the system. */\r
145 static const CLI_Command_Definition_t xTaskStats =\r
146 {\r
147         "task-stats", /* The command string to type. */\r
148         "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n",\r
149         prvTaskStatsCommand, /* The function to run. */\r
150         0 /* No parameters are expected. */\r
151 };\r
152 \r
153 /* Structure that defines the "echo_3_parameters" command line command.  This\r
154 takes exactly three parameters that the command simply echos back one at a\r
155 time. */\r
156 static const CLI_Command_Definition_t xThreeParameterEcho =\r
157 {\r
158         "echo-3-parameters",\r
159         "\r\necho-3-parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n",\r
160         prvThreeParameterEchoCommand, /* The function to run. */\r
161         3 /* Three parameters are expected, which can take any value. */\r
162 };\r
163 \r
164 /* Structure that defines the "echo_parameters" command line command.  This\r
165 takes a variable number of parameters that the command simply echos back one at\r
166 a time. */\r
167 static const CLI_Command_Definition_t xParameterEcho =\r
168 {\r
169         "echo-parameters",\r
170         "\r\necho-parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n",\r
171         prvParameterEchoCommand, /* The function to run. */\r
172         -1 /* The user can enter any number of commands. */\r
173 };\r
174 \r
175 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
176         /* Structure that defines the "trace" command line command.  This takes a single\r
177         parameter, which can be either "start" or "stop". */\r
178         static const CLI_Command_Definition_t xStartStopTrace =\r
179         {\r
180                 "trace",\r
181                 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n",\r
182                 prvStartStopTraceCommand, /* The function to run. */\r
183                 1 /* One parameter is expected.  Valid values are "start" and "stop". */\r
184         };\r
185 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r
186 \r
187 /*-----------------------------------------------------------*/\r
188 \r
189 void vRegisterSampleCLICommands( void )\r
190 {\r
191         /* Register all the command line commands defined immediately above. */\r
192         FreeRTOS_CLIRegisterCommand( &xTaskStats );\r
193         FreeRTOS_CLIRegisterCommand( &xRunTimeStats );\r
194         FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );\r
195         FreeRTOS_CLIRegisterCommand( &xParameterEcho );\r
196 \r
197         #if( configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 )\r
198         {\r
199                 FreeRTOS_CLIRegisterCommand( & xStartStopTrace );\r
200         }\r
201         #endif\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 static portBASE_TYPE prvTaskStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
206 {\r
207 const int8_t *const pcHeader = ( int8_t * ) "Task          State  Priority  Stack       #\r\n************************************************\r\n";\r
208 \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
215 \r
216         /* Generate a table of task stats. */\r
217         strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );\r
218         vTaskList( pcWriteBuffer + strlen( ( char * ) pcHeader ) );\r
219 \r
220         /* There is no more data to return after this single string, so return\r
221         pdFALSE. */\r
222         return pdFALSE;\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 static portBASE_TYPE prvRunTimeStatsCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
227 {\r
228 const int8_t * const pcHeader = ( int8_t * ) "Task            Abs Time      % Time\r\n****************************************\r\n";\r
229 \r
230         /* Remove compile time warnings about unused parameters, and check the\r
231         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
232         write buffer length is adequate, so does not check for buffer overflows. */\r
233         ( void ) pcCommandString;\r
234         ( void ) xWriteBufferLen;\r
235         configASSERT( pcWriteBuffer );\r
236 \r
237         /* Generate a table of task stats. */\r
238         strcpy( ( char * ) pcWriteBuffer, ( char * ) pcHeader );\r
239         vTaskGetRunTimeStats( pcWriteBuffer + strlen( ( char * ) pcHeader ) );\r
240 \r
241         /* There is no more data to return after this single string, so return\r
242         pdFALSE. */\r
243         return pdFALSE;\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static portBASE_TYPE prvThreeParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
248 {\r
249 int8_t *pcParameter;\r
250 portBASE_TYPE xParameterStringLength, xReturn;\r
251 static portBASE_TYPE lParameterNumber = 0;\r
252 \r
253         /* Remove compile time warnings about unused parameters, and check the\r
254         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
255         write buffer length is adequate, so does not check for buffer overflows. */\r
256         ( void ) pcCommandString;\r
257         ( void ) xWriteBufferLen;\r
258         configASSERT( pcWriteBuffer );\r
259 \r
260         if( lParameterNumber == 0 )\r
261         {\r
262                 /* The first time the function is called after the command has been\r
263                 entered just a header string is returned. */\r
264                 sprintf( ( char * ) pcWriteBuffer, "The three parameters were:\r\n" );\r
265 \r
266                 /* Next time the function is called the first parameter will be echoed\r
267                 back. */\r
268                 lParameterNumber = 1L;\r
269 \r
270                 /* There is more data to be returned as no parameters have been echoed\r
271                 back yet. */\r
272                 xReturn = pdPASS;\r
273         }\r
274         else\r
275         {\r
276                 /* Obtain the parameter string. */\r
277                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
278                                                                         (\r
279                                                                                 pcCommandString,                /* The command string itself. */\r
280                                                                                 lParameterNumber,               /* Return the next parameter. */\r
281                                                                                 &xParameterStringLength /* Store the parameter string length. */\r
282                                                                         );\r
283 \r
284                 /* Sanity check something was returned. */\r
285                 configASSERT( pcParameter );\r
286 \r
287                 /* Return the parameter string. */\r
288                 memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
289                 sprintf( ( char * ) pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
290                 strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, xParameterStringLength );\r
291                 strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
292 \r
293                 /* If this is the last of the three parameters then there are no more\r
294                 strings to return after this one. */\r
295                 if( lParameterNumber == 3L )\r
296                 {\r
297                         /* If this is the last of the three parameters then there are no more\r
298                         strings to return after this one. */\r
299                         xReturn = pdFALSE;\r
300                         lParameterNumber = 0L;\r
301                 }\r
302                 else\r
303                 {\r
304                         /* There are more parameters to return after this one. */\r
305                         xReturn = pdTRUE;\r
306                         lParameterNumber++;\r
307                 }\r
308         }\r
309 \r
310         return xReturn;\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 static portBASE_TYPE prvParameterEchoCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
315 {\r
316 int8_t *pcParameter;\r
317 portBASE_TYPE xParameterStringLength, xReturn;\r
318 static portBASE_TYPE lParameterNumber = 0;\r
319 \r
320         /* Remove compile time warnings about unused parameters, and check the\r
321         write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
322         write buffer length is adequate, so does not check for buffer overflows. */\r
323         ( void ) pcCommandString;\r
324         ( void ) xWriteBufferLen;\r
325         configASSERT( pcWriteBuffer );\r
326 \r
327         if( lParameterNumber == 0 )\r
328         {\r
329                 /* The first time the function is called after the command has been\r
330                 entered just a header string is returned. */\r
331                 sprintf( ( char * ) pcWriteBuffer, "The parameters were:\r\n" );\r
332 \r
333                 /* Next time the function is called the first parameter will be echoed\r
334                 back. */\r
335                 lParameterNumber = 1L;\r
336 \r
337                 /* There is more data to be returned as no parameters have been echoed\r
338                 back yet. */\r
339                 xReturn = pdPASS;\r
340         }\r
341         else\r
342         {\r
343                 /* Obtain the parameter string. */\r
344                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
345                                                                         (\r
346                                                                                 pcCommandString,                /* The command string itself. */\r
347                                                                                 lParameterNumber,               /* Return the next parameter. */\r
348                                                                                 &xParameterStringLength /* Store the parameter string length. */\r
349                                                                         );\r
350 \r
351                 if( pcParameter != NULL )\r
352                 {\r
353                         /* Return the parameter string. */\r
354                         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
355                         sprintf( ( char * ) pcWriteBuffer, "%d: ", ( int ) lParameterNumber );\r
356                         strncat( ( char * ) pcWriteBuffer, ( const char * ) pcParameter, xParameterStringLength );\r
357                         strncat( ( char * ) pcWriteBuffer, "\r\n", strlen( "\r\n" ) );\r
358 \r
359                         /* There might be more parameters to return after this one. */\r
360                         xReturn = pdTRUE;\r
361                         lParameterNumber++;\r
362                 }\r
363                 else\r
364                 {\r
365                         /* No more parameters were found.  Make sure the write buffer does\r
366                         not contain a valid string. */\r
367                         pcWriteBuffer[ 0 ] = 0x00;\r
368 \r
369                         /* No more data to return. */\r
370                         xReturn = pdFALSE;\r
371 \r
372                         /* Start over the next time this command is executed. */\r
373                         lParameterNumber = 0;\r
374                 }\r
375         }\r
376 \r
377         return xReturn;\r
378 }\r
379 /*-----------------------------------------------------------*/\r
380 \r
381 #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
382 \r
383         static portBASE_TYPE prvStartStopTraceCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
384         {\r
385         int8_t *pcParameter;\r
386         portBASE_TYPE lParameterStringLength;\r
387 \r
388                 /* Remove compile time warnings about unused parameters, and check the\r
389                 write buffer is not NULL.  NOTE - for simplicity, this example assumes the\r
390                 write buffer length is adequate, so does not check for buffer overflows. */\r
391                 ( void ) pcCommandString;\r
392                 ( void ) xWriteBufferLen;\r
393                 configASSERT( pcWriteBuffer );\r
394 \r
395                 /* Obtain the parameter string. */\r
396                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
397                                                                         (\r
398                                                                                 pcCommandString,                /* The command string itself. */\r
399                                                                                 1,                                              /* Return the first parameter. */\r
400                                                                                 &lParameterStringLength /* Store the parameter string length. */\r
401                                                                         );\r
402 \r
403                 /* Sanity check something was returned. */\r
404                 configASSERT( pcParameter );\r
405 \r
406                 /* There are only two valid parameter values. */\r
407                 if( strncmp( ( const char * ) pcParameter, "start", strlen( "start" ) ) == 0 )\r
408                 {\r
409                         /* Start or restart the trace. */\r
410                         vTraceStop();\r
411                         vTraceClear();\r
412                         vTraceStart();\r
413 \r
414                         sprintf( ( char * ) pcWriteBuffer, "Trace recording (re)started.\r\n" );\r
415                 }\r
416                 else if( strncmp( ( const char * ) pcParameter, "stop", strlen( "stop" ) ) == 0 )\r
417                 {\r
418                         /* End the trace, if one is running. */\r
419                         vTraceStop();\r
420                         sprintf( ( char * ) pcWriteBuffer, "Stopping trace recording.\r\n" );\r
421                 }\r
422                 else\r
423                 {\r
424                         sprintf( ( char * ) pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );\r
425                 }\r
426 \r
427                 /* There is no more data to return after this single string, so return\r
428                 pdFALSE. */\r
429                 return pdFALSE;\r
430         }\r
431 \r
432 #endif /* configINCLUDE_TRACE_RELATED_CLI_COMMANDS */\r