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: Receive uart data continuously and write data in an SD Card.

Part Number: LP-CC2652RB

Tool/software: Code Composer Studio

Hi, I am receiving uart data from another microcontroller and trying to write it into an sd card. I am able to receive it continuously but unable to write it. Please refer my code below.

#include <file.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* POSIX Header files */
#include <pthread.h>
#include <semaphore.h>
#include <third_party/fatfs/ffcio.h>
//#include <ti/display/Display.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SDFatFS.h>
#include <ti/drivers/UART2.h>
/* XDC module Headers */
#include <xdc/std.h>
#include <xdc/runtime/System.h>
/* Driver configuration */
#include "ti_drivers_config.h"
static sem_t sem;
static volatile size_t numBytesRead;
/* 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[] ="Hello from shivam";
//static Display_Handle display;
/* Set this to the current UNIX time in seconds */
const struct timespec ts = {
    .tv_sec = 1469647026,
    .tv_nsec = 0
};
/* File name prefix for this filesystem for use with TI C RTS */
char fatfsPrefix[] = "fat";
unsigned char cpy_buff[CPY_BUFF_SIZE];
/*
 *  ======== callbackFxn ========
 */
void callbackFxn(UART2_Handle handle, void *buffer, size_t count,
        void *userArg, int_fast16_t status)
{
    if (status != UART2_STATUS_SUCCESS) {
        /* RX error occured in UART2_read() */
        while (1);
    }

    numBytesRead = count;
    sem_post(&sem);
}

/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
    char              input;
    const char        echoPrompt[] = "Echoing characters:\r\n";
    UART2_Handle      uart;
    UART2_Params      uartParams;
    int32_t           semStatus;
    uint32_t          status = UART2_STATUS_SUCCESS;

    /* Call driver init functions */
    GPIO_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 */
    UART2_Params_init(&uartParams);
    uartParams.readMode = UART2_Mode_CALLBACK;
    uartParams.readCallback = callbackFxn;
    uartParams.baudRate = 115200;

    uart = UART2_open(CONFIG_UART2_0, &uartParams);

    if (uart == NULL) {
        /* UART2_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 */
    UART2_write(uart, echoPrompt, sizeof(echoPrompt), NULL);

    /* Loop forever echoing */
    while (1) {
        numBytesRead = 0;

        /* Pass NULL for bytesRead since it's not used in this example */
        status = UART2_read(uart, &input, 1, NULL);

        if (status != UART2_STATUS_SUCCESS) {
            /* UART2_read() failed */
            while (1);
        }

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

        if (numBytesRead > 0) {
            status = UART2_write(uart, &input, 1, NULL);

            if (status != UART2_STATUS_SUCCESS) {
                /* UART2_write() failed */
                while (1);
            }
        }
        SDFatFS_Handle sdfatfsHandle;
        FILE *src;
        SDFatFS_init();
        add_device(fatfsPrefix, _MSA, ffcio_open, ffcio_close, ffcio_read,
                         ffcio_write, ffcio_lseek, ffcio_unlink, ffcio_rename);
        /* Initialize real-time clock */
                     clock_settime(CLOCK_REALTIME, &ts);
        sdfatfsHandle = SDFatFS_open(CONFIG_SDFatFS_0, DRIVE_NUM);
                         /* Open file for both reading and writing */
                         src = fopen(inputfile, "w+");
                         fseek(src,0,SEEK_END);
                         fwrite(&input, 1, 1, src);
                     /* Close  inputfile[]  */
                     fclose(src);
                     /* Stopping the SDCard */
                     SDFatFS_close(sdfatfsHandle);
       // return (NULL);

    }
}

/*
 *  ======== fatfs_getFatTime ========
 */
int32_t fatfs_getFatTime(void)
{
    time_t seconds;
    uint32_t fatTime;
    struct tm *pTime;

    /*
     *  TI time() returns seconds elapsed since 1900, while other tools
     *  return seconds from 1970.  However, both TI and GNU localtime()
     *  sets tm tm_year to number of years since 1900.
     */
    seconds = time(NULL);

    pTime = localtime(&seconds);

    /*
     *  localtime() sets pTime->tm_year to number of years
     *  since 1900, so subtract 80 from tm_year to get FAT time
     *  offset from 1980.
     */
    fatTime = ((uint32_t)(pTime->tm_year - 80) << 25) |
        ((uint32_t)(pTime->tm_mon) << 21) |
        ((uint32_t)(pTime->tm_mday) << 16) |
        ((uint32_t)(pTime->tm_hour) << 11) |
        ((uint32_t)(pTime->tm_min) << 5) |
        ((uint32_t)(pTime->tm_sec) >> 1);

    return ((int32_t)fatTime);
}

  • Hi,

    Unfortunately we don't do code reviews on this platform, but a glance at the code raises the question as to why you are continuously opening and closing the file handlers - this has the potential to create holes in the heap memory. I would open the file once and leave the runtime support library (the TI C library) to close the file handler at the program termination (which should never happen anyways).

    Also, I would refrain from writing a file byte-by-byte to a flash based memory - this has the potential to exhaust the flash.

    Also, please don't post duplicate threads about similar issues, just like you did here.

    Regards,

    Rafael