]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/Source/Full-Demo/UARTCommandConsole.c
Update demos that use FreeRTOS+CLI to remove casting to int8_t * from strings.
[freertos] / FreeRTOS / Demo / CORTEX_A9_RZ_R7S72100_IAR_DS-5 / Source / Full-Demo / UARTCommandConsole.c
1 /*\r
2     FreeRTOS V7.1.0 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Standard includes. */\r
55 #include "string.h"\r
56 #include "stdio.h"\r
57 #include "stdint.h"\r
58 \r
59 /* FreeRTOS includes. */\r
60 #include "FreeRTOS.h"\r
61 #include "task.h"\r
62 #include "semphr.h"\r
63 \r
64 /* Common demo includes. */\r
65 #include "serial.h"\r
66 \r
67 /* Example includes. */\r
68 #include "FreeRTOS_CLI.h"\r
69 #include "UARTCommandConsole.h"\r
70 \r
71 /* Dimensions the buffer into which input characters are placed. */\r
72 #define cmdMAX_INPUT_SIZE               50\r
73 \r
74 /* The maximum time in ticks to wait for the UART access mutex. */\r
75 #define cmdMAX_MUTEX_WAIT               ( 200 / portTICK_RATE_MS )\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /*\r
80  * The task that implements the command console processing.\r
81  */\r
82 static void prvUARTCommandConsoleTask( void *pvParameters );\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /* Const messages output by the command console. */\r
87 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
88 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
89 static const char * const pcNewLine = "\r\n";\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )\r
94 {\r
95         /* Create that task that handles the console itself. */\r
96         xTaskCreate(    prvUARTCommandConsoleTask,                      /* The task that implements the command console. */\r
97                                         "CLI",                                                          /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
98                                         usStackSize,                                            /* The size of the stack allocated to the task. */\r
99                                         NULL,                                                           /* The parameter is not used, so NULL is passed. */\r
100                                         uxPriority,                                                     /* The priority allocated to the task. */\r
101                                         NULL );                                                         /* A handle is not required, so just pass NULL. */\r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 static void prvUARTCommandConsoleTask( void *pvParameters )\r
106 {\r
107 char cRxedChar, cInputIndex = 0, *pcOutputString;\r
108 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
109 portBASE_TYPE xReturned;\r
110 \r
111         ( void ) pvParameters;\r
112 \r
113         /* Obtain the address of the output buffer.  Note there is no mutual\r
114         exclusion on this buffer as it is assumed only one command console\r
115         interface will be used at any one time. */\r
116         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
117 \r
118         /* Send the welcome message. */\r
119         vSerialPutString( NULL, ( const signed char * ) pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) );\r
120 \r
121         for( ;; )\r
122         {\r
123                 /* Only interested in reading one character at a time. */\r
124                 while( xSerialGetChar( NULL, ( signed char * ) &cRxedChar, portMAX_DELAY ) == pdFALSE );\r
125 \r
126                 /* Echo the character back. */\r
127                 xSerialPutChar( NULL, cRxedChar, portMAX_DELAY );\r
128 \r
129                 /* Was it the end of the line? */\r
130                 if( cRxedChar == '\n' || cRxedChar == '\r' )\r
131                 {\r
132                         /* Just to space the output from the input. */\r
133                         vSerialPutString( NULL, ( const signed char * ) pcNewLine, strlen( ( char * ) pcNewLine ) );\r
134 \r
135                         /* See if the command is empty, indicating that the last command is\r
136                         to be executed again. */\r
137                         if( cInputIndex == 0 )\r
138                         {\r
139                                 /* Copy the last command back into the input string. */\r
140                                 strcpy( ( char * ) cInputString, ( char * ) cLastInputString );\r
141                         }\r
142 \r
143                         /* Pass the received command to the command interpreter.  The\r
144                         command interpreter is called repeatedly until it returns pdFALSE\r
145                         (indicating there is no more output) as it might generate more than\r
146                         one string. */\r
147                         do\r
148                         {\r
149                                 /* Get the next output string from the command interpreter. */\r
150                                 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
151 \r
152                                 /* Write the generated string to the UART. */\r
153                                 vSerialPutString( NULL, ( const signed char * ) pcOutputString, strlen( ( char * ) pcOutputString ) );\r
154 \r
155                         } while( xReturned != pdFALSE );\r
156 \r
157                         /* All the strings generated by the input command have been sent.\r
158                         Clear the input string ready to receive the next command.  Remember\r
159                         the command that was just processed first in case it is to be\r
160                         processed again. */\r
161                         strcpy( ( char * ) cLastInputString, ( char * ) cInputString );\r
162                         cInputIndex = 0;\r
163                         memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
164                         vSerialPutString( NULL, ( const signed char * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) );\r
165                 }\r
166                 else\r
167                 {\r
168                         if( cRxedChar == '\r' )\r
169                         {\r
170                                 /* Ignore the character. */\r
171                         }\r
172                         else if( cRxedChar == '\b' )\r
173                         {\r
174                                 /* Backspace was pressed.  Erase the last character in the\r
175                                 string - if any. */\r
176                                 if( cInputIndex > 0 )\r
177                                 {\r
178                                         cInputIndex--;\r
179                                         cInputString[ cInputIndex ] = '\0';\r
180                                 }\r
181                         }\r
182                         else\r
183                         {\r
184                                 /* A character was entered.  Add it to the string\r
185                                 entered so far.  When a \n is entered the complete\r
186                                 string will be passed to the command interpreter. */\r
187                                 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
188                                 {\r
189                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
190                                         {\r
191                                                 cInputString[ cInputIndex ] = cRxedChar;\r
192                                                 cInputIndex++;\r
193                                         }\r
194                                 }\r
195                         }\r
196                 }\r
197         }\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r