2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* Standard includes. */
\r
70 /* FreeRTOS includes. */
\r
71 #include "FreeRTOS.h"
\r
76 /* Library includes. */
\r
79 /* Example includes. */
\r
80 #include "FreeRTOS_CLI.h"
\r
81 #include "UARTCommandConsole.h"
\r
83 /* Dimensions the buffer into which input characters are placed. */
\r
84 #define cmdMAX_INPUT_SIZE 50
\r
86 /* The maximum time in ticks to wait for the UART access mutex. */
\r
87 #define cmdMAX_MUTEX_WAIT ( 200 / portTICK_PERIOD_MS )
\r
89 /* Characters are only ever received slowly on the CLI so it is ok to pass
\r
90 received characters from the UART interrupt to the task on a queue. This sets
\r
91 the length of the queue used for that purpose. */
\r
92 #define cmdRXED_CHARS_QUEUE_LENGTH ( 10 )
\r
94 /* DEL acts as a backspace. */
\r
95 #define cmdASCII_DEL ( 0x7F )
\r
97 /*-----------------------------------------------------------*/
\r
100 * The task that implements the command console processing.
\r
102 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
105 * Ensure a previous interrupt driven Tx has completed before sending the next
\r
106 * data block to the UART.
\r
108 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength );
\r
111 * Register the 'standard' sample CLI commands with FreeRTOS+CLI.
\r
113 extern void vRegisterSampleCLICommands( void );
\r
116 * Configure the UART used for IO.and register prvUARTRxNotificationHandler()
\r
117 * to handle UART Rx events.
\r
119 static void prvConfigureUART( struct usart_module *pxCDCUsart );
\r
122 * Callback functions registered with the Atmel UART driver. Both functions
\r
123 * just 'give' a semaphore to unblock a task that may be waiting for a
\r
124 * character to be received, or a transmission to complete.
\r
126 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );
\r
127 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );
\r
129 /*-----------------------------------------------------------*/
\r
131 /* Const messages output by the command console. */
\r
132 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
133 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
134 static const char * const pcNewLine = "\r\n";
\r
136 /* This semaphore is used to allow the task to wait for a Tx to complete
\r
137 without wasting any CPU time. */
\r
138 static SemaphoreHandle_t xTxCompleteSemaphore = NULL;
\r
140 /* This semaphore is sued to allow the task to wait for an Rx to complete
\r
141 without wasting any CPU time. */
\r
142 static SemaphoreHandle_t xRxCompleteSemaphore = NULL;
\r
144 /*-----------------------------------------------------------*/
\r
146 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )
\r
148 vRegisterSampleCLICommands();
\r
150 /* Create that task that handles the console itself. */
\r
151 xTaskCreate( prvUARTCommandConsoleTask, /* The task that implements the command console. */
\r
152 "CLI", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
153 usStackSize, /* The size of the stack allocated to the task. */
\r
154 NULL, /* The parameter is not used, so NULL is passed. */
\r
155 uxPriority, /* The priority allocated to the task. */
\r
156 NULL ); /* A handle is not required, so just pass NULL. */
\r
158 /*-----------------------------------------------------------*/
\r
160 static void prvUARTCommandConsoleTask( void *pvParameters )
\r
162 char cRxedChar, *pcOutputString;
\r
163 uint8_t ucInputIndex = 0;
\r
164 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
165 portBASE_TYPE xReturned;
\r
166 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */
\r
168 ( void ) pvParameters;
\r
170 /* A UART is used for printf() output and CLI input and output. Note there
\r
171 is no mutual exclusion on the UART, but the demo as it stands does not
\r
172 require mutual exclusion. */
\r
173 prvConfigureUART( &xCDCUsart );
\r
175 /* Obtain the address of the output buffer. Note there is no mutual
\r
176 exclusion on this buffer as it is assumed only one command console
\r
177 interface will be used at any one time. */
\r
178 pcOutputString = FreeRTOS_CLIGetOutputBuffer();
\r
180 /* Send the welcome message. */
\r
181 prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( pcWelcomeMessage ) );
\r
185 /* Wait for the next character to arrive. A semaphore is used to
\r
186 ensure no CPU time is used until data has arrived. */
\r
187 usart_read_buffer_job( &xCDCUsart, ( uint8_t * ) &cRxedChar, sizeof( cRxedChar ) );
\r
188 if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )
\r
190 /* Echo the character back. */
\r
191 prvSendBuffer( &xCDCUsart, &cRxedChar, sizeof( cRxedChar ) );
\r
193 /* Was it the end of the line? */
\r
194 if( cRxedChar == '\n' || cRxedChar == '\r' )
\r
196 /* Just to space the output from the input. */
\r
197 prvSendBuffer( &xCDCUsart, pcNewLine, strlen( pcNewLine ) );
\r
199 /* See if the command is empty, indicating that the last command is
\r
200 to be executed again. */
\r
201 if( ucInputIndex == 0 )
\r
203 /* Copy the last command back into the input string. */
\r
204 strcpy( cInputString, cLastInputString );
\r
207 /* Pass the received command to the command interpreter. The
\r
208 command interpreter is called repeatedly until it returns pdFALSE
\r
209 (indicating there is no more output) as it might generate more than
\r
213 /* Get the next output string from the command interpreter. */
\r
214 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
216 /* Write the generated string to the UART. */
\r
217 prvSendBuffer( &xCDCUsart, pcOutputString, strlen( pcOutputString ) );
\r
219 } while( xReturned != pdFALSE );
\r
221 /* All the strings generated by the input command have been sent.
\r
222 Clear the input string ready to receive the next command. Remember
\r
223 the command that was just processed first in case it is to be
\r
224 processed again. */
\r
225 strcpy( cLastInputString, cInputString );
\r
227 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
229 prvSendBuffer( &xCDCUsart, pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
\r
233 if( cRxedChar == '\r' )
\r
235 /* Ignore the character. */
\r
237 else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )
\r
239 /* Backspace was pressed. Erase the last character in the
\r
240 string - if any. */
\r
241 if( ucInputIndex > 0 )
\r
244 cInputString[ ucInputIndex ] = '\0';
\r
249 /* A character was entered. Add it to the string
\r
250 entered so far. When a \n is entered the complete
\r
251 string will be passed to the command interpreter. */
\r
252 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
\r
254 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
256 cInputString[ ucInputIndex ] = cRxedChar;
\r
265 /*-----------------------------------------------------------*/
\r
267 static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength )
\r
269 const TickType_t xBlockMax100ms = 100UL / portTICK_PERIOD_MS;
\r
271 if( xBufferLength > 0 )
\r
273 usart_write_buffer_job( pxCDCUsart, ( uint8_t * ) pcBuffer, xBufferLength );
\r
275 /* Wait for the Tx to complete so the buffer can be reused without
\r
276 corrupting the data that is being sent. */
\r
277 xSemaphoreTake( xTxCompleteSemaphore, xBlockMax100ms );
\r
280 /*-----------------------------------------------------------*/
\r
282 static void prvConfigureUART( struct usart_module *pxCDCUsart )
\r
284 struct usart_config xUARTConfig;
\r
286 /* This semaphore is used to allow the task to wait for the Tx to complete
\r
287 without wasting any CPU time. */
\r
288 vSemaphoreCreateBinary( xTxCompleteSemaphore );
\r
289 configASSERT( xTxCompleteSemaphore );
\r
291 /* This semaphore is used to allow the task to block for an Rx to complete
\r
292 without wasting any CPU time. */
\r
293 vSemaphoreCreateBinary( xRxCompleteSemaphore );
\r
294 configASSERT( xRxCompleteSemaphore );
\r
296 /* Take the semaphores so they start in the wanted state. A block time is
\r
297 not necessary, and is therefore set to 0, as it is known that the semaphores
\r
298 exists - they have just been created. */
\r
299 xSemaphoreTake( xTxCompleteSemaphore, 0 );
\r
300 xSemaphoreTake( xRxCompleteSemaphore, 0 );
\r
302 /* Configure the hardware. */
\r
303 usart_get_config_defaults( &xUARTConfig );
\r
304 xUARTConfig.baudrate = 115200;
\r
305 xUARTConfig.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
\r
306 xUARTConfig.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
\r
307 xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
\r
308 xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
\r
309 xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
\r
310 while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK )
\r
312 /* Nothing to do here. Should include a timeout really but this is
\r
315 usart_enable( pxCDCUsart );
\r
317 /* Register the driver callbacks. */
\r
318 usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );
\r
319 usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );
\r
320 usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_TRANSMITTED );
\r
321 usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_RECEIVED );
\r
323 /*-----------------------------------------------------------*/
\r
325 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )
\r
327 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
329 /* Remove compiler warnings. */
\r
332 /* Give the semaphore to unblock any tasks that might be waiting for an Rx
\r
333 to complete. If a task is unblocked, and the unblocked task has a priority
\r
334 above the currently running task, then xHigherPriorityTaskWoken will be set
\r
335 to pdTRUE inside the xSemaphoreGiveFromISR() function. */
\r
336 xSemaphoreGiveFromISR( xRxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
338 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
339 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
341 /*-----------------------------------------------------------*/
\r
343 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )
\r
345 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
347 /* Remove compiler warnings. */
\r
350 /* Give the semaphore to unblock any tasks that might be waiting for a Tx
\r
351 to complete. If a task is unblocked, and the unblocked task has a priority
\r
352 above the currently running task, then xHigherPriorityTaskWoken will be set
\r
353 to pdTRUE inside the xSemaphoreGiveFromISR() function. */
\r
354 xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
356 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
357 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r