2 * FreeRTOS Kernel V10.0.1
\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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * NOTE: This file uses a third party USB CDC driver.
\r
32 /* Standard includes. */
\r
36 /* FreeRTOS includes. */
\r
37 #include "FreeRTOS.h"
\r
41 /* Driver includes. */
\r
43 #include "cdcuser.h"
\r
45 #include "usbuser.h"
\r
47 /* Example includes. */
\r
48 #include "FreeRTOS_CLI.h"
\r
49 #include "CDCCommandConsole.h"
\r
51 /* Dimensions the buffer into which input characters are placed. */
\r
52 #define cmdMAX_INPUT_SIZE 50
\r
54 /* The maximum time in ticks to wait for the CDC access mutex. */
\r
55 #define cmdMAX_MUTEX_WAIT ( 200 / portTICK_RATE_MS )
\r
57 /*-----------------------------------------------------------*/
\r
60 * The task that implements the command console processing.
\r
62 static void prvCDCCommandConsoleTask( void *pvParameters );
\r
65 * Obtain a character from the CDC input. The calling task will be held in the
\r
66 * Blocked state (so other tasks can execute) until a character is avilable.
\r
68 char cGetCDCChar( void );
\r
71 * Initialise the third party virtual comport files driver
\r
73 static void prvSetupUSBDrivers( void );
\r
75 /*-----------------------------------------------------------*/
\r
77 /* 'Given' by the CDC interrupt to unblock the receiving task when new data
\r
79 static xSemaphoreHandle xNewDataSemaphore = NULL;
\r
81 /* Used to guard access to the CDC output, which is used by more than one
\r
83 static xSemaphoreHandle xCDCMutex = NULL;
\r
85 /* Const messages output by the command console. */
\r
86 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
87 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
88 static const char * const pcNewLine = "\r\n";
\r
90 /*-----------------------------------------------------------*/
\r
92 void vCDCCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )
\r
94 /* Create the semaphores and mutexes used by the CDC to task interface. */
\r
95 xCDCMutex = xSemaphoreCreateMutex();
\r
96 vSemaphoreCreateBinary( xNewDataSemaphore );
\r
97 configASSERT( xCDCMutex );
\r
98 configASSERT( xNewDataSemaphore );
\r
100 /* Add the semaphore and mutex to the queue registry for viewing in the
\r
101 kernel aware state viewer. */
\r
102 vQueueAddToRegistry( xCDCMutex, "CDCMu" );
\r
103 vQueueAddToRegistry( xNewDataSemaphore, "CDCDat" );
\r
105 /* Create that task that handles the console itself. */
\r
106 xTaskCreate( prvCDCCommandConsoleTask, /* The task that implements the command console. */
\r
107 "CDCCmd", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
108 usStackSize, /* The size of the stack allocated to the task. */
\r
109 NULL, /* The parameter is not used, so NULL is passed. */
\r
110 uxPriority, /* The priority allocated to the task. */
\r
111 NULL ); /* A handle is not required, so just pass NULL. */
\r
113 /*-----------------------------------------------------------*/
\r
115 static void prvCDCCommandConsoleTask( void *pvParameters )
\r
118 uint8_t ucInputIndex = 0;
\r
119 char *pcOutputString;
\r
120 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
121 BaseType_t xReturned;
\r
123 ( void ) pvParameters;
\r
125 /* Obtain the address of the output buffer. Note there is no mutual
\r
126 exclusion on this buffer as it is assumed only one command console
\r
127 interface will be used at any one time. */
\r
128 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
130 /* Initialise the virtual com port (CDC) interface. */
\r
131 prvSetupUSBDrivers();
\r
133 /* Send the welcome message. This probably won't be seen as the console
\r
134 will not have been connected yet. */
\r
135 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\r
139 /* No characters received yet for the current input string. */
\r
142 /* Only interested in reading one character at a time. */
\r
143 cRxedChar = cGetCDCChar();
\r
145 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
147 /* Echo the character back. */
\r
148 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );
\r
150 /* Was it the end of the line? */
\r
151 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
153 /* Just to space the output from the input. */
\r
154 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( pcNewLine ) );
\r
156 /* See if the command is empty, indicating that the last command is
\r
157 to be executed again. */
\r
158 if( ucInputIndex == 0 )
\r
160 /* Copy the last command back into the input string. */
\r
161 strcpy( cInputString, cLastInputString );
\r
164 /* Pass the received command to the command interpreter. The
\r
165 command interpreter is called repeatedly until it returns pdFALSE
\r
166 (indicating there is no more output) as it might generate more than
\r
170 /* Get the next output string from the command interpreter. */
\r
171 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
173 /* Write the generated string to the CDC. */
\r
174 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) );
\r
177 } while( xReturned != pdFALSE );
\r
179 /* All the strings generated by the input command have been sent.
\r
180 Clear the input string ready to receive the next command. Remember
\r
181 the command that was just processed first in case it is to be
\r
182 processed again. */
\r
183 strcpy( cLastInputString, cInputString );
\r
185 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
187 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
191 if( cRxedChar == '\r' )
\r
193 /* Ignore the character. */
\r
195 else if( cRxedChar == '\b' )
\r
197 /* Backspace was pressed. Erase the last character in the
\r
198 string - if any. */
\r
199 if( ucInputIndex > 0 )
\r
202 cInputString[ ucInputIndex ] = '\0';
\r
207 /* A character was entered. Add it to the string
\r
208 entered so far. When a \n is entered the complete
\r
209 string will be passed to the command interpreter. */
\r
210 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
212 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
214 cInputString[ ucInputIndex ] = cRxedChar;
\r
221 /* Must ensure to give the mutex back. */
\r
222 xSemaphoreGive( xCDCMutex );
\r
226 /*-----------------------------------------------------------*/
\r
228 void vOutputString( const char * const pcMessage )
\r
230 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
232 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcMessage, strlen( pcMessage ) );
\r
233 xSemaphoreGive( xCDCMutex );
\r
236 /*-----------------------------------------------------------*/
\r
238 char cGetCDCChar( void )
\r
240 int32_t lAvailableBytes, xBytes = 0;
\r
245 /* Are there any characters already available? */
\r
246 CDC_OutBufAvailChar( &lAvailableBytes );
\r
247 if( lAvailableBytes > 0 )
\r
249 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
\r
251 /* Attempt to read one character. */
\r
253 xBytes = CDC_RdOutBuf( &cInputChar, &xBytes );
\r
255 xSemaphoreGive( xCDCMutex );
\r
261 /* A character was not available. Wait until signalled by the
\r
262 CDC Rx callback function that new data has arrived. */
\r
263 xSemaphoreTake( xNewDataSemaphore, portMAX_DELAY );
\r
266 } while( xBytes == 0 );
\r
270 /*-----------------------------------------------------------*/
\r
272 /* Callback function executed by the USB interrupt when new data arrives. */
\r
273 void vCDCNewDataNotify( void )
\r
275 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
277 configASSERT( xNewDataSemaphore );
\r
279 /* 'Give' the semaphore that signals the arrival of new data to the command
\r
281 xSemaphoreGiveFromISR( xNewDataSemaphore, &xHigherPriorityTaskWoken );
\r
282 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
284 /*-----------------------------------------------------------*/
\r
286 static void prvSetupUSBDrivers( void )
\r
288 LPC_USBDRV_INIT_T xUSBCallback;
\r
290 /* Initialise the callback structure. */
\r
291 memset( ( void * ) &xUSBCallback, 0, sizeof( LPC_USBDRV_INIT_T ) );
\r
292 xUSBCallback.USB_Reset_Event = USB_Reset_Event;
\r
293 xUSBCallback.USB_P_EP[ 0 ] = USB_EndPoint0;
\r
294 xUSBCallback.USB_P_EP[ 1 ] = USB_EndPoint1;
\r
295 xUSBCallback.USB_P_EP[ 2 ] = USB_EndPoint2;
\r
296 xUSBCallback.ep0_maxp = USB_MAX_PACKET0;
\r
298 /* Initialise then connect the USB. */
\r
299 USB_Init( &xUSBCallback );
\r
300 USB_Connect( pdTRUE );
\r