2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* Standard includes. */
\r
69 /* FreeRTOS includes. */
\r
70 #include "FreeRTOS.h"
\r
75 /* Library includes. */
\r
78 /* Example includes. */
\r
79 #include "FreeRTOS_CLI.h"
\r
80 #include "UARTCommandConsole.h"
\r
82 /* Dimensions the buffer into which input characters are placed. */
\r
83 #define cmdMAX_INPUT_SIZE 50
\r
85 /* The maximum time in ticks to wait for the UART access mutex. */
\r
86 #define cmdMAX_MUTEX_WAIT ( 200 / portTICK_RATE_MS )
\r
88 /* Characters are only ever received slowly on the CLI so it is ok to pass
\r
89 received characters from the UART interrupt to the task on a queue. This sets
\r
90 the length of the queue used for that purpose. */
\r
91 #define cmdRXED_CHARS_QUEUE_LENGTH ( 10 )
\r
93 /* DEL acts as a backspace. */
\r
94 #define cmdASCII_DEL ( 0x7F )
\r
96 /*-----------------------------------------------------------*/
\r
99 * The task that implements the command console processing.
\r
101 static void prvUARTCommandConsoleTask( void *pvParameters );
\r
104 * Ensure a previous interrupt driven Tx has completed before sending the next
\r
105 * data block to the UART.
107 static void prvSendBuffer( struct usart_module *pxCDCUsart, uint8_t * pcBuffer, size_t xBufferLength );
\r
110 * A UART is used for printf() output and CLI input and output. Configure the
\r
111 * UART and register prvUARTRxNotificationHandler() to handle UART Rx events.
\r
113 static void prvConfigureUART( struct usart_module *pxCDCUsart );
\r
114 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );
\r
115 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );
\r
117 /*-----------------------------------------------------------*/
\r
119 /* Const messages output by the command console. */
\r
120 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
121 static const uint8_t * const pcEndOfOutputMessage = ( uint8_t * ) "\r\n[Press ENTER to execute the previous command again]\r\n>";
\r
122 static const uint8_t * const pcNewLine = ( uint8_t * ) "\r\n";
\r
124 /* This semaphore is used to allow the task to wait for a Tx to complete
\r
125 without wasting any CPU time. */
\r
126 static xSemaphoreHandle xTxCompleteSemaphore = NULL;
\r
128 /* This semaphore is sued to allow the task to wait for an Rx to complete
\r
129 without wasting any CPU time. */
\r
130 static xSemaphoreHandle xRxCompleteSemaphore = NULL;
\r
132 /*-----------------------------------------------------------*/
\r
134 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )
\r
136 /* Create that task that handles the console itself. */
\r
137 xTaskCreate( prvUARTCommandConsoleTask, /* The task that implements the command console. */
\r
138 ( const int8_t * const ) "CLI", /* Text name assigned to the task. This is just to assist debugging. The kernel does not use this name itself. */
\r
139 usStackSize, /* The size of the stack allocated to the task. */
\r
140 NULL, /* The parameter is not used, so NULL is passed. */
\r
141 uxPriority, /* The priority allocated to the task. */
\r
142 NULL ); /* A handle is not required, so just pass NULL. */
\r
144 /*-----------------------------------------------------------*/
\r
146 static void prvUARTCommandConsoleTask( void *pvParameters )
\r
148 uint8_t ucRxedChar, ucInputIndex = 0, *pucOutputString;
\r
149 static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
\r
150 portBASE_TYPE xReturned;
\r
151 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */
\r
153 ( void ) pvParameters;
\r
155 /* A UART is used for printf() output and CLI input and output. Note there
\r
156 is no mutual exclusion on the UART, but the demo as it stands does not
\r
157 require mutual exclusion. */
\r
158 prvConfigureUART( &xCDCUsart );
\r
160 /* Obtain the address of the output buffer. Note there is no mutual
\r
161 exclusion on this buffer as it is assumed only one command console
\r
162 interface will be used at any one time. */
\r
163 pucOutputString = ( uint8_t * ) FreeRTOS_CLIGetOutputBuffer();
\r
165 /* Send the welcome message. */
\r
166 prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) );
\r
170 /* Wait for the next character to arrive. A semaphore is used to
\r
171 ensure no CPU time is used until data has arrived. */
\r
172 usart_read_buffer_job( &xCDCUsart, &ucRxedChar, sizeof( ucRxedChar ) );
\r
173 if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )
\r
175 /* Echo the character back. */
\r
176 prvSendBuffer( &xCDCUsart, ( uint8_t * ) &ucRxedChar, sizeof( ucRxedChar ) );
\r
178 /* Was it the end of the line? */
\r
179 if( ucRxedChar == '\n' || ucRxedChar == '\r' )
\r
181 /* Just to space the output from the input. */
\r
182 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcNewLine, strlen( ( char * ) pcNewLine ) );
\r
184 /* See if the command is empty, indicating that the last command is
\r
185 to be executed again. */
\r
186 if( ucInputIndex == 0 )
\r
188 /* Copy the last command back into the input string. */
\r
189 strcpy( ( char * ) cInputString, ( char * ) cLastInputString );
\r
192 /* Pass the received command to the command interpreter. The
\r
193 command interpreter is called repeatedly until it returns pdFALSE
\r
194 (indicating there is no more output) as it might generate more than
\r
198 /* Get the next output string from the command interpreter. */
\r
199 xReturned = FreeRTOS_CLIProcessCommand( cInputString, ( int8_t * ) pucOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
\r
201 /* Write the generated string to the UART. */
\r
202 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pucOutputString, strlen( ( char * ) pucOutputString ) );
\r
204 } while( xReturned != pdFALSE );
\r
206 /* All the strings generated by the input command have been sent.
\r
207 Clear the input string ready to receive the next command. Remember
\r
208 the command that was just processed first in case it is to be
\r
209 processed again. */
\r
210 strcpy( ( char * ) cLastInputString, ( char * ) cInputString );
\r
212 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
\r
214 prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) );
\r
218 if( ucRxedChar == '\r' )
\r
220 /* Ignore the character. */
\r
222 else if( ( ucRxedChar == '\b' ) || ( ucRxedChar == cmdASCII_DEL ) )
\r
224 /* Backspace was pressed. Erase the last character in the
\r
225 string - if any. */
\r
226 if( ucInputIndex > 0 )
\r
229 cInputString[ ucInputIndex ] = '\0';
\r
234 /* A character was entered. Add it to the string
\r
235 entered so far. When a \n is entered the complete
\r
236 string will be passed to the command interpreter. */
\r
237 if( ( ucRxedChar >= ' ' ) && ( ucRxedChar <= '~' ) )
\r
239 if( ucInputIndex < cmdMAX_INPUT_SIZE )
\r
241 cInputString[ ucInputIndex ] = ucRxedChar;
\r
250 /*-----------------------------------------------------------*/
\r
252 static void prvSendBuffer( struct usart_module *pxCDCUsart, uint8_t * pcBuffer, size_t xBufferLength )
\r
254 const portTickType xBlockMax50ms = 50 / portTICK_RATE_MS;
\r
256 if( xBufferLength > 0 )
\r
258 usart_write_buffer_job( pxCDCUsart, pcBuffer, xBufferLength );
\r
260 /* Wait for the Tx to complete so the buffer can be reused without
\r
261 corrupting the data that is being sent. */
\r
262 xSemaphoreTake( xTxCompleteSemaphore, xBlockMax50ms );
\r
265 /*-----------------------------------------------------------*/
\r
267 static void prvConfigureUART( struct usart_module *pxCDCUsart )
\r
269 struct usart_config xUARTConfig;
\r
271 /* This semaphore is used to allow the task to wait for the Tx to complete
\r
272 without wasting any CPU time. */
\r
273 vSemaphoreCreateBinary( xTxCompleteSemaphore );
\r
274 configASSERT( xTxCompleteSemaphore );
\r
276 /* This semaphore is used to allow the task to block for an Rx to complete
\r
277 without wasting any CPU time. */
\r
278 vSemaphoreCreateBinary( xRxCompleteSemaphore );
\r
279 configASSERT( xRxCompleteSemaphore );
\r
281 /* Take the semaphores so they start in the wanted state. A block time is
\r
282 not necessary, and is therefore set to 0, as it is known that the semaphore s
\r
283 exists - they have just been created. */
\r
284 xSemaphoreTake( xTxCompleteSemaphore, 0 );
\r
285 xSemaphoreTake( xRxCompleteSemaphore, 0 );
\r
287 /* Configure the hardware. */
\r
288 usart_get_config_defaults( &xUARTConfig );
\r
289 xUARTConfig.baudrate = 115200;
\r
290 xUARTConfig.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
\r
291 xUARTConfig.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
\r
292 xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
\r
293 xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
\r
294 xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
\r
295 while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK )
\r
297 /* Nothing to do here. Should include a timeout really but this is
\r
300 usart_enable( pxCDCUsart );
\r
302 /* Register the driver callbacks. */
\r
303 usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );
\r
304 usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );
\r
305 usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_TRANSMITTED );
\r
306 usart_enable_callback( pxCDCUsart, USART_CALLBACK_BUFFER_RECEIVED );
\r
308 /*-----------------------------------------------------------*/
\r
310 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART )
\r
312 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
314 /* Remove compiler warnings. */
\r
317 /* Give the semaphore to unblock any tasks that might be waiting for an Rx
\r
318 to complete. If a task is unblocked, and the unblocked task has a priority
\r
319 above the currently running task, then xHigherPriorityTaskWoken will be set
\r
320 to pdTRUE inside the xSemaphoreGiveFromISR() function. */
\r
321 xSemaphoreGiveFromISR( xRxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
323 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
324 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
326 /*-----------------------------------------------------------*/
\r
328 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART )
\r
330 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
332 /* Remove compiler warnings. */
\r
335 /* Give the semaphore to unblock any tasks that might be waiting for a Tx
\r
336 to complete. If a task is unblocked, and the unblocked task has a priority
\r
337 above the currently running task, then xHigherPriorityTaskWoken will be set
\r
338 to pdTRUE inside the xSemaphoreGiveFromISR() function. */
\r
339 xSemaphoreGiveFromISR( xTxCompleteSemaphore, &xHigherPriorityTaskWoken );
\r
341 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */
\r
342 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r