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-CC26X2R1: SD card implementation with a filesystem

Part Number: LAUNCHXL-CC26X2R1
Other Parts Discussed in Thread: SYSBIOS, BLE-STACK

Tool/software: Code Composer Studio

Hello,

I've been trying to implement the Fat File system on the CC26X2 chip set. I have been successful to an extent but still face a problem when trying to write. While attempting to write, I am unable to debug when it gets to the line highlighted in red. I fail to understand where I'm going wrong.

/*
* ======== fatsd.c ========
*/
#include <file.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


#if defined(__IAR_SYSTEMS_ICC__)
/*
* Prevent time() macro in time.h from being used instead of our
* generated time() function.
*/
#define _NO_DEFINITIONS_IN_HEADER_FILES 1
#endif
#include <time.h>
#if defined(__IAR_SYSTEMS_ICC__)
#undef _NO_DEFINITIONS_IN_HEADER_FILES
#endif

//#include "sdhost.h"


/* POSIX Header files */
#include <pthread.h>

/* Driver Header files */
#include <ti/drivers/GPIO.h>
//#include <ti/drivers/SD.h>
#include <ti/display/Display.h>

#include <third_party/fatfs/ff.h>
#include <third_party/fatfs/ffcio.h>


#include <ti/drivers/SDFatFS.h>

/* Example/Board Header files */
#include "Board.h"
/*#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <third_party/fatfs/ff.h>
#include <third_party/fatfs/diskio.h>
#include <third_party/fatfs/ffcio.h>*/

/* Buffer size used for the file copy process */
//#define BUFFSIZE 1024

/* String conversion macro */
#define STR_(n) #n
#define STR(n) STR_(n)

/* Drive number used for FatFs */
#define DRIVE_NUM 0

/* Starting sector to write/read to */
//#define STARTINGSECTOR 0

//#define BYTESPERKILOBYTE 1024

/*
* Set this constant to 1 in order to write to the SD card.
* WARNING: Running this example with WRITEENABLE set to 1 will cause
* any filesystem present on the SD card to be corrupted!
*/
//#define WRITEENABLE 0

//static Display_Handle display;

//unsigned char textarray[BUFFSIZE];

//unsigned char cpy_buff[BUFFSIZE];

const char inputfile[] = "fat:"STR(DRIVE_NUM)":data.bin";
const char outputfile[] = "fat:"STR(DRIVE_NUM)":output.txt";

const char textarray[] = \
"***********************************************************************\n"
"0 1 2 3 4 5 6 7\n"
"01234567890123456789012345678901234567890123456789012345678901234567890\n"
"This is some text to be inserted into the inputfile if there isn't\n"
"already an existing file located on the media.\n"
"If an inputfile already exists, or if the file was already once\n"
"generated, then the inputfile will NOT be modified.\n"
"***********************************************************************\n";


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 + 1];


/*
* ======== mainThread ========
* Thread to perform a file copy
*
* Thread tries to open an existing file inputfile[]. If the file doesn't
* exist, create one and write some known content into it.
* The contents of the inputfile[] are then copied to an output file
* outputfile[]. Once completed, the contents of the output file are
* printed onto the system console (stdout).
*/
void *mainThread(void *arg0)
{
SDFatFS_Handle sdfatfsHandle;

FILE *src;


/* 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();
Display_init();
SDFatFS_init();
// I2C_init();
/* Configure the LED pin */
// GPIO_setConfig(Board_GPIO_LED0, 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);

/* Open the display for output */
//
display = Display_open(Display_Type_UART, NULL);
if (display == NULL) {
/* Failed to open display driver */
while (1);
}

/* Initialize real-time clock */
//clock_settime(CLOCK_REALTIME, &ts);

/* Turn on user LED */
//GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);

Display_printf(display, 0, 0, "Starting the fatsd example\n");
Display_printf(display, 0, 0,
"This example requires a FAT filesystem on the SD card.\n");
Display_printf(display, 0, 0,
"You will get errors if your SD card is not formatted with a file system.\n");

/* Mount and register the SD Card */
sdfatfsHandle = SDFatFS_open(Board_SDFatFS0, 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);
}
#if 0
/* Try to open the source file */
fresult = f_open(&src ,inputfile, FA_CREATE_NEW);
if (fresult==FR_OK) {
Display_printf(display, 0, 0, "Creating a new file....");
}

else
Display_printf(display, 0, 0, "Could not create a new file......");#
#endif

src = fopen(inputfile, "a");
if (!src)
Display_printf(display, 0, 0, "Opening file.... ",
inputfile);
int i=0;
while(i<3)
{
fwrite(textarray, 1, strlen(textarray), src);
i++;
}
#if 0
/* 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);
}
#endif
#if 0
/* Create a new file object for the file copy */
dst = fopen(outputfile, "w");
if (!dst) {
Display_printf(display, 0, 0, "Error opening \"%s\"\n", outputfile);
Display_printf(display, 0, 0, "Aborting...\n");
while (1);
}
else {
Display_printf(display, 0, 0, "Starting file copy\n");
}

/* Copy the contents from the src to the dst */
while (true) {
/* Read from source file */
bytesRead = fread(cpy_buff, 1, CPY_BUFF_SIZE, src);
if (bytesRead == 0) {
break; /* Error or EOF */
}

/* Write to dst file */
bytesWritten = fwrite(cpy_buff, 1, bytesRead, dst);
if (bytesWritten < bytesRead) {
Display_printf(display, 0, 0, "Disk Full\n");
break; /* Error or Disk Full */
}

/* Update the total number of bytes copied */
totalBytesCopied += bytesWritten;
}

fflush(dst);

/* Get the filesize of the source file */
fseek(src, 0, SEEK_END);
filesize = ftell(src);
rewind(src);

/* Close both inputfile[] and outputfile[] */
fclose(src);
fclose(dst);

Display_printf(display, 0, 0,
"File \"%s\" (%u B) copied to \"%s\" (Wrote %u B)\n",
inputfile, filesize, outputfile, totalBytesCopied);

/* Now output the outputfile[] contents onto the console */
dst = fopen(outputfile, "r");
if (!dst) {
Display_printf(display, 0, 0, "Error opening \"%s\"\n", outputfile);
Display_printf(display, 0, 0, "Aborting...\n");
while (1);
}

/* Print file contents */
while (true) {
/* Read from output file */
bytesRead = fread(cpy_buff, 1, CPY_BUFF_SIZE, dst);
if (bytesRead == 0) {
break; /* Error or EOF */
}
cpy_buff[bytesRead] = '\0';
/* Write output */
Display_printf(display, 0, 0, "%s", cpy_buff);
}

/* Close the file */
fclose(dst);
#endif
/* Stopping the SDCard */
fclose(src);
SDFatFS_close(sdfatfsHandle);
Display_printf(display, 0, 0, "Drive %u unmounted\n", DRIVE_NUM);

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

 

Regards,
Alisha