]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/UARTCommandConsole.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / UARTCommandConsole.c
index 9bbdca2ecac1af535d98fcd3f8da394ba21ef9a6..ffafd336599759614a801739ec8cb223041372b8 100644 (file)
@@ -1,66 +1,29 @@
 /*\r
-    FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
-\r
-    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
-\r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    FreeRTOS provides completely free yet professionally developed,    *\r
-     *    robust, strictly quality controlled, supported, and cross          *\r
-     *    platform software that has become a de facto standard.             *\r
-     *                                                                       *\r
-     *    Help yourself get started quickly and support the FreeRTOS         *\r
-     *    project by purchasing a FreeRTOS tutorial book, reference          *\r
-     *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
-     *                                                                       *\r
-     *    Thank you!                                                         *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
-    This file is part of the FreeRTOS distribution.\r
-\r
-    FreeRTOS is free software; you can redistribute it and/or modify it under\r
-    the terms of the GNU General Public License (version 2) as published by the\r
-    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
-\r
-    >>! NOTE: The modification to the GPL is included to allow you to distribute\r
-    >>! a combined work that includes FreeRTOS without being obliged to provide\r
-    >>! the source code for proprietary components outside of the FreeRTOS\r
-    >>! kernel.\r
-\r
-    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
-    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
-    FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
-    link: http://www.freertos.org/a00114.html\r
-\r
-    1 tab == 4 spaces!\r
-\r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    Having a problem?  Start by reading the FAQ "My application does   *\r
-     *    not run, what could be wrong?"                                     *\r
-     *                                                                       *\r
-     *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
-    http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
-    license and Real Time Engineers Ltd. contact details.\r
-\r
-    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
-    including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
-    compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
-\r
-    http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
-    Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
-    licenses offer ticketed support, indemnification and middleware.\r
-\r
-    http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
-    engineered and independently SIL3 certified version for use in safety and\r
-    mission critical applications that require provable dependability.\r
-\r
-    1 tab == 4 spaces!\r
-*/\r
+ * FreeRTOS Kernel V10.3.0\r
+ * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://www.FreeRTOS.org\r
+ * http://aws.amazon.com/freertos\r
+ *\r
+ * 1 tab == 4 spaces!\r
+ */\r
 \r
 /* Standard includes. */\r
 #include "string.h"\r
@@ -83,7 +46,7 @@
 #define cmdMAX_INPUT_SIZE              50\r
 \r
 /* The maximum time in ticks to wait for the UART access mutex. */\r
-#define cmdMAX_MUTEX_WAIT              ( 200 / portTICK_RATE_MS )\r
+#define cmdMAX_MUTEX_WAIT              ( 200 / portTICK_PERIOD_MS )\r
 \r
 /* Characters are only ever received slowly on the CLI so it is ok to pass\r
 received characters from the UART interrupt to the task on a queue.  This sets\r
@@ -102,40 +65,53 @@ static void prvUARTCommandConsoleTask( void *pvParameters );
 \r
 /*\r
  * Ensure a previous interrupt driven Tx has completed before sending the next\r
- * data block to the UART.
+ * data block to the UART.\r
+ */\r
+static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength );\r
+\r
+/*\r
+ * Register the 'standard' sample CLI commands with FreeRTOS+CLI.\r
  */\r
-static void prvSendBuffer( struct usart_module *pxCDCUsart, uint8_t * pcBuffer, size_t xBufferLength );\r
+extern void vRegisterSampleCLICommands( void );\r
 \r
 /*\r
- * A UART is used for printf() output and CLI input and output.  Configure the\r
- * UART and register prvUARTRxNotificationHandler() to handle UART Rx events.\r
+ * Configure the UART used for IO.and register prvUARTRxNotificationHandler()\r
+ * to handle UART Rx events.\r
  */\r
 static void prvConfigureUART( struct usart_module *pxCDCUsart );\r
+\r
+/*\r
+ * Callback functions registered with the Atmel UART driver.  Both functions\r
+ * just 'give' a semaphore to unblock a task that may be waiting for a\r
+ * character to be received, or a transmission to complete.\r
+ */\r
 static void prvUARTTxNotificationHandler( const struct usart_module *const pxUSART );\r
 static void prvUARTRxNotificationHandler( const struct usart_module *const pxUSART );\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
 /* Const messages output by the command console. */\r
-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
-static const uint8_t * const pcEndOfOutputMessage = ( uint8_t * ) "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
-static const uint8_t * const pcNewLine = ( uint8_t * ) "\r\n";\r
+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
+static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
+static const char * const pcNewLine = "\r\n";\r
 \r
 /* This semaphore is used to allow the task to wait for a Tx to complete\r
 without wasting any CPU time. */\r
-static xSemaphoreHandle xTxCompleteSemaphore = NULL;\r
+static SemaphoreHandle_t xTxCompleteSemaphore = NULL;\r
 \r
 /* This semaphore is sued to allow the task to wait for an Rx to complete\r
 without wasting any CPU time. */\r
-static xSemaphoreHandle xRxCompleteSemaphore = NULL;\r
+static SemaphoreHandle_t xRxCompleteSemaphore = NULL;\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )\r
 {\r
+       vRegisterSampleCLICommands();\r
+\r
        /* Create that task that handles the console itself. */\r
        xTaskCreate(    prvUARTCommandConsoleTask,                      /* The task that implements the command console. */\r
-                                       ( 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
+                                       "CLI",                                                          /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
                                        usStackSize,                                            /* The size of the stack allocated to the task. */\r
                                        NULL,                                                           /* The parameter is not used, so NULL is passed. */\r
                                        uxPriority,                                                     /* The priority allocated to the task. */\r
@@ -145,8 +121,9 @@ void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPr
 \r
 static void prvUARTCommandConsoleTask( void *pvParameters )\r
 {\r
-uint8_t ucRxedChar, ucInputIndex = 0, *pucOutputString;\r
-static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
+char cRxedChar, *pcOutputString;\r
+uint8_t ucInputIndex = 0;\r
+static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
 portBASE_TYPE xReturned;\r
 static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */\r
 \r
@@ -160,33 +137,33 @@ static struct usart_module xCDCUsart; /* Static so it doesn't take up too much s
        /* Obtain the address of the output buffer.  Note there is no mutual\r
        exclusion on this buffer as it is assumed only one command console\r
        interface will be used at any one time. */\r
-       pucOutputString = ( uint8_t * ) FreeRTOS_CLIGetOutputBuffer();\r
+       pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
 \r
        /* Send the welcome message. */\r
-       prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) );\r
+       prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( pcWelcomeMessage ) );\r
 \r
        for( ;; )\r
        {\r
                /* Wait for the next character to arrive.  A semaphore is used to\r
                ensure no CPU time is used until data has arrived. */\r
-               usart_read_buffer_job( &xCDCUsart, &ucRxedChar, sizeof( ucRxedChar ) );         \r
+               usart_read_buffer_job( &xCDCUsart, ( uint8_t * ) &cRxedChar, sizeof( cRxedChar ) );\r
                if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )\r
                {\r
                        /* Echo the character back. */\r
-                       prvSendBuffer( &xCDCUsart, ( uint8_t * ) &ucRxedChar, sizeof( ucRxedChar ) );\r
+                       prvSendBuffer( &xCDCUsart, &cRxedChar, sizeof( cRxedChar ) );\r
 \r
                        /* Was it the end of the line? */\r
-                       if( ucRxedChar == '\n' || ucRxedChar == '\r' )\r
+                       if( cRxedChar == '\n' || cRxedChar == '\r' )\r
                        {\r
                                /* Just to space the output from the input. */\r
-                               prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcNewLine, strlen( ( char * ) pcNewLine ) );\r
+                               prvSendBuffer( &xCDCUsart, pcNewLine, strlen( pcNewLine ) );\r
 \r
                                /* See if the command is empty, indicating that the last command is\r
                                to be executed again. */\r
                                if( ucInputIndex == 0 )\r
                                {\r
                                        /* Copy the last command back into the input string. */\r
-                                       strcpy( ( char * ) cInputString, ( char * ) cLastInputString );\r
+                                       strcpy( cInputString, cLastInputString );\r
                                }\r
 \r
                                /* Pass the received command to the command interpreter.  The\r
@@ -196,10 +173,10 @@ static struct usart_module xCDCUsart; /* Static so it doesn't take up too much s
                                do\r
                                {\r
                                        /* Get the next output string from the command interpreter. */\r
-                                       xReturned = FreeRTOS_CLIProcessCommand( cInputString, ( int8_t * ) pucOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
+                                       xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
 \r
                                        /* Write the generated string to the UART. */\r
-                                       prvSendBuffer( &xCDCUsart, ( uint8_t * ) pucOutputString, strlen( ( char * ) pucOutputString ) );\r
+                                       prvSendBuffer( &xCDCUsart, pcOutputString, strlen( pcOutputString ) );\r
 \r
                                } while( xReturned != pdFALSE );\r
 \r
@@ -207,19 +184,19 @@ static struct usart_module xCDCUsart; /* Static so it doesn't take up too much s
                                Clear the input string ready to receive the next command.  Remember\r
                                the command that was just processed first in case it is to be\r
                                processed again. */\r
-                               strcpy( ( char * ) cLastInputString, ( char * ) cInputString );\r
+                               strcpy( cLastInputString, cInputString );\r
                                ucInputIndex = 0;\r
                                memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
 \r
-                               prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) );\r
+                               prvSendBuffer( &xCDCUsart, pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );\r
                        }\r
                        else\r
                        {\r
-                               if( ucRxedChar == '\r' )\r
+                               if( cRxedChar == '\r' )\r
                                {\r
                                        /* Ignore the character. */\r
                                }\r
-                               else if( ( ucRxedChar == '\b' ) || ( ucRxedChar == cmdASCII_DEL ) )\r
+                               else if( ( cRxedChar == '\b' ) || ( cRxedChar == cmdASCII_DEL ) )\r
                                {\r
                                        /* Backspace was pressed.  Erase the last character in the\r
                                        string - if any. */\r
@@ -234,11 +211,11 @@ static struct usart_module xCDCUsart; /* Static so it doesn't take up too much s
                                        /* A character was entered.  Add it to the string\r
                                        entered so far.  When a \n is entered the complete\r
                                        string will be passed to the command interpreter. */\r
-                                       if( ( ucRxedChar >= ' ' ) && ( ucRxedChar <= '~' ) )\r
+                                       if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
                                        {\r
                                                if( ucInputIndex < cmdMAX_INPUT_SIZE )\r
                                                {\r
-                                                       cInputString[ ucInputIndex ] = ucRxedChar;\r
+                                                       cInputString[ ucInputIndex ] = cRxedChar;\r
                                                        ucInputIndex++;\r
                                                }\r
                                        }\r
@@ -249,17 +226,17 @@ static struct usart_module xCDCUsart; /* Static so it doesn't take up too much s
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvSendBuffer( struct usart_module *pxCDCUsart, uint8_t * pcBuffer, size_t xBufferLength )\r
+static void prvSendBuffer( struct usart_module *pxCDCUsart, const char * pcBuffer, size_t xBufferLength )\r
 {\r
-const portTickType xBlockMax50ms = 50 / portTICK_RATE_MS;\r
+const TickType_t xBlockMax100ms = 100UL / portTICK_PERIOD_MS;\r
 \r
        if( xBufferLength > 0 )\r
-       {               \r
-               usart_write_buffer_job( pxCDCUsart, pcBuffer, xBufferLength );\r
-               \r
+       {\r
+               usart_write_buffer_job( pxCDCUsart, ( uint8_t * ) pcBuffer, xBufferLength );\r
+\r
                /* Wait for the Tx to complete so the buffer can be reused without\r
                corrupting the data that is being sent. */\r
-               xSemaphoreTake( xTxCompleteSemaphore, xBlockMax50ms );\r
+               xSemaphoreTake( xTxCompleteSemaphore, xBlockMax100ms );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -272,14 +249,14 @@ struct usart_config xUARTConfig;
        without wasting any CPU time. */\r
        vSemaphoreCreateBinary( xTxCompleteSemaphore );\r
        configASSERT( xTxCompleteSemaphore );\r
-       \r
+\r
        /* This semaphore is used to allow the task to block for an Rx to complete\r
        without wasting any CPU time. */\r
        vSemaphoreCreateBinary( xRxCompleteSemaphore );\r
        configASSERT( xRxCompleteSemaphore );\r
 \r
        /* Take the semaphores so they start in the wanted state.  A block time is\r
-       not necessary, and is therefore set to 0, as it is known that the semaphore s\r
+       not necessary, and is therefore set to 0, as it is known that the semaphores\r
        exists - they have just been created. */\r
        xSemaphoreTake( xTxCompleteSemaphore, 0 );\r
        xSemaphoreTake( xRxCompleteSemaphore, 0 );\r
@@ -292,13 +269,13 @@ struct usart_config xUARTConfig;
        xUARTConfig.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;\r
        xUARTConfig.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;\r
        xUARTConfig.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;\r
-       while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK ) \r
+       while( usart_init( pxCDCUsart, EDBG_CDC_MODULE, &xUARTConfig ) != STATUS_OK )\r
        {\r
                /* Nothing to do here.  Should include a timeout really but this is\r
                init code only. */\r
        }\r
        usart_enable( pxCDCUsart );\r
-       \r
+\r
        /* Register the driver callbacks. */\r
        usart_register_callback( pxCDCUsart, prvUARTTxNotificationHandler, USART_CALLBACK_BUFFER_TRANSMITTED );\r
        usart_register_callback( pxCDCUsart, prvUARTRxNotificationHandler, USART_CALLBACK_BUFFER_RECEIVED );\r