2 FreeRTOS V7.1.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * NOTE: This file uses a third party USB CDC driver.
\r
58 /* Standard includes. */
\r
62 /* FreeRTOS includes. */
\r
63 #include "FreeRTOS.h"
\r
67 /* Driver includes. */
\r
69 #include "cdcuser.h"
\r
71 #include "usbuser.h"
\r
73 /* Example includes. */
\r
74 #include "FreeRTOS_CLI.h"
\r
75 #include "CDCCommandConsole.h"
\r
77 /* Dimensions the buffer into which input characters are placed. */
\r
78 #define cmdMAX_INPUT_SIZE 50
\r
80 /* The maximum time in ticks to wait for the CDC access mutex. */
\r
81 #define cmdMAX_MUTEX_WAIT ( 200 / portTICK_RATE_MS )
\r
83 /*-----------------------------------------------------------*/
\r
86 * The task that implements the command console processing.
\r
88 static void prvCDCCommandConsoleTask( void *pvParameters );
\r
91 * Obtain a character from the CDC input. The calling task will be held in the
\r
92 * Blocked state (so other tasks can execute) until a character is avilable.
\r
94 char cGetCDCChar( void );
\r
97 * Initialise the third party virtual comport files driver
\r
99 static void prvSetupUSBDrivers( void );
\r
101 /*-----------------------------------------------------------*/
\r
103 /* 'Given' by the CDC interrupt to unblock the receiving task when new data
\r
105 static xSemaphoreHandle xNewDataSemaphore = NULL;
\r
107 /* Used to guard access to the CDC output, which is used by more than one
\r
109 static xSemaphoreHandle xCDCMutex = NULL;
\r
111 /* Const messages output by the command console. */
\r
112 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
113 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
114 static const char * const pcNewLine = "\r\n";
\r
116 /*-----------------------------------------------------------*/
\r
118 void vCDCCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )
\r
120 /* Create the semaphores and mutexes used by the CDC to task interface. */
\r
121 xCDCMutex = xSemaphoreCreateMutex();
\r
122 vSemaphoreCreateBinary( xNewDataSemaphore );
\r
123 configASSERT( xCDCMutex );
\r
124 configASSERT( xNewDataSemaphore );
\r
126 /* Add the semaphore and mutex to the queue registry for viewing in the
\r
127 kernel aware state viewer. */
\r
128 vQueueAddToRegistry( xCDCMutex, "CDCMu" );
\r
129 vQueueAddToRegistry( xNewDataSemaphore, "CDCDat" );
\r
131 /* Create that task that handles the console itself. */
\r
132 xTaskCreate( prvCDCCommandConsoleTask, /* The task that implements the command console. */
\r
133 "CDCCmd", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
134 usStackSize, /* The size of the stack allocated to the task. */
\r
135 NULL, /* The parameter is not used, so NULL is passed. */
\r
136 uxPriority, /* The priority allocated to the task. */
\r
137 NULL ); /* A handle is not required, so just pass NULL. */
\r
139 /*-----------------------------------------------------------*/
\r
141 static void prvCDCCommandConsoleTask( void *pvParameters )
\r
144 uint8_t ucInputIndex = 0;
\r
145 char *pcOutputString;
\r
146 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
147 portBASE_TYPE xReturned;
\r
149 ( void ) pvParameters;
\r
151 /* Obtain the address of the output buffer. Note there is no mutual
\r
152 exclusion on this buffer as it is assumed only one command console
\r
153 interface will be used at any one time. */
\r
154 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
156 /* Initialise the virtual com port (CDC) interface. */
\r
157 prvSetupUSBDrivers();
\r
159 /* Send the welcome message. This probably won't be seen as the console
\r
160 will not have been connected yet. */
\r
161 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\r
165 /* No characters received yet for the current input string. */
\r
168 /* Only interested in reading one character at a time. */
\r
169 cRxedChar = cGetCDCChar();
\r
171 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
173 /* Echo the character back. */
\r
174 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );
\r
176 /* Was it the end of the line? */
\r
177 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
179 /* Just to space the output from the input. */
\r
180 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( pcNewLine ) );
\r
182 /* See if the command is empty, indicating that the last command is
\r
183 to be executed again. */
\r
184 if( ucInputIndex == 0 )
\r
186 /* Copy the last command back into the input string. */
\r
187 strcpy( cInputString, cLastInputString );
\r
190 /* Pass the received command to the command interpreter. The
\r
191 command interpreter is called repeatedly until it returns pdFALSE
\r
192 (indicating there is no more output) as it might generate more than
\r
196 /* Get the next output string from the command interpreter. */
\r
197 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
199 /* Write the generated string to the CDC. */
\r
200 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) );
\r
203 } while( xReturned != pdFALSE );
\r
205 /* All the strings generated by the input command have been sent.
\r
206 Clear the input string ready to receive the next command. Remember
\r
207 the command that was just processed first in case it is to be
\r
208 processed again. */
\r
209 strcpy( cLastInputString, cInputString );
\r
211 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
213 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
217 if( cRxedChar == '\r' )
\r
219 /* Ignore the character. */
\r
221 else if( cRxedChar == '\b' )
\r
223 /* Backspace was pressed. Erase the last character in the
\r
224 string - if any. */
\r
225 if( ucInputIndex > 0 )
\r
228 cInputString[ ucInputIndex ] = '\0';
\r
233 /* A character was entered. Add it to the string
\r
234 entered so far. When a \n is entered the complete
\r
235 string will be passed to the command interpreter. */
\r
236 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
238 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
240 cInputString[ ucInputIndex ] = cRxedChar;
\r
247 /* Must ensure to give the mutex back. */
\r
248 xSemaphoreGive( xCDCMutex );
\r
252 /*-----------------------------------------------------------*/
\r
254 void vOutputString( const char * const pcMessage )
\r
256 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
258 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcMessage, strlen( pcMessage ) );
\r
259 xSemaphoreGive( xCDCMutex );
\r
262 /*-----------------------------------------------------------*/
\r
264 char cGetCDCChar( void )
\r
266 int32_t lAvailableBytes, xBytes = 0;
\r
271 /* Are there any characters already available? */
\r
272 CDC_OutBufAvailChar( &lAvailableBytes );
\r
273 if( lAvailableBytes > 0 )
\r
275 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
277 /* Attempt to read one character. */
\r
279 xBytes = CDC_RdOutBuf( &cInputChar, &xBytes );
\r
281 xSemaphoreGive( xCDCMutex );
\r
287 /* A character was not available. Wait until signalled by the
\r
288 CDC Rx callback function that new data has arrived. */
\r
289 xSemaphoreTake( xNewDataSemaphore, portMAX_DELAY );
\r
292 } while( xBytes == 0 );
\r
296 /*-----------------------------------------------------------*/
\r
298 /* Callback function executed by the USB interrupt when new data arrives. */
\r
299 void vCDCNewDataNotify( void )
\r
301 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
303 configASSERT( xNewDataSemaphore );
\r
305 /* 'Give' the semaphore that signals the arrival of new data to the command
\r
307 xSemaphoreGiveFromISR( xNewDataSemaphore, &xHigherPriorityTaskWoken );
\r
308 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvSetupUSBDrivers( void )
\r
314 LPC_USBDRV_INIT_T xUSBCallback;
\r
316 /* Initialise the callback structure. */
\r
317 memset( ( void * ) &xUSBCallback, 0, sizeof( LPC_USBDRV_INIT_T ) );
\r
318 xUSBCallback.USB_Reset_Event = USB_Reset_Event;
\r
319 xUSBCallback.USB_P_EP[ 0 ] = USB_EndPoint0;
\r
320 xUSBCallback.USB_P_EP[ 1 ] = USB_EndPoint1;
\r
321 xUSBCallback.USB_P_EP[ 2 ] = USB_EndPoint2;
\r
322 xUSBCallback.ep0_maxp = USB_MAX_PACKET0;
\r
324 /* Initialise then connect the USB. */
\r
325 USB_Init( &xUSBCallback );
\r
326 USB_Connect( pdTRUE );
\r