]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/UARTCommandConsole.c
Start to remove unnecessary 'signed char *' casts from strings that are now just...
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / UARTCommandConsole.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 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 distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! 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_RATE_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, uint8_t * 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 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
133 static const uint8_t * const pcEndOfOutputMessage = ( uint8_t * ) "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
134 static const uint8_t * const pcNewLine = ( uint8_t * ) "\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 xSemaphoreHandle 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 xSemaphoreHandle 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 uint8_t ucRxedChar, ucInputIndex = 0, *pucOutputString;\r
163 static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
164 portBASE_TYPE xReturned;\r
165 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */\r
166 \r
167         ( void ) pvParameters;\r
168 \r
169         /* A UART is used for printf() output and CLI input and output.  Note there\r
170         is no mutual exclusion on the UART, but the demo as it stands does not\r
171         require mutual exclusion. */\r
172         prvConfigureUART( &xCDCUsart );\r
173 \r
174         /* Obtain the address of the output buffer.  Note there is no mutual\r
175         exclusion on this buffer as it is assumed only one command console\r
176         interface will be used at any one time. */\r
177         pucOutputString = ( uint8_t * ) FreeRTOS_CLIGetOutputBuffer();\r
178 \r
179         /* Send the welcome message. */\r
180         prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) );\r
181 \r
182         for( ;; )\r
183         {\r
184                 /* Wait for the next character to arrive.  A semaphore is used to\r
185                 ensure no CPU time is used until data has arrived. */\r
186                 usart_read_buffer_job( &xCDCUsart, &ucRxedChar, sizeof( ucRxedChar ) );         \r
187                 if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )\r
188                 {\r
189                         /* Echo the character back. */\r
190                         prvSendBuffer( &xCDCUsart, ( uint8_t * ) &ucRxedChar, sizeof( ucRxedChar ) );\r
191 \r
192                         /* Was it the end of the line? */\r
193                         if( ucRxedChar == '\n' || ucRxedChar == '\r' )\r
194                         {\r
195                                 /* Just to space the output from the input. */\r
196                                 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcNewLine, strlen( ( char * ) 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( ( char * ) cInputString, ( char * ) 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, ( int8_t * ) pucOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
214 \r
215                                         /* Write the generated string to the UART. */\r
216                                         prvSendBuffer( &xCDCUsart, ( uint8_t * ) pucOutputString, strlen( ( char * ) pucOutputString ) );\r
217 \r
218                                 } while( xReturned != pdFALSE );\r
219 \r
220                                 /* All the strings generated by the input command have been sent.\r
221                                 Clear the input string ready to receive the next command.  Remember\r
222                                 the command that was just processed first in case it is to be\r
223                                 processed again. */\r
224                                 strcpy( ( char * ) cLastInputString, ( char * ) cInputString );\r
225                                 ucInputIndex = 0;\r
226                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
227 \r
228                                 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) );\r
229                         }\r
230                         else\r
231                         {\r
232                                 if( ucRxedChar == '\r' )\r
233                                 {\r
234                                         /* Ignore the character. */\r
235                                 }\r
236                                 else if( ( ucRxedChar == '\b' ) || ( ucRxedChar == cmdASCII_DEL ) )\r
237                                 {\r
238                                         /* Backspace was pressed.  Erase the last character in the\r
239                                         string - if any. */\r
240                                         if( ucInputIndex > 0 )\r
241                                         {\r
242                                                 ucInputIndex--;\r
243                                                 cInputString[ ucInputIndex ] = '\0';\r
244                                         }\r
245                                 }\r
246                                 else\r
247                                 {\r
248                                         /* A character was entered.  Add it to the string\r
249                                         entered so far.  When a \n is entered the complete\r
250                                         string will be passed to the command interpreter. */\r
251                                         if( ( ucRxedChar >= ' ' ) && ( ucRxedChar <= '~' ) )\r
252                                         {\r
253                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
254                                                 {\r
255                                                         cInputString[ ucInputIndex ] = ucRxedChar;\r
256                                                         ucInputIndex++;\r
257                                                 }\r
258                                         }\r
259                                 }\r
260                         }\r
261                 }\r
262         }\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 static void prvSendBuffer( struct usart_module *pxCDCUsart, uint8_t * pcBuffer, size_t xBufferLength )\r
267 {\r
268 const portTickType xBlockMax100ms = 100UL / portTICK_RATE_MS;\r
269 \r
270         if( xBufferLength > 0 )\r
271         {               \r
272                 usart_write_buffer_job( pxCDCUsart, pcBuffer, xBufferLength );\r
273                 \r
274                 /* Wait for the Tx to complete so the buffer can be reused without\r
275                 corrupting the data that is being sent. */\r
276                 xSemaphoreTake( xTxCompleteSemaphore, xBlockMax100ms );\r
277         }\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 static void prvConfigureUART( struct usart_module *pxCDCUsart )\r
282 {\r
283 struct usart_config xUARTConfig;\r
284 \r
285         /* This semaphore is used to allow the task to wait for the Tx to complete\r
286         without wasting any CPU time. */\r
287         vSemaphoreCreateBinary( xTxCompleteSemaphore );\r
288         configASSERT( xTxCompleteSemaphore );\r
289         \r
290         /* This semaphore is used to allow the task to block for an Rx to complete\r
291         without wasting any CPU time. */\r
292         vSemaphoreCreateBinary( xRxCompleteSemaphore );\r
293         configASSERT( xRxCompleteSemaphore );\r
294 \r
295         /* Take the semaphores so they start in the wanted state.  A block time is\r
296         not necessary, and is therefore set to 0, as it is known that the semaphores\r
297         exists - they have just been created. */\r
298         xSemaphoreTake( xTxCompleteSemaphore, 0 );\r
299         xSemaphoreTake( xRxCompleteSemaphore, 0 );\r
300 \r
301         /* Configure the hardware. */\r
302         usart_get_config_defaults( &xUARTConfig );\r
303         xUARTConfig.baudrate    = 115200;\r
304         xUARTConfig.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;\r
305         xUARTConfig.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;\r
306         xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;\r
307         xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;\r
308         xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;\r
309         while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK ) \r
310         {\r
311                 /* Nothing to do here.  Should include a timeout really but this is\r
312                 init code only. */\r
313         }\r
314         usart_enable( pxCDCUsart );\r
315         \r
316         /* Register the driver callbacks. */\r
317         usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );\r
318         usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );\r
319         usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_TRANSMITTED );\r
320         usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_RECEIVED );\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )\r
325 {\r
326 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
327 \r
328         /* Remove compiler warnings. */\r
329         ( void ) pxUSART;\r
330 \r
331         /* Give the semaphore  to unblock any tasks that might be waiting for an Rx\r
332         to complete.  If a task is unblocked, and the unblocked task has a priority\r
333         above the currently running task, then xHigherPriorityTaskWoken will be set\r
334         to pdTRUE inside the xSemaphoreGiveFromISR() function. */\r
335         xSemaphoreGiveFromISR( xRxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
336 \r
337         /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
338         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
339 }\r
340 /*-----------------------------------------------------------*/\r
341 \r
342 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )\r
343 {\r
344 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
345 \r
346         /* Remove compiler warnings. */\r
347         ( void ) pxUSART;\r
348 \r
349         /* Give the semaphore  to unblock any tasks that might be waiting for a Tx\r
350         to complete.  If a task is unblocked, and the unblocked task has a priority\r
351         above the currently running task, then xHigherPriorityTaskWoken will be set\r
352         to pdTRUE inside the xSemaphoreGiveFromISR() function. */\r
353         xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
354 \r
355         /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
356         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
357 }\r
358 \r
359 \r