2 * FreeRTOS Kernel V10.2.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.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * NOTE: This file uses a third party USB CDC driver.
\r
32 /* Standard includes. */
\r
36 /* FreeRTOS includes. */
\r
37 #include "FreeRTOS.h"
\r
41 /* Example includes. */
\r
42 #include "FreeRTOS_CLI.h"
\r
44 /* Demo application includes. */
\r
47 /* Dimensions the buffer into which input characters are placed. */
\r
48 #define cmdMAX_INPUT_SIZE 50
\r
50 /* Dimentions a buffer to be used by the UART driver, if the UART driver uses a
\r
52 #define cmdQUEUE_LENGTH 25
\r
54 /* DEL acts as a backspace. */
\r
55 #define cmdASCII_DEL ( 0x7F )
\r
57 /* The maximum time to wait for the mutex that guards the UART to become
\r
59 #define cmdMAX_MUTEX_WAIT pdMS_TO_TICKS( 300 )
\r
61 #ifndef configCLI_BAUD_RATE
\r
62 #define configCLI_BAUD_RATE 115200
\r
65 /*-----------------------------------------------------------*/
\r
68 * The task that implements the command console processing.
\r
70 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
71 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
\r
73 /*-----------------------------------------------------------*/
\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
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
84 /* The handle to the UART port, which is not used by all ports. */
\r
85 static xComPortHandle xPort = 0;
\r
87 /*-----------------------------------------------------------*/
\r
89 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )
\r
91 /* Create the semaphore used to access the UART Tx. */
\r
92 xTxMutex = xSemaphoreCreateMutex();
\r
93 configASSERT( xTxMutex );
\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
103 /*-----------------------------------------------------------*/
\r
105 static void prvUARTCommandConsoleTask( void *pvParameters )
\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
114 ( void ) pvParameters;
\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
121 /* Initialise the UART. */
\r
122 xPort = xSerialPortInitMinimal( configCLI_BAUD_RATE, cmdQUEUE_LENGTH );
\r
124 /* Send the welcome message. */
\r
125 vSerialPutString( xPort, ( signed char * ) pcWelcomeMessage, ( unsigned short ) strlen( pcWelcomeMessage ) );
\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
134 /* Ensure exclusive access to the UART Tx. */
\r
135 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
137 /* Echo the character back. */
\r
138 xSerialPutChar( xPort, cRxedChar, portMAX_DELAY );
\r
140 /* Was it the end of the line? */
\r
141 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
143 /* Just to space the output from the input. */
\r
144 vSerialPutString( xPort, ( signed char * ) pcNewLine, ( unsigned short ) strlen( pcNewLine ) );
\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
150 /* Copy the last command back into the input string. */
\r
151 strcpy( cInputString, cLastInputString );
\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
160 /* Get the next output string from the command interpreter. */
\r
161 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
163 /* Write the generated string to the UART. */
\r
164 vSerialPutString( xPort, ( signed char * ) pcOutputString, ( unsigned short ) strlen( pcOutputString ) );
\r
166 } while( xReturned != pdFALSE );
\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
174 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
176 vSerialPutString( xPort, ( signed char * ) pcEndOfOutputMessage, ( unsigned short ) strlen( pcEndOfOutputMessage ) );
\r
180 if( cRxedChar == '\r' )
\r
182 /* Ignore the character. */
\r
184 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )
\r
186 /* Backspace was pressed. Erase the last character in the
\r
187 string - if any. */
\r
188 if( ucInputIndex > 0 )
\r
191 cInputString[ ucInputIndex ] = '\0';
\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
201 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
203 cInputString[ ucInputIndex ] = cRxedChar;
\r
210 /* Must ensure to give the mutex back. */
\r
211 xSemaphoreGive( xTxMutex );
\r
215 /*-----------------------------------------------------------*/
\r
217 void vOutputString( const char * const pcMessage )
\r
219 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
221 vSerialPutString( xPort, ( signed char * ) pcMessage, ( unsigned short ) strlen( pcMessage ) );
\r
222 xSemaphoreGive( xTxMutex );
\r
225 /*-----------------------------------------------------------*/
\r