2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard includes. */
\r
32 /* FreeRTOS includes. */
\r
33 #include "FreeRTOS.h"
\r
38 /* Library includes. */
\r
41 /* Example includes. */
\r
42 #include "FreeRTOS_CLI.h"
\r
43 #include "UARTCommandConsole.h"
\r
45 /* Dimensions the buffer into which input characters are placed. */
\r
46 #define cmdMAX_INPUT_SIZE 50
\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
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
56 /* DEL acts as a backspace. */
\r
57 #define cmdASCII_DEL ( 0x7F )
\r
59 /*-----------------------------------------------------------*/
\r
62 * The task that implements the command console processing.
\r
64 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
67 * Ensure a previous interrupt driven Tx has completed before sending the next
\r
68 * data block to the UART.
\r
70 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength );
\r
73 * Register the 'standard' sample CLI commands with FreeRTOS+CLI.
\r
75 extern void vRegisterSampleCLICommands( void );
\r
78 * Configure the UART used for IO.and register prvUARTRxNotificationHandler()
\r
79 * to handle UART Rx events.
\r
81 static void prvConfigureUART( struct usart_module *pxCDCUsart );
\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
88 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );
\r
89 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );
\r
91 /*-----------------------------------------------------------*/
\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
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
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
106 /*-----------------------------------------------------------*/
\r
108 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )
\r
110 vRegisterSampleCLICommands();
\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
120 /*-----------------------------------------------------------*/
\r
122 static void prvUARTCommandConsoleTask( void *pvParameters )
\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
130 ( void ) pvParameters;
\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
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
142 /* Send the welcome message. */
\r
143 prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\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
152 /* Echo the character back. */
\r
153 prvSendBuffer( &xCDCUsart, &cRxedChar, sizeof( cRxedChar ) );
\r
155 /* Was it the end of the line? */
\r
156 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
158 /* Just to space the output from the input. */
\r
159 prvSendBuffer( &xCDCUsart, pcNewLine, strlen( pcNewLine ) );
\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
165 /* Copy the last command back into the input string. */
\r
166 strcpy( cInputString, cLastInputString );
\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
175 /* Get the next output string from the command interpreter. */
\r
176 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
178 /* Write the generated string to the UART. */
\r
179 prvSendBuffer( &xCDCUsart, pcOutputString, strlen( pcOutputString ) );
\r
181 } while( xReturned != pdFALSE );
\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
189 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
191 prvSendBuffer( &xCDCUsart, pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
195 if( cRxedChar == '\r' )
\r
197 /* Ignore the character. */
\r
199 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )
\r
201 /* Backspace was pressed. Erase the last character in the
\r
202 string - if any. */
\r
203 if( ucInputIndex > 0 )
\r
206 cInputString[ ucInputIndex ] = '\0';
\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
216 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
218 cInputString[ ucInputIndex ] = cRxedChar;
\r
227 /*-----------------------------------------------------------*/
\r
229 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength )
\r
231 const TickType_t xBlockMax100ms = 100UL / portTICK_PERIOD_MS;
\r
233 if( xBufferLength > 0 )
\r
235 usart_write_buffer_job( pxCDCUsart, ( uint8_t * ) pcBuffer, xBufferLength );
\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
242 /*-----------------------------------------------------------*/
\r
244 static void prvConfigureUART( struct usart_module *pxCDCUsart )
\r
246 struct usart_config xUARTConfig;
\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
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
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
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
274 /* Nothing to do here. Should include a timeout really but this is
\r
277 usart_enable( pxCDCUsart );
\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
285 /*-----------------------------------------------------------*/
\r
287 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )
\r
289 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
291 /* Remove compiler warnings. */
\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
300 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
301 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
303 /*-----------------------------------------------------------*/
\r
305 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )
\r
307 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
309 /* Remove compiler warnings. */
\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
318 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
319 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r