]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/UARTCommandConsole.c
Get CLI functioning in SAMD20 demo.
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / UARTCommandConsole.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* Standard includes. */\r
66 #include "string.h"\r
67 #include "stdio.h"\r
68 \r
69 /* FreeRTOS includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include "queue.h"\r
73 #include "semphr.h"\r
74 \r
75 /* Library includes. */\r
76 #include "asf.h"\r
77 \r
78 /* Example includes. */\r
79 #include "FreeRTOS_CLI.h"\r
80 #include "UARTCommandConsole.h"\r
81 \r
82 /* Dimensions the buffer into which input characters are placed. */\r
83 #define cmdMAX_INPUT_SIZE               50\r
84 \r
85 /* The maximum time in ticks to wait for the UART access mutex. */\r
86 #define cmdMAX_MUTEX_WAIT               ( 200 / portTICK_RATE_MS )\r
87 \r
88 /* Characters are only ever received slowly on the CLI so it is ok to pass\r
89 received characters from the UART interrupt to the task on a queue.  This sets\r
90 the length of the queue used for that purpose. */\r
91 #define cmdRXED_CHARS_QUEUE_LENGTH                      ( 10 )\r
92 \r
93 /* DEL acts as a backspace. */\r
94 #define cmdASCII_DEL            ( 0x7F )\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /*\r
99  * The task that implements the command console processing.\r
100  */\r
101 static void prvUARTCommandConsoleTask( void *pvParameters );\r
102 \r
103 /*\r
104  * Ensure a previous interrupt driven Tx has completed before sending the next\r
105  * data block to the UART.
106  */\r
107 static void prvSendBuffer( struct usart_module *pxCDCUsart, uint8_t * pcBuffer, size_t xBufferLength );\r
108 \r
109 /*\r
110  * A UART is used for printf() output and CLI input and output.  Configure the\r
111  * UART and register prvUARTRxNotificationHandler() to handle UART Rx events.\r
112  */\r
113 static void prvConfigureUART( struct usart_module *pxCDCUsart );\r
114 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );\r
115 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* Const messages output by the command console. */\r
120 static uint8_t * const pcWelcomeMessage = ( uint8_t * ) "\r\n\r\nFreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
121 static const uint8_t * const pcEndOfOutputMessage = ( uint8_t * ) "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
122 static const uint8_t * const pcNewLine = ( uint8_t * ) "\r\n";\r
123 \r
124 /* This semaphore is used to allow the task to wait for a Tx to complete\r
125 without wasting any CPU time. */\r
126 static xSemaphoreHandle xTxCompleteSemaphore = NULL;\r
127 \r
128 /* This semaphore is sued to allow the task to wait for an Rx to complete\r
129 without wasting any CPU time. */\r
130 static xSemaphoreHandle xRxCompleteSemaphore = NULL;\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )\r
135 {\r
136         /* Create that task that handles the console itself. */\r
137         xTaskCreate(    prvUARTCommandConsoleTask,                      /* The task that implements the command console. */\r
138                                         ( const int8_t * const ) "CLI",         /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
139                                         usStackSize,                                            /* The size of the stack allocated to the task. */\r
140                                         NULL,                                                           /* The parameter is not used, so NULL is passed. */\r
141                                         uxPriority,                                                     /* The priority allocated to the task. */\r
142                                         NULL );                                                         /* A handle is not required, so just pass NULL. */\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 static void prvUARTCommandConsoleTask( void *pvParameters )\r
147 {\r
148 uint8_t ucRxedChar, ucInputIndex = 0, *pucOutputString;\r
149 static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
150 portBASE_TYPE xReturned;\r
151 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */\r
152 \r
153         ( void ) pvParameters;\r
154 \r
155         /* A UART is used for printf() output and CLI input and output.  Note there\r
156         is no mutual exclusion on the UART, but the demo as it stands does not\r
157         require mutual exclusion. */\r
158         prvConfigureUART( &xCDCUsart );\r
159 \r
160         /* Obtain the address of the output buffer.  Note there is no mutual\r
161         exclusion on this buffer as it is assumed only one command console\r
162         interface will be used at any one time. */\r
163         pucOutputString = ( uint8_t * ) FreeRTOS_CLIGetOutputBuffer();\r
164 \r
165         /* Send the welcome message. */\r
166         prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) );\r
167 \r
168         for( ;; )\r
169         {\r
170                 /* Wait for the next character to arrive.  A semaphore is used to\r
171                 ensure no CPU time is used until data has arrived. */\r
172                 usart_read_buffer_job( &xCDCUsart, &ucRxedChar, sizeof( ucRxedChar ) );         \r
173                 if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )\r
174                 {\r
175                         /* Echo the character back. */\r
176                         prvSendBuffer( &xCDCUsart, ( uint8_t * ) &ucRxedChar, sizeof( ucRxedChar ) );\r
177 \r
178                         /* Was it the end of the line? */\r
179                         if( ucRxedChar == '\n' || ucRxedChar == '\r' )\r
180                         {\r
181                                 /* Just to space the output from the input. */\r
182                                 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcNewLine, strlen( ( char * ) pcNewLine ) );\r
183 \r
184                                 /* See if the command is empty, indicating that the last command is\r
185                                 to be executed again. */\r
186                                 if( ucInputIndex == 0 )\r
187                                 {\r
188                                         /* Copy the last command back into the input string. */\r
189                                         strcpy( ( char * ) cInputString, ( char * ) cLastInputString );\r
190                                 }\r
191 \r
192                                 /* Pass the received command to the command interpreter.  The\r
193                                 command interpreter is called repeatedly until it returns pdFALSE\r
194                                 (indicating there is no more output) as it might generate more than\r
195                                 one string. */\r
196                                 do\r
197                                 {\r
198                                         /* Get the next output string from the command interpreter. */\r
199                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, ( int8_t * ) pucOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
200 \r
201                                         /* Write the generated string to the UART. */\r
202                                         prvSendBuffer( &xCDCUsart, ( uint8_t * ) pucOutputString, strlen( ( char * ) pucOutputString ) );\r
203 \r
204                                 } while( xReturned != pdFALSE );\r
205 \r
206                                 /* All the strings generated by the input command have been sent.\r
207                                 Clear the input string ready to receive the next command.  Remember\r
208                                 the command that was just processed first in case it is to be\r
209                                 processed again. */\r
210                                 strcpy( ( char * ) cLastInputString, ( char * ) cInputString );\r
211                                 ucInputIndex = 0;\r
212                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
213 \r
214                                 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) );\r
215                         }\r
216                         else\r
217                         {\r
218                                 if( ucRxedChar == '\r' )\r
219                                 {\r
220                                         /* Ignore the character. */\r
221                                 }\r
222                                 else if( ( ucRxedChar == '\b' ) || ( ucRxedChar == cmdASCII_DEL ) )\r
223                                 {\r
224                                         /* Backspace was pressed.  Erase the last character in the\r
225                                         string - if any. */\r
226                                         if( ucInputIndex > 0 )\r
227                                         {\r
228                                                 ucInputIndex--;\r
229                                                 cInputString[ ucInputIndex ] = '\0';\r
230                                         }\r
231                                 }\r
232                                 else\r
233                                 {\r
234                                         /* A character was entered.  Add it to the string\r
235                                         entered so far.  When a \n is entered the complete\r
236                                         string will be passed to the command interpreter. */\r
237                                         if( ( ucRxedChar >= ' ' ) && ( ucRxedChar <= '~' ) )\r
238                                         {\r
239                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
240                                                 {\r
241                                                         cInputString[ ucInputIndex ] = ucRxedChar;\r
242                                                         ucInputIndex++;\r
243                                                 }\r
244                                         }\r
245                                 }\r
246                         }\r
247                 }\r
248         }\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 static void prvSendBuffer( struct usart_module *pxCDCUsart, uint8_t * pcBuffer, size_t xBufferLength )\r
253 {\r
254 const portTickType xBlockMax50ms = 50 / portTICK_RATE_MS;\r
255 \r
256         if( xBufferLength > 0 )\r
257         {               \r
258                 usart_write_buffer_job( pxCDCUsart, pcBuffer, xBufferLength );\r
259                 \r
260                 /* Wait for the Tx to complete so the buffer can be reused without\r
261                 corrupting the data that is being sent. */\r
262                 xSemaphoreTake( xTxCompleteSemaphore, xBlockMax50ms );\r
263         }\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 static void prvConfigureUART( struct usart_module *pxCDCUsart )\r
268 {\r
269 struct usart_config xUARTConfig;\r
270 \r
271         /* This semaphore is used to allow the task to wait for the Tx to complete\r
272         without wasting any CPU time. */\r
273         vSemaphoreCreateBinary( xTxCompleteSemaphore );\r
274         configASSERT( xTxCompleteSemaphore );\r
275         \r
276         /* This semaphore is used to allow the task to block for an Rx to complete\r
277         without wasting any CPU time. */\r
278         vSemaphoreCreateBinary( xRxCompleteSemaphore );\r
279         configASSERT( xRxCompleteSemaphore );\r
280 \r
281         /* Take the semaphores so they start in the wanted state.  A block time is\r
282         not necessary, and is therefore set to 0, as it is known that the semaphore s\r
283         exists - they have just been created. */\r
284         xSemaphoreTake( xTxCompleteSemaphore, 0 );\r
285         xSemaphoreTake( xRxCompleteSemaphore, 0 );\r
286 \r
287         /* Configure the hardware. */\r
288         usart_get_config_defaults( &xUARTConfig );\r
289         xUARTConfig.baudrate    = 115200;\r
290         xUARTConfig.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;\r
291         xUARTConfig.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;\r
292         xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;\r
293         xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;\r
294         xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;\r
295         while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK ) \r
296         {\r
297                 /* Nothing to do here.  Should include a timeout really but this is\r
298                 init code only. */\r
299         }\r
300         usart_enable( pxCDCUsart );\r
301         \r
302         /* Register the driver callbacks. */\r
303         usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );\r
304         usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );\r
305         usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_TRANSMITTED );\r
306         usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_RECEIVED );\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )\r
311 {\r
312 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
313 \r
314         /* Remove compiler warnings. */\r
315         ( void ) pxUSART;\r
316 \r
317         /* Give the semaphore  to unblock any tasks that might be waiting for an Rx\r
318         to complete.  If a task is unblocked, and the unblocked task has a priority\r
319         above the currently running task, then xHigherPriorityTaskWoken will be set\r
320         to pdTRUE inside the xSemaphoreGiveFromISR() function. */\r
321         xSemaphoreGiveFromISR( xRxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
322 \r
323         /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
324         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )\r
329 {\r
330 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
331 \r
332         /* Remove compiler warnings. */\r
333         ( void ) pxUSART;\r
334 \r
335         /* Give the semaphore  to unblock any tasks that might be waiting for a Tx\r
336         to complete.  If a task is unblocked, and the unblocked task has a priority\r
337         above the currently running task, then xHigherPriorityTaskWoken will be set\r
338         to pdTRUE inside the xSemaphoreGiveFromISR() function. */\r
339         xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
340 \r
341         /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
342         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
343 }\r
344 \r
345 \r