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.

Problem with f_write function of FAT library in SD card

Hello everyone,

I'm having a trouble with the f_write function of the tiva C fat library, when I run mi code there is no errors, and I can debug it, so when I put my Launchpad TM4C123G in my application circuit the file is created in the SD Card but it have not write any of the headers in the file. I have seen that after I extract the SD card and I put it in the card port of my laptop. Besides, when I tried to open de file created a message appears, it says:

“The format and file extension "testfile.xls" does not match. The file is damaged or can not be safe. Do not open it unless it trusts its origin. Do you want to open it anyway?
                                 yes                   no             help”

So I clic on yes, I open the file, but nothing was written. I verified the f_write function in the tivaware folder and I wrote it in my code according of the structure. I am using Code Composer Studio 6.

This is a part of my code related with SD card:

#include <stdint.h>

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <string.h>

#include <time.h>

#include <math.h>

#include "inc/hw_ints.h"

#include "inc/hw_types.h"

#include "inc/hw_memmap.h"

#include "inc/hw_gpio.h"

#include "inc/hw_hibernate.h"

#include "inc/hw_timer.h"

#include "driverlib/adc.h"

#include "driverlib/fpu.h"

#include "driverlib/gpio.h"

#include "driverlib/interrupt.h"

#include "driverlib/debug.h"

#include "driverlib/fpu.h"

#include "driverlib/timer.h"

#include "driverlib/hibernate.h"

#include "driverlib/pin_map.h"

#include "driverlib/rom.h"

#include "driverlib/sysctl.h"

#include "driverlib/systick.h"

#include "driverlib/uart.h"

#include "driverlib/adc.h"

#include "grlib/grlib.h"

#include "utils/cmdline.h"

#include "utils/uartstdio.h"

#include "utils/ustdlib.h"

#include "fatfs/src/ff.h"

#include "fatfs/src/diskio.h"

// The following are data structures used by FatFs.

//*****************************************************************************

static FATFS g_sFatFs;

//*****************************************************************************

// This is the handler for this SysTick interrupt. FatFs requires a timer tick

// every 10 ms for internal timing purposes.

//*****************************************************************************

void

SysTickHandler(void)

{   // Call the FatFs tick timer.

   disk_timerproc();

}

void ConfigureSDCard(void)

{

       // Enable the peripherals for SDcard

       ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);

   // Configure SysTick for a 100Hz interrupt. The FatFs driver wants a 10 ms tick.

   ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);

   ROM_SysTickEnable();

   ROM_SysTickIntEnable();

   // Enable Interrupts

   ROM_IntMasterEnable();

       // Mount the file system, using logical disk 0.

       f_mount(0, &g_sFatFs);

}

void StartSDCard(void)

{

       uint32_t count ;

       //uint32_t i;

       ConfigureSDCard();

   FIL fil;

 

   f_open(&fil, "testfile.xls", FA_OPEN_ALWAYS|FA_WRITE|FA_READ);

   f_write(&fil, "Temperature(°C)\t", 17, &count);

   f_write(&fil, "Battery Level(V)\t", 18, &count);

   f_write(&fil, "Solar Panel Voltage(V)\t", 24, &count);

}

int main(void)

{

 

 

 

       while(1)

       {

             StartSDCard();

 

       }           

}


That is the connection between my SD Card and the SSI port of my Launchpad:

 

Thanks for your help.

  • Hello Lia,

    Instead of writing to an XLS file, can you instead write it to a text file and then see if the data is there?

    Regards
    Amit
  • Hello Amit, I tried with a text file but I can not write anything, I do not why ?
  • Lis Mariela Villanueva Blas said:

    That is the connection between my SD Card and the SSI port of my Launchpad:

     Hi Lis, from the schematics you posted I doubt some pull down and other pullup resistor are necessary to correctly access SD card, can you check if configuration is ok from a working example? I am not sure but I remember more resistor are connected to SD card interface.

  • Hello Roberto,

    I checked it against the DK-TM4C123 SD-Card Interface and while connections are fine, it seems that the Pull Up on CS is missing. But that would not be fully explaining the issue as the file still gets created though is corrupted.

    Regards
    Amit
  • Lis Mariela Villanueva Blas said:

    my application circuit the file is created in the SD Card but it have not write any of the headers in the file. I have seen that after I extract the SD card and I put it in the card port of my laptop. Besides, when I tried to open de file created a message appears, it says:

    “The format and file extension "testfile.xls" does not match. The file is damaged or can not be safe. Do not open it unless it trusts its origin. Do you want to open it anyway?
                                     yes                   no             help”

     Hi Lis, Windows errors are the worst useless font of information so don't care of, and please check these step:

     first see size of file if zero or garbage is in, do from dir command in a cmd box or graphics interface selecting details, in case filesize is different from zero installl and hex editor to see what is in.

    - add uartstudio and print debug status of all file system commands to see what it fails, I suppose mount and f_open are ok.

     - f_close is missing so can be buffer never get written to SD, please can you insert closing function at end of write and test again.

     -  Is stack set for correct dimension? In case of large SD with many files space where necessary too.

     Tell us if all is ok and in first if adding f_close write some bytes on sd sector.

  • Hi,

    Keep in mind the real .xls format is a binary compressed (PKzip) one, you do not have all the means to use this format for saving. Instead, you may use .csv format, which is real text and opens flawless into sheet application, whatever is that.

    Petrei

    EDIT: Not obvious if you added the file fatfs/port/mmc-dk-tm4c123g.c to your project -  there are all correct initialisations routines.