]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/UARTCommandConsole.c
Update version number in preparation for official V8.2.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / UARTCommandConsole.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 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 /* Standard includes. */\r
71 #include "string.h"\r
72 #include "stdio.h"\r
73 \r
74 /* FreeRTOS includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 #include "queue.h"\r
78 #include "semphr.h"\r
79 \r
80 /* Library includes. */\r
81 #include "asf.h"\r
82 \r
83 /* Example includes. */\r
84 #include "FreeRTOS_CLI.h"\r
85 #include "UARTCommandConsole.h"\r
86 \r
87 /* Dimensions the buffer into which input characters are placed. */\r
88 #define cmdMAX_INPUT_SIZE               50\r
89 \r
90 /* The maximum time in ticks to wait for the UART access mutex. */\r
91 #define cmdMAX_MUTEX_WAIT               ( 200 / portTICK_PERIOD_MS )\r
92 \r
93 /* Characters are only ever received slowly on the CLI so it is ok to pass\r
94 received characters from the UART interrupt to the task on a queue.  This sets\r
95 the length of the queue used for that purpose. */\r
96 #define cmdRXED_CHARS_QUEUE_LENGTH                      ( 10 )\r
97 \r
98 /* DEL acts as a backspace. */\r
99 #define cmdASCII_DEL            ( 0x7F )\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /*\r
104  * The task that implements the command console processing.\r
105  */\r
106 static void prvUARTCommandConsoleTask( void *pvParameters );\r
107 \r
108 /*\r
109  * Ensure a previous interrupt driven Tx has completed before sending the next\r
110  * data block to the UART.\r
111  */\r
112 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength );\r
113 \r
114 /*\r
115  * Register the 'standard' sample CLI commands with FreeRTOS+CLI.\r
116  */\r
117 extern void vRegisterSampleCLICommands( void );\r
118 \r
119 /*\r
120  * Configure the UART used for IO.and register prvUARTRxNotificationHandler()\r
121  * to handle UART Rx events.\r
122  */\r
123 static void prvConfigureUART( struct usart_module *pxCDCUsart );\r
124 \r
125 /*\r
126  * Callback functions registered with the Atmel UART driver.  Both functions\r
127  * just 'give' a semaphore to unblock a task that may be waiting for a\r
128  * character to be received, or a transmission to complete.\r
129  */\r
130 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );\r
131 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 /* Const messages output by the command console. */\r
136 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
137 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
138 static const char * const pcNewLine = "\r\n";\r
139 \r
140 /* This semaphore is used to allow the task to wait for a Tx to complete\r
141 without wasting any CPU time. */\r
142 static SemaphoreHandle_t xTxCompleteSemaphore = NULL;\r
143 \r
144 /* This semaphore is sued to allow the task to wait for an Rx to complete\r
145 without wasting any CPU time. */\r
146 static SemaphoreHandle_t xRxCompleteSemaphore = NULL;\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )\r
151 {\r
152         vRegisterSampleCLICommands();\r
153 \r
154         /* Create that task that handles the console itself. */\r
155         xTaskCreate(    prvUARTCommandConsoleTask,                      /* The task that implements the command console. */\r
156                                         "CLI",                                                          /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
157                                         usStackSize,                                            /* The size of the stack allocated to the task. */\r
158                                         NULL,                                                           /* The parameter is not used, so NULL is passed. */\r
159                                         uxPriority,                                                     /* The priority allocated to the task. */\r
160                                         NULL );                                                         /* A handle is not required, so just pass NULL. */\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 static void prvUARTCommandConsoleTask( void *pvParameters )\r
165 {\r
166 char cRxedChar, *pcOutputString;\r
167 uint8_t ucInputIndex = 0;\r
168 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
169 portBASE_TYPE xReturned;\r
170 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */\r
171 \r
172         ( void ) pvParameters;\r
173 \r
174         /* A UART is used for printf() output and CLI input and output.  Note there\r
175         is no mutual exclusion on the UART, but the demo as it stands does not\r
176         require mutual exclusion. */\r
177         prvConfigureUART( &xCDCUsart );\r
178 \r
179         /* Obtain the address of the output buffer.  Note there is no mutual\r
180         exclusion on this buffer as it is assumed only one command console\r
181         interface will be used at any one time. */\r
182         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
183 \r
184         /* Send the welcome message. */\r
185         prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( pcWelcomeMessage ) );\r
186 \r
187         for( ;; )\r
188         {\r
189                 /* Wait for the next character to arrive.  A semaphore is used to\r
190                 ensure no CPU time is used until data has arrived. */\r
191                 usart_read_buffer_job( &xCDCUsart, ( uint8_t * ) &cRxedChar, sizeof( cRxedChar ) );\r
192                 if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )\r
193                 {\r
194                         /* Echo the character back. */\r
195                         prvSendBuffer( &xCDCUsart, &cRxedChar, sizeof( cRxedChar ) );\r
196 \r
197                         /* Was it the end of the line? */\r
198                         if( cRxedChar == '\n' || cRxedChar == '\r' )\r
199                         {\r
200                                 /* Just to space the output from the input. */\r
201                                 prvSendBuffer( &xCDCUsart, pcNewLine, strlen( pcNewLine ) );\r
202 \r
203                                 /* See if the command is empty, indicating that the last command is\r
204                                 to be executed again. */\r
205                                 if( ucInputIndex == 0 )\r
206                                 {\r
207                                         /* Copy the last command back into the input string. */\r
208                                         strcpy( cInputString, cLastInputString );\r
209                                 }\r
210 \r
211                                 /* Pass the received command to the command interpreter.  The\r
212                                 command interpreter is called repeatedly until it returns pdFALSE\r
213                                 (indicating there is no more output) as it might generate more than\r
214                                 one string. */\r
215                                 do\r
216                                 {\r
217                                         /* Get the next output string from the command interpreter. */\r
218                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
219 \r
220                                         /* Write the generated string to the UART. */\r
221                                         prvSendBuffer( &xCDCUsart, pcOutputString, strlen( pcOutputString ) );\r
222 \r
223                                 } while( xReturned != pdFALSE );\r
224 \r
225                                 /* All the strings generated by the input command have been sent.\r
226                                 Clear the input string ready to receive the next command.  Remember\r
227                                 the command that was just processed first in case it is to be\r
228                                 processed again. */\r
229                                 strcpy( cLastInputString, cInputString );\r
230                                 ucInputIndex = 0;\r
231                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
232 \r
233                                 prvSendBuffer( &xCDCUsart, pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );\r
234                         }\r
235                         else\r
236                         {\r
237                                 if( cRxedChar == '\r' )\r
238                                 {\r
239                                         /* Ignore the character. */\r
240                                 }\r
241                                 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )\r
242                                 {\r
243                                         /* Backspace was pressed.  Erase the last character in the\r
244                                         string - if any. */\r
245                                         if( ucInputIndex > 0 )\r
246                                         {\r
247                                                 ucInputIndex--;\r
248                                                 cInputString[ ucInputIndex ] = '\0';\r
249                                         }\r
250                                 }\r
251                                 else\r
252                                 {\r
253                                         /* A character was entered.  Add it to the string\r
254                                         entered so far.  When a \n is entered the complete\r
255                                         string will be passed to the command interpreter. */\r
256                                         if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
257                                         {\r
258                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
259                                                 {\r
260                                                         cInputString[ ucInputIndex ] = cRxedChar;\r
261                                                         ucInputIndex++;\r
262                                                 }\r
263                                         }\r
264                                 }\r
265                         }\r
266                 }\r
267         }\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength )\r
272 {\r
273 const TickType_t xBlockMax100ms = 100UL / portTICK_PERIOD_MS;\r
274 \r
275         if( xBufferLength > 0 )\r
276         {\r
277                 usart_write_buffer_job( pxCDCUsart, ( uint8_t * ) pcBuffer, xBufferLength );\r
278 \r
279                 /* Wait for the Tx to complete so the buffer can be reused without\r
280                 corrupting the data that is being sent. */\r
281                 xSemaphoreTake( xTxCompleteSemaphore, xBlockMax100ms );\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static void prvConfigureUART( struct usart_module *pxCDCUsart )\r
287 {\r
288 struct usart_config xUARTConfig;\r
289 \r
290         /* This semaphore is used to allow the task to wait for the Tx to complete\r
291         without wasting any CPU time. */\r
292         vSemaphoreCreateBinary( xTxCompleteSemaphore );\r
293         configASSERT( xTxCompleteSemaphore );\r
294 \r
295         /* This semaphore is used to allow the task to block for an Rx to complete\r
296         without wasting any CPU time. */\r
297         vSemaphoreCreateBinary( xRxCompleteSemaphore );\r
298         configASSERT( xRxCompleteSemaphore );\r
299 \r
300         /* Take the semaphores so they start in the wanted state.  A block time is\r
301         not necessary, and is therefore set to 0, as it is known that the semaphores\r
302         exists - they have just been created. */\r
303         xSemaphoreTake( xTxCompleteSemaphore, 0 );\r
304         xSemaphoreTake( xRxCompleteSemaphore, 0 );\r
305 \r
306         /* Configure the hardware. */\r
307         usart_get_config_defaults( &xUARTConfig );\r
308         xUARTConfig.baudrate    = 115200;\r
309         xUARTConfig.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;\r
310         xUARTConfig.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;\r
311         xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;\r
312         xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;\r
313         xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;\r
314         while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK )\r
315         {\r
316                 /* Nothing to do here.  Should include a timeout really but this is\r
317                 init code only. */\r
318         }\r
319         usart_enable( pxCDCUsart );\r
320 \r
321         /* Register the driver callbacks. */\r
322         usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );\r
323         usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );\r
324         usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_TRANSMITTED );\r
325         usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_RECEIVED );\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )\r
330 {\r
331 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
332 \r
333         /* Remove compiler warnings. */\r
334         ( void ) pxUSART;\r
335 \r
336         /* Give the semaphore  to unblock any tasks that might be waiting for an Rx\r
337         to complete.  If a task is unblocked, and the unblocked task has a priority\r
338         above the currently running task, then xHigherPriorityTaskWoken will be set\r
339         to pdTRUE inside the xSemaphoreGiveFromISR() function. */\r
340         xSemaphoreGiveFromISR( xRxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
341 \r
342         /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
343         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
344 }\r
345 /*-----------------------------------------------------------*/\r
346 \r
347 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )\r
348 {\r
349 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
350 \r
351         /* Remove compiler warnings. */\r
352         ( void ) pxUSART;\r
353 \r
354         /* Give the semaphore  to unblock any tasks that might be waiting for a Tx\r
355         to complete.  If a task is unblocked, and the unblocked task has a priority\r
356         above the currently running task, then xHigherPriorityTaskWoken will be set\r
357         to pdTRUE inside the xSemaphoreGiveFromISR() function. */\r
358         xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
359 \r
360         /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
361         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
362 }\r
363 \r
364 \r