]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/Examples/USB_CDC/CDCCommandConsole.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / Examples / USB_CDC / CDCCommandConsole.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * NOTE:  This file uses a third party USB CDC driver.\r
30  */\r
31 \r
32 /* Standard includes. */\r
33 #include "string.h"\r
34 #include "stdio.h"\r
35 \r
36 /* FreeRTOS includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 #include "semphr.h"\r
40 \r
41 /* Driver includes. */\r
42 #include "usbhw.h"\r
43 #include "cdcuser.h"\r
44 #include "usbcfg.h"\r
45 #include "usbuser.h"\r
46 \r
47 /* Example includes. */\r
48 #include "FreeRTOS_CLI.h"\r
49 #include "CDCCommandConsole.h"\r
50 \r
51 /* Dimensions the buffer into which input characters are placed. */\r
52 #define cmdMAX_INPUT_SIZE               50\r
53 \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
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /*\r
60  * The task that implements the command console processing.\r
61  */\r
62 static void prvCDCCommandConsoleTask( void *pvParameters );\r
63 \r
64 /*\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
67  */\r
68 char cGetCDCChar( void );\r
69 \r
70 /*\r
71  * Initialise the third party virtual comport files driver\r
72  */\r
73 static void prvSetupUSBDrivers( void );\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /* 'Given' by the CDC interrupt to unblock the receiving task when new data\r
78 is available. */\r
79 static xSemaphoreHandle xNewDataSemaphore = NULL;\r
80 \r
81 /* Used to guard access to the CDC output, which is used by more than one\r
82 task. */\r
83 static xSemaphoreHandle xCDCMutex = NULL;\r
84 \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
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 void vCDCCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )\r
93 {\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
99 \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
104 \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
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 static void prvCDCCommandConsoleTask( void *pvParameters )\r
116 {\r
117 char cRxedChar;\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
122 \r
123         ( void ) pvParameters;\r
124 \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
129 \r
130         /* Initialise the virtual com port (CDC) interface. */\r
131         prvSetupUSBDrivers();\r
132 \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
136 \r
137         for( ;; )\r
138         {\r
139                 /* No characters received yet for the current input string. */\r
140                 cRxedChar = 0;\r
141 \r
142                 /* Only interested in reading one character at a time. */\r
143                 cRxedChar = cGetCDCChar();\r
144 \r
145                 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
146                 {\r
147                         /* Echo the character back. */\r
148                         USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );\r
149 \r
150                         /* Was it the end of the line? */\r
151                         if( cRxedChar == '\n' || cRxedChar == '\r' )\r
152                         {\r
153                                 /* Just to space the output from the input. */\r
154                                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( pcNewLine ) );\r
155 \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
159                                 {\r
160                                         /* Copy the last command back into the input string. */\r
161                                         strcpy( cInputString, cLastInputString );\r
162                                 }\r
163 \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
167                                 one string. */\r
168                                 do\r
169                                 {\r
170                                         /* Get the next output string from the command interpreter. */\r
171                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
172 \r
173                                         /* Write the generated string to the CDC. */\r
174                                         USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) );\r
175                                         vTaskDelay( 1 );\r
176 \r
177                                 } while( xReturned != pdFALSE );\r
178 \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
184                                 ucInputIndex = 0;\r
185                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
186 \r
187                                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );\r
188                         }\r
189                         else\r
190                         {\r
191                                 if( cRxedChar == '\r' )\r
192                                 {\r
193                                         /* Ignore the character. */\r
194                                 }\r
195                                 else if( cRxedChar == '\b' )\r
196                                 {\r
197                                         /* Backspace was pressed.  Erase the last character in the\r
198                                         string - if any. */\r
199                                         if( ucInputIndex > 0 )\r
200                                         {\r
201                                                 ucInputIndex--;\r
202                                                 cInputString[ ucInputIndex ] = '\0';\r
203                                         }\r
204                                 }\r
205                                 else\r
206                                 {\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
211                                         {\r
212                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
213                                                 {\r
214                                                         cInputString[ ucInputIndex ] = cRxedChar;\r
215                                                         ucInputIndex++;\r
216                                                 }\r
217                                         }\r
218                                 }\r
219                         }\r
220 \r
221                         /* Must ensure to give the mutex back. */\r
222                         xSemaphoreGive( xCDCMutex );\r
223                 }\r
224         }\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 void vOutputString( const char * const pcMessage )\r
229 {\r
230         if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
231         {\r
232                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcMessage, strlen( pcMessage ) );\r
233                 xSemaphoreGive( xCDCMutex );\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 char cGetCDCChar( void )\r
239 {\r
240 int32_t lAvailableBytes, xBytes = 0;\r
241 char cInputChar;\r
242 \r
243         do\r
244         {\r
245                 /* Are there any characters already available? */\r
246                 CDC_OutBufAvailChar( &lAvailableBytes );\r
247                 if( lAvailableBytes > 0 )\r
248                 {\r
249                         if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
250                         {\r
251                                 /* Attempt to read one character. */\r
252                                 xBytes = 1;\r
253                                 xBytes = CDC_RdOutBuf( &cInputChar, &xBytes );\r
254 \r
255                                 xSemaphoreGive( xCDCMutex );\r
256                         }\r
257                 }\r
258 \r
259                 if( xBytes == 0 )\r
260                 {\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
264                 }\r
265 \r
266         } while( xBytes == 0 );\r
267 \r
268         return cInputChar;\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 /* Callback function executed by the USB interrupt when new data arrives. */\r
273 void vCDCNewDataNotify( void )\r
274 {\r
275 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
276 \r
277         configASSERT( xNewDataSemaphore );\r
278 \r
279         /* 'Give' the semaphore that signals the arrival of new data to the command\r
280         console task. */\r
281         xSemaphoreGiveFromISR( xNewDataSemaphore, &xHigherPriorityTaskWoken );\r
282         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static void prvSetupUSBDrivers( void )\r
287 {\r
288 LPC_USBDRV_INIT_T xUSBCallback;\r
289 \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
297 \r
298         /* Initialise then connect the USB. */\r
299         USB_Init( &xUSBCallback );\r
300         USB_Connect( pdTRUE );\r
301 }\r