Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

What happens when a microSD card is hot-unplugged?

+4
−0

Consider a device with a microSD card. The microcontroller writes to the SD card periodically. The operators have an ability to remove the microSD card at any time (they will be removing it every few weeks). Nothing prevents the operators from hot-yanking the microSD card while it’s being written to. Do the microSD cards or the FAT have provisions for such contingency? What provisions should I make in firmware or in hardware?

My device has a switch which puts it into a maintenance mode (the device is headless). The device stops writing to the microSD card in the maintenance mode. The operators are instructed about the switch, but they may forget to flip it before removing the microSD card.

present hardware:
STM32G4 running FatFs. SPI to the microSD card. The card socket is push-in-push-out type, and it has a card detection switch (Hirose DM3AT).

update:
The FatFs stops seeing the microSD card when it's removed and reinserted. That happens even when the card is removed during idle time. Some details and proposed solution here: What is the proper method to re-initialize SD card interface to support removal/reinsertion of SD card?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+3
−0

There is no provision in FatFS or the fat32 filesystem in general regarding hot-unplug, so this behaviour depends wholly on the implementation layer for your hardware.

As an example, in my use case I have tested the behaviour of my system when I remove the memory card without calling f_close() on open files. I found that there is a 0-sized file that was created when I called f_open(), and when I used filesystem repair tool in Disks on Linux, it created "orphaned" files it found - the data in them was usable up to the removal of the card. If your card socket supports it (ie the card detect triggers before pin contact is lost), you will need to implement card detect interrupt that would flush all filesystem IO and close all opened files if you want to handle it robustly. Alternatively, you will need to buffer data in RAM and write it in f_open -> f_write -> f_sync -> f_close sequence every time. Depending on implementation, it can take a while to do those (a hundred ms is not uncommon).

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »