Author Topic: Ufat use  (Read 5671 times)

Offline Kuroro

  • Newbie
  • *
  • Posts: 38
    • View Profile
Ufat use
« on: May 04, 2010, 10:51:16 AM »
Hi mark

I have a strange behaviour wise ufat, i try to write the same file many time ,each time i had new data to previous one.
The problem is that it look that the last write command was not executed, for example if want to write 4 time the same string i need to execute 5 time the write command.

Here's the code I used, did I done something wrong ?

I call multi_write()

Code: [Select]
UTDIRECTORY *ptr_utDirectory = 0;
char Init_Tmp[300];

void Write_XML_Data(char* Nom_Fichier)
{
UTFILE utFile;
char ID[] = "test.txt";
char Version[] = "1.0";
char Type[] = "THL";
char First_Use = 0;
char *Last_Entry = 0;
char File_Open = 0;


if (ptr_utDirectory == 0)
{
    ptr_utDirectory = utAllocateDirectory(DISK_D, UT_PATH_LENGTH); // allocate a directory for use by this module associated with D: and reserve its path name string length
    }
    if (!(ptr_utDirectory->ucDirectoryFlags & UTDIR_VALID)) // directory not valid
    {       
    if (utOpenDirectory(0, ptr_utDirectory) != UTFAT_SUCCESS) // open the root directory
   
        fnDebugMsg("No SD-Card ready\r\n");
        return;
        }
    }


    utFile.ptr_utDirObject = ptr_utDirectory;


if (utOpenFile(Nom_Fichier, &utFile, (UTFAT_OPEN_FOR_READ | UTFAT_OPEN_FOR_WRITE | UTFAT_CREATE)) != UTFAT_PATH_IS_FILE) // change the directory location
fnDebugMsg("Create file failed\r\n");



if(utFile.ulFileSize == 0)
First_Use = 1;


if(First_Use == 1)
{
Last_Entry = uStrcpy(Init_Tmp,XML_Header);
Last_Entry = uStrcpy(Last_Entry,"<head Version=\"");
Last_Entry = uStrcpy(Last_Entry,Version);
Last_Entry = uStrcpy(Last_Entry,"\" Type=\"");
Last_Entry = uStrcpy(Last_Entry,Type);
Last_Entry = uStrcpy(Last_Entry,"\" Id=\"");
Last_Entry = uStrcpy(Last_Entry,ID);
Last_Entry = uStrcpy(Last_Entry,"\">\n");
}
else
{
utSeek(&utFile, 0, UTFAT_SEEK_END);
 
Last_Entry = uStrcpy(Init_Tmp,"\n<Data>");
Last_Entry = uStrcpy(Last_Entry,"\n\t<Ts>");
Last_Entry = uStrcpy(Last_Entry,"1272962844");
Last_Entry = uStrcpy(Last_Entry,"</Ts>");
Last_Entry = uStrcpy(Last_Entry,"\n\t<Tc_1>");
Last_Entry = uStrcpy(Last_Entry,"32.2");
Last_Entry = uStrcpy(Last_Entry,"</Tc_1>");
Last_Entry = uStrcpy(Last_Entry,"\n\t<Tc_2>");
Last_Entry = uStrcpy(Last_Entry,"24.2");
Last_Entry = uStrcpy(Last_Entry,"</Tc_2>");
Last_Entry = uStrcpy(Last_Entry,"\n</Data>");
}

if(utWriteFile(&utFile,(unsigned char*)Init_Tmp,uStrlen(Init_Tmp)) != UTFAT_SUCCESS)
fnDebugMsg("Write failed\r\n");

utCloseFile(&utFile);

}

void multi_write(void)
{
Write_XML_Data("test1");
Write_XML_Data("test1");
Write_XML_Data("test1");
Write_XML_Data("test1");
Write_XML_Data("test1");
}

The result obtained :

<?xml version="1.0" encoding="ISO-8859-1"?>
<head Version="1.0" Type="THL" Id="test.txt">

<Data>
   <Ts>1272962844</Ts>
   <Tc_1>32.2</Tc_1>
   <Tc_2>24.2</Tc_2>
</Data>
<Data>
   <Ts>1272962844</Ts>
   <Tc_1>32.2</Tc_1>
   <Tc_2>24.2</Tc_2>
</Data>
<Data>
   <Ts>1272962844</Ts>
   <Tc_1>32.2</Tc_1>
   <Tc_2>24.2</Tc_2>
</Data>
<Data>

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3191
    • View Profile
    • uTasker
Re: Ufat use
« Reply #1 on: May 04, 2010, 11:33:21 AM »
Hi

I wonder whether it is the SD card doing this?

Although I never had such problems I did hear from someone who seems to lose the last write performed to a file if the board was powered down.

The possible explanation is that the SD card is buffering the data but not committing it to its memory (there is no command to actually buffer data and it is assumed that all data is written). It is possible that the SD card is trying to reduce writes (reading back would also read from the buffer so all data is there).

Try to do a dummy write to a second file after performing a sequence of writes. Also try reading the data back without taking the card out or resetting etc. This will show whether the data has been lost or not.

As long as there are no error messages (debug interface) no write problem was experienced so the data should have been successfully copied to the SD card - the question is whether it has committed this data to memory or whether it is buffering it , which could get lost when the SD card is powered down. If this does turn out to be the case with certain card types I think that a dummy write to force committing buffered memory is the only method, but this would be a shame and can not necessarily be guarantied when the board is powered down before it is performed.

As mentioned before I never experienced this but there are indications that maybe certain SD card types possibly do something like this. See whether you can identify whether your SD card type is responsible for it.

Regards

Mark

Offline Kuroro

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Ufat use
« Reply #2 on: May 04, 2010, 01:00:41 PM »
Hi mark

Well done, once again your right, it seem that the SD card buffer the last write command , to bypass this problem I just put a dummy read at the end of my write sequence as you suggest and they work's fine.

Thanks again