2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
71 * NOTE: This file uses a third party USB CDC driver.
\r
74 /* Standard includes. */
\r
78 /* FreeRTOS includes. */
\r
79 #include "FreeRTOS.h"
\r
83 /* Example includes. */
\r
84 #include "FreeRTOS_CLI.h"
\r
86 /* Demo application includes. */
\r
89 /* Dimensions the buffer into which input characters are placed. */
\r
90 #define cmdMAX_INPUT_SIZE 50
\r
92 /* Dimentions a buffer to be used by the UART driver, if the UART driver uses a
\r
94 #define cmdQUEUE_LENGTH 25
\r
96 /* DEL acts as a backspace. */
\r
97 #define cmdASCII_DEL ( 0x7F )
\r
99 /* The maximum time to wait for the mutex that guards the UART to become
\r
101 #define cmdMAX_MUTEX_WAIT pdMS_TO_TICKS( 300 )
\r
103 #ifndef configCLI_BAUD_RATE
\r
104 #define configCLI_BAUD_RATE 115200
\r
107 /*-----------------------------------------------------------*/
\r
110 * The task that implements the command console processing.
\r
112 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
113 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
\r
115 /*-----------------------------------------------------------*/
\r
117 /* Const messages output by the command console. */
\r
118 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
119 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
120 static const char * const pcNewLine = "\r\n";
\r
122 /* Used to guard access to the UART in case messages are sent to the UART from
\r
123 more than one task. */
\r
124 static SemaphoreHandle_t xTxMutex = NULL;
\r
126 /* The handle to the UART port, which is not used by all ports. */
\r
127 static xComPortHandle xPort = 0;
\r
129 /*-----------------------------------------------------------*/
\r
131 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )
\r
133 /* Create the semaphore used to access the UART Tx. */
\r
134 xTxMutex = xSemaphoreCreateMutex();
\r
135 configASSERT( xTxMutex );
\r
137 /* Create that task that handles the console itself. */
\r
138 xTaskCreate( prvUARTCommandConsoleTask, /* The task that implements the command console. */
\r
139 "CLI", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
140 usStackSize, /* The size of the stack allocated to the task. */
\r
141 NULL, /* The parameter is not used, so NULL is passed. */
\r
142 uxPriority, /* The priority allocated to the task. */
\r
143 NULL ); /* A handle is not required, so just pass NULL. */
\r
145 /*-----------------------------------------------------------*/
\r
147 static void prvUARTCommandConsoleTask( void *pvParameters )
\r
149 signed char cRxedChar;
\r
150 uint8_t ucInputIndex = 0;
\r
151 char *pcOutputString;
\r
152 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
153 BaseType_t xReturned;
\r
154 xComPortHandle xPort;
\r
156 ( void ) pvParameters;
\r
158 /* Obtain the address of the output buffer. Note there is no mutual
\r
159 exclusion on this buffer as it is assumed only one command console interface
\r
160 will be used at any one time. */
\r
161 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
163 /* Initialise the UART. */
\r
164 xPort = xSerialPortInitMinimal( configCLI_BAUD_RATE, cmdQUEUE_LENGTH );
\r
166 /* Send the welcome message. */
\r
167 vSerialPutString( xPort, ( signed char * ) pcWelcomeMessage, ( unsigned short ) strlen( pcWelcomeMessage ) );
\r
171 /* Wait for the next character. The while loop is used in case
\r
172 INCLUDE_vTaskSuspend is not set to 1 - in which case portMAX_DELAY will
\r
173 be a genuine block time rather than an infinite block time. */
\r
174 while( xSerialGetChar( xPort, &cRxedChar, portMAX_DELAY ) != pdPASS );
\r
176 /* Ensure exclusive access to the UART Tx. */
\r
177 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
179 /* Echo the character back. */
\r
180 xSerialPutChar( xPort, cRxedChar, portMAX_DELAY );
\r
182 /* Was it the end of the line? */
\r
183 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
185 /* Just to space the output from the input. */
\r
186 vSerialPutString( xPort, ( signed char * ) pcNewLine, ( unsigned short ) strlen( pcNewLine ) );
\r
188 /* See if the command is empty, indicating that the last command
\r
189 is to be executed again. */
\r
190 if( ucInputIndex == 0 )
\r
192 /* Copy the last command back into the input string. */
\r
193 strcpy( cInputString, cLastInputString );
\r
196 /* Pass the received command to the command interpreter. The
\r
197 command interpreter is called repeatedly until it returns
\r
198 pdFALSE (indicating there is no more output) as it might
\r
199 generate more than one string. */
\r
202 /* Get the next output string from the command interpreter. */
\r
203 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
205 /* Write the generated string to the UART. */
\r
206 vSerialPutString( xPort, ( signed char * ) pcOutputString, ( unsigned short ) strlen( pcOutputString ) );
\r
208 } while( xReturned != pdFALSE );
\r
210 /* All the strings generated by the input command have been
\r
211 sent. Clear the input string ready to receive the next command.
\r
212 Remember the command that was just processed first in case it is
\r
213 to be processed again. */
\r
214 strcpy( cLastInputString, cInputString );
\r
216 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
218 vSerialPutString( xPort, ( signed char * ) pcEndOfOutputMessage, ( unsigned short ) strlen( pcEndOfOutputMessage ) );
\r
222 if( cRxedChar == '\r' )
\r
224 /* Ignore the character. */
\r
226 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )
\r
228 /* Backspace was pressed. Erase the last character in the
\r
229 string - if any. */
\r
230 if( ucInputIndex > 0 )
\r
233 cInputString[ ucInputIndex ] = '\0';
\r
238 /* A character was entered. Add it to the string entered so
\r
239 far. When a \n is entered the complete string will be
\r
240 passed to the command interpreter. */
\r
241 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
243 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
245 cInputString[ ucInputIndex ] = cRxedChar;
\r
252 /* Must ensure to give the mutex back. */
\r
253 xSemaphoreGive( xTxMutex );
\r
257 /*-----------------------------------------------------------*/
\r
259 void vOutputString( const char * const pcMessage )
\r
261 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
263 vSerialPutString( xPort, ( signed char * ) pcMessage, ( unsigned short ) strlen( pcMessage ) );
\r
264 xSemaphoreGive( xTxMutex );
\r
267 /*-----------------------------------------------------------*/
\r