2 FreeRTOS V8.1.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * NOTE: This file uses a third party USB CDC driver.
\r
70 /* Standard includes. */
\r
74 /* FreeRTOS includes. */
\r
75 #include "FreeRTOS.h"
\r
79 /* Example includes. */
\r
80 #include "FreeRTOS_CLI.h"
\r
82 /* Demo application includes. */
\r
85 /* Dimensions the buffer into which input characters are placed. */
\r
86 #define cmdMAX_INPUT_SIZE 50
\r
88 #define cmdQUEUE_LENGTH 25
\r
90 /* DEL acts as a backspace. */
\r
91 #define cmdASCII_DEL ( 0x7F )
\r
93 #define cmdMAX_MUTEX_WAIT ( ( ( TickType_t ) 300 ) / ( portTICK_PERIOD_MS ) )
\r
95 #ifndef configCLI_BAUD_RATE
\r
96 #define configCLI_BAUD_RATE 115200
\r
99 /*-----------------------------------------------------------*/
\r
102 * The task that implements the command console processing.
\r
104 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
105 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
\r
107 /*-----------------------------------------------------------*/
\r
109 /* Const messages output by the command console. */
\r
110 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
111 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
112 static const char * const pcNewLine = "\r\n";
\r
114 SemaphoreHandle_t xTxMutex = NULL;
\r
115 static xComPortHandle xPort = 0;
\r
117 /*-----------------------------------------------------------*/
\r
119 void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )
\r
121 /* Create the semaphore used to access the UART Tx. */
\r
122 xTxMutex = xSemaphoreCreateMutex();
\r
123 configASSERT( xTxMutex );
\r
125 /* Create that task that handles the console itself. */
\r
126 xTaskCreate( prvUARTCommandConsoleTask, /* The task that implements the command console. */
\r
127 "CLI", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
128 usStackSize, /* The size of the stack allocated to the task. */
\r
129 NULL, /* The parameter is not used, so NULL is passed. */
\r
130 uxPriority, /* The priority allocated to the task. */
\r
131 NULL ); /* A handle is not required, so just pass NULL. */
\r
133 /*-----------------------------------------------------------*/
\r
135 static void prvUARTCommandConsoleTask( void *pvParameters )
\r
137 signed char cRxedChar;
\r
138 uint8_t ucInputIndex = 0;
\r
139 char *pcOutputString;
\r
140 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
141 BaseType_t xReturned;
\r
142 xComPortHandle xPort;
\r
144 ( void ) pvParameters;
\r
146 /* Obtain the address of the output buffer. Note there is no mutual
\r
147 exclusion on this buffer as it is assumed only one command console interface
\r
148 will be used at any one time. */
\r
149 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
151 /* Initialise the UART. */
\r
152 xPort = xSerialPortInitMinimal( configCLI_BAUD_RATE, cmdQUEUE_LENGTH );
\r
154 /* Send the welcome message. */
\r
155 vSerialPutString( xPort, ( signed char * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\r
159 /* Wait for the next character. The while loop is used in case
\r
160 INCLUDE_vTaskSuspend is not set to 1 - in which case portMAX_DELAY will
\r
161 be a genuine block time rather than an infinite block time. */
\r
162 while( xSerialGetChar( xPort, &cRxedChar, portMAX_DELAY ) != pdPASS );
\r
164 /* Ensure exclusive access to the UART Tx. */
\r
165 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
167 /* Echo the character back. */
\r
168 xSerialPutChar( xPort, cRxedChar, portMAX_DELAY );
\r
170 /* Was it the end of the line? */
\r
171 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
173 /* Just to space the output from the input. */
\r
174 vSerialPutString( xPort, ( signed char * ) pcNewLine, strlen( pcNewLine ) );
\r
176 /* See if the command is empty, indicating that the last command
\r
177 is to be executed again. */
\r
178 if( ucInputIndex == 0 )
\r
180 /* Copy the last command back into the input string. */
\r
181 strcpy( cInputString, cLastInputString );
\r
184 /* Pass the received command to the command interpreter. The
\r
185 command interpreter is called repeatedly until it returns
\r
186 pdFALSE (indicating there is no more output) as it might
\r
187 generate more than one string. */
\r
190 /* Get the next output string from the command interpreter. */
\r
191 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
193 /* Write the generated string to the UART. */
\r
194 vSerialPutString( xPort, ( signed char * ) pcOutputString, strlen( pcOutputString ) );
\r
196 } while( xReturned != pdFALSE );
\r
198 /* All the strings generated by the input command have been
\r
199 sent. Clear the input string ready to receive the next command.
\r
200 Remember the command that was just processed first in case it is
\r
201 to be processed again. */
\r
202 strcpy( cLastInputString, cInputString );
\r
204 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
206 vSerialPutString( xPort, ( signed char * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
210 if( cRxedChar == '\r' )
\r
212 /* Ignore the character. */
\r
214 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )
\r
216 /* Backspace was pressed. Erase the last character in the
\r
217 string - if any. */
\r
218 if( ucInputIndex > 0 )
\r
221 cInputString[ ucInputIndex ] = '\0';
\r
226 /* A character was entered. Add it to the string entered so
\r
227 far. When a \n is entered the complete string will be
\r
228 passed to the command interpreter. */
\r
229 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
231 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
233 cInputString[ ucInputIndex ] = cRxedChar;
\r
240 /* Must ensure to give the mutex back. */
\r
241 xSemaphoreGive( xTxMutex );
\r
245 /*-----------------------------------------------------------*/
\r
247 void vOutputString( const char * const pcMessage )
\r
249 if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
251 vSerialPutString( xPort, ( signed char * ) pcMessage, strlen( pcMessage ) );
\r
252 xSemaphoreGive( xTxMutex );
\r
255 /*-----------------------------------------------------------*/
\r