This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GXL tiva c series: I am not able to receive and store a string from console using UART. I can receive only one character at a time using uart.h or uartstdio.h

I tried to store the characters in an array, but when I echo te characters on the console (Tera term/putty), those echoed characters are taken as inputs as well. 

I am not able to clear the echoed buffer. And I'm not exactly sure how to use cmdline.h/cmdline.c functions, any tutotiral? Any help would be appreciated.

  • Hello Kirthi

    Please explain what you are trying to do with a code.

    Did you check the cmdline examples under TivaWare board DK-TM4C123G.
  • //Kirthi R. Kulkarni
    //October 2016
    //Program to control Device and other functionalities or peripherals using UART from the console.
    //*****************************************************************************
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_memmap.h" //
    #include "driverlib/gpio.h" //
    #include "driverlib/interrupt.h"
    #include "driverlib/pin_map.h" //
    #include "driverlib/sysctl.h" //
    #include "driverlib/systick.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    #include "driverlib/adc.h"
    #include "inc/tm4c123gh6pm.h"
    #include "inc/hw_types.h"
    #include "driverlib/flash.h"
    //*****************************************************************************
    //! The following UART signals are configured only for displaying console
    //! messages for this example.  These are not required for operation of
    //! Systick.
    //! - UART0 peripheral
    //! - GPIO Port A peripheral (for UART0 pins)
    //! - UART0RX - PA0
    //! - UART0TX - PA1
    //!
    //! This example uses the following interrupt handlers.  To use this example
    //! in your own application you must add these interrupt handlers to your
    //! vector table.
    //! - SysTickIntHandler
    //
    //*****************************************************************************
    //*****************************************************************************
    //
    // This function sets up UART0 to be used for a console to display information
    // as the example is running.
    //
    //*****************************************************************************
    
    int
    main(void)
    {
    	unsigned char c;
        //
        // Set the clocking to run directly from the external crystal/oscillator.
        // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the crystal on your board.
        //
    
        SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);
    
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
        GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
        GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
    
        InitConsole();
    
        //
        // Set up the serial console to use for displaying messages.  This is
        // just for this example program and is not needed for Systick operation.
        //
        // Display the setup on the console.
        //
        UARTprintf("Enter your option");
        UARTprintf("\nType '1' for blinking RED LED");
        UARTprintf("\nType '2' for blinking GREEN LED");
        UARTprintf("\nType '3' for blinking BLUE LED");
        UARTprintf("\nType '4' for reading temperature and initializing ADC");
        UARTprintf("\nType '5' for generating a sqaure wave- 50% Duty Cycle->250Hz using PWM");
        UARTprintf("\nType '6' for LCD initializations");
        UARTprintf("\n>");
        //
        // Enable interrupts to the processor.
        //
        IntMasterEnable();
        adc_init();
        pwm_init();
    
        //
        while(1)
        {
        	c= UARTgetc();
        	if(c=='1')
        	{UARTCharPut(UART0_BASE,'1');
        		GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
        		GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
        		GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
        		UARTprintf(" RED LED is on");
        		UARTprintf("\n>");
        	}
        	 if(c=='2')
        		{
        			UARTCharPut(UART0_BASE,'2');
        			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
        			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
        			GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
        			UARTprintf(" BLUE LED is on");
        			UARTprintf("\n>");
        		}
        	 if(c=='3')
        			{
        				UARTCharPut(UART0_BASE,'3');
        				GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
        				GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
        				GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
        				UARTprintf(" GREEN LED is on");
        				UARTprintf("\n>");
        			}
            if(c=='4')
            {UARTCharPut(UART0_BASE,'4');
            UARTprintf(" TEMPERATURE:");
            	adc();
            	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
            	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
            	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
            	UARTprintf("\n>");
            }
            if(c=='5')
                   {
            	    UARTCharPut(UART0_BASE,'5');
                    UARTprintf(" Check the square wave on CRO (port D pin 0)");
                   	pwm();
                   	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
                   	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
                   	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
                   	UARTprintf("\n>");
                   }
            if(c=='6')
                   {
            	   UARTCharPut(UART0_BASE,'6');
                   UARTprintf(" LCD ");
                   InitLcd();
                   LCDClearScreen();
                   SysCtlDelay(200000);
                   LCDPutStr( "HEY", 70, 70, 1, 0xFFF, 0x000);
                   LCDSetPixel(100, 120, 0xF00);
                   LCDSetCircle(60,60,30,0x0FF);
                   	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);
                   	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
                   	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
                   	UARTprintf("\n>");
                   	while(1);
                   }
            }
    }
    

    So, basiccally a general user interface, I have other modules like console,lcd,pwm seperately which i havent posted.

    So, instead of writing 1,2,3..i would rather take strings, or especially in case i want to input numbers. Yes, i checked out the qs rgb fucntion. but dint quite understand all the fucntionalities, since it was called and modified at multiple places. Im a beginer, Are there any simpler examples?

  • Hello Kirthi,

    If you put a breakpoint on the if condition and then send a character from the PC, what do you get in the variable "c"?
  • I am receiving the correct values. For ex: if I type '1', I receive the character '1'.

    Basically I want to be able to receive and simultaneously display a string (number or character input): like 7000, hibernate, help,etc.

    When I receive the string and simultaneously try to print the same, my receive buffer is filled up again. How do I clear the buffer? Are there any API(s)?

    I trying cmdline function with UARTPeek function to check the incoming characters. But it says UARTPeek is not defined, eventhough I'm able to use other fucntions like uartprintf and uartgetc from uartstdio.c file

    Thanks for the help!

  • Basically this is the code I tried with cmdline function, with help from qs_rgb program; but it says: UARTpeek is not defined, I have included uartstdio c file and header file.


    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    #include "driverlib/debug.h"
    #include "driverlib/systick.h"
    #include "driverlib/pwm.h"
    #include "driverlib/rom.h"
    #include "driverlib/rom_map.h"
    #include "driverlib/pin_map.h"
    #include "inc/hw_gpio.h"
    #include "utils/cmdline.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    #include "utils/ustdlib.h"
    
    static char g_cInput[8];
    //static unsigned char g_pcUARTTxBuffer[8];
    
    int CMD_intensity (int argc, char **argv);
    
    tCmdLineEntry g_psCmdTable[] =
    {
        {"intensity",CMD_intensity, " : Adjust brightness 0 to 100 percent"},
        { 0, 0, 0 }
    };
    int
    CMD_intensity(int argc, char **argv)
    {
        uint32_t ui32Intensity;
    
        //
        // This command requires one parameter.
        //
        if(argc == 2)
        {
            //
            // Extract the intensity from the command line parameter.
            //
            ui32Intensity = ustrtoul(argv[1], 0, 10);
            //
            // Convert the value to a fractional floating point value.
            //
           // g_sAppState.fIntensity = ((float) ui32Intensity) / 100.0f;
    
            //
            // Set the intensity of the RGB LED.
            //
       //     RGBIntensitySet(g_sAppState.fIntensity);
        }
        if(ui32Intensity==1)
        	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
        if(ui32Intensity==2)
        	GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
        return(0);
    
    }
    void
    ConfigureUART(void)
    {
        //
        // Enable the GPIO Peripheral used by the UART.
        //
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    
        //
        // Enable UART0
        //
        SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    
        //
        // Configure GPIO Pins for UART mode.
        //
        GPIOPinConfigure(GPIO_PA0_U0RX);
        GPIOPinConfigure(GPIO_PA1_U0TX);
        GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
        //
        // Use the internal 16MHz oscillator as the UART clock source.
        //
        UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    
        //
        // Initialize the UART for console I/O.
        //
        UARTStdioConfig(0, 115200, 16000000);
    }
    
    
    int main(void)
    {
    
        int32_t i32CommandStatus;
    
    	SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
    	                       SYSCTL_OSC_MAIN);
    
    	ConfigureUART();
    	UARTprintf("> ");
    	while(1)
    	    {
    
    	        UARTprintf("\n>");
    
    	        while(UARTPeek('\r') == -1)
    	        {
    	        	SysCtlDelay(SysCtlClockGet() / (1000 / 3));
    	        	UARTgets(g_cInput,sizeof(g_cInput));
    
    	        	        //
    	        	        // Pass the line from the user to the command processor.
    	        	        // It will be parsed and valid commands executed.
    	        	        //
    	        	        i32CommandStatus = CmdLineProcess(g_cInput);
    
    	        	        //
    	        	        // Handle the case of bad command.
    	        	        //
    	        	        if(i32CommandStatus == CMDLINE_BAD_CMD)
    	        	        {
    	        	            UARTprintf("Bad command!\n");
    	        	        }
    	        	        else if(i32CommandStatus == CMDLINE_TOO_MANY_ARGS)
    	        	                {
    	        	                    UARTprintf("Too many arguments for command processor!\n");
    	        	                }
    
    
    }
    }
    }
    

  • Hello Kirthi,

    UARTpeek requires that the define UART_BUFFERED be placed during compilation of uartstdio.c
  • Yes, in the uartstdio.c; UART_BUFFERED has been placed.

    //*****************************************************************************
    //
    //! Looks ahead in the receive buffer for a particular character.
    //!
    //! \param ucChar is the character that is to be searched for.
    //!
    //! This function, available only when the module is built to operate in
    //! buffered mode using \b UART_BUFFERED, may be used to look ahead in the
    //! receive buffer for a particular character and report its position if found.
    //! It is typically used to determine whether a complete line of user input is
    //! available, in which case ucChar should be set to CR ('\\r') which is used
    //! as the line end marker in the receive buffer.
    //!
    //! \return Returns -1 to indicate that the requested character does not exist
    //! in the receive buffer.  Returns a non-negative number if the character was
    //! found in which case the value represents the position of the first instance
    //! of \e ucChar relative to the receive buffer read pointer.
    //
    //*****************************************************************************
    #if defined(UART_BUFFERED) || defined(DOXYGEN)
    int
    UARTPeek(unsigned char ucChar)
    {
        int iCount;
        int iAvail;
        uint32_t ui32ReadIndex;
    
        //
        // How many characters are there in the receive buffer?
        //
        iAvail = (int)RX_BUFFER_USED;
        ui32ReadIndex = g_ui32UARTRxReadIndex;
    
        //
        // Check all the unread characters looking for the one passed.
        //
        for(iCount = 0; iCount < iAvail; iCount++)
        {
            if(g_pcUARTRxBuffer[ui32ReadIndex] == ucChar)
            {
                //
                // We found it so return the index
                //
                return(iCount);
            }
            else
            {
                //
                // This one didn't match so move on to the next character.
                //
                ADVANCE_RX_BUFFER_INDEX(ui32ReadIndex);
            }
        }
    
        //
        // If we drop out of the loop, we didn't find the character in the receive
        // buffer.
        //
        return(-1);
    }
    #endif
    

  • Hello Kirti

    But have you defined it as one of the pre-processor defines in Project settings.
  • How exactly should I do that? I am using GCC compiler; should I add a preprocessor flag under project properties settings?

    If so, how do i go about it?

    Thank you

  • Hello Kirthi

    Yes that is correct. If you are using GCC compile in CCS, then right click the project, then under project settings, go to Compiler. You should be able to find Pre-Processor define in the drop down list (may be under Advanced).
  • Sorry again! I'm a beginner! So basically under GCC build->preprocessor-> add preprocessor file.
    What exactly should I type under that section and in what format?
    For example: while adding directories-> I type: ${TIVAWARE_INSTALL}.
    Similarly how should I include the UART BUFFERED in the pre processor?

    Thank you
  • Hello Krithi

    Not add a preprocessor file but add the define in the preprocessor section. There should be already TARGET_IS_TM4C123_RB1 there.