2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * NOTE: This file uses a third party USB CDC driver.
\r
33 /* Standard includes. */
\r
37 /* FreeRTOS includes. */
\r
38 #include "FreeRTOS.h"
\r
42 /* Driver includes. */
\r
44 #include "cdcuser.h"
\r
46 #include "usbuser.h"
\r
48 /* Example includes. */
\r
49 #include "FreeRTOS_CLI.h"
\r
50 #include "CDCCommandConsole.h"
\r
52 /* Dimensions the buffer into which input characters are placed. */
\r
53 #define cmdMAX_INPUT_SIZE 50
\r
55 /* The maximum time in ticks to wait for the CDC access mutex. */
\r
56 #define cmdMAX_MUTEX_WAIT ( 200 / portTICK_RATE_MS )
\r
58 /*-----------------------------------------------------------*/
\r
61 * The task that implements the command console processing.
\r
63 static void prvCDCCommandConsoleTask( void *pvParameters );
\r
66 * Obtain a character from the CDC input. The calling task will be held in the
\r
67 * Blocked state (so other tasks can execute) until a character is avilable.
\r
69 char cGetCDCChar( void );
\r
72 * Initialise the third party virtual comport files driver
\r
74 static void prvSetupUSBDrivers( void );
\r
76 /*-----------------------------------------------------------*/
\r
78 /* 'Given' by the CDC interrupt to unblock the receiving task when new data
\r
80 static xSemaphoreHandle xNewDataSemaphore = NULL;
\r
82 /* Used to guard access to the CDC output, which is used by more than one
\r
84 static xSemaphoreHandle xCDCMutex = NULL;
\r
86 /* Const messages output by the command console. */
\r
87 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
88 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
89 static const char * const pcNewLine = "\r\n";
\r
91 /*-----------------------------------------------------------*/
\r
93 void vCDCCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )
\r
95 /* Create the semaphores and mutexes used by the CDC to task interface. */
\r
96 xCDCMutex = xSemaphoreCreateMutex();
\r
97 vSemaphoreCreateBinary( xNewDataSemaphore );
\r
98 configASSERT( xCDCMutex );
\r
99 configASSERT( xNewDataSemaphore );
\r
101 /* Add the semaphore and mutex to the queue registry for viewing in the
\r
102 kernel aware state viewer. */
\r
103 vQueueAddToRegistry( xCDCMutex, "CDCMu" );
\r
104 vQueueAddToRegistry( xNewDataSemaphore, "CDCDat" );
\r
106 /* Create that task that handles the console itself. */
\r
107 xTaskCreate( prvCDCCommandConsoleTask, /* The task that implements the command console. */
\r
108 "CDCCmd", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
109 usStackSize, /* The size of the stack allocated to the task. */
\r
110 NULL, /* The parameter is not used, so NULL is passed. */
\r
111 uxPriority, /* The priority allocated to the task. */
\r
112 NULL ); /* A handle is not required, so just pass NULL. */
\r
114 /*-----------------------------------------------------------*/
\r
116 static void prvCDCCommandConsoleTask( void *pvParameters )
\r
119 uint8_t ucInputIndex = 0;
\r
120 char *pcOutputString;
\r
121 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
122 BaseType_t xReturned;
\r
124 ( void ) pvParameters;
\r
126 /* Obtain the address of the output buffer. Note there is no mutual
\r
127 exclusion on this buffer as it is assumed only one command console
\r
128 interface will be used at any one time. */
\r
129 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
131 /* Initialise the virtual com port (CDC) interface. */
\r
132 prvSetupUSBDrivers();
\r
134 /* Send the welcome message. This probably won't be seen as the console
\r
135 will not have been connected yet. */
\r
136 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\r
140 /* No characters received yet for the current input string. */
\r
143 /* Only interested in reading one character at a time. */
\r
144 cRxedChar = cGetCDCChar();
\r
146 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
148 /* Echo the character back. */
\r
149 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );
\r
151 /* Was it the end of the line? */
\r
152 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
154 /* Just to space the output from the input. */
\r
155 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( pcNewLine ) );
\r
157 /* See if the command is empty, indicating that the last command is
\r
158 to be executed again. */
\r
159 if( ucInputIndex == 0 )
\r
161 /* Copy the last command back into the input string. */
\r
162 strcpy( cInputString, cLastInputString );
\r
165 /* Pass the received command to the command interpreter. The
\r
166 command interpreter is called repeatedly until it returns pdFALSE
\r
167 (indicating there is no more output) as it might generate more than
\r
171 /* Get the next output string from the command interpreter. */
\r
172 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
174 /* Write the generated string to the CDC. */
\r
175 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) );
\r
178 } while( xReturned != pdFALSE );
\r
180 /* All the strings generated by the input command have been sent.
\r
181 Clear the input string ready to receive the next command. Remember
\r
182 the command that was just processed first in case it is to be
\r
183 processed again. */
\r
184 strcpy( cLastInputString, cInputString );
\r
186 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
188 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
192 if( cRxedChar == '\r' )
\r
194 /* Ignore the character. */
\r
196 else if( cRxedChar == '\b' )
\r
198 /* Backspace was pressed. Erase the last character in the
\r
199 string - if any. */
\r
200 if( ucInputIndex > 0 )
\r
203 cInputString[ ucInputIndex ] = '\0';
\r
208 /* A character was entered. Add it to the string
\r
209 entered so far. When a \n is entered the complete
\r
210 string will be passed to the command interpreter. */
\r
211 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
213 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
215 cInputString[ ucInputIndex ] = cRxedChar;
\r
222 /* Must ensure to give the mutex back. */
\r
223 xSemaphoreGive( xCDCMutex );
\r
227 /*-----------------------------------------------------------*/
\r
229 void vOutputString( const char * const pcMessage )
\r
231 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
233 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcMessage, strlen( pcMessage ) );
\r
234 xSemaphoreGive( xCDCMutex );
\r
237 /*-----------------------------------------------------------*/
\r
239 char cGetCDCChar( void )
\r
241 int32_t lAvailableBytes, xBytes = 0;
\r
246 /* Are there any characters already available? */
\r
247 CDC_OutBufAvailChar( &lAvailableBytes );
\r
248 if( lAvailableBytes > 0 )
\r
250 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
252 /* Attempt to read one character. */
\r
254 xBytes = CDC_RdOutBuf( &cInputChar, &xBytes );
\r
256 xSemaphoreGive( xCDCMutex );
\r
262 /* A character was not available. Wait until signalled by the
\r
263 CDC Rx callback function that new data has arrived. */
\r
264 xSemaphoreTake( xNewDataSemaphore, portMAX_DELAY );
\r
267 } while( xBytes == 0 );
\r
271 /*-----------------------------------------------------------*/
\r
273 /* Callback function executed by the USB interrupt when new data arrives. */
\r
274 void vCDCNewDataNotify( void )
\r
276 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
278 configASSERT( xNewDataSemaphore );
\r
280 /* 'Give' the semaphore that signals the arrival of new data to the command
\r
282 xSemaphoreGiveFromISR( xNewDataSemaphore, &xHigherPriorityTaskWoken );
\r
283 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
285 /*-----------------------------------------------------------*/
\r
287 static void prvSetupUSBDrivers( void )
\r
289 LPC_USBDRV_INIT_T xUSBCallback;
\r
291 /* Initialise the callback structure. */
\r
292 memset( ( void * ) &xUSBCallback, 0, sizeof( LPC_USBDRV_INIT_T ) );
\r
293 xUSBCallback.USB_Reset_Event = USB_Reset_Event;
\r
294 xUSBCallback.USB_P_EP[ 0 ] = USB_EndPoint0;
\r
295 xUSBCallback.USB_P_EP[ 1 ] = USB_EndPoint1;
\r
296 xUSBCallback.USB_P_EP[ 2 ] = USB_EndPoint2;
\r
297 xUSBCallback.ep0_maxp = USB_MAX_PACKET0;
\r
299 /* Initialise then connect the USB. */
\r
300 USB_Init( &xUSBCallback );
\r
301 USB_Connect( pdTRUE );
\r