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