2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* Standard includes. */
\r
74 /* FreeRTOS includes. */
\r
75 #include "FreeRTOS.h"
\r
80 /* Library includes. */
\r
83 /* Example includes. */
\r
84 #include "FreeRTOS_CLI.h"
\r
85 #include "UARTCommandConsole.h"
\r
87 /* Dimensions the buffer into which input characters are placed. */
\r
88 #define cmdMAX_INPUT_SIZE 50
\r
90 /* The maximum time in ticks to wait for the UART access mutex. */
\r
91 #define cmdMAX_MUTEX_WAIT ( 200 / portTICK_PERIOD_MS )
\r
93 /* Characters are only ever received slowly on the CLI so it is ok to pass
\r
94 received characters from the UART interrupt to the task on a queue. This sets
\r
95 the length of the queue used for that purpose. */
\r
96 #define cmdRXED_CHARS_QUEUE_LENGTH ( 10 )
\r
98 /* DEL acts as a backspace. */
\r
99 #define cmdASCII_DEL ( 0x7F )
\r
101 /*-----------------------------------------------------------*/
\r
104 * The task that implements the command console processing.
\r
106 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
109 * Ensure a previous interrupt driven Tx has completed before sending the next
\r
110 * data block to the UART.
\r
112 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength );
\r
115 * Register the 'standard' sample CLI commands with FreeRTOS+CLI.
\r
117 extern void vRegisterSampleCLICommands( void );
\r
120 * Configure the UART used for IO.and register prvUARTRxNotificationHandler()
\r
121 * to handle UART Rx events.
\r
123 static void prvConfigureUART( struct usart_module *pxCDCUsart );
\r
126 * Callback functions registered with the Atmel UART driver. Both functions
\r
127 * just 'give' a semaphore to unblock a task that may be waiting for a
\r
128 * character to be received, or a transmission to complete.
\r
130 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );
\r
131 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );
\r
133 /*-----------------------------------------------------------*/
\r
135 /* Const messages output by the command console. */
\r
136 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
137 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
138 static const char * const pcNewLine = "\r\n";
\r
140 /* This semaphore is used to allow the task to wait for a Tx to complete
\r
141 without wasting any CPU time. */
\r
142 static SemaphoreHandle_t xTxCompleteSemaphore = NULL;
\r
144 /* This semaphore is sued to allow the task to wait for an Rx to complete
\r
145 without wasting any CPU time. */
\r
146 static SemaphoreHandle_t xRxCompleteSemaphore = NULL;
\r
148 /*-----------------------------------------------------------*/
\r
150 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )
\r
152 vRegisterSampleCLICommands();
\r
154 /* Create that task that handles the console itself. */
\r
155 xTaskCreate( prvUARTCommandConsoleTask, /* The task that implements the command console. */
\r
156 "CLI", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
157 usStackSize, /* The size of the stack allocated to the task. */
\r
158 NULL, /* The parameter is not used, so NULL is passed. */
\r
159 uxPriority, /* The priority allocated to the task. */
\r
160 NULL ); /* A handle is not required, so just pass NULL. */
\r
162 /*-----------------------------------------------------------*/
\r
164 static void prvUARTCommandConsoleTask( void *pvParameters )
\r
166 char cRxedChar, *pcOutputString;
\r
167 uint8_t ucInputIndex = 0;
\r
168 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
169 portBASE_TYPE xReturned;
\r
170 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */
\r
172 ( void ) pvParameters;
\r
174 /* A UART is used for printf() output and CLI input and output. Note there
\r
175 is no mutual exclusion on the UART, but the demo as it stands does not
\r
176 require mutual exclusion. */
\r
177 prvConfigureUART( &xCDCUsart );
\r
179 /* Obtain the address of the output buffer. Note there is no mutual
\r
180 exclusion on this buffer as it is assumed only one command console
\r
181 interface will be used at any one time. */
\r
182 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
184 /* Send the welcome message. */
\r
185 prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\r
189 /* Wait for the next character to arrive. A semaphore is used to
\r
190 ensure no CPU time is used until data has arrived. */
\r
191 usart_read_buffer_job( &xCDCUsart, ( uint8_t * ) &cRxedChar, sizeof( cRxedChar ) );
\r
192 if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )
\r
194 /* Echo the character back. */
\r
195 prvSendBuffer( &xCDCUsart, &cRxedChar, sizeof( cRxedChar ) );
\r
197 /* Was it the end of the line? */
\r
198 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
200 /* Just to space the output from the input. */
\r
201 prvSendBuffer( &xCDCUsart, pcNewLine, strlen( pcNewLine ) );
\r
203 /* See if the command is empty, indicating that the last command is
\r
204 to be executed again. */
\r
205 if( ucInputIndex == 0 )
\r
207 /* Copy the last command back into the input string. */
\r
208 strcpy( cInputString, cLastInputString );
\r
211 /* Pass the received command to the command interpreter. The
\r
212 command interpreter is called repeatedly until it returns pdFALSE
\r
213 (indicating there is no more output) as it might generate more than
\r
217 /* Get the next output string from the command interpreter. */
\r
218 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
220 /* Write the generated string to the UART. */
\r
221 prvSendBuffer( &xCDCUsart, pcOutputString, strlen( pcOutputString ) );
\r
223 } while( xReturned != pdFALSE );
\r
225 /* All the strings generated by the input command have been sent.
\r
226 Clear the input string ready to receive the next command. Remember
\r
227 the command that was just processed first in case it is to be
\r
228 processed again. */
\r
229 strcpy( cLastInputString, cInputString );
\r
231 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
233 prvSendBuffer( &xCDCUsart, pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
237 if( cRxedChar == '\r' )
\r
239 /* Ignore the character. */
\r
241 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )
\r
243 /* Backspace was pressed. Erase the last character in the
\r
244 string - if any. */
\r
245 if( ucInputIndex > 0 )
\r
248 cInputString[ ucInputIndex ] = '\0';
\r
253 /* A character was entered. Add it to the string
\r
254 entered so far. When a \n is entered the complete
\r
255 string will be passed to the command interpreter. */
\r
256 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
258 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
260 cInputString[ ucInputIndex ] = cRxedChar;
\r
269 /*-----------------------------------------------------------*/
\r
271 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength )
\r
273 const TickType_t xBlockMax100ms = 100UL / portTICK_PERIOD_MS;
\r
275 if( xBufferLength > 0 )
\r
277 usart_write_buffer_job( pxCDCUsart, ( uint8_t * ) pcBuffer, xBufferLength );
\r
279 /* Wait for the Tx to complete so the buffer can be reused without
\r
280 corrupting the data that is being sent. */
\r
281 xSemaphoreTake( xTxCompleteSemaphore, xBlockMax100ms );
\r
284 /*-----------------------------------------------------------*/
\r
286 static void prvConfigureUART( struct usart_module *pxCDCUsart )
\r
288 struct usart_config xUARTConfig;
\r
290 /* This semaphore is used to allow the task to wait for the Tx to complete
\r
291 without wasting any CPU time. */
\r
292 vSemaphoreCreateBinary( xTxCompleteSemaphore );
\r
293 configASSERT( xTxCompleteSemaphore );
\r
295 /* This semaphore is used to allow the task to block for an Rx to complete
\r
296 without wasting any CPU time. */
\r
297 vSemaphoreCreateBinary( xRxCompleteSemaphore );
\r
298 configASSERT( xRxCompleteSemaphore );
\r
300 /* Take the semaphores so they start in the wanted state. A block time is
\r
301 not necessary, and is therefore set to 0, as it is known that the semaphores
\r
302 exists - they have just been created. */
\r
303 xSemaphoreTake( xTxCompleteSemaphore, 0 );
\r
304 xSemaphoreTake( xRxCompleteSemaphore, 0 );
\r
306 /* Configure the hardware. */
\r
307 usart_get_config_defaults( &xUARTConfig );
\r
308 xUARTConfig.baudrate = 115200;
\r
309 xUARTConfig.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
\r
310 xUARTConfig.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
\r
311 xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
\r
312 xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
\r
313 xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
\r
314 while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK )
\r
316 /* Nothing to do here. Should include a timeout really but this is
\r
319 usart_enable( pxCDCUsart );
\r
321 /* Register the driver callbacks. */
\r
322 usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );
\r
323 usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );
\r
324 usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_TRANSMITTED );
\r
325 usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_RECEIVED );
\r
327 /*-----------------------------------------------------------*/
\r
329 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )
\r
331 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
333 /* Remove compiler warnings. */
\r
336 /* Give the semaphore to unblock any tasks that might be waiting for an Rx
\r
337 to complete. If a task is unblocked, and the unblocked task has a priority
\r
338 above the currently running task, then xHigherPriorityTaskWoken will be set
\r
339 to pdTRUE inside the xSemaphoreGiveFromISR() function. */
\r
340 xSemaphoreGiveFromISR( xRxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
342 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
343 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
345 /*-----------------------------------------------------------*/
\r
347 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )
\r
349 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
351 /* Remove compiler warnings. */
\r
354 /* Give the semaphore to unblock any tasks that might be waiting for a Tx
\r
355 to complete. If a task is unblocked, and the unblocked task has a priority
\r
356 above the currently running task, then xHigherPriorityTaskWoken will be set
\r
357 to pdTRUE inside the xSemaphoreGiveFromISR() function. */
\r
358 xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
360 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
361 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r