2 FreeRTOS V9.0.0rc2 - 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 /* Driver includes. */
\r
85 #include "cdcuser.h"
\r
87 #include "usbuser.h"
\r
89 /* Example includes. */
\r
90 #include "FreeRTOS_CLI.h"
\r
91 #include "CDCCommandConsole.h"
\r
93 /* Dimensions the buffer into which input characters are placed. */
\r
94 #define cmdMAX_INPUT_SIZE 50
\r
96 /* The maximum time in ticks to wait for the CDC access mutex. */
\r
97 #define cmdMAX_MUTEX_WAIT ( 200 / portTICK_RATE_MS )
\r
99 /*-----------------------------------------------------------*/
\r
102 * The task that implements the command console processing.
\r
104 static void prvCDCCommandConsoleTask( void *pvParameters );
\r
107 * Obtain a character from the CDC input. The calling task will be held in the
\r
108 * Blocked state (so other tasks can execute) until a character is avilable.
\r
110 char cGetCDCChar( void );
\r
113 * Initialise the third party virtual comport files driver
\r
115 static void prvSetupUSBDrivers( void );
\r
117 /*-----------------------------------------------------------*/
\r
119 /* 'Given' by the CDC interrupt to unblock the receiving task when new data
\r
121 static xSemaphoreHandle xNewDataSemaphore = NULL;
\r
123 /* Used to guard access to the CDC output, which is used by more than one
\r
125 static xSemaphoreHandle xCDCMutex = NULL;
\r
127 /* Const messages output by the command console. */
\r
128 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
129 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
130 static const char * const pcNewLine = "\r\n";
\r
132 /*-----------------------------------------------------------*/
\r
134 void vCDCCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )
\r
136 /* Create the semaphores and mutexes used by the CDC to task interface. */
\r
137 xCDCMutex = xSemaphoreCreateMutex();
\r
138 vSemaphoreCreateBinary( xNewDataSemaphore );
\r
139 configASSERT( xCDCMutex );
\r
140 configASSERT( xNewDataSemaphore );
\r
142 /* Add the semaphore and mutex to the queue registry for viewing in the
\r
143 kernel aware state viewer. */
\r
144 vQueueAddToRegistry( xCDCMutex, "CDCMu" );
\r
145 vQueueAddToRegistry( xNewDataSemaphore, "CDCDat" );
\r
147 /* Create that task that handles the console itself. */
\r
148 xTaskCreate( prvCDCCommandConsoleTask, /* The task that implements the command console. */
\r
149 "CDCCmd", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
150 usStackSize, /* The size of the stack allocated to the task. */
\r
151 NULL, /* The parameter is not used, so NULL is passed. */
\r
152 uxPriority, /* The priority allocated to the task. */
\r
153 NULL ); /* A handle is not required, so just pass NULL. */
\r
155 /*-----------------------------------------------------------*/
\r
157 static void prvCDCCommandConsoleTask( void *pvParameters )
\r
160 uint8_t ucInputIndex = 0;
\r
161 char *pcOutputString;
\r
162 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
163 BaseType_t xReturned;
\r
165 ( void ) pvParameters;
\r
167 /* Obtain the address of the output buffer. Note there is no mutual
\r
168 exclusion on this buffer as it is assumed only one command console
\r
169 interface will be used at any one time. */
\r
170 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
172 /* Initialise the virtual com port (CDC) interface. */
\r
173 prvSetupUSBDrivers();
\r
175 /* Send the welcome message. This probably won't be seen as the console
\r
176 will not have been connected yet. */
\r
177 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\r
181 /* No characters received yet for the current input string. */
\r
184 /* Only interested in reading one character at a time. */
\r
185 cRxedChar = cGetCDCChar();
\r
187 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
189 /* Echo the character back. */
\r
190 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );
\r
192 /* Was it the end of the line? */
\r
193 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
195 /* Just to space the output from the input. */
\r
196 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( pcNewLine ) );
\r
198 /* See if the command is empty, indicating that the last command is
\r
199 to be executed again. */
\r
200 if( ucInputIndex == 0 )
\r
202 /* Copy the last command back into the input string. */
\r
203 strcpy( cInputString, cLastInputString );
\r
206 /* Pass the received command to the command interpreter. The
\r
207 command interpreter is called repeatedly until it returns pdFALSE
\r
208 (indicating there is no more output) as it might generate more than
\r
212 /* Get the next output string from the command interpreter. */
\r
213 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
215 /* Write the generated string to the CDC. */
\r
216 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) );
\r
219 } while( xReturned != pdFALSE );
\r
221 /* All the strings generated by the input command have been sent.
\r
222 Clear the input string ready to receive the next command. Remember
\r
223 the command that was just processed first in case it is to be
\r
224 processed again. */
\r
225 strcpy( cLastInputString, cInputString );
\r
227 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
229 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
233 if( cRxedChar == '\r' )
\r
235 /* Ignore the character. */
\r
237 else if( cRxedChar == '\b' )
\r
239 /* Backspace was pressed. Erase the last character in the
\r
240 string - if any. */
\r
241 if( ucInputIndex > 0 )
\r
244 cInputString[ ucInputIndex ] = '\0';
\r
249 /* A character was entered. Add it to the string
\r
250 entered so far. When a \n is entered the complete
\r
251 string will be passed to the command interpreter. */
\r
252 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
254 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
256 cInputString[ ucInputIndex ] = cRxedChar;
\r
263 /* Must ensure to give the mutex back. */
\r
264 xSemaphoreGive( xCDCMutex );
\r
268 /*-----------------------------------------------------------*/
\r
270 void vOutputString( const char * const pcMessage )
\r
272 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
274 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcMessage, strlen( pcMessage ) );
\r
275 xSemaphoreGive( xCDCMutex );
\r
278 /*-----------------------------------------------------------*/
\r
280 char cGetCDCChar( void )
\r
282 int32_t lAvailableBytes, xBytes = 0;
\r
287 /* Are there any characters already available? */
\r
288 CDC_OutBufAvailChar( &lAvailableBytes );
\r
289 if( lAvailableBytes > 0 )
\r
291 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
293 /* Attempt to read one character. */
\r
295 xBytes = CDC_RdOutBuf( &cInputChar, &xBytes );
\r
297 xSemaphoreGive( xCDCMutex );
\r
303 /* A character was not available. Wait until signalled by the
\r
304 CDC Rx callback function that new data has arrived. */
\r
305 xSemaphoreTake( xNewDataSemaphore, portMAX_DELAY );
\r
308 } while( xBytes == 0 );
\r
312 /*-----------------------------------------------------------*/
\r
314 /* Callback function executed by the USB interrupt when new data arrives. */
\r
315 void vCDCNewDataNotify( void )
\r
317 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
319 configASSERT( xNewDataSemaphore );
\r
321 /* 'Give' the semaphore that signals the arrival of new data to the command
\r
323 xSemaphoreGiveFromISR( xNewDataSemaphore, &xHigherPriorityTaskWoken );
\r
324 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
326 /*-----------------------------------------------------------*/
\r
328 static void prvSetupUSBDrivers( void )
\r
330 LPC_USBDRV_INIT_T xUSBCallback;
\r
332 /* Initialise the callback structure. */
\r
333 memset( ( void * ) &xUSBCallback, 0, sizeof( LPC_USBDRV_INIT_T ) );
\r
334 xUSBCallback.USB_Reset_Event = USB_Reset_Event;
\r
335 xUSBCallback.USB_P_EP[ 0 ] = USB_EndPoint0;
\r
336 xUSBCallback.USB_P_EP[ 1 ] = USB_EndPoint1;
\r
337 xUSBCallback.USB_P_EP[ 2 ] = USB_EndPoint2;
\r
338 xUSBCallback.ep0_maxp = USB_MAX_PACKET0;
\r
340 /* Initialise then connect the USB. */
\r
341 USB_Init( &xUSBCallback );
\r
342 USB_Connect( pdTRUE );
\r