SD Card

th3jesterth3jester Posts: 81
edited May 2011 in Propeller 1 Vote Up0Vote Down
I'm using the SD Card object to write sensor signals to. Although every once in a while I get an error and I have to stick the SD card in my computer and fix it. Is there a way the Propeller can preform this? For example:
PUB main | errorNumber, errorString

    waitcnt(clkfreq * 3 + cnt)

    fat.fatEngineStart( _dopin, _clkpin, _dipin, _cspin, _wppin, _cdpin, {
                                } _rtcdatpin, _rtcclkpin, _rtcbuslck)
    cog := cognew(Get_Data, @stack[0])
    cognew(Display, @stack[150])
    dira[9] := 1
    outa[9] := 1
    errorString := \code ' Returns the address of the error string or null.
    errorNumber := fat.partitionError ' Returns the error number or zero.
    if(errorNumber) ' Light a LED if an error occurs.
      outa[23] := 1
      dira[23] := 1

  repeat ' Wait until reset or power down.

where the IF errorNumber statement should light an LED to signify there is an error, then unmount partition, format it, and reboot. I've tried this yet it didn't work, it just kept rebooting. I know all the other code works fine.

I basically want the Propeller to fix the SD card in case there is a power failure during a write command.

This is my first time using the SD card driver. Any help will be appreciated.


  • 14 Comments sorted by Date Added Votes
  • MagIO2MagIO2 Posts: 2,181
    edited May 2011 Vote Up0Vote Down
    I think you should rather find out what the real problem is. Up to now I found SD card access being very reliable. So, what can cause problems during write?
    1. No cap in the power supply of the SD card or value to low
    2. SD card waering out

    What to do in case one is easy.
    For case 2:
    Is it an old card or a card from a bad brand? Then change the card.
    Maybe you should change the handling of the card? For example if you copy the content daily and immediately delete the file, some sectors are very often used, which increases the speed of wearout. So, fill the card until it's nearly full. (Read acces is not a problem.)
  • Mike GreenMike Green Posts: 22,093
    edited May 2011 Vote Up0Vote Down
    There is no simple way to do what you want. You're looking for a Propeller version of something like Windows' CHKDSK program which reads through the directories and FAT on the SD card looking for errors, then trying to fix any errors found. It's a complicated process, particularly if you want to try to keep as much of the data as possible.

    If you just want to erase the whole SD card and start over, you might want to stop the FATEngine and restart it from the beginning (call FATEngineStart), then try formatPartition. You also may want to display the actual error number and error string to get more information on why the formatPartition fails.

    As MagIO2 suggested, you might spend your time better increasing the capacity of your power supply to handle power failures and sensing when the power begins to fail so your program can stop writing to the SD card while there's still enough power for the card to complete the writes of what's already in its buffers.
  • th3jesterth3jester Posts: 81
    edited May 2011 Vote Up0Vote Down
    Thank you for the reply.

    It's a brand new card.

    I have to admit I didn't clarify in the first post (the power failure was a poor example). Let's say I'm writing some code, I load it to the Prop and it gives me an SD card error. I look at my code and notice one little problem, fix it and reload it. Yet it won't work unless I pull the SD card out and let the computer fix it. Basically I'm getting tired of always have to switch the SD card back and forth from the computer to the Prop.

    Could I reformat the SD card at every start-up of the Propeller? At the moment I don't need old data on it so losing data isn't a problem, its the new data I want.

    EDIT: I'll try the format suggestion, thanks Mike.
  • Mike GreenMike Green Posts: 22,093
    edited May 2011 Vote Up0Vote Down
    It depends on what's broken on the SD card. If the partition table has gotten corrupted, you'll have to do a complete format on your computer. If you read the description of formatPartition in the application note, you'll see that it's really an "erase partition" function. It won't really reformat the SD card. There's no program currently for the Propeller that will do that for you.

    formatPartition is useful if you have a program that creates lots of files or files and subdirectories and you want to get rid of all of them at one time.
  • th3jesterth3jester Posts: 81
    edited May 2011 Vote Up0Vote Down
    Yeah I just read through it. It seems to work a little better now though.

    I replaced the mountPartition() command with the formatPartition().

    what is the fileSize value? bit, byte, kilo, mega, giga.....
  • Cluso99Cluso99 Posts: 11,108
    edited May 2011 Vote Up0Vote Down
    Not sure if this may help..
    In ZiCog we setup a big file to handle each disk. This way, we just use direct sector addressing and therefore the partition table is never changed. However, there is a gotcha because we rely on the file being contiguous because in reality we access the file as a relative sector from the first sector of the file. Of course, you would need to know where the eof was, or you could just write blanks to it each time you initialise.

    However, this does not solve what is causing your problem. SD cards should have a 10uF tantalum and 0.1uF bypass caps at the socket connector on the pcb.
    My Prop boards: CpuBlade, TriBlade, RamBlade,
    Prop Tools (Index)
    Emulators (Index) ZiCog (Z80)
    Prop OS (also see Sphinx, PropDos, PropCmd)
  • Duane DegnDuane Degn Posts: 9,268
    edited May 2011 Vote Up0Vote Down
    I don't think the format command is going to do you much good.

    You're using Kye's driver right? Kye said the format command just deletes the files.

    I've also had SD cards that needed to be reformatted to work properly with Kye's driver. I've also found the "quick format" option doesn't fix the problems.

    I'll need to add a bypass capacitor as others have suggested (though I haven't had trouble recently).

  • th3jesterth3jester Posts: 81
    edited May 2011 Vote Up0Vote Down
    Thanks for all the suggestions.

    I think I will have to follow what Mike and MagIO2 suggested, creating some safety nets. i.e.- "low power or something big is about to happen, quick stop writing!"

    I have switched it back to just mounting the partition. I think it was having problems overwriting existing files, not too sure, but the fix is already what I planned on doing. Each cycle a new file is created so a complete archive will be available instead of overwriting the 1 file every time. Having the archive is much more pleasing as this project is something of a historian. Recording signal data over a period of months....I still have to do power requirements, but if all else fails I'll use wall outlets.

    @Duane - Yes, I am using Kye's new SD driver.
  • Heater.Heater. Posts: 15,573
    edited May 2011 Vote Up0Vote Down
    SD cards are basically very unreliable.
    Last year I had a 2GB card that suddenly decided that some of it's blocks were write protected despite the fact I never asked for such a thing.
    Searching around on the net and asking on the forum here never produced an answer to that problem.
    This week I have a brand new Kingston uSD card that is unreadable with "dd" under Linux.
    It's hopeless.
  • Duane DegnDuane Degn Posts: 9,268
    edited May 2011 Vote Up0Vote Down
    I use a SD card for data collection with my day job (chemist). I always have a second recording system running.

    Right now I'm using an USB thumb drive on the board (Propeller Proto) attached to the equipment. The same board transmits the data across the room to a second(Propeller Proto) board that records the data on a SD card and displays the data on a TV screen. Both boards have UPS. It is now very rarely lose data.

    I keep the last file's name in EEPROM so my program can use a new name on each file. Adding this data logging ability has made my life a lot easier.
  • AleAle Posts: 2,191
    edited May 2011 Vote Up0Vote Down
    Sorry to maybe highjack the thread but I have a question regarding low-level access to the SD... is it ok to toggle CS between bytes when the card is accessed or should it remain selected during the whole transaction ?
  • KyeKye Posts: 2,200
    edited May 2011 Vote Up0Vote Down
    @ Ale - Must remain asserted during the whole transaction. Some, cards are more flexible than others about this.

    @th3jester - Um... what you are asking can easily be done. Its mainly just how to code it. All the function calls you need are there.

    You shouldn't have to worry about partition information corruption too much using my driver if the physical link is reliable. But, that must be the case. If your SD card loses power or browns out during operations no coding will help. Other that that. You can write a complex error handling system to take account of all the files and folders on the SD card and decide what to do with them. If you've read the application note on parallax semicondcutor you should be able to understand what to do.
  • AleAle Posts: 2,191
    edited May 2011 Vote Up0Vote Down
    Thanks Kye!... that means I have to rethink this circuit...
Sign In or Register to comment.