SD hot swap and low voltage protection question

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys

    -How can I implement a SD card hot swap mechanism? -How can I protect the data loss during my program is writing data into a SD card, and my device is suffer from unexpected power off/low voltage etc?

here is what I want to do:

    -be able to pull out the sd card without data loss problem while the device is operating. -and my device is able to switch on/off any time.

I don't have a clue where do start, so any advice, keywords I should be googling is appreciated.

Thanks.

Zhuhua Wu - Electronic Engineering Student

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hotswap is pretty much built into the card.

I sometimes use a socket with a card detect pin (and a write protect pin) these are usually just metal prongs to the case shield. If you connect these to the a pin change interrupt, then you can use a C interrupt vector to capture when the card is fully inserted. Typically I just set a flag based on the pin mask in the interrupt and poll on the flag. No calls to the petite fat file system are allowed if the flag is set.

Gets messy if the card is pulled during a write. The pin change interrupt will fire. Probably during a write. SD is not as bad as Compact flash. I bricked a few CF cards that way. I typically write the fat first, as I allocate blocks to the file then the directory entry. Possibly if the directory entry is written first, then one could find
where the FAT sectors should be, but the file format would need to be known to recover the data. There is not much one can do if the card is hot swapped during a write.

Have not implemented voltage checking. I did reserve a line which I could use an extra ADC pin for such. One just sets up a periodic timer to start the ADC and average the value. Most of my designs use a second regulator (I think it is a LM317.) fixed at 3.3 volts. I need 5VCC for other interfaces.

To keep this somewhat on track for the C forum (this is really a general electronics question.) use C to set a pin that controls a FET to the secondary power supply.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I assume you are using FatFs? It has support for WP and CP. However if there's a chance the card could be removed at any moment you either want to open/write/close every time or at least f_sync after writes to be sure there's nothing left cached in the sector buffer.

As Julie says this has nothing specific to do with GCC so, on the assumption you are at least talking about using an AVR I'll move this thread to AVR Forum.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please move this thread to an appropriate forum, I thought I could deal with it in software, but it looks like it can not be done in software.

And I'm talking about AVR, arduino more precisely on VC2012 with Visual Micro plugin, but I can drop the arduino libraries and core if necessary.

Zhuhua Wu - Electronic Engineering Student

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well the SD/MMC library most folks here use is the plain C code "FatFs". As I say it monitors the Card Present (CP) and Write Protect (WP) in a ticker interrupt. If it detects that the card has been removed and then reinserted (possibly a different one) it triggers it to re-initialise. So the software handles hot-swaps.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But that won't "fix" things if it is in the middle of a write when the SD card is removed. I don't think there is any way of handling that one beyond what the SD does, internally. Power loss is one thing - backup will "solve" that. But, improper removal (example: while writing) is something you have to live with.

Recovery from improper removal may require something like never writing over an existing block. Maybe you write an entire new copy, then change the the file address in the file table to point to the new one when it is successfully finished. That way, if the write is interrupted, you always have the old one to fall back upon.

If you are writing a totally new file, then you have to do something a bit different.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But that won't "fix" things if it is in the middle of a write when the SD card is removed

True enough but if you only have the file open (or not f_sync'd) for minute fractions of time the chance of the user catching such a write is reduced.

This isn't unique of course. Start copying a 2GB file to an SD in a PC (or some size that takes a "finite" time) and then pull the card out half way through. Even a PC does not have some magic way to protect against this.

In fact worse still is the HDD/SSD/SD/memory stick in your PC that is being written when the cleaner comes into your office and unplugs the PC to plug in her Hoover. That can be fatal and no filing system (not even the advanced ones like NTFS and EXT4) can protect against this totally. Journalling filesystems help a bit but even those aren't immune.

So says the guy who was trying to find a way to prevent corruption in 5,000+ Linux computers that were habitually switched off outside office ours simply like the lights in a shop when it closes for the night (in fact the exact location where the computers were located). We'd have about 3 units a week got in to a "not automatically restartable" situation as a result of this activity.

Most people with desktop PCs are good boys and girls and use "Shutdown" first if their intention is then to remove power so don't see this scenario so often.

One sure fire way to combat it is to offer the user a "soft off" switch. They switch it and the thing turns off (like the screen goes off) but it then slowly puts itself to sleep beyond this as if a shutdown then power off were done.

In the AVR+SD case this would involve simply closing files and unmounting the "drive" before power was finally cut.