Author Topic: Writing data to the SD  (Read 14745 times)

Offline timadria

  • Newbie
  • *
  • Posts: 9
    • View Profile
Writing data to the SD
« on: July 12, 2012, 03:08:12 PM »
Hi,

When I try to write data to the SD card. The first write seems not to be saved on the card.
When I open the complete SD-Card on my PC (interesting programm to do this: HxD - Hexeditor) the data is written on the SD itself, but the FAT is not adapted. The file itself is still empty.

The strange thing is that when I reed the data on the uTasker the data is in the file and can be read. I'm thinking that the filesize is not correctly stored in the FAT table.

In short: Every last write is not saved in the file after a powerup of my application.

What can be this problem?

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3243
    • View Profile
    • uTasker
Re: Writing data to the SD
« Reply #1 on: July 16, 2012, 01:01:12 AM »
Hi

When data is written to a file the data content is copied first and stored in the file's cluster chain.

As long as there was no error doing this the FAT is updated accordingly.

The code to update the FAT looks something like this (in utWriteFile()):
    if (ptr_utFile->usLastReadWriteLength != 0) {                        // updata the file entry if there was a change
        DIR_ENTRY_STRUCTURE_FAT32 *ptrFileEntry = (DIR_ENTRY_STRUCTURE_FAT32 *)ptr_utDisk->ptrSectorData; // the directory entry in the sector buffer
        ptrFileEntry += ptr_utFile->private_disk_location.ucDirectoryEntry; // move to the file entry
        if (fnLoadSector(ptr_utDisk, ptr_utFile->private_disk_location.directory_location.ulSector) != UTFAT_SUCCESS) { // ensure that the directory sector is loaded
            return UTFAT_DISK_READ_ERROR;
        }
        fnSetFileInformation(ptrFileEntry, ptr_utFile->ulFileSize);
        while (utCommitSector(ptr_utDisk, ptr_utFile->private_disk_location.directory_location.ulSector) == CARD_BUSY_WAIT) {} // force writeback to finalise the operation
        ptr_utDisk->usDiskFlags &= ~WRITEBACK_BUFFER_FLAG;
    }


As long as there had been a change made the directory entry is read to a local buffer and then the content is changed - meaning that the file size is updated and its write data (if supported) and the buffer is written back.

If your file content changes but the FAT is not updated there must be a problem around here, such as the FAT write failing or there being a reset before it was written. Any failures can be detected by the caller since UTFAT_SUCCESS will not be returned, meaning that you should be able to detect if something like this is taking place.

Regards

Mark