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 int8_t 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 uint8_t * const pcWelcomeMessage = ( uint8_t * ) "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
113 static const uint8_t * const pcEndOfOutputMessage = ( uint8_t * ) "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
114 static const uint8_t * const pcNewLine = ( uint8_t * ) "\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, ( signed char * ) "CDCMu" );
\r
129 vQueueAddToRegistry( xNewDataSemaphore, ( signed char * ) "CDCDat" );
\r
131 /* Create that task that handles the console itself. */
\r
132 xTaskCreate( prvCDCCommandConsoleTask, /* The task that implements the command console. */
\r
133 ( const int8_t * const ) "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
143 int8_t cRxedChar, cInputIndex = 0, *pcOutputString;
\r
144 static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
145 portBASE_TYPE xReturned;
\r
147 ( void ) pvParameters;
\r
149 /* Obtain the address of the output buffer. Note there is no mutual
\r
150 exclusion on this buffer as it is assumed only one command console
\r
151 interface will be used at any one time. */
\r
152 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
154 /* Initialise the virtual com port (CDC) interface. */
\r
155 prvSetupUSBDrivers();
\r
157 /* Send the welcome message. This probably won't be seen as the console
\r
158 will not have been connected yet. */
\r
159 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ) );
\r
163 /* No characters received yet for the current input string. */
\r
166 /* Only interested in reading one character at a time. */
\r
167 cRxedChar = cGetCDCChar();
\r
169 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
171 /* Echo the character back. */
\r
172 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );
\r
174 /* Was it the end of the line? */
\r
175 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
177 /* Just to space the output from the input. */
\r
178 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( ( const char * ) pcNewLine ) );
\r
180 /* See if the command is empty, indicating that the last command is
\r
181 to be executed again. */
\r
182 if( cInputIndex == 0 )
\r
184 /* Copy the last command back into the input string. */
\r
185 strcpy( ( char * ) cInputString, ( char * ) cLastInputString );
\r
188 /* Pass the received command to the command interpreter. The
\r
189 command interpreter is called repeatedly until it returns pdFALSE
\r
190 (indicating there is no more output) as it might generate more than
\r
194 /* Get the next output string from the command interpreter. */
\r
195 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
197 /* Write the generated string to the CDC. */
\r
198 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( ( const char * ) pcOutputString ) );
\r
201 } while( xReturned != pdFALSE );
\r
203 /* All the strings generated by the input command have been sent.
\r
204 Clear the input string ready to receive the next command. Remember
\r
205 the command that was just processed first in case it is to be
\r
206 processed again. */
\r
207 strcpy( ( char * ) cLastInputString, ( char * ) cInputString );
\r
209 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
211 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( ( const char * ) pcEndOfOutputMessage ) );
\r
215 if( cRxedChar == '\r' )
\r
217 /* Ignore the character. */
\r
219 else if( cRxedChar == '\b' )
\r
221 /* Backspace was pressed. Erase the last character in the
\r
222 string - if any. */
\r
223 if( cInputIndex > 0 )
\r
226 cInputString[ cInputIndex ] = '\0';
\r
231 /* A character was entered. Add it to the string
\r
232 entered so far. When a \n is entered the complete
\r
233 string will be passed to the command interpreter. */
\r
234 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
236 if( cInputIndex < cmdMAX_INPUT_SIZE )
\r
238 cInputString[ cInputIndex ] = cRxedChar;
\r
245 /* Must ensure to give the mutex back. */
\r
246 xSemaphoreGive( xCDCMutex );
\r
250 /*-----------------------------------------------------------*/
\r
252 void vOutputString( const uint8_t * const pucMessage )
\r
254 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
256 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pucMessage, strlen( ( const char * ) pucMessage ) );
\r
257 xSemaphoreGive( xCDCMutex );
\r
260 /*-----------------------------------------------------------*/
\r
262 int8_t cGetCDCChar( void )
\r
264 int32_t lAvailableBytes, xBytes = 0;
\r
269 /* Are there any characters already available? */
\r
270 CDC_OutBufAvailChar( &lAvailableBytes );
\r
271 if( lAvailableBytes > 0 )
\r
273 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
275 /* Attempt to read one character. */
\r
277 xBytes = CDC_RdOutBuf( ( char * ) &cInputChar, &xBytes );
\r
279 xSemaphoreGive( xCDCMutex );
\r
285 /* A character was not available. Wait until signalled by the
\r
286 CDC Rx callback function that new data has arrived. */
\r
287 xSemaphoreTake( xNewDataSemaphore, portMAX_DELAY );
\r
290 } while( xBytes == 0 );
\r
294 /*-----------------------------------------------------------*/
\r
296 /* Callback function executed by the USB interrupt when new data arrives. */
\r
297 void vCDCNewDataNotify( void )
\r
299 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
301 configASSERT( xNewDataSemaphore );
\r
303 /* 'Give' the semaphore that signals the arrival of new data to the command
\r
305 xSemaphoreGiveFromISR( xNewDataSemaphore, &xHigherPriorityTaskWoken );
\r
306 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
308 /*-----------------------------------------------------------*/
\r
310 static void prvSetupUSBDrivers( void )
\r
312 LPC_USBDRV_INIT_T xUSBCallback;
\r
314 /* Initialise the callback structure. */
\r
315 memset( ( void * ) &xUSBCallback, 0, sizeof( LPC_USBDRV_INIT_T ) );
\r
316 xUSBCallback.USB_Reset_Event = USB_Reset_Event;
\r
317 xUSBCallback.USB_P_EP[ 0 ] = USB_EndPoint0;
\r
318 xUSBCallback.USB_P_EP[ 1 ] = USB_EndPoint1;
\r
319 xUSBCallback.USB_P_EP[ 2 ] = USB_EndPoint2;
\r
320 xUSBCallback.ep0_maxp = USB_MAX_PACKET0;
\r
322 /* Initialise then connect the USB. */
\r
323 USB_Init( &xUSBCallback );
\r
324 USB_Connect( pdTRUE );
\r