]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/UARTCommandConsole.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / UARTCommandConsole.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Standard includes. */\r
30 #include "string.h"\r
31 #include "stdio.h"\r
32 \r
33 /* FreeRTOS includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 #include "queue.h"\r
37 #include "semphr.h"\r
38 \r
39 /* Library includes. */\r
40 #include "asf.h"\r
41 \r
42 /* Example includes. */\r
43 #include "FreeRTOS_CLI.h"\r
44 #include "UARTCommandConsole.h"\r
45 \r
46 /* Dimensions the buffer into which input characters are placed. */\r
47 #define cmdMAX_INPUT_SIZE               50\r
48 \r
49 /* The maximum time in ticks to wait for the UART access mutex. */\r
50 #define cmdMAX_MUTEX_WAIT               ( 200 / portTICK_PERIOD_MS )\r
51 \r
52 /* Characters are only ever received slowly on the CLI so it is ok to pass\r
53 received characters from the UART interrupt to the task on a queue.  This sets\r
54 the length of the queue used for that purpose. */\r
55 #define cmdRXED_CHARS_QUEUE_LENGTH                      ( 10 )\r
56 \r
57 /* DEL acts as a backspace. */\r
58 #define cmdASCII_DEL            ( 0x7F )\r
59 \r
60 /*-----------------------------------------------------------*/\r
61 \r
62 /*\r
63  * The task that implements the command console processing.\r
64  */\r
65 static void prvUARTCommandConsoleTask( void *pvParameters );\r
66 \r
67 /*\r
68  * Ensure a previous interrupt driven Tx has completed before sending the next\r
69  * data block to the UART.\r
70  */\r
71 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength );\r
72 \r
73 /*\r
74  * Register the 'standard' sample CLI commands with FreeRTOS+CLI.\r
75  */\r
76 extern void vRegisterSampleCLICommands( void );\r
77 \r
78 /*\r
79  * Configure the UART used for IO.and register prvUARTRxNotificationHandler()\r
80  * to handle UART Rx events.\r
81  */\r
82 static void prvConfigureUART( struct usart_module *pxCDCUsart );\r
83 \r
84 /*\r
85  * Callback functions registered with the Atmel UART driver.  Both functions\r
86  * just 'give' a semaphore to unblock a task that may be waiting for a\r
87  * character to be received, or a transmission to complete.\r
88  */\r
89 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );\r
90 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* Const messages output by the command console. */\r
95 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
96 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
97 static const char * const pcNewLine = "\r\n";\r
98 \r
99 /* This semaphore is used to allow the task to wait for a Tx to complete\r
100 without wasting any CPU time. */\r
101 static SemaphoreHandle_t xTxCompleteSemaphore = NULL;\r
102 \r
103 /* This semaphore is sued to allow the task to wait for an Rx to complete\r
104 without wasting any CPU time. */\r
105 static SemaphoreHandle_t xRxCompleteSemaphore = NULL;\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )\r
110 {\r
111         vRegisterSampleCLICommands();\r
112 \r
113         /* Create that task that handles the console itself. */\r
114         xTaskCreate(    prvUARTCommandConsoleTask,                      /* The task that implements the command console. */\r
115                                         "CLI",                                                          /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
116                                         usStackSize,                                            /* The size of the stack allocated to the task. */\r
117                                         NULL,                                                           /* The parameter is not used, so NULL is passed. */\r
118                                         uxPriority,                                                     /* The priority allocated to the task. */\r
119                                         NULL );                                                         /* A handle is not required, so just pass NULL. */\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 static void prvUARTCommandConsoleTask( void *pvParameters )\r
124 {\r
125 char cRxedChar, *pcOutputString;\r
126 uint8_t ucInputIndex = 0;\r
127 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
128 portBASE_TYPE xReturned;\r
129 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */\r
130 \r
131         ( void ) pvParameters;\r
132 \r
133         /* A UART is used for printf() output and CLI input and output.  Note there\r
134         is no mutual exclusion on the UART, but the demo as it stands does not\r
135         require mutual exclusion. */\r
136         prvConfigureUART( &xCDCUsart );\r
137 \r
138         /* Obtain the address of the output buffer.  Note there is no mutual\r
139         exclusion on this buffer as it is assumed only one command console\r
140         interface will be used at any one time. */\r
141         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
142 \r
143         /* Send the welcome message. */\r
144         prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( pcWelcomeMessage ) );\r
145 \r
146         for( ;; )\r
147         {\r
148                 /* Wait for the next character to arrive.  A semaphore is used to\r
149                 ensure no CPU time is used until data has arrived. */\r
150                 usart_read_buffer_job( &xCDCUsart, ( uint8_t * ) &cRxedChar, sizeof( cRxedChar ) );\r
151                 if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )\r
152                 {\r
153                         /* Echo the character back. */\r
154                         prvSendBuffer( &xCDCUsart, &cRxedChar, sizeof( cRxedChar ) );\r
155 \r
156                         /* Was it the end of the line? */\r
157                         if( cRxedChar == '\n' || cRxedChar == '\r' )\r
158                         {\r
159                                 /* Just to space the output from the input. */\r
160                                 prvSendBuffer( &xCDCUsart, pcNewLine, strlen( pcNewLine ) );\r
161 \r
162                                 /* See if the command is empty, indicating that the last command is\r
163                                 to be executed again. */\r
164                                 if( ucInputIndex == 0 )\r
165                                 {\r
166                                         /* Copy the last command back into the input string. */\r
167                                         strcpy( cInputString, cLastInputString );\r
168                                 }\r
169 \r
170                                 /* Pass the received command to the command interpreter.  The\r
171                                 command interpreter is called repeatedly until it returns pdFALSE\r
172                                 (indicating there is no more output) as it might generate more than\r
173                                 one string. */\r
174                                 do\r
175                                 {\r
176                                         /* Get the next output string from the command interpreter. */\r
177                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
178 \r
179                                         /* Write the generated string to the UART. */\r
180                                         prvSendBuffer( &xCDCUsart, pcOutputString, strlen( pcOutputString ) );\r
181 \r
182                                 } while( xReturned != pdFALSE );\r
183 \r
184                                 /* All the strings generated by the input command have been sent.\r
185                                 Clear the input string ready to receive the next command.  Remember\r
186                                 the command that was just processed first in case it is to be\r
187                                 processed again. */\r
188                                 strcpy( cLastInputString, cInputString );\r
189                                 ucInputIndex = 0;\r
190                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
191 \r
192                                 prvSendBuffer( &xCDCUsart, pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );\r
193                         }\r
194                         else\r
195                         {\r
196                                 if( cRxedChar == '\r' )\r
197                                 {\r
198                                         /* Ignore the character. */\r
199                                 }\r
200                                 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )\r
201                                 {\r
202                                         /* Backspace was pressed.  Erase the last character in the\r
203                                         string - if any. */\r
204                                         if( ucInputIndex > 0 )\r
205                                         {\r
206                                                 ucInputIndex--;\r
207                                                 cInputString[ ucInputIndex ] = '\0';\r
208                                         }\r
209                                 }\r
210                                 else\r
211                                 {\r
212                                         /* A character was entered.  Add it to the string\r
213                                         entered so far.  When a \n is entered the complete\r
214                                         string will be passed to the command interpreter. */\r
215                                         if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
216                                         {\r
217                                                 if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
218                                                 {\r
219                                                         cInputString[ ucInputIndex ] = cRxedChar;\r
220                                                         ucInputIndex++;\r
221                                                 }\r
222                                         }\r
223                                 }\r
224                         }\r
225                 }\r
226         }\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength )\r
231 {\r
232 const TickType_t xBlockMax100ms = 100UL / portTICK_PERIOD_MS;\r
233 \r
234         if( xBufferLength > 0 )\r
235         {\r
236                 usart_write_buffer_job( pxCDCUsart, ( uint8_t * ) pcBuffer, xBufferLength );\r
237 \r
238                 /* Wait for the Tx to complete so the buffer can be reused without\r
239                 corrupting the data that is being sent. */\r
240                 xSemaphoreTake( xTxCompleteSemaphore, xBlockMax100ms );\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvConfigureUART( struct usart_module *pxCDCUsart )\r
246 {\r
247 struct usart_config xUARTConfig;\r
248 \r
249         /* This semaphore is used to allow the task to wait for the Tx to complete\r
250         without wasting any CPU time. */\r
251         vSemaphoreCreateBinary( xTxCompleteSemaphore );\r
252         configASSERT( xTxCompleteSemaphore );\r
253 \r
254         /* This semaphore is used to allow the task to block for an Rx to complete\r
255         without wasting any CPU time. */\r
256         vSemaphoreCreateBinary( xRxCompleteSemaphore );\r
257         configASSERT( xRxCompleteSemaphore );\r
258 \r
259         /* Take the semaphores so they start in the wanted state.  A block time is\r
260         not necessary, and is therefore set to 0, as it is known that the semaphores\r
261         exists - they have just been created. */\r
262         xSemaphoreTake( xTxCompleteSemaphore, 0 );\r
263         xSemaphoreTake( xRxCompleteSemaphore, 0 );\r
264 \r
265         /* Configure the hardware. */\r
266         usart_get_config_defaults( &xUARTConfig );\r
267         xUARTConfig.baudrate    = 115200;\r
268         xUARTConfig.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;\r
269         xUARTConfig.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;\r
270         xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;\r
271         xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;\r
272         xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;\r
273         while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK )\r
274         {\r
275                 /* Nothing to do here.  Should include a timeout really but this is\r
276                 init code only. */\r
277         }\r
278         usart_enable( pxCDCUsart );\r
279 \r
280         /* Register the driver callbacks. */\r
281         usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );\r
282         usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );\r
283         usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_TRANSMITTED );\r
284         usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_RECEIVED );\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )\r
289 {\r
290 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
291 \r
292         /* Remove compiler warnings. */\r
293         ( void ) pxUSART;\r
294 \r
295         /* Give the semaphore  to unblock any tasks that might be waiting for an Rx\r
296         to complete.  If a task is unblocked, and the unblocked task has a priority\r
297         above the currently running task, then xHigherPriorityTaskWoken will be set\r
298         to pdTRUE inside the xSemaphoreGiveFromISR() function. */\r
299         xSemaphoreGiveFromISR( xRxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
300 \r
301         /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
302         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )\r
307 {\r
308 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
309 \r
310         /* Remove compiler warnings. */\r
311         ( void ) pxUSART;\r
312 \r
313         /* Give the semaphore  to unblock any tasks that might be waiting for a Tx\r
314         to complete.  If a task is unblocked, and the unblocked task has a priority\r
315         above the currently running task, then xHigherPriorityTaskWoken will be set\r
316         to pdTRUE inside the xSemaphoreGiveFromISR() function. */\r
317         xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );\r
318 \r
319         /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
320         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
321 }\r
322 \r
323 \r