]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/Examples/USB_CDC/CDCCommandConsole.c
Update FreeRTOS+ more demos that use FreeRTOS+CLI to remove casting to int8_t * from...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / Examples / USB_CDC / CDCCommandConsole.c
1 /*\r
2     FreeRTOS V7.1.0 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * NOTE:  This file uses a third party USB CDC driver.\r
56  */\r
57 \r
58 /* Standard includes. */\r
59 #include "string.h"\r
60 #include "stdio.h"\r
61 \r
62 /* FreeRTOS includes. */\r
63 #include "FreeRTOS.h"\r
64 #include "task.h"\r
65 #include "semphr.h"\r
66 \r
67 /* Driver includes. */\r
68 #include "usbhw.h"\r
69 #include "cdcuser.h"\r
70 #include "usbcfg.h"\r
71 #include "usbuser.h"\r
72 \r
73 /* Example includes. */\r
74 #include "FreeRTOS_CLI.h"\r
75 #include "CDCCommandConsole.h"\r
76 \r
77 /* Dimensions the buffer into which input characters are placed. */\r
78 #define cmdMAX_INPUT_SIZE               50\r
79 \r
80 /* The maximum time in ticks to wait for the CDC access mutex. */\r
81 #define cmdMAX_MUTEX_WAIT               ( 200 / portTICK_RATE_MS )\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /*\r
86  * The task that implements the command console processing.\r
87  */\r
88 static void prvCDCCommandConsoleTask( void *pvParameters );\r
89 \r
90 /*\r
91  * Obtain a character from the CDC input.  The calling task will be held in the\r
92  * Blocked state (so other tasks can execute) until a character is avilable.\r
93  */\r
94 char cGetCDCChar( void );\r
95 \r
96 /*\r
97  * Initialise the third party virtual comport files driver\r
98  */\r
99 static void prvSetupUSBDrivers( void );\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /* 'Given' by the CDC interrupt to unblock the receiving task when new data\r
104 is available. */\r
105 static xSemaphoreHandle xNewDataSemaphore = NULL;\r
106 \r
107 /* Used to guard access to the CDC output, which is used by more than one\r
108 task. */\r
109 static xSemaphoreHandle xCDCMutex = NULL;\r
110 \r
111 /* Const messages output by the command console. */\r
112 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
113 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
114 static const char * const pcNewLine = "\r\n";\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 void vCDCCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )\r
119 {\r
120         /* Create the semaphores and mutexes used by the CDC to task interface. */\r
121         xCDCMutex = xSemaphoreCreateMutex();\r
122         vSemaphoreCreateBinary( xNewDataSemaphore );\r
123         configASSERT( xCDCMutex );\r
124         configASSERT( xNewDataSemaphore );\r
125 \r
126         /* Add the semaphore and mutex to the queue registry for viewing in the\r
127         kernel aware state viewer. */\r
128         vQueueAddToRegistry( xCDCMutex, "CDCMu" );\r
129         vQueueAddToRegistry( xNewDataSemaphore, "CDCDat" );\r
130 \r
131         /* Create that task that handles the console itself. */\r
132         xTaskCreate(    prvCDCCommandConsoleTask,       /* The task that implements the command console. */\r
133                                         "CDCCmd",                                       /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
134                                         usStackSize,                            /* The size of the stack allocated to the task. */\r
135                                         NULL,                                           /* The parameter is not used, so NULL is passed. */\r
136                                         uxPriority,                                     /* The priority allocated to the task. */\r
137                                         NULL );                                         /* A handle is not required, so just pass NULL. */\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 static void prvCDCCommandConsoleTask( void *pvParameters )\r
142 {\r
143 char cRxedChar;\r
144 uint8_t ucInputIndex = 0;\r
145 char *pcOutputString;\r
146 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
147 portBASE_TYPE xReturned;\r
148 \r
149         ( void ) pvParameters;\r
150 \r
151         /* Obtain the address of the output buffer.  Note there is no mutual\r
152         exclusion on this buffer as it is assumed only one command console\r
153         interface will be used at any one time. */\r
154         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
155 \r
156         /* Initialise the virtual com port (CDC) interface. */\r
157         prvSetupUSBDrivers();\r
158 \r
159         /* Send the welcome message.  This probably won't be seen as the console\r
160         will not have been connected yet. */\r
161         USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );\r
162 \r
163         for( ;; )\r
164         {\r
165                 /* No characters received yet for the current input string. */\r
166                 cRxedChar = 0;\r
167 \r
168                 /* Only interested in reading one character at a time. */\r
169                 cRxedChar = cGetCDCChar();\r
170 \r
171                 if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
172                 {\r
173                         /* Echo the character back. */\r
174                         USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );\r
175 \r
176                         /* Was it the end of the line? */\r
177                         if( cRxedChar == '\n' || cRxedChar == '\r' )\r
178                         {\r
179                                 /* Just to space the output from the input. */\r
180                                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( pcNewLine ) );\r
181 \r
182                                 /* See if the command is empty, indicating that the last command is\r
183                                 to be executed again. */\r
184                                 if( ucInputIndex == 0 )\r
185                                 {\r
186                                         /* Copy the last command back into the input string. */\r
187                                         strcpy( cInputString, cLastInputString );\r
188                                 }\r
189 \r
190                                 /* Pass the received command to the command interpreter.  The\r
191                                 command interpreter is called repeatedly until it returns pdFALSE\r
192                                 (indicating there is no more output) as it might generate more than\r
193                                 one string. */\r
194                                 do\r
195                                 {\r
196                                         /* Get the next output string from the command interpreter. */\r
197                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
198 \r
199                                         /* Write the generated string to the CDC. */\r
200                                         USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) );\r
201                                         vTaskDelay( 1 );\r
202 \r
203                                 } while( xReturned != pdFALSE );\r
204 \r
205                                 /* All the strings generated by the input command have been sent.\r
206                                 Clear the input string ready to receive the next command.  Remember\r
207                                 the command that was just processed first in case it is to be\r
208                                 processed again. */\r
209                                 strcpy( cLastInputString, cInputString );\r
210                                 ucInputIndex = 0;\r
211                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
212 \r
213                                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );\r
214                         }\r
215                         else\r
216                         {\r
217                                 if( cRxedChar == '\r' )\r
218                                 {\r
219                                         /* Ignore the character. */\r
220                                 }\r
221                                 else if( cRxedChar == '\b' )\r
222                                 {\r
223                                         /* Backspace was pressed.  Erase the last character in the\r
224                                         string - if any. */\r
225                                         if( ucInputIndex > 0 )\r
226                                         {\r
227                                                 ucInputIndex--;\r
228                                                 cInputString[ ucInputIndex ] = '\0';\r
229                                         }\r
230                                 }\r
231                                 else\r
232                                 {\r
233                                         /* A character was entered.  Add it to the string\r
234                                         entered so far.  When a \n is entered the complete\r
235                                         string will be passed to the command interpreter. */\r
236                                         if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
237                                         {\r
238                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
239                                                 {\r
240                                                         cInputString[ ucInputIndex ] = cRxedChar;\r
241                                                         ucInputIndex++;\r
242                                                 }\r
243                                         }\r
244                                 }\r
245                         }\r
246 \r
247                         /* Must ensure to give the mutex back. */\r
248                         xSemaphoreGive( xCDCMutex );\r
249                 }\r
250         }\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 void vOutputString( const char * const pcMessage )\r
255 {\r
256         if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
257         {\r
258                 USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcMessage, strlen( pcMessage ) );\r
259                 xSemaphoreGive( xCDCMutex );\r
260         }\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 char cGetCDCChar( void )\r
265 {\r
266 int32_t lAvailableBytes, xBytes = 0;\r
267 char cInputChar;\r
268 \r
269         do\r
270         {\r
271                 /* Are there any characters already available? */\r
272                 CDC_OutBufAvailChar( &lAvailableBytes );\r
273                 if( lAvailableBytes > 0 )\r
274                 {\r
275                         if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )\r
276                         {\r
277                                 /* Attempt to read one character. */\r
278                                 xBytes = 1;\r
279                                 xBytes = CDC_RdOutBuf( &cInputChar, &xBytes );\r
280 \r
281                                 xSemaphoreGive( xCDCMutex );\r
282                         }\r
283                 }\r
284 \r
285                 if( xBytes == 0 )\r
286                 {\r
287                         /* A character was not available.  Wait until signalled by the\r
288                         CDC Rx callback function that new data has arrived. */\r
289                         xSemaphoreTake( xNewDataSemaphore, portMAX_DELAY );\r
290                 }\r
291 \r
292         } while( xBytes == 0 );\r
293 \r
294         return cInputChar;\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 /* Callback function executed by the USB interrupt when new data arrives. */\r
299 void vCDCNewDataNotify( void )\r
300 {\r
301 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
302 \r
303         configASSERT( xNewDataSemaphore );\r
304 \r
305         /* 'Give' the semaphore that signals the arrival of new data to the command\r
306         console task. */\r
307         xSemaphoreGiveFromISR( xNewDataSemaphore, &xHigherPriorityTaskWoken );\r
308         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 static void prvSetupUSBDrivers( void )\r
313 {\r
314 LPC_USBDRV_INIT_T xUSBCallback;\r
315 \r
316         /* Initialise the callback structure. */\r
317         memset( ( void * ) &xUSBCallback, 0, sizeof( LPC_USBDRV_INIT_T ) );\r
318         xUSBCallback.USB_Reset_Event = USB_Reset_Event;\r
319         xUSBCallback.USB_P_EP[ 0 ] = USB_EndPoint0;\r
320         xUSBCallback.USB_P_EP[ 1 ] = USB_EndPoint1;\r
321         xUSBCallback.USB_P_EP[ 2 ] = USB_EndPoint2;\r
322         xUSBCallback.ep0_maxp = USB_MAX_PACKET0;\r
323 \r
324         /* Initialise then connect the USB. */\r
325         USB_Init( &xUSBCallback );\r
326         USB_Connect( pdTRUE );\r
327 }\r