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.

RTOS/CC3220: How to read UART in UART_MODE_CALLBACK ?

Part Number: CC3220

Tool/software: TI-RTOS

Hi, I am trying access the UART read in UART_MODE_CALLBACK. Please find below code,

UART_Params uartParams;

    UART_init();
    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode = UART_RETURN_NEWLINE;//UART_RETURN_FULL;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.readTimeout = UART_WAIT_FOREVER;
    uartParams.baudRate = 115200;
    uartParams.readCallback = &uart0ReadCallback;
    uartParams.readMode = UART_MODE_CALLBACK; //UART_MODE_BLOCKING for peripheral's

    uartHandle = UART_open(Board_UART0, &uartParams);
void uart0ReadCallback(UART_Handle handle, void *rxBuf, size_t size)
{
        UART_write(uartHandle, rxBuf, size);

}

The calback is not calling at any time. I tried for different modes like 

UART_RETURN_FULL and UART_RETURN_NEWLINE
UART_MODE_CALLBACK and UART_MODE_BLOCKING

But the same will call the callback when I do like below
    UART_Params uartParams;

    UART_init();
    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode = UART_RETURN_NEWLINE;//UART_RETURN_FULL;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.readTimeout = UART_WAIT_FOREVER;
    uartParams.baudRate = 115200;
    uartParams.readCallback = &uart0ReadCallback;
    uartParams.readMode = UART_MODE_CALLBACK; //UART_MODE_BLOCKING for peripheral's

    uartHandle = UART_open(Board_UART0, &uartParams);

    char input;
    while (1) {
          UART_read(uartHandle, &input, 1);
        }
}

So, the data is Receiving, why not in call back. Many threads regarding similar issues are not resolved my issues.

Thanks

Raghu

  • Raghu,

    Please try calling UART_Read once, then waiting for the callback, so you aren't constantly calling UART_Read.

    BR,
    VInce
  • Hi Vince,

    I tried calling,

       char input;

       UART_read(uartHandle, &input, 128);

    in the main thread after the successful init of UART0, no callback coming to "uart0ReadCallback"

    Tried like below also

    UART_Handle InitTerm(void)
    {
        UART_Params uartParams;
    
        UART_init();
        UART_Params_init(&uartParams);
        uartParams.writeDataMode = UART_DATA_BINARY;
        uartParams.readDataMode = UART_DATA_BINARY;
        uartParams.readReturnMode = UART_RETURN_NEWLINE;//UART_RETURN_FULL;
        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.readTimeout = UART_WAIT_FOREVER;
        uartParams.baudRate = 115200;
        uartParams.readCallback = &uart0ReadCallback;
        uartParams.readMode = UART_MODE_CALLBACK; //UART_MODE_BLOCKING for peripheral's
    
        uartHandle = UART_open(Board_UART0, &uartParams);
    
        char input;
              UART_read(uartHandle, &input, 128);//tried for size 1 also
    }
    
    void uart0ReadCallback(UART_Handle handle, void *rxBuf, size_t size)
    {
            UART_write(uartHandle, rxBuf, size);
    }

    But no use

  • Looks like your issue is with the UART_RETURN_NEWLINE. What this means, is that the Callback or Blocking read will return only when it gets a new line character. What you want is UART_RETURN_FULL. This will return when the buffer is full. If you then use 1 as the buffer, any character you type will return in the call back. I would suggest looking at the UARTEcho example in the SDK for more info. Here is what i did to test.

    /*
     * Copyright (c) 2015-2017, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     *  ======== uartecho.c ========
     */
    #include <stdint.h>
    #include <stddef.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    
    /* Example/Board Header files */
    #include "Board.h"
    
    UART_Handle uart;
    
    
    void uart0ReadCallback(UART_Handle handle, void *rxBuf, size_t size)
    {
        UART_write(uart, "\n\r", sizeof("\n\r"));
        UART_write(uart, rxBuf, size);
    }
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        char        input;
        const char  echoPrompt[] = "Echoing characters:\r\n";
        UART_Params uartParams;
    
        /* Call driver init functions */
        GPIO_init();
        UART_init();
    
        /* Configure the LED pin */
        GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Turn on user LED */
        GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
    
        /* Create a UART with data processing off. */
        UART_Params_init(&uartParams);
        uartParams.writeDataMode = UART_DATA_BINARY;
        uartParams.readDataMode = UART_DATA_BINARY;
        uartParams.readReturnMode = UART_RETURN_FULL;
        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.baudRate = 115200;
        uartParams.readCallback = &uart0ReadCallback;
        uartParams.readMode = UART_MODE_CALLBACK; //UART_MODE_BLOCKING for peripheral's
    
        uart = UART_open(Board_UART0, &uartParams);
    
        if (uart == NULL) {
            /* UART_open() failed */
            while (1);
        }
    
        UART_write(uart, echoPrompt, sizeof(echoPrompt));
    
        /* Loop forever echoing */
        while (1) {
            UART_read(uart, &input, 1);
           // UART_write(uart, &input, 1);
        }
    }
    

  • Hi Vince,
    I tried as above, in your way SW is always looping to read UART0.
    But I have other tasks to do after this, more I want UART1 also to enable. I don't think this solution will help. I want this to run on TI-RTOS project which is running 5 application tasks.
    Thank you.

  • Place a semaphore pend after the read if it's not blocking, and post when you get into the interrupt handler. That way other tasks can run.
  • Hi Vincent,

    After Semaphore pend and Semaphore post cal, Now I am receiving Data which is coming to particular UART.

    But after a while the receiving is stopped, but transmitting is happening(tested by prints for button press).

    I mean after few characters received callback and Semaphore_pend are not calling (tested with break points on debug mode)

    Please have look at my code:

     UART_Params uartParams;
    UInt32 sleepTickCount;
    
    Task_Struct task1Struct;
    Char task1Stack[TASKSTACKSIZE];
    Semaphore_Struct semStruct;
    Semaphore_Handle semHandle;
    UART_Handle uartHandle;
    
        /* Call driver init functions */
        UART_init();
    
        /* Create a UART with data processing off. */
        UART_Params_init(&uartParams);
        uartParams.writeDataMode = UART_DATA_BINARY;
        uartParams.readDataMode = UART_DATA_BINARY;
        uartParams.readReturnMode = UART_RETURN_NEWLINE;
        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.baudRate = 115200;
        uartParams.readCallback = &uart0ReadCallback;
        uartParams.readMode = UART_MODE_CALLBACK;//UART_MODE_CALLBACK; //UART_MODE_BLOCKING for peripheral's
    
        uartHandle = UART_open(Board_UART0, &uartParams);
    
        if (uartHandle == NULL) {
            /* UART_open() failed */
            while (1);
        }
    
        /* Construct BIOS objects */
           Task_Params taskParams;
           Semaphore_Params semParams;
    
           /* Construct writer/reader Task threads */
           Task_Params_init(&taskParams);
           taskParams.stackSize = TASKSTACKSIZE;
           taskParams.stack = &task1Stack;
           taskParams.priority = 1;
           Task_construct(&task1Struct, (Task_FuncPtr)task1Fxn, &taskParams, NULL);
    
           /* Construct a Semaphore object to be use as a resource lock, inital count 1 */
           Semaphore_Params_init(&semParams);
           Semaphore_construct(&semStruct, 1, &semParams);
    
           /* Obtain instance handle */
           semHandle = Semaphore_handle(&semStruct);
           /* We want to sleep for 1000 microseconds */
           sleepTickCount = 1000 / Clock_tickPeriod;
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    void uart0ReadCallback(UART_Handle handle, void *rxBuf, size_t size)
    {
        UART_write(uartHandle, rxBuf, size);
        Semaphore_post(semHandle);
    }
    
    
    Void task1Fxn(UArg arg0, UArg arg1)
    {
        char        input;
    
        for (;;) {
    
            UART_read(uartHandle, &input, 1);
    
                /* Get access to resource */
            Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
    
            Task_sleep(sleepTickCount);
        }
    }
    

    Tried for different baud rates.

    Thank you

  • Try removing the UART_Write from the callback and place it after the semaphore pend in your task1fxn.

    Best Regards,
    Vince