/* Parse entered command */ uint8_t prvParseCommand(char* inputBuffer, char* outputBuffer, uint8_t inputBufferSize, uint8_t outputBufferSize) { BaseType_t xMoreDataToFollow; /* The command interpreter is called repeatedly until it returns pdFALSE. See the "Implementing a command" documentation for an exaplanation of why this is. */ do { /* Send the command string to the command interpreter. Any output generated by the command interpreter will be placed in the pcOutputString buffer. */ xMoreDataToFollow = FreeRTOS_CLIProcessCommand ( inputBuffer, /* The command string.*/ outputBuffer, /* The output buffer. */ outputBufferSize /* The size of the output buffer. */ ); /* Write the output generated by the command interpreter to the console. */ xHardwareUartTx(outputBuffer, strlen(outputBuffer)); } while(xMoreDataToFollow != pdFALSE); /* All the strings generated by the input command have been sent. Processing of the command is complete. Clear the input string ready to receive the next command. */ memset(inputBuffer, 0x00, inputBufferSize); return 0; }
/* * Task that provides the input and output for the FreeRTOS+CLI command * interpreter. In this case a UDP port is used. See the URL in the comments * within main.c for the location of the online documentation. */ void vUDPCommandInterpreterTask( void *pvParameters ) { long lBytes, lByte; signed char cInChar, cInputIndex = 0; static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ]; BaseType_t xMoreDataToFollow; volatile int iErrorCode = 0; struct sockaddr_in xClient; int xClientAddressLength = sizeof( struct sockaddr_in ); SOCKET xSocket; /* Just to prevent compiler warnings. */ ( void ) pvParameters; /* Attempt to open the socket. */ xSocket = prvOpenUDPSocket(); if( xSocket != INVALID_SOCKET ) { for( ;; ) { /* Wait for incoming data on the opened socket. */ lBytes = recvfrom( xSocket, cLocalBuffer, sizeof( cLocalBuffer ), 0, ( struct sockaddr * ) &xClient, &xClientAddressLength ); if( lBytes == SOCKET_ERROR ) { /* Something went wrong, but it is not handled by this simple example. */ iErrorCode = WSAGetLastError(); } else { /* Process each received byte in turn. */ lByte = 0; while( lByte < lBytes ) { /* The next character in the input buffer. */ cInChar = cLocalBuffer[ lByte ]; lByte++; /* Newline characters are taken as the end of the command string. */ if( cInChar == '\n' ) { /* Process the input string received prior to the newline. */ do { /* Pass the string to FreeRTOS+CLI. */ xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE ); /* Send the output generated by the command's implementation. */ sendto( xSocket, cOutputString, strlen( cOutputString ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength ); } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */ /* All the strings generated by the command processing have been sent. Clear the input string ready to receive the next command. */ cInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); /* Transmit a spacer, just to make the command console easier to read. */ sendto( xSocket, "\r\n", strlen( "\r\n" ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength ); } else { if( cInChar == '\r' ) { /* Ignore the character. Newlines are used to detect the end of the input string. */ } else if( cInChar == '\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( cInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ cInputIndex ] = cInChar; cInputIndex++; } } } } } } } else { /* The socket could not be opened. */ vTaskDelete( NULL ); } }
/* * Task that provides the input and output for the FreeRTOS+CLI command * interpreter. In this case a UDP port is used. See the URL in the comments * within main.c for the location of the online documentation. */ void vUDPCommandInterpreterTask( void *pvParameters ) { long lBytes, lByte; signed char cInChar, cInputIndex = 0; static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ]; BaseType_t xMoreDataToFollow; struct freertos_sockaddr xClient; socklen_t xClientAddressLength = 0; /* This is required as a parameter to maintain the sendto() Berkeley sockets API - but it is not actually used so can take any value. */ xSocket_t xSocket; extern const uint8_t ucIPAddress[ 4 ]; extern const uint8_t ucMACAddress[ 6 ]; /* Just to prevent compiler warnings. */ ( void ) pvParameters; /* Attempt to open the socket. The port number is passed in the task parameter. The strange casting is to remove compiler warnings on 32-bit machines. */ xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL ); if( xSocket != FREERTOS_INVALID_SOCKET ) { for( ;; ) { /* Wait for incoming data on the opened socket. */ lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength ); if( lBytes != FREERTOS_SOCKET_ERROR ) { /* Process each received byte in turn. */ lByte = 0; while( lByte < lBytes ) { /* The next character in the input buffer. */ cInChar = cLocalBuffer[ lByte ]; lByte++; /* Newline characters are taken as the end of the command string. */ if( cInChar == '\n' ) { /* Process the input string received prior to the newline. */ do { /* Pass the string to FreeRTOS+CLI. */ xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE ); /* Send the output generated by the command's implementation. */ FreeRTOS_sendto( xSocket, cOutputString, strlen( cOutputString ), 0, &xClient, xClientAddressLength ); } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */ /* All the strings generated by the command processing have been sent. Clear the input string ready to receive the next command. */ cInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); /* Transmit a spacer, just to make the command console easier to read. */ FreeRTOS_sendto( xSocket, "\r\n", strlen( "\r\n" ), 0, &xClient, xClientAddressLength ); } else { if( cInChar == '\r' ) { /* Ignore the character. Newlines are used to detect the end of the input string. */ } else if( cInChar == '\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( cInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ cInputIndex ] = cInChar; cInputIndex++; } } } } } } } else { /* The socket could not be opened. */ vTaskDelete( NULL ); } }
static void prvCDCCommandConsoleTask( void *pvParameters ) { uint8_t ucInputIndex = 0; char *pcOutputString, cRxedChar; static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ]; BaseType_t xReturned; uint32_t ulBufferIndex = 0; ( 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(); /* Initialise the CDC driver. */ prvCDCInit(); /* Start receiving into the buffer. */ prvStartRx(); /* Send the welcome message. */ prvCDCSend( pcWelcomeMessage, strlen( pcWelcomeMessage ) ); for( ;; ) { /* Wait for my characters to be available. */ prvCDCGetChar(); /* Process the bytes char for char on the assumption that as input comes from typing it is unlikely that more than a single byte will be received at a time anyway. */ while( ulBytesAvailable > 0 ) { /* Read next byte from the rx buffer. */ cRxedChar = pcRxBuffer[ ulBufferIndex ]; taskENTER_CRITICAL(); { ulBytesAvailable--; } taskEXIT_CRITICAL(); /* Echo the character back. */ prvCDCSend( &cRxedChar, sizeof( cRxedChar ) ); /* Was it the end of the line? */ if( cRxedChar == '\n' || cRxedChar == '\r' ) { /* Just to space the output from the input. */ prvCDCSend( pcNewLine, strlen( 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( 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. */ prvCDCSend( 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 ); ucInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); prvCDCSend( pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) ); } else { if( cRxedChar == '\r' ) { /* Ignore the character. */ } else if( ( cRxedChar == '\b' ) || ( cRxedChar == 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( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) ) { if( ucInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ ucInputIndex ] = cRxedChar; ucInputIndex++; } } } } /* Move onto the next byte the next time around. */ ulBufferIndex++; if( ulBufferIndex >= cmdMAX_INPUT_SIZE ) { ulBufferIndex = 0; } } } }
void vBasicSocketsCommandInterpreterTask( void *pvParameters ) { long lSocket, lClientFd, lBytes, lAddrLen = sizeof( struct sockaddr_in ), lInputIndex; struct sockaddr_in sLocalAddr; struct sockaddr_in client_addr; const char *pcWelcomeMessage = "FreeRTOS command server - connection accepted.\r\nType Help to view a list of registered commands.\r\n\r\n>"; char cInChar; static char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ]; portBASE_TYPE xReturned; extern void vRegisterSampleCLICommands( void ); ( void ) pvParameters; /* Register the standard CLI commands. */ vRegisterSampleCLICommands(); lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0); if( lSocket >= 0 ) { memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr)); sLocalAddr.sin_family = AF_INET; sLocalAddr.sin_len = sizeof(sLocalAddr); sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY); sLocalAddr.sin_port = ntohs( ( ( unsigned short ) 23 ) ); if( lwip_bind( lSocket, ( struct sockaddr *) &sLocalAddr, sizeof( sLocalAddr ) ) < 0 ) { lwip_close( lSocket ); vTaskDelete( NULL ); } if( lwip_listen( lSocket, 20 ) != 0 ) { lwip_close( lSocket ); vTaskDelete( NULL ); } for( ;; ) { lClientFd = lwip_accept(lSocket, ( struct sockaddr * ) &client_addr, ( u32_t * ) &lAddrLen ); if( lClientFd > 0L ) { lwip_send( lClientFd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 ); lInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); do { lBytes = lwip_recv( lClientFd, &cInChar, sizeof( cInChar ), 0 ); if( lBytes > 0L ) { if( cInChar == '\n' ) { /* The input string has been terminated. Was the input a quit command? */ if( strcmp( "quit", ( const char * ) cInputString ) == 0 ) { /* Set lBytes to 0 to close the connection. */ lBytes = 0L; } else { /* The input string was not a quit command. Pass the string to the command interpreter. */ do { /* Get the next output string from the command interpreter. */ xReturned = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_INPUT_SIZE ); lwip_send( lClientFd, cOutputString, strlen( ( const char * ) cOutputString ), 0 ); } while( xReturned != pdFALSE ); /* All the strings generated by the input command have been sent. Clear the input string ready to receive the next command. */ lInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); lwip_send( lClientFd, "\r\n>", strlen( "\r\n>" ), 0 ); } } else { if( cInChar == '\r' ) { /* Ignore the character. */ } else if( cInChar == '\b' ) { /* Backspace was pressed. Erase the last character in the string - if any. */ if( lInputIndex > 0 ) { lInputIndex--; cInputString[ lInputIndex ] = '\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( lInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ lInputIndex ] = cInChar; lInputIndex++; } } } } } while( lBytes > 0L ); lwip_close( lClientFd ); } } } /* Will only get here if a listening socket could not be created. */ vTaskDelete( NULL ); }
static void prvCDCCommandConsoleTask( void *pvParameters ) { char cRxedChar; uint8_t ucInputIndex = 0; char *pcOutputString; 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(); /* Initialise the virtual com port (CDC) interface. */ prvSetupUSBDrivers(); /* Send the welcome message. This probably won't be seen as the console will not have been connected yet. */ USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) ); for( ;; ) { /* No characters received yet for the current input string. */ cRxedChar = 0; /* Only interested in reading one character at a time. */ cRxedChar = cGetCDCChar(); if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS ) { /* Echo the character back. */ USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) ); /* Was it the end of the line? */ if( cRxedChar == '\n' || cRxedChar == '\r' ) { /* Just to space the output from the input. */ USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( 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( 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 CDC. */ USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) ); vTaskDelay( 1 ); } 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 ); ucInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) 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( 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( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) ) { if( ucInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ ucInputIndex ] = cRxedChar; ucInputIndex++; } } } } /* Must ensure to give the mutex back. */ xSemaphoreGive( xCDCMutex ); } } }
static void usb_cdc_command_console_task(void *pvParameters) { uint8_t received_char, input_index = 0, *output_string; static int8_t input_string[MAX_INPUT_SIZE], last_input_string[MAX_INPUT_SIZE]; portBASE_TYPE returned_value; /* Just to remove compiler warnings. */ (void) pvParameters; udc_start(); if (udc_include_vbus_monitoring() == false) { /* VBUS monitoring is not available on this product. Assume VBUS is present. */ cli_vbus_event(true); } /* 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. */ output_string = (uint8_t *) FreeRTOS_CLIGetOutputBuffer(); for (;;) { /* Wait for new data. */ xSemaphoreTake(cdc_new_data_semaphore, portMAX_DELAY); /* Ensure mutually exclusive access is obtained as other tasks can write to the CLI. */ xSemaphoreTake(access_mutex, portMAX_DELAY); /* While there are input characters. */ while (udi_cdc_is_rx_ready() == true) { received_char = (uint8_t) udi_cdc_getc(); /* Echo the character. */ udi_cdc_putc(received_char); if (received_char == '\r') { /* Transmit a line separator, just to make the output easier to read. */ udi_cdc_write_buf((void *) new_line, strlen((char *) new_line)); /* See if the command is empty, indicating that the last command is to be executed again. */ if (input_index == 0) { strcpy((char *) input_string, (char *) last_input_string); } /* Pass the received command to the command interpreter. The command interpreter is called repeatedly until it returns pdFALSE as it might generate more than one string. */ do { /* Get the string to write to the UART from the command interpreter. */ returned_value = FreeRTOS_CLIProcessCommand( input_string, (int8_t *) output_string, configCOMMAND_INT_MAX_OUTPUT_SIZE); /* Transmit the generated string. */ udi_cdc_write_buf((void *) output_string, strlen( (char *) output_string)); } while (returned_value != 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 *) last_input_string, (char *) input_string); input_index = 0; memset(input_string, 0x00, MAX_INPUT_SIZE); /* Start to transmit a line separator, just to make the output easier to read. */ udi_cdc_write_buf((void *) line_separator, strlen( (char *) line_separator)); } else { if (received_char == '\n') { /* Ignore the character. */ } else if (received_char == '\b') { /* Backspace was pressed. Erase the last character in the string - if any. */ if (input_index > 0) { input_index--; input_string[input_index] = '\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 (input_index < MAX_INPUT_SIZE) { input_string[input_index] = received_char; input_index++; } } } } /* Finished with the CDC port, return the mutex until more characters arrive. */ xSemaphoreGive(access_mutex); } }
void vCommandInterpreterTask( void *pvParameters ) { long lBytes, lByte; static signed char cInChar, cInputIndex = 0; static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ]; BaseType_t xMoreDataToFollow; volatile int iErrorCode = 0; portBASE_TYPE xStatus; /* Just to prevent compiler warnings. */ ( void ) pvParameters; xRxQueue = xQueueCreate(cmdSOCKET_INPUT_BUFFER_SIZE, sizeof(char)); if (xRxQueue!=NULL) { for( ;; ) { /* Wait for incoming data on the queue. */ lBytes=0; xStatus=xQueueReceive(xRxQueue, &cLocalBuffer[lBytes++], 10/portTICK_RATE_MS); if (xStatus==pdPASS) { while (xQueueReceive(xRxQueue, &cLocalBuffer[lBytes++], 10/portTICK_RATE_MS)==pdPASS) { } lBytes--; /* Process each received byte in turn. */ lByte = 0; while( lByte < lBytes ) { /* The next character in the input buffer. */ cInChar = cLocalBuffer[ lByte ]; lByte++; /* Echo the character back. */ xputc(cInChar); /* Newline characters are taken as the end of the command string. */ if( cInChar == '\n' || cInChar == '\r') { xputs("\r\n"); /* Process the input string received prior to the newline. */ do { /* Pass the string to FreeRTOS+CLI. */ xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE ); /* Send the output generated by the command's implementation. */ xprintf("%s",cOutputString); } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */ /* All the strings generated by the command processing have been sent. Clear the input string ready to receive the next command. */ cInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); /* Transmit a spacer, just to make the command console easier to read. */ // xprintf("\r\n"); } else { if( cInChar == '\r' ) { /* Ignore the character. Newlines are used to detect the end of the input string. */ } else if( (cInChar == '\b') || (cInChar == cmdASCII_DEL) ) { /* 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( cInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ cInputIndex ] = cInChar; cInputIndex++; } } } } } } } else { /* The socket could not be opened. */ vTaskDelete( NULL ); } }
static void taskUARTRXControl(void *pvParameters) { struct CarMessage pxAllocMessage; int8_t localVelocity = 0; int8_t localAccleration = 0 ; enum ButtonPressed localButton = NONE; pxAllocMessage.Velocity = localVelocity; pxAllocMessage.Acceleration = localAccleration; xTaskParameter_t *pxTaskParameter; portTickType xStartTime; /* The parameter points to an xTaskParameters_t structure. */ pxTaskParameter = (xTaskParameter_t *) pvParameters; struct UARTMessage *pxRxedMessage; struct UARTMessage Message2; pxRxedMessage = &Message2; int valid_command = 0; //char cRxedChar; UART_DATA data; //struct UARTMessage *pxRxedMessage; //struct UARTMessage Message2; //pxRxedMessage = &Message2; int j; int offset = 0; int done = 0; //Peripheral_Descriptor_t xConsole; int8_t cRxedChar, cInputIndex = 0; portBASE_TYPE xMoreDataToFollow; /* The input and output buffers are declared static to keep them off the stack. */ static int8_t pcOutputString[ MAX_OUTPUT_LENGTH ], pcInputString[ MAX_INPUT_LENGTH ]; //= "task-stats\0";//help\0"; while(1) { //take the semaphor, wait forever if it is not ready yet. xSemaphoreTake( InputByteBuffer, portMAX_DELAY ); //get data from global variable by calling UARTGetChar() cRxedChar = UARTGetChar(); //echo it on the tx buffer by queueing a message Message2.ucMessage[0] = cRxedChar; Message2.ucMessage[1] = 0; if( xQueueSendToBack( xUARTQueue, //QueueHandle_t xQueue, &Message2, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } //only one char is recieved at a time, therefore an "enter" press will send a \r\n, but the \n will be cut off. Going to hack it by making it just look for a \r. //if( cRxedChar == '\n' ) if( cRxedChar == '\r' ) { /* //echo a newline Message2.ucMessage[0] = '\n'; Message2.ucMessage[1] = 0; if( xQueueSendToBack( xUARTQueue, //QueueHandle_t xQueue, &Message2, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } */ //pcInputString[ cInputIndex ] = '\n'; //pcInputString[ cInputIndex + 1 ] = '\0'; /* A newline character was received, so the input command stirng is complete and can be processed. Transmit a line separator, just to make the output easier to read. */ //FreeRTOS_write( xConsole, "\r\n", strlen( "\r\n" ); /* The command interpreter is called repeatedly until it returns pdFALSE. See the ?Implementing a command? documentation for an exaplanation of why this is. */ do { /* Send the command string to the command interpreter. Any output generated by the command interpreter will be placed in the pcOutputString buffer. */ xMoreDataToFollow = FreeRTOS_CLIProcessCommand ( pcInputString, /* The command string.*/ pcOutputString, /* The output buffer. */ MAX_OUTPUT_LENGTH/* The size of the output buffer. */ ); /* Write the output generated by the command interpreter to the console. */ for(j = 0; j < 49 & pcOutputString[j] != 0; j++) { Message2.ucMessage[j] = pcOutputString[j]; } Message2.ucMessage[j] = STATICNULL; if( xQueueSendToBack( xUARTQueue, //QueueHandle_t xQueue, &Message2, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } if(j > 48) { done = 1; } while(done == 1) { offset = offset + 49; for(j = 0; j < 49 & pcOutputString[j+offset] != 0; j++) { Message2.ucMessage[j] = pcOutputString[j+offset]; } if(j < 48) { done = 0; for(; j < 49; j++) { Message2.ucMessage[j] = STATICNULL; } } //Message2.ucMessage[j] = STATICNULL; if( xQueueSendToBack( xUARTQueue, //QueueHandle_t xQueue, &Message2, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } offset = 0; //FreeRTOS_write( xConsole, pcOutputString, strlen( pcOutputString ) ); } while( xMoreDataToFollow != pdFALSE ); /* All the strings generated by the input command have been sent. Processing of the command is complete. Clear the input string ready to receive the next command. */ cInputIndex = 0; memset( pcInputString, 0x00, MAX_INPUT_LENGTH ); } else { if( cRxedChar == 'q' ) // Keyboard ?q? ? Emergency Clear inside car { pxAllocMessage.button = Q; if( xQueueSendToBack( xCarMessageQueue, //QueueHandle_t xQueue, &pxAllocMessage, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } else if( cRxedChar == 'w' ) //Keyboard ?w? ? P1Call DN outside car { pxAllocMessage.button = W; if( xQueueSendToBack( xCarMessageQueue, //QueueHandle_t xQueue, &pxAllocMessage, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } else if( cRxedChar == 'e' ) //Keyboard ?e? ? P1Call UP outside car { pxAllocMessage.button = E; if( xQueueSendToBack( xCarMessageQueue, //QueueHandle_t xQueue, &pxAllocMessage, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } else if( cRxedChar == 'r' ) //Keyboard ?r? ? P2 Call outside car { pxAllocMessage.button = R; if( xQueueSendToBack( xCarMessageQueue, //QueueHandle_t xQueue, &pxAllocMessage, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } else if( cRxedChar == 't' ) //Keyboard ?t? ? GD Call button inside car { pxAllocMessage.button = T; if( xQueueSendToBack( xCarMessageQueue, //QueueHandle_t xQueue, &pxAllocMessage, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } else if( cRxedChar == 'y' ) //Keyboard ?y? ? Emergency Stop inside car { pxAllocMessage.button = Y; if( xQueueSendToBack( xCarMessageQueue, //QueueHandle_t xQueue, &pxAllocMessage, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } else if( cRxedChar == 'u' ) //Keyboard ?u? ? Emergency Clear inside car { pxAllocMessage.button = U; if( xQueueSendToBack( xCarMessageQueue, //QueueHandle_t xQueue, &pxAllocMessage, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } else if( cRxedChar == 'i' ) //Keyboard ?i? ? Door Interference { pxAllocMessage.button = I; if( xQueueSendToBack( xCarMessageQueue, //QueueHandle_t xQueue, &pxAllocMessage, //const void * pvItemToQueue, 0 //TickType_t xTicksToWait ) != pdPASS ) { //task was not able to be created after the xTicksToWait //a = 0; } } else if( cRxedChar == '\r' ) { /* Ignore carriage returns. */ } else if( cRxedChar == '\b' ) { /* Backspace was pressed. Erase the last character in the input buffer - if there are any. */ if( cInputIndex > 0 ) { cInputIndex--; pcInputString[ cInputIndex ] = '\0'; } } else { /* A character was entered. It was not a new line, backspace or carriage return, so it is accepted as part of the input and placed into the input buffer. When a \n is entered the complete string will be passed to the command interpreter. */ if( cInputIndex < MAX_INPUT_LENGTH ) { pcInputString[ cInputIndex ] = cRxedChar; cInputIndex++; } } } } }
static void prvUARTCommandConsoleTask( void *pvParameters ) { char cRxedChar; int8_t cInputIndex = 0, *pcOutputString; uint8_t port; static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ]; portBASE_TYPE xReturned; ( void ) pvParameters; /* Wait indefinitly until a '\r' is received on one of the ports */ ulTaskNotifyTake(pdTRUE, portMAX_DELAY); port = PcPort; cRxedChar = '\0'; /* 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(); /* By default, the UART interrupt priority will have been set to the lowest possible. It must be kept at or below configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, but can be raised above its default priority using a FreeRTOS_ioctl() call with the ioctlSET_INTERRUPT_PRIORITY command. */ //xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlSET_INTERRUPT_PRIORITY, ( void * ) ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY - 1 ) ); //configASSERT( xReturned ); /* Send the welcome message. */ writePxITMutex(port, pcWelcomeMessage, strlen(pcWelcomeMessage), cmd50ms); for( ;; ) { /* Only interested in reading one character at a time. */ readPxMutex(port, &cRxedChar, sizeof( cRxedChar ), cmd50ms, HAL_MAX_DELAY); /* Echo the character back. */ writePxITMutex(port, &cRxedChar, 1, cmd50ms); if( cRxedChar == '\r' ) { /* The input command string is complete. Ensure the previous UART transmission has finished before sending any more data. This task will be held in the Blocked state while the Tx completes, if it has not already done so, so no CPU time will be wasted by polling. */ writePxITMutex(port, pcNewLine, strlen(pcNewLine), cmd50ms); /* See if the command is empty, indicating that the last command is to be executed again. */ if( cInputIndex == 0 ) { strcpy( ( char * ) cInputString, ( char * ) cLastInputString ); } /* Pass the received command to the command interpreter. The command interpreter is called repeatedly until it returns pdFALSE as it might generate more than one string. */ do { /* Once again, just check to ensure the UART has completed sending whatever it was sending last. This task will be held in the Blocked state while the Tx completes, if it has not already done so, so no CPU time is wasted polling. */ /* Get the string to write to the UART from the command interpreter. */ xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE ); /* Write the generated string to the UART. */ //writePxITMutex(port, (char*) pcOutputString, strlen((char*) pcOutputString), cmd50ms); writePxMutex(port, (char*) pcOutputString, strlen((char*) pcOutputString), cmd50ms, HAL_MAX_DELAY); } 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 ); cInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); /* Start to transmit a line separator, just to make the output easier to read. */ writePxITMutex(port, pcEndOfCommandOutputString, strlen(pcEndOfCommandOutputString), cmd50ms); } else { if( cRxedChar == '\n' ) { /* 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++; } } } } taskYIELD(); } }
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++; } } } } } } }
void vCommandConsoleTask( void *pvParameters ) { int8_t cRxedChar, cInputIndex = 0, *pcOutputString; static int8_t cInputString[ cmdMAX_INPUT_SIZE ]; portBASE_TYPE xReturned; //Peripheral_Descriptor_t xConsoleUART; ( 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(); /* Open the UART port used for console input. The second parameter (ulFlags) is not used in this case. The default board rate is set by the boardDEFAULT_UART_BAUD parameter. The baud rate can be changed using a FreeRTOS_ioctl() call with the ioctlSET_SPEED command. */ // xConsoleUART = FreeRTOS_open( boardCOMMAND_CONSOLE_UART, ( uint32_t ) cmdPARAMTER_NOT_USED ); // configASSERT( xConsoleUART ); /* Change the Tx usage model from straight polled mode to use zero copy buffers with interrupts. In this mode, the UART will transmit characters directly from the buffer passed to the FreeRTOS_write() function. */ // xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlUSE_ZERO_COPY_TX, cmdPARAMTER_NOT_USED ); configASSERT( xReturned );// /* Change the Rx usage model from straight polled mode to use a character queue. Character queue reception is appropriate in this case as characters can only be received as quickly as they can be typed, and need to be parsed character by character. */ // xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlUSE_CHARACTER_QUEUE_RX, ( void * ) cmdMAX_INPUT_SIZE ); // configASSERT( xReturned ); /* By default, the UART interrupt priority will have been set to the lowest possible. It must be kept at or below configMAX_LIBRARY_INTERRUPT_PRIORITY, but can be raised above its default priority using a FreeRTOS_ioctl() call with the ioctlSET_INTERRUPT_PRIORITY command. */ // xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlSET_INTERRUPT_PRIORITY, ( void * ) ( configMIN_LIBRARY_INTERRUPT_PRIORITY - 1 ) ); // configASSERT( xReturned ); /* Send the welcome message. */ /* if( FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ) == pdPASS ) { FreeRTOS_write( xConsoleUART, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) ); } */ for( ;; ) { /* Only interested in reading one character at a time. */ cRxedChar = VCOM_getchar(); if( cRxedChar == '\n' ) { /* The input command string is complete. Ensure the previous UART transmission has finished before sending any more data. This task will be held in the Blocked state while the Tx completes, if it has not already done so, so no CPU time will be wasted by polling. */ /*if( FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ) == pdPASS ) { // Start to transmit a line separator, just to make the output //easier to read. FreeRTOS_write( xConsoleUART, pcNewLine, strlen( ( char * ) pcNewLine ) ); } */ VCOM_puts("\r\n>"); /* Pass the received command to the command interpreter. The command interpreter is called repeatedly until it returns pdFALSE as it might generate more than one string. */ do { /* Once again, just check to ensure the UART has completed sending whatever it was sending last. This task will be held in the Blocked state while the Tx completes, if it has not already done so, so no CPU time is wasted polling. */ //xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ); if( xReturned == pdPASS ) { /* Get the string to write to the UART from the command interpreter. */ xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE ); /* Write the generated string to the UART. */ //FreeRTOS_write( xConsoleUART, pcOutputString, strlen( ( char * ) pcOutputString ) ); VCOM_puts(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. */ cInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); /* Ensure the last string to be transmitted has completed. */ //if( FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ) == pdPASS ) { /* Start to transmit a line separator, just to make the output easier to read. */ //FreeRTOS_write( xConsoleUART, "\r\n>", strlen( "\r\n>" ) ); VCOM_puts("\r\n>"); } } 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( cInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ cInputIndex ] = cRxedChar; cInputIndex++; } } } } }
// // // //lecture - many features are from FREERTOS+CLI subsystem - explore related headers and // source files as needed ??? // //lecture - several calls are to FREERTOS+IO subsystems - explore related headers and // source files as needed ??? // // // // // static void prvUARTCommandConsoleTask( void *pvParameters ) { int8_t cRxedChar, cInputIndex = 0, *pcOutputString; static int8_t 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(); /* Open the UART port used for console input. The second parameter (ulFlags) is not used in this case. The default board rate is set by the boardDEFAULT_UART_BAUD parameter. The baud rate can be changed using a FreeRTOS_ioctl() call with the ioctlSET_SPEED command. */ //lecture - UART - refer to FreeRTOS_DriverInterface.c for FreeRTOS_open() api // - UART - refer to LPCXpresso17xx-base-board.h for // boardCOMMAND_CONSOLE_UART - it is UART3 of the four UARTs // // //lecture - UART - we will receive NULL, if there is a failure // // xConsoleUART = FreeRTOS_open( boardCOMMAND_CONSOLE_UART, ( uint32_t ) cmdPARAMTER_NOT_USED ); configASSERT( xConsoleUART ); // //lecture - UART - refer to FreeRTOS_DriverInterface.c for FreeRTOS_ioctl() api // // #if 1 /* Change the Tx usage model from straight polled mode to use zero copy buffers with interrupts. In this mode, the UART will transmit characters directly from the buffer passed to the FreeRTOS_write() function. */ // xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlUSE_ZERO_COPY_TX, cmdPARAMTER_NOT_USED ); //configASSERT( xReturned ); xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlUSE_TASKNOTIFICATION_TX, uartstCIRCULAR_BUFFER_SIZE ); configASSERT( xReturned ); #endif #if 1 //xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlUSE_CIRCULAR_BUFFER_RX, uartstCIRCULAR_BUFFER_SIZE ); //configASSERT( xReturned ); xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlUSE_TASKNOTIFICATION_RX, uartstCIRCULAR_BUFFER_SIZE ); configASSERT( xReturned ); #endif // // // //lecture -UART - refer to FreeRTOS_DriverInterface.c for FreeRTOS_ioctl() api // // /* Change the Rx usage model from straight polled mode to use a character queue. Character queue reception is appropriate in this case as characters can only be received as quickly as they can be typed, and need to be parsed character by character. */ //xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlUSE_CHARACTER_QUEUE_RX, ( void * ) cmdMAX_INPUT_SIZE ); //configASSERT( xReturned ); // //lecture - UART - refer to refer to FreeRTOS_DriverInterface.c for FreeRTOS_ioctl() api - // eventually, invokes the uart specific ioctl method and respective setting // //lecture - UART - may set different priorities and check ??? // /* By default, the UART interrupt priority will have been set to the lowest possible. It must be kept at or below configMAX_LIBRARY_INTERRUPT_PRIORITY, but can be raised above its default priority using a FreeRTOS_ioctl() call with the ioctlSET_INTERRUPT_PRIORITY command. */ xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlSET_INTERRUPT_PRIORITY, ( void * ) ( configMIN_LIBRARY_INTERRUPT_PRIORITY - 1 ) ); configASSERT( xReturned ); // // // //lecture - UART - as per zero copy tx mode, we must first acquire the mutex, before we can // initiate a write on the device instance - in this case, it is uart // //lecture - UART - refer to FreeRTOS_DriverInterface.c for code and details of FreeRTOS_ioctl() // // #if 1 // /* Send the welcome message. */ // if( FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ) == pdPASS ) { FreeRTOS_write( xConsoleUART, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) ); } #endif #if 0 if(FreeRTOS_ioctl( xConsoleUART, ioctlRELEASE_WRITE_MUTEX, cmd50ms ) == pdPASS ) { printf("mutex released\n"); } #endif //FreeRTOS_write( xConsoleUART, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) ); //for(;;); //aaa-zzz : for testing for( ;; ) { xReturned = FreeRTOS_read( xConsoleUART, &cRxedChar, sizeof( cRxedChar ) ); configASSERT( xReturned ); //printf("LSR=%d",) /* Echo the character back. */ if( FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ) == pdPASS ) { FreeRTOS_write( xConsoleUART, &cRxedChar, sizeof( cRxedChar ) ); } if( cRxedChar == '\n' ) { /* The input command string is complete. Ensure the previous UART transmission has finished before sending any more data. This task will be held in the Blocked state while the Tx completes, if it has not already done so, so no CPU time will be wasted by polling. */ // if( FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ) == pdPASS ) { /* Start to transmit a line separator, just to make the output easier to read. */ FreeRTOS_write( xConsoleUART, pcNewLine, strlen( ( char * ) pcNewLine ) ); } /* See if the command is empty, indicating that the last command is to be executed again. */ if( cInputIndex == 0 ) { strcpy( ( char * ) cInputString, ( char * ) cLastInputString ); } /* Pass the received command to the command interpreter. The command interpreter is called repeatedly until it returns pdFALSE as it might generate more than one string. */ do { /* Once again, just check to ensure the UART has completed sending whatever it was sending last. This task will be held in the Blocked state while the Tx completes, if it has not already done so, so no CPU time is wasted polling. */ xReturned = FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ); if( xReturned == pdPASS ) { /* Get the string to write to the UART from the command interpreter. */ xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE ); /* Write the generated string to the UART. */ FreeRTOS_write( xConsoleUART, pcOutputString, strlen( ( char * ) 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( ( char * ) cLastInputString, ( char * ) cInputString ); cInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); /* Ensure the last string to be transmitted has completed. */ if( FreeRTOS_ioctl( xConsoleUART, ioctlOBTAIN_WRITE_MUTEX, cmd50ms ) == pdPASS ) { /* Start to transmit a line separator, just to make the output easier to read. */ FreeRTOS_write( xConsoleUART, pcEndOfCommandOutputString, strlen( ( char * ) pcEndOfCommandOutputString ) ); } } 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++; } } } } } }
/* USB CLI receive task */ void vUSB_cli_receive_task(void *pvParameters) { char cRxedChar; char cInputString[20]; char cInputIndex = 0; int8_t *pcOutputString; portBASE_TYPE xReturned; //Initialise pointer to CLI output buffer. memset(cInputString, 0, sizeof(cInputString)); pcOutputString = FreeRTOS_CLIGetOutputBuffer(); // Cli receive loop for (;;) { //Receive character from USB receive cRxedChar = ucUSBReadByte(); if ((cRxedChar != 0) && (cRxedChar != 5)) { //reflect byte vUSBSendByte(cRxedChar); //Process only if return is received. if (cRxedChar == '\r') { //Put null character in command input string. cInputString[cInputIndex] = '\0'; //debug_printf("\r\n---%s---%d---\r\n", pcOutputString, strlen(pcOutputString)); //memcpy(ucRemoteBuffer, cInputString, strlen(cInputString)); sprintf(ucRemoteBuffer, "%s", cInputString); vRemoteSend(ucRemoteBuffer, strlen(ucRemoteBuffer)); do { //Process command input string. xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE ); //Display CLI output string sprintf(ucRemoteBuffer, "%s", pcOutputString); remote_printf(ucRemoteBuffer); // Wait for 10ms vTaskDelay(10); // Loop until repeat is done } while (xReturned == cliREPEAT); memset(cInputString, 0, sizeof(cInputString)); cInputIndex = 0; } 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( cInputIndex < 20 ) { cInputString[ cInputIndex ] = cRxedChar; cInputIndex++; } } } } vTaskDelay(50); } }
static void prvUARTCommandConsoleTask( void *pvParameters ) { char cRxedChar, cInputIndex = 0, *pcOutputString; 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. */ vSerialPutString( NULL, ( const signed char * ) pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) ); for( ;; ) { /* Only interested in reading one character at a time. */ while( xSerialGetChar( NULL, ( signed char * ) &cRxedChar, portMAX_DELAY ) == pdFALSE ); /* Echo the character back. */ xSerialPutChar( NULL, cRxedChar, portMAX_DELAY ); /* Was it the end of the line? */ if( cRxedChar == '\n' || cRxedChar == '\r' ) { /* Just to space the output from the input. */ vSerialPutString( NULL, ( const signed char * ) pcNewLine, strlen( ( char * ) 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( ( 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, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE ); /* Write the generated string to the UART. */ vSerialPutString( NULL, ( const signed char * ) pcOutputString, strlen( ( char * ) 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( ( char * ) cLastInputString, ( char * ) cInputString ); cInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); vSerialPutString( NULL, ( const signed char * ) pcEndOfOutputMessage, strlen( ( char * ) 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 ) { signed char cRxedChar; uint8_t ucInputIndex = 0; char *pcOutputString; static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ]; BaseType_t xReturned; xComPortHandle xPort; ( 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(); /* Initialise the UART. */ xPort = xSerialPortInitMinimal( configCLI_BAUD_RATE, cmdQUEUE_LENGTH ); /* Send the welcome message. */ vSerialPutString( xPort, ( signed char * ) pcWelcomeMessage, ( unsigned short ) strlen( pcWelcomeMessage ) ); for( ;; ) { /* Wait for the next character. The while loop is used in case INCLUDE_vTaskSuspend is not set to 1 - in which case portMAX_DELAY will be a genuine block time rather than an infinite block time. */ while( xSerialGetChar( xPort, &cRxedChar, portMAX_DELAY ) != pdPASS ); /* Ensure exclusive access to the UART Tx. */ if( xSemaphoreTake( xTxMutex, cmdMAX_MUTEX_WAIT ) == pdPASS ) { /* Echo the character back. */ xSerialPutChar( xPort, cRxedChar, portMAX_DELAY ); /* Was it the end of the line? */ if( cRxedChar == '\n' || cRxedChar == '\r' ) { /* Just to space the output from the input. */ vSerialPutString( xPort, ( signed char * ) pcNewLine, ( unsigned short ) strlen( 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( 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. */ vSerialPutString( xPort, ( signed char * ) pcOutputString, ( unsigned short ) 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 ); ucInputIndex = 0; memset( cInputString, 0x00, cmdMAX_INPUT_SIZE ); vSerialPutString( xPort, ( signed char * ) pcEndOfOutputMessage, ( unsigned short ) strlen( pcEndOfOutputMessage ) ); } else { if( cRxedChar == '\r' ) { /* Ignore the character. */ } else if( ( cRxedChar == '\b' ) || ( cRxedChar == 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( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) ) { if( ucInputIndex < cmdMAX_INPUT_SIZE ) { cInputString[ ucInputIndex ] = cRxedChar; ucInputIndex++; } } } } /* Must ensure to give the mutex back. */ xSemaphoreGive( xTxMutex ); } } }
void vCommandConsoleTask( void *pvParameters ) { (void)pvParameters; unsigned portCHAR cRxedChar, cInputIndex = 0; portBASE_TYPE xMoreDataToFollow; /* The input and output buffers are declared static to keep them off the stack. */ portCHAR *pcOutputString; static unsigned portCHAR pcInputString[ MAX_INPUT_LENGTH ]; /* This code assumes the peripheral being used as the console has already been opened and configured, and is passed into the task as the task parameter. Cast the task parameter to the correct type. */ pcOutputString = (portCHAR *) FreeRTOS_CLIGetOutputBuffer(); strcpy_P(pcOutputString, pm_cstr_WelcomeMessage); /* Send a welcome message to the user knows they are connected. */ FreeRTOS_write( pcOutputString, strlen(pcOutputString) ); for( ;; ) { /* This implementation reads a single character at a time. Wait in the Blocked state until a character is received. */ FreeRTOS_read( &cRxedChar, sizeof( cRxedChar ) ); if( cRxedChar == '\n' ) { /* A newline character was received, so the input command stirng is complete and can be processed. Transmit a line separator, just to make the output easier to read. */ FreeRTOS_write( "\r\n", strlen( "\r\n" )); /* The command interpreter is called repeatedly until it returns pdFALSE. See the "Implementing a command" documentation for an exaplanation of why this is. */ do { /* Send the command string to the command interpreter. Any output generated by the command interpreter will be placed in the pcOutputString buffer. */ FreeRTOS_CLITakeOutputBuffer(); xMoreDataToFollow = FreeRTOS_CLIProcessCommand ( (const int8_t * const)pcInputString, /* The command string.*/ (int8_t *)pcOutputString, /* The output buffer. */ configCOMMAND_INT_MAX_OUTPUT_SIZE/* The size of the output buffer. */ ); /* Write the output generated by the command interpreter to the console. */ FreeRTOS_write( pcOutputString, strlen(pcOutputString ) ); FreeRTOS_CLIGiveOutputBuffer(); } while( xMoreDataToFollow != pdFALSE ); /* All the strings generated by the input command have been sent. Processing of the command is complete. Clear the input string ready to receive the next command. */ cInputIndex = 0; memset( pcInputString, 0x00, MAX_INPUT_LENGTH ); } else { /* The if() clause performs the processing after a newline character is received. This else clause performs the processing if any other character is received. */ if( cRxedChar == '\r' ) { /* Ignore carriage returns. */ } else if( cRxedChar == '\b' ) { /* Backspace was pressed. Erase the last character in the input buffer - if there are any. */ if( cInputIndex > 0 ) { cInputIndex--; pcInputString[ cInputIndex ] = '\0'; } } else { /* A character was entered. It was not a new line, backspace or carriage return, so it is accepted as part of the input and placed into the input buffer. When a \n is entered the complete string will be passed to the command interpreter. */ if( cInputIndex < MAX_INPUT_LENGTH ) { pcInputString[ cInputIndex ] = cRxedChar; cInputIndex++; } } } } }