2 FreeRTOS V7.6.0 - Copyright (C) 2013 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 distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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_RATE_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, uint8_t * 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 uint8_t * const pcWelcomeMessage = ( uint8_t * ) "\r\n\r\nFreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";
\r
133 static const uint8_t * const pcEndOfOutputMessage = ( uint8_t * ) "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
134 static const uint8_t * const pcNewLine = ( uint8_t * ) "\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 xSemaphoreHandle 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 xSemaphoreHandle 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 uint8_t ucRxedChar, ucInputIndex = 0, *pucOutputString;
\r
163 static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
164 portBASE_TYPE xReturned;
\r
165 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */
\r
167 ( void ) pvParameters;
\r
169 /* A UART is used for printf() output and CLI input and output. Note there
\r
170 is no mutual exclusion on the UART, but the demo as it stands does not
\r
171 require mutual exclusion. */
\r
172 prvConfigureUART( &xCDCUsart );
\r
174 /* Obtain the address of the output buffer. Note there is no mutual
\r
175 exclusion on this buffer as it is assumed only one command console
\r
176 interface will be used at any one time. */
\r
177 pucOutputString = ( uint8_t * ) FreeRTOS_CLIGetOutputBuffer();
\r
179 /* Send the welcome message. */
\r
180 prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) );
\r
184 /* Wait for the next character to arrive. A semaphore is used to
\r
185 ensure no CPU time is used until data has arrived. */
\r
186 usart_read_buffer_job( &xCDCUsart, &ucRxedChar, sizeof( ucRxedChar ) );
\r
187 if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )
\r
189 /* Echo the character back. */
\r
190 prvSendBuffer( &xCDCUsart, ( uint8_t * ) &ucRxedChar, sizeof( ucRxedChar ) );
\r
192 /* Was it the end of the line? */
\r
193 if( ucRxedChar == '\n' || ucRxedChar == '\r' )
\r
195 /* Just to space the output from the input. */
\r
196 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcNewLine, strlen( ( char * ) pcNewLine ) );
\r
198 /* See if the command is empty, indicating that the last command is
\r
199 to be executed again. */
\r
200 if( ucInputIndex == 0 )
\r
202 /* Copy the last command back into the input string. */
\r
203 strcpy( ( char * ) cInputString, ( char * ) cLastInputString );
\r
206 /* Pass the received command to the command interpreter. The
\r
207 command interpreter is called repeatedly until it returns pdFALSE
\r
208 (indicating there is no more output) as it might generate more than
\r
212 /* Get the next output string from the command interpreter. */
\r
213 xReturned = FreeRTOS_CLIProcessCommand( cInputString, ( int8_t * ) pucOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
215 /* Write the generated string to the UART. */
\r
216 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pucOutputString, strlen( ( char * ) pucOutputString ) );
\r
218 } while( xReturned != pdFALSE );
\r
220 /* All the strings generated by the input command have been sent.
\r
221 Clear the input string ready to receive the next command. Remember
\r
222 the command that was just processed first in case it is to be
\r
223 processed again. */
\r
224 strcpy( ( char * ) cLastInputString, ( char * ) cInputString );
\r
226 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
228 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) );
\r
232 if( ucRxedChar == '\r' )
\r
234 /* Ignore the character. */
\r
236 else if( ( ucRxedChar == '\b' ) || ( ucRxedChar == cmdASCII_DEL ) )
\r
238 /* Backspace was pressed. Erase the last character in the
\r
239 string - if any. */
\r
240 if( ucInputIndex > 0 )
\r
243 cInputString[ ucInputIndex ] = '\0';
\r
248 /* A character was entered. Add it to the string
\r
249 entered so far. When a \n is entered the complete
\r
250 string will be passed to the command interpreter. */
\r
251 if( ( ucRxedChar >= ' ' ) && ( ucRxedChar <= '~' ) )
\r
253 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
255 cInputString[ ucInputIndex ] = ucRxedChar;
\r
264 /*-----------------------------------------------------------*/
\r
266 static void prvSendBuffer( struct usart_module *pxCDCUsart, uint8_t * pcBuffer, size_t xBufferLength )
\r
268 const portTickType xBlockMax100ms = 100UL / portTICK_RATE_MS;
\r
270 if( xBufferLength > 0 )
\r
272 usart_write_buffer_job( pxCDCUsart, pcBuffer, xBufferLength );
\r
274 /* Wait for the Tx to complete so the buffer can be reused without
\r
275 corrupting the data that is being sent. */
\r
276 xSemaphoreTake( xTxCompleteSemaphore, xBlockMax100ms );
\r
279 /*-----------------------------------------------------------*/
\r
281 static void prvConfigureUART( struct usart_module *pxCDCUsart )
\r
283 struct usart_config xUARTConfig;
\r
285 /* This semaphore is used to allow the task to wait for the Tx to complete
\r
286 without wasting any CPU time. */
\r
287 vSemaphoreCreateBinary( xTxCompleteSemaphore );
\r
288 configASSERT( xTxCompleteSemaphore );
\r
290 /* This semaphore is used to allow the task to block for an Rx to complete
\r
291 without wasting any CPU time. */
\r
292 vSemaphoreCreateBinary( xRxCompleteSemaphore );
\r
293 configASSERT( xRxCompleteSemaphore );
\r
295 /* Take the semaphores so they start in the wanted state. A block time is
\r
296 not necessary, and is therefore set to 0, as it is known that the semaphores
\r
297 exists - they have just been created. */
\r
298 xSemaphoreTake( xTxCompleteSemaphore, 0 );
\r
299 xSemaphoreTake( xRxCompleteSemaphore, 0 );
\r
301 /* Configure the hardware. */
\r
302 usart_get_config_defaults( &xUARTConfig );
\r
303 xUARTConfig.baudrate = 115200;
\r
304 xUARTConfig.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
\r
305 xUARTConfig.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
\r
306 xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
\r
307 xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
\r
308 xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
\r
309 while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK )
\r
311 /* Nothing to do here. Should include a timeout really but this is
\r
314 usart_enable( pxCDCUsart );
\r
316 /* Register the driver callbacks. */
\r
317 usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );
\r
318 usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );
\r
319 usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_TRANSMITTED );
\r
320 usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_RECEIVED );
\r
322 /*-----------------------------------------------------------*/
\r
324 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )
\r
326 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
328 /* Remove compiler warnings. */
\r
331 /* Give the semaphore to unblock any tasks that might be waiting for an Rx
\r
332 to complete. If a task is unblocked, and the unblocked task has a priority
\r
333 above the currently running task, then xHigherPriorityTaskWoken will be set
\r
334 to pdTRUE inside the xSemaphoreGiveFromISR() function. */
\r
335 xSemaphoreGiveFromISR( xRxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
337 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
338 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
340 /*-----------------------------------------------------------*/
\r
342 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )
\r
344 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
346 /* Remove compiler warnings. */
\r
349 /* Give the semaphore to unblock any tasks that might be waiting for a Tx
\r
350 to complete. If a task is unblocked, and the unblocked task has a priority
\r
351 above the currently running task, then xHigherPriorityTaskWoken will be set
\r
352 to pdTRUE inside the xSemaphoreGiveFromISR() function. */
\r
353 xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
355 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
356 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r