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