static void prvUARTCommandConsoleTask( void *pvParameters ) { char cRxedChar, *pcOutputString; unsigned char cInputIndex = 0; static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ]; portBASE_TYPE xReturned; ( void ) pvParameters; /* Obtain the address of the output buffer. Note there is no mutual exclusion on this buffer as it is assumed only one command console interface will be used at any one time. */ pcOutputString = FreeRTOS_CLIGetOutputBuffer(); /* Send the welcome message. */ prvSendBuffer( pcWelcomeMessage, strlen( pcWelcomeMessage ) ); for( ;; ) { /* Wait for the next character to arrive. */ if( xQueueReceive( xRxedChars, &cRxedChar, portMAX_DELAY ) == pdPASS ) { /* Echo the character back. */ prvSendBuffer( &cRxedChar, sizeof( cRxedChar ) ); /* Was it the end of the line? */ if( cRxedChar == '\n' || cRxedChar == '\r' ) { /* Just to space the output from the input. */ prvSendBuffer( pcNewLine, strlen( pcNewLine ) ); /* See if the command is empty, indicating that the last command is to be executed again. */ if( cInputIndex == 0 ) { /* Copy the last command back into the input string. */ strcpy( cInputString, cLastInputString ); } /* Pass the received command to the command interpreter. The command interpreter is called repeatedly until it returns pdFALSE (indicating there is no more output) as it might generate more than one string. */ do { /* Get the next output string from the command interpreter. */ xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE ); /* Write the generated string to the UART. */ prvSendBuffer( pcOutputString, strlen( pcOutputString ) ); } while( xReturned != pdFALSE ); /* All the strings generated by the input command have been sent. Clear the input string ready to receive the next command. Remember the command that was just processed first in case it is to be processed again. */ strcpy( cLastInputString, cInputString ); cInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); prvSendBuffer( pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) ); } else { if( cRxedChar == '\r' ) { /* Ignore the character. */ } else if( cRxedChar == '\b' ) { /* Backspace was pressed. Erase the last character in the string - if any. */ if( cInputIndex > 0 ) { cInputIndex--; cInputString[ cInputIndex ] = '\0'; } } else { /* A character was entered. Add it to the string entered so far. When a \n is entered the complete string will be passed to the command interpreter. */ if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) ) { if( cInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ cInputIndex ] = cRxedChar; cInputIndex++; } } } } } } }
static void prvUARTCommandConsoleTask( void *pvParameters ) { uint8_t ucRxedChar, ucInputIndex = 0, *pucOutputString; static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ]; portBASE_TYPE xReturned; static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */ ( void ) pvParameters; /* A UART is used for printf() output and CLI input and output. Note there is no mutual exclusion on the UART, but the demo as it stands does not require mutual exclusion. */ prvConfigureUART( &xCDCUsart ); /* Obtain the address of the output buffer. Note there is no mutual exclusion on this buffer as it is assumed only one command console interface will be used at any one time. */ pucOutputString = ( uint8_t * ) FreeRTOS_CLIGetOutputBuffer(); /* Send the welcome message. */ prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) ); for( ;; ) { /* Wait for the next character to arrive. A semaphore is used to ensure no CPU time is used until data has arrived. */ usart_read_buffer_job( &xCDCUsart, &ucRxedChar, sizeof( ucRxedChar ) ); if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS ) { /* Echo the character back. */ prvSendBuffer( &xCDCUsart, ( uint8_t * ) &ucRxedChar, sizeof( ucRxedChar ) ); /* Was it the end of the line? */ if( ucRxedChar == '\n' || ucRxedChar == '\r' ) { /* Just to space the output from the input. */ prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcNewLine, strlen( ( char * ) pcNewLine ) ); /* See if the command is empty, indicating that the last command is to be executed again. */ if( ucInputIndex == 0 ) { /* Copy the last command back into the input string. */ strcpy( ( char * ) cInputString, ( char * ) cLastInputString ); } /* Pass the received command to the command interpreter. The command interpreter is called repeatedly until it returns pdFALSE (indicating there is no more output) as it might generate more than one string. */ do { /* Get the next output string from the command interpreter. */ xReturned = FreeRTOS_CLIProcessCommand( cInputString, ( int8_t * ) pucOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE ); /* Write the generated string to the UART. */ prvSendBuffer( &xCDCUsart, ( uint8_t * ) pucOutputString, strlen( ( char * ) pucOutputString ) ); } while( xReturned != pdFALSE ); /* All the strings generated by the input command have been sent. Clear the input string ready to receive the next command. Remember the command that was just processed first in case it is to be processed again. */ strcpy( ( char * ) cLastInputString, ( char * ) cInputString ); ucInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) ); } else { if( ucRxedChar == '\r' ) { /* Ignore the character. */ } else if( ( ucRxedChar == '\b' ) || ( ucRxedChar == cmdASCII_DEL ) ) { /* Backspace was pressed. Erase the last character in the string - if any. */ if( ucInputIndex > 0 ) { ucInputIndex--; cInputString[ ucInputIndex ] = '\0'; } } else { /* A character was entered. Add it to the string entered so far. When a \n is entered the complete string will be passed to the command interpreter. */ if( ( ucRxedChar >= ' ' ) && ( ucRxedChar <= '~' ) ) { if( ucInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ ucInputIndex ] = ucRxedChar; ucInputIndex++; } } } } } } }