]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/UARTCommandConsole.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / UARTCommandConsole.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97  * NOTE:  This file uses a third party USB CDC driver.\r
98  */\r
99 \r
100 /* Standard includes. */\r
101 #include "string.h"\r
102 #include "stdio.h"\r
103 \r
104 /* FreeRTOS includes. */\r
105 #include "FreeRTOS.h"\r
106 #include "task.h"\r
107 #include "semphr.h"\r
108 \r
109 /* Example includes. */\r
110 #include "FreeRTOS_CLI.h"\r
111 \r
112 /* Demo application includes. */\r
113 #include "serial.h"\r
114 \r
115 /* Dimensions the buffer into which input characters are placed. */\r
116 #define cmdMAX_INPUT_SIZE               50\r
117 \r
118 #define cmdQUEUE_LENGTH                 25\r
119 \r
120 /* DEL acts as a backspace. */\r
121 #define cmdASCII_DEL            ( 0x7F )\r
122 \r
123 #define cmdMAX_MUTEX_WAIT               ( ( ( TickType_t ) 300 ) / ( portTICK_PERIOD_MS ) )\r
124 \r
125 #ifndef configCLI_BAUD_RATE\r
126         #define configCLI_BAUD_RATE     115200\r
127 #endif\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /*\r
132  * The task that implements the command console processing.\r
133  */\r
134 static void prvUARTCommandConsoleTask( void *pvParameters );\r
135 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /* Const messages output by the command console. */\r
140 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
141 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
142 static const char * const pcNewLine = "\r\n";\r
143 \r
144 SemaphoreHandle_t xTxMutex = NULL;\r
145 static xComPortHandle xPort = 0;\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )\r
150 {\r
151         /* Create the semaphore used to access the UART Tx. */\r
152         xTxMutex = xSemaphoreCreateMutex();\r
153         configASSERT( xTxMutex );\r
154 \r
155         /* Create that task that handles the console itself. */\r
156         xTaskCreate(    prvUARTCommandConsoleTask,      /* The task that implements the command console. */\r
157                                         "CLI",                                          /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
158                                         usStackSize,                            /* The size of the stack allocated to the task. */\r
159                                         NULL,                                           /* The parameter is not used, so NULL is passed. */\r
160                                         uxPriority,                                     /* The priority allocated to the task. */\r
161                                         NULL );                                         /* A handle is not required, so just pass NULL. */\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 static void prvUARTCommandConsoleTask( void *pvParameters )\r
166 {\r
167 signed char cRxedChar;\r
168 uint8_t ucInputIndex = 0;\r
169 char *pcOutputString;\r
170 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
171 BaseType_t xReturned;\r
172 xComPortHandle xPort;\r
173 \r
174         ( void ) pvParameters;\r
175 \r
176         /* Obtain the address of the output buffer.  Note there is no mutual\r
177         exclusion on this buffer as it is assumed only one command console interface\r
178         will be used at any one time. */\r
179         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
180 \r
181         /* Initialise the UART. */\r
182         xPort = xSerialPortInitMinimal( configCLI_BAUD_RATE, cmdQUEUE_LENGTH );\r
183 \r
184         /* Send the welcome message. */\r
185         vSerialPutString( xPort, ( signed char * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );\r
186 \r
187         for( ;; )\r
188         {\r
189                 /* Wait for the next character.  The while loop is used in case\r
190                 INCLUDE_vTaskSuspend is not set to 1 - in which case portMAX_DELAY will\r
191                 be a genuine block time rather than an infinite block time. */\r
192                 while( xSerialGetChar( xPort, &cRxedChar, portMAX_DELAY ) != pdPASS );\r
193 \r
194                 /* Ensure exclusive access to the UART Tx. */\r
195                 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
196                 {\r
197                         /* Echo the character back. */\r
198                         xSerialPutChar( xPort, cRxedChar, portMAX_DELAY );\r
199 \r
200                         /* Was it the end of the line? */\r
201                         if( cRxedChar == '\n' || cRxedChar == '\r' )\r
202                         {\r
203                                 /* Just to space the output from the input. */\r
204                                 vSerialPutString( xPort, ( signed char * ) pcNewLine, strlen( pcNewLine ) );\r
205 \r
206                                 /* See if the command is empty, indicating that the last command\r
207                                 is to be executed again. */\r
208                                 if( ucInputIndex == 0 )\r
209                                 {\r
210                                         /* Copy the last command back into the input string. */\r
211                                         strcpy( cInputString, cLastInputString );\r
212                                 }\r
213 \r
214                                 /* Pass the received command to the command interpreter.  The\r
215                                 command interpreter is called repeatedly until it returns\r
216                                 pdFALSE (indicating there is no more output) as it might\r
217                                 generate more than one string. */\r
218                                 do\r
219                                 {\r
220                                         /* Get the next output string from the command interpreter. */\r
221                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
222 \r
223                                         /* Write the generated string to the UART. */\r
224                                         vSerialPutString( xPort, ( signed char * ) pcOutputString, strlen( pcOutputString ) );\r
225 \r
226                                 } while( xReturned != pdFALSE );\r
227 \r
228                                 /* All the strings generated by the input command have been\r
229                                 sent.  Clear the input string ready to receive the next command.\r
230                                 Remember the command that was just processed first in case it is\r
231                                 to be processed again. */\r
232                                 strcpy( cLastInputString, cInputString );\r
233                                 ucInputIndex = 0;\r
234                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
235 \r
236                                 vSerialPutString( xPort, ( signed char * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );\r
237                         }\r
238                         else\r
239                         {\r
240                                 if( cRxedChar == '\r' )\r
241                                 {\r
242                                         /* Ignore the character. */\r
243                                 }\r
244                                 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )\r
245                                 {\r
246                                         /* Backspace was pressed.  Erase the last character in the\r
247                                         string - if any. */\r
248                                         if( ucInputIndex > 0 )\r
249                                         {\r
250                                                 ucInputIndex--;\r
251                                                 cInputString[ ucInputIndex ] = '\0';\r
252                                         }\r
253                                 }\r
254                                 else\r
255                                 {\r
256                                         /* A character was entered.  Add it to the string entered so\r
257                                         far.  When a \n is entered the complete string will be\r
258                                         passed to the command interpreter. */\r
259                                         if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
260                                         {\r
261                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
262                                                 {\r
263                                                         cInputString[ ucInputIndex ] = cRxedChar;\r
264                                                         ucInputIndex++;\r
265                                                 }\r
266                                         }\r
267                                 }\r
268                         }\r
269 \r
270                         /* Must ensure to give the mutex back. */\r
271                         xSemaphoreGive( xTxMutex );\r
272                 }\r
273         }\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 void vOutputString( const char * const pcMessage )\r
278 {\r
279         if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
280         {\r
281                 vSerialPutString( xPort, ( signed char * ) pcMessage, strlen( pcMessage ) );\r
282                 xSemaphoreGive( xTxMutex );\r
283         }\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r