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: Write uart data in sd card with cc2652rb-LP

Part Number: LP-CC2652RB

Tool/software: Code Composer Studio

Hello, how can i write uart incoming data to sd card interfaced with cc2652. Is the following line of code correct.

char str = uart_read(uart, &input,1)

fwrite(str, 1, strlen(str), src)

Please provide me with inputs.

Thanks,

shivam

  • Please take a look at the two example we have for writing/reading to the SD card:

    https://dev.ti.com/tirex/explore/node?node=ANVH9beEds77i7cDbHxqNg__pTTHBmu__LATEST

    https://dev.ti.com/tirex/explore/node?node=AB3d59IBueXW8YI4K0Kfdg__pTTHBmu__LATEST

    also, take a look at the SD driver documentation on how to perform basic transfers directly to the SD card.

    BR

    Siri

  • Thanks, but my question is different.
    If i perform a uart_read operation, then can i store the returned character in sd card file using fwrite?

  • I am not familiar with fwrite. Where in the SDK is it defined?

    Why do you not use our SDRAW example as a starting point?

    dev.ti.com/.../node

    Siri

  • Again sdraw will write raw data in sd card, whereas i have to first create a .txt file in sd card and then write data onto the created .txt file.
    Further fwrite is mentioned in the fatsd example code.
    By my study i think if i can just store the uart data into a variable then i can easily write it into a file.

    Thanks,

    shivam

  • Do you try to use fatsd example at ?

  • yes i did try and verified it successfully.
    .TXT file is created and data is being written into it.The data being written into it is the data defined in the program but now i want to store the uart data.

  • I suppose you can combine uartecho example to read data from UART to save to SD card.

  • Yeah i am trying that only but unable to do it.
    If possible please let me know where i can read about fwrite function mentioned in fatsd example.

  • You should be more specific to elaborate what you unable to do.

  • Okay, so i have merged the fatsd and uartecho code but i am unable to get the desired result.
    Please refer the attached file where i have tried to combine both the codes.
    Also let me know what changes i need to do in code of fatsd example to receive the uart data.

    #include <file.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdint.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";
    const char outputfile[] = "fat:"STR(DRIVE_NUM)":output.txt";
    const char textarray[] = \
    "***********************************************************************\n"
    "0         1         2         3         4         5         6         7\n"
    "Hello from System Integration Lab  Ncflexe\n";
    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;
        	UART_Handle uart;
        	UART_Params uartParams;
        SDFatFS_Handle sdfatfsHandle;
        /* Variables for the CIO functions */
        FILE *src, *dst;
        /* Variables to keep track of the file copy progress */
        unsigned int bytesRead = 0;
        unsigned int bytesWritten = 0;
        unsigned int filesize;
        unsigned int totalBytesCopied = 0;
        /* 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);
        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);
            }
        
            UART_read(uart, &input, 1);
            fwrite(input, 1, 1, src);
            fflush(src);
            /* Reset the internal file pointer */
            rewind(src);
            Display_printf(display, 0, 0, "done\n");
        }
        
        /* Get the filesize of the source file */
        fseek(src, 0, SEEK_END);
        filesize = ftell(src);
        rewind(src);
        /* Close both inputfile[] and outputfile[] */
        fclose(src);
        /* Stopping the SDCard */
        SDFatFS_close(sdfatfsHandle);
        Display_printf(display, 0, 0, "Drive %u unmounted\n", DRIVE_NUM);
        return (NULL);
    }
    /*
     *  ======== 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 suggest you to create another task to collect UART data and post a semaphore or event to do sd write in another task.

  • Okay Thanks, but is it not possible to receive UART characters in an infinite loop and then write those characters in the sd card in that loop itself as I am trying to do in the attached file itself.

    As I don't how to apply semaphore.

  • Try to refer to TI-RTOS Kernel User’s Guide at

  • Thanks, but it can be done without using RTOS as well.
    I want to make a nortos code.

  • Yes, I think it should be no problem.

  • so can you please suggest me what changes i need to make in the attached file for c code so that i can perform the above action.

    Thanks a lot!

  • I can not help further since this is more related to your own application.

  • Okay! But i think its just a 5 minute problem for you.
    There is some data type mismatch that has to be solved.