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/LP-CC2652RB: Help with a merged code for uartecho & fatsd

Part Number: LP-CC2652RB
Other Parts Discussed in Thread: CC2652RB

Tool/software: Code Composer Studio

Hello, 

I have an application to write incoming uart data to an sd card. For this application i have tried to merge uartecho & fatsd but unable to do it.
Can someone suggest me how to proceed ahead?
I am attaching the developed code in txt file. 
I am not getting any message in the sd card.

#include <file.h>
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <third_party/fatfs/ffcio.h>
/* Driver Header files */
#include <ti/display/Display.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/UART.h>
#include <ti/drivers/SDFatFS.h>
/* Driver configuration */
#include "ti_drivers_config.h"
/* Buffer size used for the file copy process */
#ifndef CPY_BUFF_SIZE
#define CPY_BUFF_SIZE       2048
#endif
/* String conversion macro */
#define STR_(n)             #n
#define STR(n)              STR_(n)
/* Drive number used for FatFs */
#define DRIVE_NUM           0
const char inputfile[] = "fat:"STR(DRIVE_NUM)":input.txt";
char textarray[100];
static Display_Handle display;
/* File name prefix for this filesystem for use with TI C RTS */
char fatfsPrefix[] = "fat";
unsigned char cpy_buff[CPY_BUFF_SIZE + 1];
void *mainThread(void *arg0)
{
    char        input;
    unsigned int i=0;
    UART_Handle uart;
    UART_Params uartParams;
    SDFatFS_Handle sdfatfsHandle;
    /* Variables for the CIO functions */
    FILE *src;
    unsigned int filesize;
/* Call driver init functions */
    GPIO_init();
    UART_init();
    Display_init();
    SDFatFS_init();
/* Configure the LED pin */
    GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    /* add_device() should be called once and is used for all media types */
    add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
            ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
/* Turn on user LED */
    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_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.baudRate = 115200;
    uart = UART_open(CONFIG_UART_0, &uartParams);
    if (uart == NULL) {
        /* UART_open() failed */
        while (1);
    }
    /* Loop forever echoing */
    while (1)
    {
        
        /* Mount and register the SD Card */
                       sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM);
                       if (sdfatfsHandle == NULL) {
                           Display_printf(display, 0, 0, "Error starting the SD card\n");
                           while (1);
                       }
                       else {
                           Display_printf(display, 0, 0, "Drive %u is mounted\n", DRIVE_NUM);
                       }

        for (i=0; i < strlen (textarray); i++ )
        {
            char c = UART_read(uart, &input, 1);
            UART_write(uart, &input, 1);
            textarray [i] = c;
        }
        /* Try to open the source file */
        src = fopen(inputfile, "r");
        if (!src) {
        Display_printf(display, 0, 0, "Creating a new file \"%s\"...", inputfile);
        /* Open file for both reading and writing */
        src = fopen(inputfile, "w+");
        if (!src) {
            Display_printf(display, 0, 0,    "Error: \"%s\" could not be created.\nPlease check the "
                                              "Board.html if additional jumpers are necessary.\n",
                                              inputfile);
            Display_printf(display, 0, 0, "Aborting...\n");
            while (1);
            }
        fwrite (textarray,1,strlen(textarray),src);
        fflush(src);
        /* Reset the internal file pointer */
        rewind(src);
        Display_printf(display, 0, 0, "done\n");
    }
        else {
                Display_printf(display, 0, 0, "Using existing copy of \"%s\"\n",
                    inputfile);
            }
}
}

/*
 *  ======== fatfs_getFatTime ========
 */
int32_t fatfs_getFatTime(void)
{
    /*
     *  FatFs uses this API to get the current time in FatTime format.  User's
     *  must implement this function based on their system's timekeeping
     *  mechanism.  See FatFs documentation for details on FatTime format.
     */
    /* Jan 1 2017 00:00:00 */
    return (0x4A210000);
}





  • I believe that this is not something we have tested hence the slow feedback. 

    Have you had any progress on this? 

  • I remember I had helped you on similar topic. What specific problem do you see now?

  • Yes you had helped and thanks for your support but the problem was not solved. I am able to write into the sd card and also read the incoming uart data from the cc2652rb.
    My goal is to write the incoming uart data onto the sd card which is pending. You had suggested to use a different task but i believe that it is not required we can still do that in the same code. I am attaching the code file which i am trying to make it work. Please suggest me changes in the same.

    #include <file.h>
    #include <stdbool.h>
    #include <stdint.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <third_party/fatfs/ffcio.h>
    /* Driver Header files */
    #include <ti/display/Display.h>
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    #include <ti/drivers/SDFatFS.h>
    /* Driver configuration */
    #include "ti_drivers_config.h"
    /* Buffer size used for the file copy process */
    #ifndef CPY_BUFF_SIZE
    #define CPY_BUFF_SIZE       2048
    #endif
    /* String conversion macro */
    #define STR_(n)             #n
    #define STR(n)              STR_(n)
    /* Drive number used for FatFs */
    #define DRIVE_NUM           0
    const char inputfile[] = "fat:"STR(DRIVE_NUM)":input.txt";
    char textarray[10];
    static Display_Handle display;
    /* File name prefix for this filesystem for use with TI C RTS */
    char fatfsPrefix[] = "fat";
    unsigned char cpy_buff[CPY_BUFF_SIZE + 1];
    void *mainThread(void *arg0)
    {
        char        input;
        unsigned int i=0;
        UART_Handle uart;
        UART_Params uartParams;
        SDFatFS_Handle sdfatfsHandle;
        /* Variables for the CIO functions */
        FILE *src;
       // unsigned int filesize;
    /* Call driver init functions */
        GPIO_init();
        UART_init();
        Display_init();
        SDFatFS_init();
    /* Configure the LED pin */
        GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
        /* add_device() should be called once and is used for all media types */
        add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
                ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
    /* Turn on user LED */
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_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.baudRate = 115200;
        uart = UART_open(CONFIG_UART_0, &uartParams);
        if (uart == NULL) {
            /* UART_open() failed */
            while (1);
        }
        /* Mount and register the SD Card */
        sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM);
        if (sdfatfsHandle == NULL)
        {
          Display_printf(display, 0, 0, "Error starting the SD card\n");
          while (1);
        }
        else
        {
          Display_printf(display, 0, 0, "Drive %u is mounted\n", DRIVE_NUM);
        }
        /* Try to open the source file */
        src = fopen(inputfile, "r");
        if (!src)
        {
          Display_printf(display, 0, 0, "Creating a new file \"%s\"...", inputfile);
          /* Open file for both reading and writing */
          src = fopen(inputfile, "w+");
          if (!src)
          {
            Display_printf(display, 0, 0,    "Error: \"%s\" could not be created.\nPlease check the "
                                             "Board.html if additional jumpers are necessary.\n",inputfile);
                       Display_printf(display, 0, 0, "Aborting...\n");
                       while (1);
           }
        /* Loop forever echoing */
        //while (i<10)
        //{
            UART_read(uart, &input, 1);
            UART_write(uart, &input, 1);
            textarray [i] = input;
           // i++;
         //}
            fwrite (textarray,1,strlen(textarray),src);
            fflush(src);
            /* Reset the internal file pointer */
            rewind(src);
            Display_printf(display, 0, 0, "done\n");
           }
               else {
                       Display_printf(display, 0, 0, "Using existing copy of \"%s\"\n",
                           inputfile);
                   }
    }
    
    /*
     *  ======== fatfs_getFatTime ========
     */
    int32_t fatfs_getFatTime(void)
    {
        /*
         *  FatFs uses this API to get the current time in FatTime format.  User's
         *  must implement this function based on their system's timekeeping
         *  mechanism.  See FatFs documentation for details on FatTime format.
         */
        /* Jan 1 2017 00:00:00 */
        return (0x4A210000);
    }
    
    
    
    
    
    
     

  • No sir, i am trying to make the following attached code work for me. Please suggest me changes in the same so that i can complete the task as soon as possible. My task is to write the incoming uart data from an arduino onto an sd card interfaced with cc2652rb launchpad.

    Thanks,

    shivam

    #include <file.h>
    #include <stdbool.h>
    #include <stdint.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <third_party/fatfs/ffcio.h>
    /* Driver Header files */
    #include <ti/display/Display.h>
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/UART.h>
    #include <ti/drivers/SDFatFS.h>
    /* Driver configuration */
    #include "ti_drivers_config.h"
    /* Buffer size used for the file copy process */
    #ifndef CPY_BUFF_SIZE
    #define CPY_BUFF_SIZE       2048
    #endif
    /* String conversion macro */
    #define STR_(n)             #n
    #define STR(n)              STR_(n)
    /* Drive number used for FatFs */
    #define DRIVE_NUM           0
    const char inputfile[] = "fat:"STR(DRIVE_NUM)":input.txt";
    char textarray[10];
    static Display_Handle display;
    /* File name prefix for this filesystem for use with TI C RTS */
    char fatfsPrefix[] = "fat";
    unsigned char cpy_buff[CPY_BUFF_SIZE + 1];
    void *mainThread(void *arg0)
    {
        char        input;
        unsigned int i=0;
        UART_Handle uart;
        UART_Params uartParams;
        SDFatFS_Handle sdfatfsHandle;
        /* Variables for the CIO functions */
        FILE *src;
       // unsigned int filesize;
    /* Call driver init functions */
        GPIO_init();
        UART_init();
        Display_init();
        SDFatFS_init();
    /* Configure the LED pin */
        GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
        /* add_device() should be called once and is used for all media types */
        add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
                ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
    /* Turn on user LED */
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_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.baudRate = 115200;
        uart = UART_open(CONFIG_UART_0, &uartParams);
        if (uart == NULL) {
            /* UART_open() failed */
            while (1);
        }
        /* Mount and register the SD Card */
        sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM);
        if (sdfatfsHandle == NULL)
        {
          Display_printf(display, 0, 0, "Error starting the SD card\n");
          while (1);
        }
        else
        {
          Display_printf(display, 0, 0, "Drive %u is mounted\n", DRIVE_NUM);
        }
        /* Try to open the source file */
        src = fopen(inputfile, "r");
        if (!src)
        {
          Display_printf(display, 0, 0, "Creating a new file \"%s\"...", inputfile);
          /* Open file for both reading and writing */
          src = fopen(inputfile, "w+");
          if (!src)
          {
            Display_printf(display, 0, 0,    "Error: \"%s\" could not be created.\nPlease check the "
                                             "Board.html if additional jumpers are necessary.\n",inputfile);
                       Display_printf(display, 0, 0, "Aborting...\n");
                       while (1);
           }
        /* Loop forever echoing */
        //while (i<10)
        //{
            UART_read(uart, &input, 1);
            UART_write(uart, &input, 1);
            textarray [i] = input;
           // i++;
         //}
            fwrite (textarray,1,strlen(textarray),src);
            fflush(src);
            /* Reset the internal file pointer */
            rewind(src);
            Display_printf(display, 0, 0, "done\n");
           }
               else {
                       Display_printf(display, 0, 0, "Using existing copy of \"%s\"\n",
                           inputfile);
                   }
    }
    
    /*
     *  ======== fatfs_getFatTime ========
     */
    int32_t fatfs_getFatTime(void)
    {
        /*
         *  FatFs uses this API to get the current time in FatTime format.  User's
         *  must implement this function based on their system's timekeeping
         *  mechanism.  See FatFs documentation for details on FatTime format.
         */
        /* Jan 1 2017 00:00:00 */
        return (0x4A210000);
    }
    
    
    
    
    
    
     

  • Are you able to get UART working and are you able to get the SD card code to work if you just use UART or SD?

  • @TER I had help Shivam to make UART and SD examples work separately...

  • Instead of trying to combine everything at once, try smaller steps. Start with the SD part. Are you able to pass a string/ file from a different part of the program and write this? 

  • Yes sir both are working separately fine.

  • Different part of the program means?
    Yes i am trying to do small steps like i have successfully merged uart code and also able to create a .txt file in the sd card at the same time. But i am not able to write the incoming uart data in sd card.

  • Both UART and SD card writing is processes that takes time. I would do these in two different tasks and use semaphores to ensure proper handshaking.

    Have you stepped through the code line by line and tried to pinpoint where your existing code fails? 

  • That’s what I had suggested exactly to Shivam.

  • Okay sir i understood but i want to implement it without rtos as i have no idea about rtos implementation and i have urgent requirement. 
    Yes i have stepped through the code line by line and i am able to pinpoint where the existing code fails.
    It fails in the while (1) loop.

  • I am also wondering why it is not possible to do it simply without different task and semaphore when its easily doable in arduino.

  • It's possible to do this without TI-RTOS but either you have to use noRTOS or bare metal. The latter increase the complexity since you have to implement all the required steps done by TI-RTOS and the drivers your self which is not a recommended approach.

    For this, you don't have to learn a lot about TI-RTOS to be able to implement what you need. 

    Since basically all your code is in the while(1) loop this information does not give any useful information. 

  • Okay the code is getting stopped in the step when i am trying to write the data in sd card in while loop.
    Anyways thanks for the suggestion for TI RTOS, i will try to implement it.