]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/Examples/USB_CDC/CDCCommandConsole.c
Update FreeRTOS+ version number ready for version 9 release candidate 1.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / Examples / USB_CDC / CDCCommandConsole.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71  * NOTE:  This file uses a third party USB CDC driver.\r
72  */\r
73 \r
74 /* Standard includes. */\r
75 #include "string.h"\r
76 #include "stdio.h"\r
77 \r
78 /* FreeRTOS includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "semphr.h"\r
82 \r
83 /* Driver includes. */\r
84 #include "usbhw.h"\r
85 #include "cdcuser.h"\r
86 #include "usbcfg.h"\r
87 #include "usbuser.h"\r
88 \r
89 /* Example includes. */\r
90 #include "FreeRTOS_CLI.h"\r
91 #include "CDCCommandConsole.h"\r
92 \r
93 /* Dimensions the buffer into which input characters are placed. */\r
94 #define cmdMAX_INPUT_SIZE               50\r
95 \r
96 /* The maximum time in ticks to wait for the CDC access mutex. */\r
97 #define cmdMAX_MUTEX_WAIT               ( 200 / portTICK_RATE_MS )\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /*\r
102  * The task that implements the command console processing.\r
103  */\r
104 static void prvCDCCommandConsoleTask( void *pvParameters );\r
105 \r
106 /*\r
107  * Obtain a character from the CDC input.  The calling task will be held in the\r
108  * Blocked state (so other tasks can execute) until a character is avilable.\r
109  */\r
110 char cGetCDCChar( void );\r
111 \r
112 /*\r
113  * Initialise the third party virtual comport files driver\r
114  */\r
115 static void prvSetupUSBDrivers( void );\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* 'Given' by the CDC interrupt to unblock the receiving task when new data\r
120 is available. */\r
121 static xSemaphoreHandle xNewDataSemaphore = NULL;\r
122 \r
123 /* Used to guard access to the CDC output, which is used by more than one\r
124 task. */\r
125 static xSemaphoreHandle xCDCMutex = NULL;\r
126 \r
127 /* Const messages output by the command console. */\r
128 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
129 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
130 static const char * const pcNewLine = "\r\n";\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 void vCDCCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority )\r
135 {\r
136         /* Create the semaphores and mutexes used by the CDC to task interface. */\r
137         xCDCMutex = xSemaphoreCreateMutex();\r
138         vSemaphoreCreateBinary( xNewDataSemaphore );\r
139         configASSERT( xCDCMutex );\r
140         configASSERT( xNewDataSemaphore );\r
141 \r
142         /* Add the semaphore and mutex to the queue registry for viewing in the\r
143         kernel aware state viewer. */\r
144         vQueueAddToRegistry( xCDCMutex, "CDCMu" );\r
145         vQueueAddToRegistry( xNewDataSemaphore, "CDCDat" );\r
146 \r
147         /* Create that task that handles the console itself. */\r
148         xTaskCreate(    prvCDCCommandConsoleTask,       /* The task that implements the command console. */\r
149                                         "CDCCmd",                                       /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
150                                         usStackSize,                            /* The size of the stack allocated to the task. */\r
151                                         NULL,                                           /* The parameter is not used, so NULL is passed. */\r
152                                         uxPriority,                                     /* The priority allocated to the task. */\r
153                                         NULL );                                         /* A handle is not required, so just pass NULL. */\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 static void prvCDCCommandConsoleTask( void *pvParameters )\r
158 {\r
159 char cRxedChar;\r
160 uint8_t ucInputIndex = 0;\r
161 char *pcOutputString;\r
162 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
163 BaseType_t xReturned;\r
164 \r
165         ( void ) pvParameters;\r
166 \r
167         /* Obtain the address of the output buffer.  Note there is no mutual\r
168         exclusion on this buffer as it is assumed only one command console\r
169         interface will be used at any one time. */\r
170         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
171 \r
172         /* Initialise the virtual com port (CDC) interface. */\r
173         prvSetupUSBDrivers();\r
174 \r
175         /* Send the welcome message.  This probably won't be seen as the console\r
176         will not have been connected yet. */\r
177         USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );\r
178 \r
179         for( ;; )\r
180         {\r
181                 /* No characters received yet for the current input string. */\r
182                 cRxedChar = 0;\r
183 \r
184                 /* Only interested in reading one character at a time. */\r
185                 cRxedChar = cGetCDCChar();\r
186 \r
187                 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
188                 {\r
189                         /* Echo the character back. */\r
190                         USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );\r
191 \r
192                         /* Was it the end of the line? */\r
193                         if( cRxedChar == '\n' || cRxedChar == '\r' )\r
194                         {\r
195                                 /* Just to space the output from the input. */\r
196                                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( pcNewLine ) );\r
197 \r
198                                 /* See if the command is empty, indicating that the last command is\r
199                                 to be executed again. */\r
200                                 if( ucInputIndex == 0 )\r
201                                 {\r
202                                         /* Copy the last command back into the input string. */\r
203                                         strcpy( cInputString, cLastInputString );\r
204                                 }\r
205 \r
206                                 /* Pass the received command to the command interpreter.  The\r
207                                 command interpreter is called repeatedly until it returns pdFALSE\r
208                                 (indicating there is no more output) as it might generate more than\r
209                                 one string. */\r
210                                 do\r
211                                 {\r
212                                         /* Get the next output string from the command interpreter. */\r
213                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
214 \r
215                                         /* Write the generated string to the CDC. */\r
216                                         USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) );\r
217                                         vTaskDelay( 1 );\r
218 \r
219                                 } while( xReturned != pdFALSE );\r
220 \r
221                                 /* All the strings generated by the input command have been sent.\r
222                                 Clear the input string ready to receive the next command.  Remember\r
223                                 the command that was just processed first in case it is to be\r
224                                 processed again. */\r
225                                 strcpy( cLastInputString, cInputString );\r
226                                 ucInputIndex = 0;\r
227                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
228 \r
229                                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );\r
230                         }\r
231                         else\r
232                         {\r
233                                 if( cRxedChar == '\r' )\r
234                                 {\r
235                                         /* Ignore the character. */\r
236                                 }\r
237                                 else if( cRxedChar == '\b' )\r
238                                 {\r
239                                         /* Backspace was pressed.  Erase the last character in the\r
240                                         string - if any. */\r
241                                         if( ucInputIndex > 0 )\r
242                                         {\r
243                                                 ucInputIndex--;\r
244                                                 cInputString[ ucInputIndex ] = '\0';\r
245                                         }\r
246                                 }\r
247                                 else\r
248                                 {\r
249                                         /* A character was entered.  Add it to the string\r
250                                         entered so far.  When a \n is entered the complete\r
251                                         string will be passed to the command interpreter. */\r
252                                         if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
253                                         {\r
254                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
255                                                 {\r
256                                                         cInputString[ ucInputIndex ] = cRxedChar;\r
257                                                         ucInputIndex++;\r
258                                                 }\r
259                                         }\r
260                                 }\r
261                         }\r
262 \r
263                         /* Must ensure to give the mutex back. */\r
264                         xSemaphoreGive( xCDCMutex );\r
265                 }\r
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 void vOutputString( const char * const pcMessage )\r
271 {\r
272         if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
273         {\r
274                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcMessage, strlen( pcMessage ) );\r
275                 xSemaphoreGive( xCDCMutex );\r
276         }\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 char cGetCDCChar( void )\r
281 {\r
282 int32_t lAvailableBytes, xBytes = 0;\r
283 char cInputChar;\r
284 \r
285         do\r
286         {\r
287                 /* Are there any characters already available? */\r
288                 CDC_OutBufAvailChar( &lAvailableBytes );\r
289                 if( lAvailableBytes > 0 )\r
290                 {\r
291                         if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
292                         {\r
293                                 /* Attempt to read one character. */\r
294                                 xBytes = 1;\r
295                                 xBytes = CDC_RdOutBuf( &cInputChar, &xBytes );\r
296 \r
297                                 xSemaphoreGive( xCDCMutex );\r
298                         }\r
299                 }\r
300 \r
301                 if( xBytes == 0 )\r
302                 {\r
303                         /* A character was not available.  Wait until signalled by the\r
304                         CDC Rx callback function that new data has arrived. */\r
305                         xSemaphoreTake( xNewDataSemaphore, portMAX_DELAY );\r
306                 }\r
307 \r
308         } while( xBytes == 0 );\r
309 \r
310         return cInputChar;\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 /* Callback function executed by the USB interrupt when new data arrives. */\r
315 void vCDCNewDataNotify( void )\r
316 {\r
317 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
318 \r
319         configASSERT( xNewDataSemaphore );\r
320 \r
321         /* 'Give' the semaphore that signals the arrival of new data to the command\r
322         console task. */\r
323         xSemaphoreGiveFromISR( xNewDataSemaphore, &xHigherPriorityTaskWoken );\r
324         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 static void prvSetupUSBDrivers( void )\r
329 {\r
330 LPC_USBDRV_INIT_T xUSBCallback;\r
331 \r
332         /* Initialise the callback structure. */\r
333         memset( ( void * ) &xUSBCallback, 0, sizeof( LPC_USBDRV_INIT_T ) );\r
334         xUSBCallback.USB_Reset_Event = USB_Reset_Event;\r
335         xUSBCallback.USB_P_EP[ 0 ] = USB_EndPoint0;\r
336         xUSBCallback.USB_P_EP[ 1 ] = USB_EndPoint1;\r
337         xUSBCallback.USB_P_EP[ 2 ] = USB_EndPoint2;\r
338         xUSBCallback.ep0_maxp = USB_MAX_PACKET0;\r
339 \r
340         /* Initialise then connect the USB. */\r
341         USB_Init( &xUSBCallback );\r
342         USB_Connect( pdTRUE );\r
343 }\r