2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Standard includes. */
\r
33 /* FreeRTOS includes. */
\r
34 #include "FreeRTOS.h"
\r
39 /* Library includes. */
\r
42 /* Example includes. */
\r
43 #include "FreeRTOS_CLI.h"
\r
44 #include "UARTCommandConsole.h"
\r
46 /* Dimensions the buffer into which input characters are placed. */
\r
47 #define cmdMAX_INPUT_SIZE 50
\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
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
57 /* DEL acts as a backspace. */
\r
58 #define cmdASCII_DEL ( 0x7F )
\r
60 /*-----------------------------------------------------------*/
\r
63 * The task that implements the command console processing.
\r
65 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
68 * Ensure a previous interrupt driven Tx has completed before sending the next
\r
69 * data block to the UART.
\r
71 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength );
\r
74 * Register the 'standard' sample CLI commands with FreeRTOS+CLI.
\r
76 extern void vRegisterSampleCLICommands( void );
\r
79 * Configure the UART used for IO.and register prvUARTRxNotificationHandler()
\r
80 * to handle UART Rx events.
\r
82 static void prvConfigureUART( struct usart_module *pxCDCUsart );
\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
89 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );
\r
90 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );
\r
92 /*-----------------------------------------------------------*/
\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
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
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
107 /*-----------------------------------------------------------*/
\r
109 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )
\r
111 vRegisterSampleCLICommands();
\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
121 /*-----------------------------------------------------------*/
\r
123 static void prvUARTCommandConsoleTask( void *pvParameters )
\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
131 ( void ) pvParameters;
\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
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
143 /* Send the welcome message. */
\r
144 prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\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
153 /* Echo the character back. */
\r
154 prvSendBuffer( &xCDCUsart, &cRxedChar, sizeof( cRxedChar ) );
\r
156 /* Was it the end of the line? */
\r
157 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
159 /* Just to space the output from the input. */
\r
160 prvSendBuffer( &xCDCUsart, pcNewLine, strlen( pcNewLine ) );
\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
166 /* Copy the last command back into the input string. */
\r
167 strcpy( cInputString, cLastInputString );
\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
176 /* Get the next output string from the command interpreter. */
\r
177 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
179 /* Write the generated string to the UART. */
\r
180 prvSendBuffer( &xCDCUsart, pcOutputString, strlen( pcOutputString ) );
\r
182 } while( xReturned != pdFALSE );
\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
190 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
192 prvSendBuffer( &xCDCUsart, pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
196 if( cRxedChar == '\r' )
\r
198 /* Ignore the character. */
\r
200 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )
\r
202 /* Backspace was pressed. Erase the last character in the
\r
203 string - if any. */
\r
204 if( ucInputIndex > 0 )
\r
207 cInputString[ ucInputIndex ] = '\0';
\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
217 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
219 cInputString[ ucInputIndex ] = cRxedChar;
\r
228 /*-----------------------------------------------------------*/
\r
230 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength )
\r
232 const TickType_t xBlockMax100ms = 100UL / portTICK_PERIOD_MS;
\r
234 if( xBufferLength > 0 )
\r
236 usart_write_buffer_job( pxCDCUsart, ( uint8_t * ) pcBuffer, xBufferLength );
\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
243 /*-----------------------------------------------------------*/
\r
245 static void prvConfigureUART( struct usart_module *pxCDCUsart )
\r
247 struct usart_config xUARTConfig;
\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
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
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
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
275 /* Nothing to do here. Should include a timeout really but this is
\r
278 usart_enable( pxCDCUsart );
\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
286 /*-----------------------------------------------------------*/
\r
288 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )
\r
290 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
292 /* Remove compiler warnings. */
\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
301 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
302 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
304 /*-----------------------------------------------------------*/
\r
306 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )
\r
308 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
310 /* Remove compiler warnings. */
\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
319 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
320 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r