• AVR Freaks

How to: Protect SD card against power loss?

Author
jschaenzle
Starting Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2010/06/29 06:25:21
  • Location: 0
  • Status: offline
2010/12/30 05:57:09 (permalink)
0

How to: Protect SD card against power loss?

I am working on a device that uses the Microchip MDDFS library to store data to an SD card. The logger will log data at a maximum rate of 1 entry (56bytes) every minute. The problem is the device may lose power at any time, potentially in the middle of a write sequence.  I am wondering what is the best way to protect my data against corruption.  I have found that if the file is open when the power is lost, all data that was written to the file after the last file-close is lost. I don't know if the same holds true if power is lost in the middle of the write sequence. 

Since the write procedure doesn't happen very frequently I could open the file, write the data, and then close the file, every time data is logged. Would this approach damage the sd card over time? 
Another approach could be to keep the file open but after every 10 or 50 writes I could close the file and then re-open it.


I could also buffer data in memory, then flush the data occasionally maybe after a kbyte or so. 

The last idea I had was, in my circuit, I could add a large capacitor that would provide power to my pic/sd card long enough after the power is disconnected to quickly close the file. The problem with this approach is that the time it takes to close the file and/or save data is very consistent. From my understanding, this time can very depending on the current place in a flash page that the file is in.

Anyways, what would you guys suggest?
#1

8 Replies Related Threads

    kaptec
    Senior Member
    • Total Posts : 171
    • Reward points : 0
    • Joined: 2005/09/12 13:22:13
    • Location: England
    • Status: offline
    Re:How to: Protect SD card against power loss? 2010/12/30 17:06:20 (permalink)
    0
    Hi
     
    I've been playing around with similar flash memory and having similar thoughts. I think the large capacitor idea is the way I shall go. You would need to configure an analogue peripheral to monitor the power supply. Something that I have learned that a sector write (512bytes)  takes about 2.7mS on the devices I am using. The Pic talks for about 1.5mS and the balance is a background operation handled entirely within the flash memory. With these time scales the capacitor could be quite modest.
     
    Flash endurance on these devices is about 100,000 erase cycles and certanly with the devices I am using you have write to a buffer and then initiate an internal erase - write from buffer cycle. Your data rates are quite slow so could you afford to loose the last 10minutes or so of data.
     
    It is possible to do the erase cycle once and in your case do about 10 writes as and when they are generated. I'm using my own routines to write to the flash so I'm not sure how flexible the Microchip ones are. There is a good section in "Learning to fly the Pic" about writing to SD cards. Might be worth a look.
     
    Ken
    post edited by kaptec - 2010/12/30 17:23:19
    #2
    jschaenzle
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2010/06/29 06:25:21
    • Location: 0
    • Status: offline
    Re:How to: Protect SD card against power loss? 2010/12/31 13:50:57 (permalink)
    0
    Ken, Thanks for the info. I have been doing some tests using a large cap to keep the PIC alive after power loss and it seems to be working well. My power supply powers up the cap to 5V and then it goes though a 3.3V regulator. With a .022F cap I get about 1 second before I see the 3.3V output of the regulator drop below 3.3. With a 1F cap (about the size of a quarter) it lasts at least 10 seconds. I haven't tested longer than that. BTW these time i mentioned are starting when I detect the power loss using an IO pin. I am not using any comparitor input or anything. I just use a voltage divider to bring the 5V down to 3v and I use a CN interrupt to trigger the power-loss sequence. 

    The last thing I plan to do is tweak my firmware, particularly the sd-card read write stuff to be more efficient.  If you go to electronics.stackexchange.com I asked this same question and got some really good suggestions for doing this. A simple search should find it. 

    I agree that Learning To Fly the PIC24 is a good resource for this. I started out using his SD communcation example code (Not the FAT chapter) for this project but then I switched to using the Mircochip MDD library so that my log file would be compatible with PC's.  I always keep that book within arms reach! Wow... I am a major nerd!


    #3
    jschaenzle
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2010/06/29 06:25:21
    • Location: 0
    • Status: offline
    Re:How to: Protect SD card against power loss? 2010/12/31 13:51:01 (permalink)
    0
    Ken, Thanks for the info. I have been doing some tests using a large cap to keep the PIC alive after power loss and it seems to be working well. My power supply powers up the cap to 5V and then it goes though a 3.3V regulator. With a .022F cap I get about 1 second before I see the 3.3V output of the regulator drop below 3.3. With a 1F cap (about the size of a quarter) it lasts at least 10 seconds. I haven't tested longer than that. BTW these time i mentioned are starting when I detect the power loss using an IO pin. I am not using any comparitor input or anything. I just use a voltage divider to bring the 5V down to 3v and I use a CN interrupt to trigger the power-loss sequence. 

    The last thing I plan to do is tweak my firmware, particularly the sd-card read write stuff to be more efficient.  If you go to electronics.stackexchange.com I asked this same question and got some really good suggestions for doing this. A simple search should find it. 

    I agree that Learning To Fly the PIC24 is a good resource for this. I started out using his SD communcation example code (Not the FAT chapter) for this project but then I switched to using the Mircochip MDD library so that my log file would be compatible with PC's.  I always keep that book within arms reach! Wow... I am a major nerd!


    #4
    kaptec
    Senior Member
    • Total Posts : 171
    • Reward points : 0
    • Joined: 2005/09/12 13:22:13
    • Location: England
    • Status: offline
    Re:How to: Protect SD card against power loss? 2011/01/01 06:53:34 (permalink)
    0
    Hi
     
    I had a look on the forum that you posted the original question on. Interesting stuff there. The super caps look usefull as well. The information I have been posting is based on the the use of an AT45DB321D-SU SERIAL FLASH SMT 32MB RC  . If you follow this link there is a programming specification. This is not an SD card but it is similar. I am still trying to determine if I can use SD cards in respect of the license fee to be in the "SD Card Club" at around $3k a year.
     
    Returning to your original question. If you are using a CN input to determine power failure via the 5v raw supply why not consider reprogramming this to  be an analogue. You would then be able to determine loss of power sooner because you make the decision based on any voltage level you choose rather than the logic levels you currantly use. I think your hardware circuit would not have to change for this.
     
    The timing information I posted is based on SPI communication and SPI clock at 7.3 Mhz.
    My plan is to check the supply before writing a block on the basis that the supply will be good for that period. I think the bigger issue is that with SD cards the user could remove them at any time causing the same problems and this is much harder to detect.
     
    Ken
    #5
    jschaenzle
    Starting Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2010/06/29 06:25:21
    • Location: 0
    • Status: offline
    Re:How to: Protect SD card against power loss? 2011/01/01 20:01:48 (permalink)
    0
    I thought the fees for using SD cards only apply if you use their proprietary protocol for communicating with them. I thought using the SPI interface got around that. 

    Also, regarding the wear-leveling issue, I have brought this up on several forums including this one before and everyone tells me that I should't worry about it because all of the flash controllers, like in the sd card, implement wear-leveling internally.  Anyways, I plan on doing some accelerated tests with my device before selling any to see if I can create a problem.
    #6
    kaptec
    Senior Member
    • Total Posts : 171
    • Reward points : 0
    • Joined: 2005/09/12 13:22:13
    • Location: England
    • Status: offline
    Re:How to: Protect SD card against power loss? 2011/01/03 04:45:47 (permalink)
    0
    Hi
    I thought the fees for using SD cards only apply if you use their proprietary protocol for communicating with them. I thought using the SPI interface got around that.

     
    Also, regarding the wear-leveling issue........

     
    Two interesting ponts.  I was'nt aware of either of these points.
     
    Ken
     
    #7
    eddygo
    Super Member
    • Total Posts : 691
    • Reward points : 0
    • Joined: 2004/03/04 15:45:37
    • Location: Bucharest, ROMANIA
    • Status: offline
    Re:How to: Protect SD card against power loss? 2011/01/03 05:57:02 (permalink)
    0
    This is a quite simple problem :) I remember there is also a App Note from Microchip presenting application of HVLV peripheral usage.
    You could use analog comparator, A/D module or better, dedicated HVLV peripheral if available.

    Basically, you have to supply your MCU + SD through a small Vf schottky diode like 1PS76SB10, then buffer isolated supply with a large cap.
    There are a lot of small size "Super Caps" by Panasonic, used as RAM or RTCC backup in range of 0.1F...0.68F @ 4Vcc
    Super Caps 0.2F in Farnell

    When main supply drops, the cap will sustain even hundreds of mA few seconds, just enough to abort/complete write. I guess one approach will be to bufferize your data with some NVRAM (check ferroelectric rams) or if you accept lower write speed (5~10mS/page) you could use regular EE (SPI or I2C) if I remember well, 24LC515 has max page of 256byte. Since you are on higher level (file system) you'll never be optimised vs. SD card because file layer will hide buffering from you, offering only "Open, Close, Read, Write... etc". Very quick will be to buffer entire SD page in some NVRAM (which btw is 512 or 1024 bytes) and write all once, but this applies only to raw writes to memory...

    The super cap is the "always work" simple method :)

    Regards,
    Edi

    ENEA, OSECK
    http://www.enea.com
    #8
    fernando306
    Senior Member
    • Total Posts : 180
    • Reward points : 0
    • Joined: 2006/06/06 07:04:08
    • Location: Earth
    • Status: offline
    Re:How to: Protect SD card against power loss? 2011/01/03 07:53:44 (permalink)
    0
    jschaenzle

    I am working on a device that uses the Microchip MDDFS library to store data to an SD card. The logger will log data at a maximum rate of 1 entry (56bytes) every minute. The problem is the device may lose power at any time, potentially in the middle of a write sequence.  I am wondering what is the best way to protect my data against corruption.  I have found that if the file is open when the power is lost, all data that was written to the file after the last file-close is lost. I don't know if the same holds true if power is lost in the middle of the write sequence. 

    Since the write procedure doesn't happen very frequently I could open the file, write the data, and then close the file, every time data is logged. Would this approach damage the sd card over time? 
    Another approach could be to keep the file open but after every 10 or 50 writes I could close the file and then re-open it.


    I could also buffer data in memory, then flush the data occasionally maybe after a kbyte or so. 

    The last idea I had was, in my circuit, I could add a large capacitor that would provide power to my pic/sd card long enough after the power is disconnected to quickly close the file. The problem with this approach is that the time it takes to close the file and/or save data is very consistent. From my understanding, this time can very depending on the current place in a flash page that the file is in.

    Anyways, what would you guys suggest?


    Seems that you already have it all figured out! Yes, you should write the file and then close it. It won't wear out the SD card!

    Supercap seems to be a fine option, although some testing would be nice to assure that it won't run out of power before writing all the data, which I doubt! You could also use a simple battery, Ni-Cd or Ni-Mh, like those used it cordless phones. You gotta sense the power using an I/O, and then if the power is lost, you can finish logging the data (or if that's useful, keep logging for a given period of time, as the battery is likely to keep it running for HOURS) and then enter a deep sleep, which you'll be able to wait for years for the power to come back again. I would stick to the battery if reability is a big concern!

    Cheers!

    ICD2 + PicKit 3
    PIC32MX360F512L development board with SD card
    PIC24F Starter Kit 1
    PIC24FJ64GB108 development board
    PIC18F8622 development board
    PIC18F8310 development board
    PIC18F4431 on breadboard
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5