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.

CCS/LAUNCHXL-CC3235S: Unable to send data over UART (cc3235s as master) to slave (works on rs232).

Part Number: LAUNCHXL-CC3235S
Other Parts Discussed in Thread: CC3235S

Tool/software: Code Composer Studio

Hi TI,

I am trying to establish connection between my launch pad cc3235s as master and power supply (uses rs232) for that i have usb to ttl converter.

and here i am using both uart port of cc3235s, one for debugger and another for ttl. see following picture to get idea what i am trying to do.

firstly i want to establish connection with single slave once it is done than i will send same data with some modification(as per requirement) to the another slave.

in first step: 

i am using uartecho program.

uint8_t getStatusB[] = { 0xFF, 0x08, 0x02, 0x00, 0x01, 0x01, 0x00, 0x0B };

this is the command which i want to sent over uart and in response i should get 28 byte of packet. for i have declared

uint8_t receivedDataPacket[28];

here is my uart initialization part 

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_FULL;
        uartParams.readCallback=(UART_Callback)readCallback;
//        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.dataLength = UART_LEN_8;
        uartParams.stopBits = UART_STOP_ONE;
        uartParams.parityType = UART_PAR_NONE;
        uartParams.baudRate = 9600;

        uart = UART_open(CONFIG_UART_0, &uartParams);
        uart2 = UART_open(CONFIG_UART_1, &uartParams);

        if (uart2 == NULL)
        {
            /* UART_open() failed */
            while (1)
                ;
        }
        if (uart == NULL)
        {
           /* UART_open() failed */
           while (1)
               ;
        }

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

i am using both uarts.

hardware xds110 is connected with uart 0 so debugger is working well. and second uart is connected with my ttl pin p03 and p04 , and my slave is not connected only ttl is connected with controller. i am getting echo character over p03 and p04 (or on serial port).

but when i am connecting slave and sending command data instead of single char, no response is coming back.

please help me out and do let me known where i am making mistake.

here is my complete code.

/*
 * Copyright (c) 2015-2019, 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>
#include <unistd.h>
#include <string.h>

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

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

typedef enum
{
    INTERNAL_LIBRARY, EXTERNAL_LIBRARY
} intEx;

intEx interanlLIB_or_externaLIB = INTERNAL_LIBRARY;

int i = 0;
static void sendData(void);
static void setData(void);
static void readCallback(UART_Handle handle, void *buffer, size_t size);

char input2;

uint8_t getStatusB[] = { 0xFF, 0x08, 0x02, 0x00, 0x01, 0x01, 0x00, 0x0B };

uint8_t setToRectBQry[] = { 0xFF, 0x12, 0x02, 0x00, 0x00, 0x01, 0x0C, 0x01,
                            0xC7, 0x03, 0xE8, 0x02, 0x4E, 0x01, 0x00, 0x00,
                            0x00, 0x24 };

UART_Handle uart;
UART_Handle uart2;

uint8_t receivedDataPacket[28];

/*
 *  ======== mainThread ========
 */
void* mainThread(void *arg0)
{
    uint8_t input;

    const char echoPrompt[] = "Echoing characters from uart:\r\n";
    const char echoPrompt2[] = "Echoing characters from uart2:\r\n";
    UART_Params uartParams;

    /* Call driver init functions */
    GPIO_init();

    switch (interanlLIB_or_externaLIB)
    {
    case INTERNAL_LIBRARY:
        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_FULL;
        uartParams.readCallback=(UART_Callback)readCallback;
//        uartParams.readEcho = UART_ECHO_OFF;
        uartParams.dataLength = UART_LEN_8;
        uartParams.stopBits = UART_STOP_ONE;
        uartParams.parityType = UART_PAR_NONE;
        uartParams.baudRate = 9600;

        uart = UART_open(CONFIG_UART_0, &uartParams);
        uart2 = UART_open(CONFIG_UART_1, &uartParams);

        if (uart2 == NULL)
        {
            /* UART_open() failed */
            while (1)
                ;
        }
        if (uart == NULL)
        {
           /* UART_open() failed */
           while (1)
               ;
        }

        UART_write(uart, echoPrompt, sizeof(echoPrompt));
        UART_write(uart2, echoPrompt2, sizeof(echoPrompt2));
        break;
    case EXTERNAL_LIBRARY:
        InitTerm();
        UART_PRINT("Echoing characters:---\r\n");
        break;
    };

    /* Configure the LED pin */
    GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

    /* Turn on user LED */
    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);

    memset((void*) receivedDataPacket, 0, sizeof(receivedDataPacket));

//    sendData();
   setData();
//    while (1)
//    {
//        usleep(20000);
//        UART_read(uart2, &input, sizeof(input));
//        UART_write(uart2, &input, sizeof(input));
//    }
}

static void sendData()
{
    /* Loop forever echoing */
    while (i < sizeof(setToRectBQry))
    {
        switch (interanlLIB_or_externaLIB)
        {
        case INTERNAL_LIBRARY:
//            UART_write(uart2, &getStatusB[i], sizeof(getStatusB[i]));
            UART_writePolling(uart2, &setToRectBQry[i], sizeof(setToRectBQry[i]));
            i++;
            break;
        case EXTERNAL_LIBRARY:
            Message((char*)getStatusB[i++]);
            usleep(20000);
//            UART_writePolling(uart, getStatusB, sizeof(getStatusB));
            break;
        };
        //        UART_write(uart, sendDataPacket, sizeof(sendDataPacket));
        //        UART_read(uart, &input, 1);
    }
}

static void setData()
{
    i = 0;
    /* Loop forever echoing */
    while (i < sizeof(setToRectBQry))
    {
        switch (interanlLIB_or_externaLIB)
        {
        case INTERNAL_LIBRARY:
            UART_write(uart2, &setToRectBQry[i], sizeof(setToRectBQry[i]));
//            UART_writePolling(uart2, &setToRectBQry[i],sizeof(setToRectBQry[i]));
            i++;
            break;
        case EXTERNAL_LIBRARY:
            UART_writePolling(uart, getStatusB, sizeof(getStatusB));
            break;
        };
        //        UART_write(uart, sendDataPacket, sizeof(sendDataPacket));
        //        UART_read(uart, &input, 1);
    }
}

static void readCallback(UART_Handle handle, void *buffer, size_t size)
{
    if(0xFF==(*(uint8_t*)buffer))
    {
        if(size==28)
        {
//            ddress_h = ((uint8_t*)buffer)[1];
//            address_l = ((uint8_t*)buffer)[2];
//            cmd = ((uint8_t*)buffer)[3];
//            data = ((uint8_t*)buffer)[4];
//            checksum = ((uint8_t*)buffer)[5];
            UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket));
        }
        else // size will always be 28 so this is unneccessary
        {
            UART_readCancel(uart2);
        }
    }
    else 
   {
       UART_readCancel(uart2);
   }
//    i=0;
//    while(i<sizeof(receivedDataPacket))
//    {
//        UART_write(uart2, &receivedDataPacket[i], sizeof(receivedDataPacket[i]));
//        i++;
//    }
}

thanks in advance.

Br, 

sarju bhatnagar

  • Hi Sarju,

    The initialization code looks fine. Running that on my end results in the UART interface being setup and opened correctly. That being said, if you are using a readCallback, please also set the readMode as UART_MODE_CALLBACK. Without that set, you will not get returned data to your callback.

    Running your provided sample code also seems to result in the expected behavior occurring.Have you ensured you performed a UART_read() with callback mode enabled?

    Have you also checked the UART signals with a logic analyzer to ensure that the TX signals are getting to the slave correctly, and that it is sending back the expected response?

    Regards,

    Michael

  • Hi Michael,

    Thanks for reply, at this stage i am able to send command to slave and it's changing it's state accordingly. and it should send back response packet of 28 byte. 

    but, i am not able to get that packet.

    in short my callback function is not getting call ever. i did the changes as you said. have a look..

     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_FULL;
            uartParams.readMode=UART_MODE_CALLBACK;
            uartParams.readCallback=(UART_Callback)readCallback;
    //        uartParams.readEcho = UART_ECHO_OFF;
            uartParams.dataLength = UART_LEN_8;
            uartParams.stopBits = UART_STOP_ONE;
            uartParams.parityType = UART_PAR_NONE;
            uartParams.baudRate = 9600;
    
            uart = UART_open(CONFIG_UART_0, &uartParams);
            uart2 = UART_open(CONFIG_UART_1, &uartParams);
    
            if (uart2 == NULL)
            {
                /* UART_open() failed */
                while (1)
                    ;
            }
            if (uart == NULL)
            {
               /* UART_open() failed */
               while (1)
                   ;
            }

    and here is my callback function

    static void readCallback(UART_Handle handle, void *buffer, size_t size)
    {
            UART_write(uart2, echoPrompt, sizeof(echoPrompt));
    
    //    UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket));
        if(0xFF==(*(uint8_t*)buffer))
        {
            if(size==28)
            {
    //            ddress_h = ((uint8_t*)buffer)[1];
    //            address_l = ((uint8_t*)buffer)[2];
    //            cmd = ((uint8_t*)buffer)[3];
    //            data = ((uint8_t*)buffer)[4];
    //            checksum = ((uint8_t*)buffer)[5];
                UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket));
            }
            else // size will always be 28 so this is unneccessary
            {
                UART_readCancel(uart2);
            }
        }
        else
        {
            UART_readCancel(uart2);
        }
    //    i=0;
    //    while(i<sizeof(receivedDataPacket))
    //    {
    //        UART_write(uart2, &receivedDataPacket[i], sizeof(receivedDataPacket[i]));
    //        i++;
    //    }
    
    //    UART_write(uart2, receivedDataPacket, sizeof(receivedDataPacket));
    
    }

    this function is not getting call ever.

    every time whenever i am running my program , this is what i am getting on the terminal windows.

    and slave is changing it's stage immediately, which is good as expected. it means i am able to send data(command), but not able to receive.

    please help me to get rid from this problem.

    thank you so much for your reply once again.

    waiting for your response further...

    Br,

    sarju bhatnagar

  • Hi Sarju,

    Do you perform a UART_read() call in your application after you write the data to the slave?

    As you can see in the original example, you need to perform a UART_read(), and provide an input buffer as well as a total read size before the UART interface will read any data. If you perform a UART_read() in your code after sending the command data do you get the expected data back?

    Reworking your code, it should look something like this:

    void* mainThread(void *arg0)
    {
        uint8_t input;
    
        const char echoPrompt[] = "Echoing characters from uart:\r\n";
        const char echoPrompt2[] = "Echoing characters from uart2:\r\n";
        UART_Params uartParams;
    
        /* Call driver init functions */
        GPIO_init();
    
        switch (interanlLIB_or_externaLIB)
        {
        case INTERNAL_LIBRARY:
            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_FULL;
            uartParams.readCallback=(UART_Callback)readCallback;
    //        uartParams.readEcho = UART_ECHO_OFF;
            uartParams.dataLength = UART_LEN_8;
            uartParams.stopBits = UART_STOP_ONE;
            uartParams.parityType = UART_PAR_NONE;
            uartParams.baudRate = 115200;
    
            uart = UART_open(CONFIG_UART_0, &uartParams);
            uart2 = UART_open(CONFIG_UART_1, &uartParams);
    
            if (uart2 == NULL)
            {
                /* UART_open() failed */
                while (1)
                    ;
            }
            if (uart == NULL)
            {
               /* UART_open() failed */
               while (1)
                   ;
            }
    
            UART_write(uart, echoPrompt, sizeof(echoPrompt));
            UART_write(uart2, echoPrompt2, sizeof(echoPrompt2));
            break;
        case EXTERNAL_LIBRARY:
            InitTerm();
            UART_PRINT("Echoing characters:---\r\n");
            break;
        };
    
        /* Configure the LED pin */
        GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Turn on user LED */
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
    
        memset((void*) receivedDataPacket, 0, sizeof(receivedDataPacket));
    
    //    sendData();
       setData();
       UART_read(uart2,receivedDataPacket,sizeof(receivedDataPacket));
    //    while (1)
    //    {
    //        usleep(20000);
    //        UART_read(uart2, &input, sizeof(input));
    //        UART_write(uart2, &input, sizeof(input));
    //    }
    }
    
    
    static void readCallback(UART_Handle handle, void *buffer, size_t size)
    {
        if(0xFF==(*(uint8_t*)buffer))
        {
            if(size==28)
            {
    //            ddress_h = ((uint8_t*)buffer)[1];
    //            address_l = ((uint8_t*)buffer)[2];
    //            cmd = ((uint8_t*)buffer)[3];
    //            data = ((uint8_t*)buffer)[4];
    //            checksum = ((uint8_t*)buffer)[5];
                //Signal to application here through a semaphore/mutex/message queue that data is ready
            }
            else // size will always be 28 so this is unneccessary
            {
                //Error state, didn't get correct data from slave
                //UART_readCancel(uart2);
            }
        }
        else
       {
           //UART_readCancel(uart2);
       }
    
    }
    

    Regards,
    Michael

  • Hi Michael,

    you: you need to perform a UART_read()

    Yes i tried this as well see this

     setData();
        usleep(20000);
        SendToGetStatusRespFromRect2(getStatusFromRect2Packet, sizeof(getStatusFromRect2Packet));
        usleep(20000);
        UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket));

    setData(); functions is able to send the data and salve changes its state. so i can say communication is established.

    secondly.

    SendToGetStatusRespFromRect2(getStatusFromRect2Packet, sizeof(getStatusFromRect2Packet));

    is to get response back from slave.

    UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket)); is the function which used to receive data.

    Ques: I need to use only UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket)); to receive data, but what about to print data on the terminal screen.Ques: when readCallback function will be called?should i use UART_write(uart2, receivedDataPacket, sizeof(receivedDataPacket));inside readcallback function print the data on terminal screen.i tried many ways to get data over uart

    1:
    setData();
        usleep(20000);
        SendToGetStatusRespFromRect2(getStatusFromRect2Packet, sizeof(getStatusFromRect2Packet));
        usleep(20000);
       // UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket));
        usleep(20000);
    
        while(UART_readPolling(uart2, receivedDataPacket, sizeof(receivedDataPacket))!='\0');
        {
          //  i=0;
         //   while(i<sizeof(receivedDataPacket))
           // {
                UART_write(uart2, receivedDataPacket, sizeof(receivedDataPacket));
             //   i++;
           // }
        }
    2:
    
     setData();
        usleep(20000);
        SendToGetStatusRespFromRect2(getStatusFromRect2Packet, sizeof(getStatusFromRect2Packet));
        usleep(20000);
        UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket));
        usleep(20000);
    
    //    while(UART_readPolling(uart2, &receivedDataPacket, sizeof(receivedDataPacket))!='\0');
    //    {
            i=0;
            while(i<sizeof(receivedDataPacket))
            {
                UART_write(uart2, &receivedDataPacket[i], sizeof(receivedDataPacket[i]));
                i++;
            }

    3:

     setData();
        usleep(20000);
        SendToGetStatusRespFromRect2(getStatusFromRect2Packet, sizeof(getStatusFromRect2Packet));
        usleep(20000);
    //    UART_read(uart2, receivedDataPacket, sizeof(receivedDataPacket));
        UART_readPolling(uart2, &receivedDataPacket, sizeof(receivedDataPacket));
        usleep(20000);
    
    //    while(UART_readPolling(uart2, &receivedDataPacket, sizeof(receivedDataPacket))!='\0');
    //    {
            i=0;
            while(i<sizeof(receivedDataPacket))
            {
                UART_write(uart2, &receivedDataPacket[i], sizeof(receivedDataPacket[i]));
                i++;
            }
    //    }

    but, not able to get data.

    please do let me know what else should i try?

    Thanks,

    sarju bhatnagar






  • Dear Team,

    Waiting for reply?

    thanks

    sarju

  • this time i am able to send data to UART0 using pin03 and p04 , 

    but as i mentioned above i want to talk to two uart slaves at the same time, in short i want to take data from one uart that is uart0 and same data i want to show to another slave(display) through uart1 . 

    but i am unable to show the data on slave(display) through uart1.

    how can i use another UART1 on pin55(tx/gp01) and pin57(rx/gp02), please do let me know?

    thanks

    sarju

  • Hi Sarju,

    I made a simple test program based on the uartecho SDK example to test uart0 + uart1 echo functionality much like what you're trying to do by reading data from slave 0 and passing it to slave 1 and everything works as expected.

    I have this code using p55/57 as UART0 through the XDS110 launchpad debugger, as well as p3/4 for UART1 through an FTDI UART to USB adaptor, and I have verified that the functionality works correctly on my CC3235 launchpad.

    The example code is attached here, simply import the uartecho project and overwrite the existing uartecho.c with this code: /cfs-file/__key/communityserver-discussions-components-files/968/uartecho2.c

    Please use that code as an example of how to have two UART peripherals passing data to each other, and I advise you either to base your UART communication code off of my example or to carefully compare your UART code with the working code provided and debug the changes needed.

    Regards,

    Michael

  • Thank you so much for your help,

    my problem has been resolved.

    Thanks again.

    sarju bhatnagar