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.

Sending value through UART7

Other Parts Discussed in Thread: TM4C123GH6PM

Hi,

I am using TM4C123GH6PM evaluation kit and CCS. I need to develop a code, to transmit a value and receive another value through UART7 continuously and the received value need to be displayed in the windows terminal through UART0. I developed the following code from uart_echo example.

void
UARTIntHandler(void)
{

	uint32_t ui32Status;
    ui32Status = ROM_UARTIntStatus(UART0_BASE, true);

    ROM_UARTIntClear(UART0_BASE, ui32Status);

    while(ROM_UARTCharsAvail(UART0_BASE))
    {

        ROM_UARTCharPutNonBlocking(UART7_BASE,
                                   ROM_UARTCharGetNonBlocking(UART0_BASE));

        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);

        SysCtlDelay(SysCtlClockGet() / (1000 * 3));

        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

    }
}

void
UART7IntHandler(void)
{

	uint32_t ui32Status;
    ui32Status = ROM_UARTIntStatus(UART7_BASE, true);

    ROM_UARTIntClear(UART7_BASE, ui32Status);

    while(ROM_UARTCharsAvail(UART7_BASE))
    {

        ROM_UARTCharPutNonBlocking(UART0_BASE,
                                   ROM_UARTCharGetNonBlocking(UART7_BASE));

        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);

        SysCtlDelay(SysCtlClockGet() / (1000 * 3));

        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

    }
}

void
UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count)
{

    while(ui32Count--)
    {
        ROM_UARTCharPutNonBlocking(UART7_BASE, *pui8Buffer++);
    }
}

int
main(void)
{

    ROM_FPUEnable();
    ROM_FPULazyStackingEnable();

    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);

    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART7);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

    ROM_IntMasterEnable();

    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinConfigure(GPIO_PE0_U7RX);
    GPIOPinConfigure(GPIO_PE1_U7TX);

    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    ROM_GPIOPinTypeUART(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
                            (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                             UART_CONFIG_PAR_NONE));
    ROM_UARTConfigSetExpClk(UART7_BASE, ROM_SysCtlClockGet(), 115200,
                            (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                             UART_CONFIG_PAR_NONE));

    ROM_IntEnable(INT_UART0);
    ROM_IntEnable(INT_UART7);

    ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
    ROM_UARTIntEnable(UART7_BASE, UART_INT_RX | UART_INT_RT);
    while(1)
    {
        UARTSend((uint8_t *)"\n 1 \n ", 3);
    }
}

This code works with transmitting '1'. But I need to send a value which is changing over time. like "TempValueC". For that how can I modify the code?

  • Hello Mary

    What is TempValueC?

    Regards
    Amit
  • Hi Amit,

    Thank you for your reply.

    TempValueC is just a variable to measure temperature which is updated with time.

                  TempAvg=((ui32ADC1Value[0]));


                  TempValueC=(1475-((2475*TempAvg))/4096)/10;

    That is not a problem (Just an example).

    But, my question is,

    1. I am getting input from UART7 Rx pin, that need to be displayed in windows terminal using UART0, and also stored in a variable named "A".

    2. Then depends on that (A), I need to generate another variable named "B"

    Just for an example:

    while

    {

    If(A=234)

    B=Yes; 

    else if(A=2A2) 

    B=234;

    }

    and the value of B want to be send to another controller through UART7 Tx.

    I am trying with two methods.

    1. UARTprintf() method - That only works for UART0,1 and 2. And also that is not works with interrupt.

    2. UARTSend() method of uart_echo example - there if I get the value A=234 from UART7 Rx, and if I try to send the value of A to terminal through UART0 that

    only send A, not 234. How can I modify the UARTSend code to transfer the data stored in a variable.

  • Hello Mary,

    First include string.h and utils/ustdlib.h in your main file. Also add the file utils/ustdlib.c into the compilation flow.

    Declare a character array like

    char pcBuf[16];

    Now convert the variable to a string

    usprintf(pcBuf, "%d", TempValueC);

    Now print the char array

    UARTSend((uint8_t *)pcBuf, 16);

    Regards
    Amit
  • Had to click "Like" Amit - very neat & crystal clear response.

    Suspect many here would do well to cut/paste your post - it's likely to arise often - and you've "filled in the (usual) blanks" quite well.   Thanks!

  • Hi Amit,

    Thank you for your reply.

    I have a doubt and a problem.

    1.
    usprintf(pcBuf, "%d", TempValueC);  


    usprintf(pcBuf, "%s", TempValueC); 


    which is correct?

    2.


    usprintf(pcBuf, "%d", TempValueC);

    while building, this function shows an warning as "function declared implicitly". I did all steps you suggested.

  • Hello Mary,

    The one with %d is the correct one.

    Did you check the step

    "Also add the file utils/ustdlib.c into the compilation flow."

    Regards
    Amit
  • Hi Amit,

    Right click project name - In add files, I select the file and link the file to my project.

    Is it right? or how can I add that file into the compilation flow.
  • Hello Mary,

    That should do it. Do make sure that the include is added to you main project C file

    Regards
    Amit
  • Hi Amit,

    Thank you. That works. Now I have new issue. My project stops running and hangs on a line inside the while loop. Then that line
    denoted as "Debug Call Stack" by an arrow.
  • I suggest you check the stack size for your project (in CCS: right click on project folder -> properties -> build -> ARM linker -> basic options). You might also need to modify the .cmd file, there's a line defining STACK_TOP that also has the stack size hard-coded. Amit can verify the procedure, it's been a while since I've had to tinker with that. The default size (512 bytes IIRC) probably isn't enough for stack-hungry functions like usprintf.

    A stack overflow can result in most interesting side-effects, including the debugger stopping at "creative" locations. Or, a stuck gas pedal that ends up killing people...

  • Veikko Immonen said:
    Or, a stuck gas pedal that ends up killing people...

    If - referring to a recent "US court case" - while stuck pedal was "blamed" - jury found (beyond all doubt) that, inadequately designed/chosen software caused, "unintended acceleration."

  • Indeed, and if we're referring to the same case, stack overflow was the culprit, resulting in "the pedal being stuck" - which it of course wasn't physically, only in software.
  • Indeed we are - and your recall is "spot-on" - and the SW meant to check the SW - was (itself) fatally flawed...  (i.e. the controller was serving as its (own) "traffic cop!")

    The "live" courtroom demonstration of that SW flaw was impressive - getting "jury of one's {non-tech} peers" to understand - never quick nor easy...