Flush SD card error

Following this discussion I implemented the flush of the SD card as described here.

I found that this doesn’t work, because DeviceIoControl() fails.

Calling GetLastError() returns ERROR_INVALID_PARAMETER (87 - 0x57).

This document should explain how to use IOCTL_DISK_FLUSH_CACHE, and most of the parameters are not used.

One sentence at the beginning of Toradex KB states

You need to set lpInBuffer = -1 to use
the function!

but this parameter is set to &forceFlush and this is the address of a DWORD containing -1.

Could you verify and clarify, please?

Can you try to pass NULL and 0 as both input and output parameters to DeviceIoControl and check that the handle of your device is not NULL or 0xFFFFFFFF?

Hi @valter.tx
I tried in this way
DeviceIoControl(hStore, IOCTL_DISK_FLUSH_CACHE, NULL, 0, NULL, 0, NULL, NULL)
and I verified that hStore is not NULL and not 0xFFFFFFFF.

I see the same failure, with GetLastError() returning ERROR_INVALID_PARAMETER.

Dear @vix,

Could you please download and try this demo project and let us know the result.

Hello @raja.tx
if I use your project asit is (DSK0: is the internal NAND flash as shown by Win CE storade manager), DeviceIoControl() fails and GetLastError() returns 0xFFFFFFFF.

If I change DSK0: to DSK1: (which is the SD card), DeviceIoControl() fails and GetLastError() returns (87 - 0x57).

The behavior is the same both on the Evaluation Board and on my custom carrier board.

Dear @vix,

Could you please share your detailed use case of flushing the SD card? Are you facing any issue on SD card?

Disk flush is supposed to be cache flushing at the lowest HW level. It makes sense to have it for devices that have internal caches (like SATA HDs) or may keep a SW cache internally (like NAND filesystems). Probably on USB and SD writes are atomic operations and when you have sent data out you suppose that it’s going to be written ASAP. So, IOCTL_DISK_FLUSH_CACHE is not implemented in the SD memory client driver. If I remember correctly, disk cache is handled by the SD device itself.

Hi @raja.tx

I don’t have any issue on SD card.
I want to be sure that file are written to SD card without risks of either corruption or data loss (in case of sudden power loss).
This is the reason why I read this topic where someone suggested to flush the cache for SD card.

I’m not an expert at all, and if this doesn’t have sense for SD card I’ll remove this function call from my code.

Dear @vix,

Thank you for your reply. For the safer side, you can use DismountStore API to unmount the SD card.

Hi @raja.tx
do you mean I should call DismountStore() after having saved a file to protect against corruption due to power loss?

It seems that Mounting and Dismounting Stores is useful for different needs.

For sure I can dismount before every expected (and controlled) power down.

Dismount store can be used to perform a shutdown. This will ensure that all pending operations are completed. However on our flash storage we use tfat/exfat filesystems (depending on OS version and platform) that should ensure that a single interrupted file write operation will not crash the filesystem as a whole. You may lose the data that was being written, of course, but not the whole filesystem.