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