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.

LAUNCHCC3235MOD: UART in non blocking mode not getting callback

Part Number: LAUNCHCC3235MOD
Other Parts Discussed in Thread: SYSCONFIG

I am using simpleLink SDK version 4.10

I am using UART port in non-blocking mode initialized as below:

UART_Handle InitTerm(void)
{
   UART_Params uartParams;
   int32_t semStatus;

   /* Create semaphore */
   semStatus = sem_init(&sem, 0, 0);

   if (semStatus != 0) {
   /* Error creating semaphore */
   while (1);
   }

   UART_init();
   UART_Params_init(&uartParams);

   uartParams.writeDataMode = UART_DATA_BINARY;
   uartParams.readDataMode = UART_DATA_BINARY;
   uartParams.readMode = UART_MODE_CALLBACK;
   uartParams.readTimeout = 1000;
   uartParams.readCallback = callbackFxn;
   uartParams.baudRate = 115200;

   uartHandle = UART_open(CONFIG_UART_0, &uartParams);
   if(uartHandle)
      GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
   /* remove uart receive from LPDS dependency */
   UART_control(uartHandle, UART_CMD_RXDISABLE, NULL);

   return(uartHandle);
}

Callback function is defined as below:

void callbackFxn(UART_Handle handle, void *buffer, size_t count,
                                             void *userArg, int_fast16_t status)
{
   if (status != UART_STATUS_SUCCESS) {
   /* RX error occured in UART2_read() */
   while (1);
   }

   numBytesRead = count;
   sem_post(&sem);
}

I am trying to receive data using read function as below:

int GetRawCmd(char *pcBuffer, unsigned int uiBufLen)
{
   char cChar;
   int buffindex = 0;
   uint32_t status = UART_STATUS_SUCCESS;

   while (1) {
       numBytesRead = 0;

       /* Pass NULL for bytesRead since it's not used in this example */
       status = UART_read(uartHandle, &cChar, 1);

       Display_printf(display, 0, 0, "out of read\r\n");

       if (status != UART_STATUS_SUCCESS) {
            /* UART_read() failed */
           Display_printf(display, 0, 0, "read failed\r\n");
            while (1);
       }

       Display_printf(display, 0, 0, "waiting for callback\r\n");

       /* Do not write until read callback executes */
       sem_wait(&sem);

       Display_printf(display, 0, 0, "callback recv\r\n");

       if((cChar == '\r') || (cChar == '\n')){
           pcBuffer[buffindex] = '\0';
           break;
       }else{
           pcBuffer[buffindex] = cChar;
          buffindex++;
      }
   }

   return buffindex;

}

I am using second uart port for debug statements. The following debug statements are displayed:

out of read

waiting for callback

No further debug statements are received. This means that it keep waiting for semaphore which is released in callback function. Call back is not received even if I sent characters from tera term on PC, I am using USB to serial converter to connect second uart port to PC. This setup function good if I am using blocking uart mode.

Please help with debugging as why callback function is not received.

  • Hi,

    Using UART_control(uartHandle, UART_CMD_RXDISABLE, NULL); is problematic as it allows ELP and disable all interrupts.

    This is why you probably don't get the callback interrupt.

    See thread with explanations here https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/1049642/cc3220sf-what-is-uart_control-uarthandle-uart_cmd_rxdisable-null-in-demo-code-at_commands?tisearch=e2e-sitesearch&keymatch=UART_CMD_RXDISABLE#

    Regards,

    Shlomi

  • Hi,

    I removed UART_control(uartHandle, UART_CMD_RXDISABLE, NULL);  from the program still the response is same i.e. it stuck in GetRawCmd() at sem_wait(&sem);

    I have a question :

    1. When UART_read(uartHandle, &cChar, 1); come out after set timeout as there is no input on uart port, then will we receive callback or not?

    2. I have set timeout value as uartParams.readTimeout = 1000;. I am not sure if value 1000 is taken in microsecond or milisecond? 

    Thanks,

    Pradeep

  • Hi,

    see this thread for the timeout units https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/648684/cc3220-simplelink-sdk-uart_params-readtimeout

    I need to look into it further to why it is not working. Do you use two UART interfaces in your project?

    Regards,

    Shlomi

  • Thanks for link for timeout units.

    Yes I am using two UART ports in my project. 

    I am using simplesample_http example project and updating same to use another UART port. 

  • Hi,

    I believe that your issue may be related to the readReturnMode.

    As you can see on the default values when calling UART_Params_init(&uartParams);, the values are:

    const UART_Params UART_defaultParams = {
    UART_MODE_BLOCKING, /* readMode */
    UART_MODE_BLOCKING, /* writeMode */
    UART_WAIT_FOREVER, /* readTimeout */
    UART_WAIT_FOREVER, /* writeTimeout */
    NULL, /* readCallback */
    NULL, /* writeCallback */
    UART_RETURN_NEWLINE, /* readReturnMode */
    UART_DATA_TEXT, /* readDataMode */
    UART_DATA_TEXT, /* writeDataMode */
    UART_ECHO_ON, /* readEcho */
    115200, /* baudRate */
    UART_LEN_8, /* dataLength */
    UART_STOP_ONE, /* stopBits */
    UART_PAR_NONE, /* parityType */
    NULL /* custom */
    };

    meaning you would get a callback only on UART_RETURN_NEWLINE.

    can you change to: uartParams.readDataMode = UART_DATA_BINARY;

    Regards,

    Shlomi

  • fixing my last response, I meant changing readReturnMode.

    uartParams.readReturnMode = UART_RETURN_FULL;
  • Hi,

    I tried but again it wait at "sem_wait(&sem);" in GetRawCmd() function which means there is no callback function called. I have a doubt here:

    Uart_read() function come out after timeout as no data received from other side. In such a case, can we still expect callback function to be called? 

    if no, then there is no meaning in waiting for sem_wait(&sem); because sem_post(&sem) will never happen as callback is not called.

    Having this thought, I commented out sem_wait(&sem) and checked if(numbytesread ==0), if yes then  go back to uart_read(). But when i call uart_read() for second time, it returns UART_STATUS_FAILED.

  • Hi,

    There seems to be some confusion here.

    How does your syscfg file looks like?

    you mentioned that you are using two UART interfaces.

    The one that is configured in your code snippet is of type  UART_Params and the callback in this structure is of type:

    void callbackFxn(UART_Handle handle, void *buffer, size_t count,
                                                 void *userArg, int_fast16_t status)

    which does not make sense since under UART_Params, the readCallback is of type UART_Callback which is:

    void (*UART_Callback) (UART_Handle handle, void *buf, size_t count);

    how does it even compile?

    Shlomi

  • Hi,

    Attached is my syscfg file. 

    I changed uart callback function the way you have indicated. It also gets compiled but callback is not received.

    I am using simpleLink SDK version 4.10, Does this has any connection with format of uart callback function?

    I also noticed that syscfg file has option of enter "Error callback function name". I tried this too but not did not get success. Do we have to enter this name? if yes, should it be only name of function like callbackfxn or complete format like void (*UART_Callback) (UART_Handle handle, void *buf, size_t count);?

  • Hi,

    SDK version 4.10 is very old but I do not believe this is your issue.

    The sysconfig file is also not attached.

    Anyway, it is still confusing whether you are using UART or UART2.

    What I suggest is:

    • if you are using UART2, there is an example called uart2callback in the SDK. I checked it and it is working (placed a breakpoint inside the callback and it stopped there).
    • if you are using UART, there is no example with callback. So I based it on uart2callback and created the same and tested.
      You can find the uartcallback.c and the sysconfig file attached.

    Regards,

    Shlomi

    https://e2e.ti.com/cfs-file/__key/communityserver-discussions-components-files/968/uartcallback.syscfg

    /*
     * Copyright (c) 2020, 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.
     */
    
    /*
     *  ======== uartcallback.c ========
     */
    #include <stdint.h>
    #include <stddef.h>
    
    /* POSIX Header files */
    #include <semaphore.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    
    /* Driver configuration */
    #include "ti_drivers_config.h"
    
    static sem_t sem;
    static volatile size_t numBytesRead;
    
    /*
     *  ======== callbackFxn ========
     */
    void callbackFxn(UART_Handle handle, void *buffer, size_t count)
    {
    
    
        numBytesRead = count;
        sem_post(&sem);
    }
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        char              input;
        const char        echoPrompt[] = "Echoing characters:\r\n";
        UART_Handle      uart;
        UART_Params      uartParams;
        int32_t           semStatus;
        uint32_t          status = UART_STATUS_SUCCESS;
    
        /* Call driver init functions */
        GPIO_init();
    
        UART_init();
    
        /* Configure the LED pin */
        GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Create semaphore */
        semStatus = sem_init(&sem, 0, 0);
    
        if (semStatus != 0) {
            /* Error creating semaphore */
            while (1);
        }
    
        /* Create a UART in CALLBACK read mode */
        UART_Params_init(&uartParams);
        uartParams.writeDataMode = UART_DATA_BINARY;
        uartParams.readDataMode = UART_DATA_BINARY;
        uartParams.readMode = UART_MODE_CALLBACK;
        uartParams.readCallback = callbackFxn;
        uartParams.baudRate = 115200;
    
        uart = UART_open(CONFIG_UART_0, &uartParams);
    
        if (uart == NULL) {
            /* UART_open() failed */
            while (1);
        }
    
        /* Turn on user LED to indicate successful initialization */
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
    
        /* Pass NULL for bytesWritten since it's not used in this example */
        UART_write(uart, echoPrompt, sizeof(echoPrompt));
    
        /* Loop forever echoing */
        while (1) {
            numBytesRead = 0;
    
            /* Pass NULL for bytesRead since it's not used in this example */
            status = UART_read(uart, &input, 1);
    
            if (status != UART_STATUS_SUCCESS) {
                /* UART_read() failed */
                while (1);
            }
    
            /* Do not write until read callback executes */
            sem_wait(&sem);
    
            if (numBytesRead > 0) {
                status = UART_write(uart, &input, 1);
    
                if (status != 1) {
                    /* UART_write() failed */
                    while (1);
                }
            }
        }
    }
    

  • Hi,

    How do i attach a file? Last time I did drag and drop but it seems it didn't got attached.

  • Hi,

    Let me try to explain the problem again:

    I am using simplesample_http example. This is implemented with freeRTOS. In example I have single task which takes hard coded data and send to Iot Hub. Till this everything works fine.

    Now, I have introduced second task which takes input data from UART port and will share the same data with simplesample task to send data to Iot hub. So, instead of sending hardcoded data, now I am sending data received from UART port.

    In my second task, UART_read()  blocks the task and it also blocks simplesample task. Due to this I am not able to receive any data from IoT Hub. Hence, for UART configuration, I am setting callback mode so that it does not block the task. However, it still blocks the task as default timeout in callback mode is 0. So i tried to set timeout as 1000. In this case UART_read() come out and wait for semaphore which again blocks both the tasks.

    I am expecting UART callback to be received even if UART_read() come out because of timeout. is this correct expectation? 

  • Hi,

    Can you please guide me further on above issue?

    Thanks,

    Pradeep

  • Hi,

    Sorry but I was out of office.

    Not sure why the callback is not working for you but my code snippet works on my end.

    Anyway, as for the read timeout, it is for blocking mode and not callback mode. This is why it is not working.

    As a workaround for the callback mode, you can use blocking mode with low timeout so the API is not completely blocked and see if it works for you.

    Regards,

    Shlomi