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