2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * NOTE: This file uses a third party USB CDC driver.
\r
33 /* Standard includes. */
\r
37 /* FreeRTOS includes. */
\r
38 #include "FreeRTOS.h"
\r
42 /* Example includes. */
\r
43 #include "FreeRTOS_CLI.h"
\r
45 /* Demo application includes. */
\r
48 /* Dimensions the buffer into which input characters are placed. */
\r
49 #define cmdMAX_INPUT_SIZE 50
\r
51 /* Dimentions a buffer to be used by the UART driver, if the UART driver uses a
\r
53 #define cmdQUEUE_LENGTH 25
\r
55 /* DEL acts as a backspace. */
\r
56 #define cmdASCII_DEL ( 0x7F )
\r
58 /* The maximum time to wait for the mutex that guards the UART to become
\r
60 #define cmdMAX_MUTEX_WAIT pdMS_TO_TICKS( 300 )
\r
62 #ifndef configCLI_BAUD_RATE
\r
63 #define configCLI_BAUD_RATE 115200
\r
66 /*-----------------------------------------------------------*/
\r
69 * The task that implements the command console processing.
\r
71 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
72 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
\r
74 /*-----------------------------------------------------------*/
\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
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
85 /* The handle to the UART port, which is not used by all ports. */
\r
86 static xComPortHandle xPort = 0;
\r
88 /*-----------------------------------------------------------*/
\r
90 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )
\r
92 /* Create the semaphore used to access the UART Tx. */
\r
93 xTxMutex = xSemaphoreCreateMutex();
\r
94 configASSERT( xTxMutex );
\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
104 /*-----------------------------------------------------------*/
\r
106 static void prvUARTCommandConsoleTask( void *pvParameters )
\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
115 ( void ) pvParameters;
\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
122 /* Initialise the UART. */
\r
123 xPort = xSerialPortInitMinimal( configCLI_BAUD_RATE, cmdQUEUE_LENGTH );
\r
125 /* Send the welcome message. */
\r
126 vSerialPutString( xPort, ( signed char * ) pcWelcomeMessage, ( unsigned short ) strlen( pcWelcomeMessage ) );
\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
135 /* Ensure exclusive access to the UART Tx. */
\r
136 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
138 /* Echo the character back. */
\r
139 xSerialPutChar( xPort, cRxedChar, portMAX_DELAY );
\r
141 /* Was it the end of the line? */
\r
142 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
144 /* Just to space the output from the input. */
\r
145 vSerialPutString( xPort, ( signed char * ) pcNewLine, ( unsigned short ) strlen( pcNewLine ) );
\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
151 /* Copy the last command back into the input string. */
\r
152 strcpy( cInputString, cLastInputString );
\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
161 /* Get the next output string from the command interpreter. */
\r
162 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
164 /* Write the generated string to the UART. */
\r
165 vSerialPutString( xPort, ( signed char * ) pcOutputString, ( unsigned short ) strlen( pcOutputString ) );
\r
167 } while( xReturned != pdFALSE );
\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
175 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
177 vSerialPutString( xPort, ( signed char * ) pcEndOfOutputMessage, ( unsigned short ) strlen( pcEndOfOutputMessage ) );
\r
181 if( cRxedChar == '\r' )
\r
183 /* Ignore the character. */
\r
185 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )
\r
187 /* Backspace was pressed. Erase the last character in the
\r
188 string - if any. */
\r
189 if( ucInputIndex > 0 )
\r
192 cInputString[ ucInputIndex ] = '\0';
\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
202 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
204 cInputString[ ucInputIndex ] = cRxedChar;
\r
211 /* Must ensure to give the mutex back. */
\r
212 xSemaphoreGive( xTxMutex );
\r
216 /*-----------------------------------------------------------*/
\r
218 void vOutputString( const char * const pcMessage )
\r
220 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
222 vSerialPutString( xPort, ( signed char * ) pcMessage, ( unsigned short ) strlen( pcMessage ) );
\r
223 xSemaphoreGive( xTxMutex );
\r
226 /*-----------------------------------------------------------*/
\r