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