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.

CC3220SF: Why read callback fail with UART.h?

Part Number: CC3220SF


Hi, Support Team,

 

Our product use CC3220SF SoC. SimpleLink-SDK version: simplelink_cc32xx_sdk_4_30_00_06.

In demo code “at_commands”(path: C:\ti\simplelink_cc32xx_sdk_4_30_00_06\examples\rtos\CC3220SF_LAUNCHXL\demos\at_commands\tirtos\ccs), use UART.h not UART2.h.

 

I use example code “uartecho” and modify "uartecho.c" code and run code in Launchpad.

My question:

  1. callback function is ISR(Interrupt Service Routine) ?
  2. Line 112, if no UART_read(uart, &input, 1), then callback function not working, why?
  3. I send “$4C$53$31$32$33$34$35$23” at the same time by Terminal v1.9v program, But I never get “RightMatch” message.
  4. Can not use readcallback function in UART.h ? Because I did not see any example or demo code use readcallback function with UART.h.

 

/*
 * Copyright (c) 2015-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.
 */

/*
 *  ======== uartecho.c ========
 */
#include <stdint.h>
#include <stddef.h>

/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/UART.h>

/* Driver configuration */
#include "ti_drivers_config.h"

volatile char g_ucInput = 0;
volatile char g_ucRxDBuf[8] = {0};
volatile char g_ucTestLEDFlag = 0;

UART_Handle uart;
UART_Params uartParams;

void readcallback_LS (UART_Handle handle, void *buf, size_t count)
{
  char i;
  char i_input;
  UART_read(uart, &i_input, 1);
  g_ucInput = i_input;
  for(i=7; i>=1; i--)
  {
    g_ucRxDBuf[i] = g_ucRxDBuf[i-1];
  }
  g_ucRxDBuf[0]=g_ucInput;
} // End void readcallback_LS (UART_Handle handle, void *buf, size_t count)


/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
    char        input;
    const char  echoPrompt[] = "Echoing characters:\r\n";
    const char  RightMatch[] = "Match J-MEX command\r\n";


    /* 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 a UART with data processing off. */
    UART_Params_init(&uartParams);
    uartParams.readMode = UART_MODE_CALLBACK;
    uartParams.writeMode = UART_MODE_BLOCKING;
    uartParams.readCallback = readcallback_LS;
    uartParams.readReturnMode = UART_RETURN_FULL;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.baudRate = 115200;
    uartParams.dataLength = UART_LEN_8;
    uartParams.stopBits = UART_STOP_ONE;
    uartParams.parityType = UART_PAR_NONE;

    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);

    UART_write(uart, echoPrompt, sizeof(echoPrompt));

    UART_control(uart, UART_CMD_RXENABLE, NULL);

    UART_read(uart, &input, 1);

    /* Loop forever echoing */
    while (1) {
      // Lishen add[S]

      if( (g_ucRxDBuf[7]==0x4C) && (g_ucRxDBuf[6]==0x53) && (g_ucRxDBuf[0]==0x23) )
      {
        UART_write(uart, RightMatch, sizeof(RightMatch));
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
      }
      else
      {
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);
      }
    }
}

Thanks,

Lishen

  • Hi,

    1. callback function is ISR(Interrupt Service Routine) ?

    yes, the context is of a hardware interrupt

    1. Line 112, if no UART_read(uart, &input, 1), then callback function not working, why?

    callback function should be triggered by calling the read() or write().

    When the read or write finishes, the UART driver should call the user's callback function.

    1. I send “$4C$53$31$32$33$34$35$23” at the same time by Terminal v1.9v program, But I never get “RightMatch” message.

    what do you get instead?

    1. Can not use readcallback function in UART.h ? Because I did not see any example or demo code use readcallback function with UART.h.

    you are right, there is no example with UART although it should work.

    Shlomi

  • Dear Shlomi,

    I reference uart2callback sample code, I modify and test this code is good for me. Next I will create a new thread to service this while(1){...} and get 24 Bytes RX data. Thanks for your help.

    /*
     * Copyright (c) 2015-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.
     */
    
    /*
     *  ======== uartecho.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"
    
    volatile char g_ucInput = 0;
    //volatile char g_ucRxDBuf[8] = {0};
    volatile char g_ucRxDBuf[24] = {0};
    volatile char g_ucTestLEDFlag = 0;
    
    
    static sem_t sem_UartRxCallBack;
    
    #if (0)
    void readcallback_LS (UART_Handle handle, void *buf, size_t count)
    {
      char i;
      char i_input;
      UART_read(uart, &i_input, 1);
      g_ucInput = i_input;
      for(i=7; i>=1; i--)
      {
        g_ucRxDBuf[i] = g_ucRxDBuf[i-1];
      }
      g_ucRxDBuf[0]=g_ucInput;
    } // End void readcallback_LS (UART_Handle handle, void *buf, size_t count)
    #endif
    
    void readcallback_LS (UART_Handle handle, void *buf, size_t count)
    {
      sem_post(&sem_UartRxCallBack);
    }
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        unsigned char i;
        char        input;
        const char  echoPrompt[] = "Echoing characters:\r\n";
        const char  RightMatch[] = "Match J-MEX WiFi-Moti command\r\n";
        UART_Params uartParams;
        UART_Handle uart;
    
        /* 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 */
        sem_init(&sem_UartRxCallBack, 0, 0);
    
        /* Create a UART with data processing off. */
        UART_Params_init(&uartParams);
        uartParams.readMode = UART_MODE_CALLBACK;
        uartParams.writeMode = UART_MODE_BLOCKING;
        uartParams.readCallback = readcallback_LS;
        uartParams.readReturnMode = UART_RETURN_FULL;
        uartParams.readDataMode = UART_DATA_BINARY;
        uartParams.writeDataMode = UART_DATA_BINARY;
        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.baudRate = 115200;
        uartParams.dataLength = UART_LEN_8;
        uartParams.stopBits = UART_STOP_ONE;
        uartParams.parityType = UART_PAR_NONE;
    
        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);
    
        UART_write(uart, echoPrompt, sizeof(echoPrompt));
    
        UART_control(uart, UART_CMD_RXENABLE, NULL);      //callback function will work. so this is enable RX interrupt.
        //UART_control(uart, UART_CMD_RXDISABLE, NULL);     //callback function will not working. so this is disable RX interrupt.
    
        /* Loop forever echoing */
        while (1)
        {
          UART_read(uart, &input, 1);
          /* until read callback executes */
          sem_wait(&sem_UartRxCallBack);
          for(i=23; i>=1; i--)
          {
            g_ucRxDBuf[i] = g_ucRxDBuf[i-1];
          }
          g_ucRxDBuf[0]=input;
    
          if( (g_ucRxDBuf[23]==0x4C) && (g_ucRxDBuf[22]==0x53) && (g_ucRxDBuf[21]==0x14) && (g_ucRxDBuf[0]==0x23) )
          {
            UART_write(uart, RightMatch, sizeof(RightMatch));
            GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
            for(i=0; i<=23; i++)
            {
              g_ucRxDBuf[i] = 0;    // reset all UART RxD command Buffer
            }
          }
          else
          {
            GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);
          }
        }
    }
    

    Best Regards,

    Lishen