]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/UARTCommandConsole.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / UARTCommandConsole.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * NOTE:  This file uses a third party USB CDC driver.\r
31  */\r
32 \r
33 /* Standard includes. */\r
34 #include "string.h"\r
35 #include "stdio.h"\r
36 \r
37 /* FreeRTOS includes. */\r
38 #include "FreeRTOS.h"\r
39 #include "task.h"\r
40 #include "semphr.h"\r
41 \r
42 /* Example includes. */\r
43 #include "FreeRTOS_CLI.h"\r
44 \r
45 /* Demo application includes. */\r
46 #include "serial.h"\r
47 \r
48 /* Dimensions the buffer into which input characters are placed. */\r
49 #define cmdMAX_INPUT_SIZE               50\r
50 \r
51 /* Dimentions a buffer to be used by the UART driver, if the UART driver uses a\r
52 buffer at all. */\r
53 #define cmdQUEUE_LENGTH                 25\r
54 \r
55 /* DEL acts as a backspace. */\r
56 #define cmdASCII_DEL            ( 0x7F )\r
57 \r
58 /* The maximum time to wait for the mutex that guards the UART to become\r
59 available. */\r
60 #define cmdMAX_MUTEX_WAIT               pdMS_TO_TICKS( 300 )\r
61 \r
62 #ifndef configCLI_BAUD_RATE\r
63         #define configCLI_BAUD_RATE     115200\r
64 #endif\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 /*\r
69  * The task that implements the command console processing.\r
70  */\r
71 static void prvUARTCommandConsoleTask( void *pvParameters );\r
72 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );\r
73 \r
74 /*-----------------------------------------------------------*/\r
75 \r
76 /* Const messages output by the command console. */\r
77 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
78 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
79 static const char * const pcNewLine = "\r\n";\r
80 \r
81 /* Used to guard access to the UART in case messages are sent to the UART from\r
82 more than one task. */\r
83 static SemaphoreHandle_t xTxMutex = NULL;\r
84 \r
85 /* The handle to the UART port, which is not used by all ports. */\r
86 static xComPortHandle xPort = 0;\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )\r
91 {\r
92         /* Create the semaphore used to access the UART Tx. */\r
93         xTxMutex = xSemaphoreCreateMutex();\r
94         configASSERT( xTxMutex );\r
95 \r
96         /* Create that task that handles the console itself. */\r
97         xTaskCreate(    prvUARTCommandConsoleTask,      /* The task that implements the command console. */\r
98                                         "CLI",                                          /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
99                                         usStackSize,                            /* The size of the stack allocated to the task. */\r
100                                         NULL,                                           /* The parameter is not used, so NULL is passed. */\r
101                                         uxPriority,                                     /* The priority allocated to the task. */\r
102                                         NULL );                                         /* A handle is not required, so just pass NULL. */\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 static void prvUARTCommandConsoleTask( void *pvParameters )\r
107 {\r
108 signed char cRxedChar;\r
109 uint8_t ucInputIndex = 0;\r
110 char *pcOutputString;\r
111 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
112 BaseType_t xReturned;\r
113 xComPortHandle xPort;\r
114 \r
115         ( void ) pvParameters;\r
116 \r
117         /* Obtain the address of the output buffer.  Note there is no mutual\r
118         exclusion on this buffer as it is assumed only one command console interface\r
119         will be used at any one time. */\r
120         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
121 \r
122         /* Initialise the UART. */\r
123         xPort = xSerialPortInitMinimal( configCLI_BAUD_RATE, cmdQUEUE_LENGTH );\r
124 \r
125         /* Send the welcome message. */\r
126         vSerialPutString( xPort, ( signed char * ) pcWelcomeMessage, ( unsigned short ) strlen( pcWelcomeMessage ) );\r
127 \r
128         for( ;; )\r
129         {\r
130                 /* Wait for the next character.  The while loop is used in case\r
131                 INCLUDE_vTaskSuspend is not set to 1 - in which case portMAX_DELAY will\r
132                 be a genuine block time rather than an infinite block time. */\r
133                 while( xSerialGetChar( xPort, &cRxedChar, portMAX_DELAY ) != pdPASS );\r
134 \r
135                 /* Ensure exclusive access to the UART Tx. */\r
136                 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
137                 {\r
138                         /* Echo the character back. */\r
139                         xSerialPutChar( xPort, cRxedChar, portMAX_DELAY );\r
140 \r
141                         /* Was it the end of the line? */\r
142                         if( cRxedChar == '\n' || cRxedChar == '\r' )\r
143                         {\r
144                                 /* Just to space the output from the input. */\r
145                                 vSerialPutString( xPort, ( signed char * ) pcNewLine, ( unsigned short ) strlen( pcNewLine ) );\r
146 \r
147                                 /* See if the command is empty, indicating that the last command\r
148                                 is to be executed again. */\r
149                                 if( ucInputIndex == 0 )\r
150                                 {\r
151                                         /* Copy the last command back into the input string. */\r
152                                         strcpy( cInputString, cLastInputString );\r
153                                 }\r
154 \r
155                                 /* Pass the received command to the command interpreter.  The\r
156                                 command interpreter is called repeatedly until it returns\r
157                                 pdFALSE (indicating there is no more output) as it might\r
158                                 generate more than one string. */\r
159                                 do\r
160                                 {\r
161                                         /* Get the next output string from the command interpreter. */\r
162                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
163 \r
164                                         /* Write the generated string to the UART. */\r
165                                         vSerialPutString( xPort, ( signed char * ) pcOutputString, ( unsigned short ) strlen( pcOutputString ) );\r
166 \r
167                                 } while( xReturned != pdFALSE );\r
168 \r
169                                 /* All the strings generated by the input command have been\r
170                                 sent.  Clear the input string ready to receive the next command.\r
171                                 Remember the command that was just processed first in case it is\r
172                                 to be processed again. */\r
173                                 strcpy( cLastInputString, cInputString );\r
174                                 ucInputIndex = 0;\r
175                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
176 \r
177                                 vSerialPutString( xPort, ( signed char * ) pcEndOfOutputMessage, ( unsigned short ) strlen( pcEndOfOutputMessage ) );\r
178                         }\r
179                         else\r
180                         {\r
181                                 if( cRxedChar == '\r' )\r
182                                 {\r
183                                         /* Ignore the character. */\r
184                                 }\r
185                                 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )\r
186                                 {\r
187                                         /* Backspace was pressed.  Erase the last character in the\r
188                                         string - if any. */\r
189                                         if( ucInputIndex > 0 )\r
190                                         {\r
191                                                 ucInputIndex--;\r
192                                                 cInputString[ ucInputIndex ] = '\0';\r
193                                         }\r
194                                 }\r
195                                 else\r
196                                 {\r
197                                         /* A character was entered.  Add it to the string entered so\r
198                                         far.  When a \n is entered the complete string will be\r
199                                         passed to the command interpreter. */\r
200                                         if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
201                                         {\r
202                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
203                                                 {\r
204                                                         cInputString[ ucInputIndex ] = cRxedChar;\r
205                                                         ucInputIndex++;\r
206                                                 }\r
207                                         }\r
208                                 }\r
209                         }\r
210 \r
211                         /* Must ensure to give the mutex back. */\r
212                         xSemaphoreGive( xTxMutex );\r
213                 }\r
214         }\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 void vOutputString( const char * const pcMessage )\r
219 {\r
220         if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
221         {\r
222                 vSerialPutString( xPort, ( signed char * ) pcMessage, ( unsigned short ) strlen( pcMessage ) );\r
223                 xSemaphoreGive( xTxMutex );\r
224         }\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r