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