Boo0ns / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

SD wrapper: SD.exists() buggy and SD.stop() missing #465

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I am using the new SD wrapper library and find it very simple and useful.
But the following issues make it unusable in some case:

Let's assume someone needs to remove the SD card from the socket (or disables 
the power to it when system can go idle) and put the same or another card back 
to the socket while the system is running, the following problems occur:

1) it seems that the SD object holds a copy of the FAT because
SD.exists(someFile) returns true even if there is no SDCard in the socket or
another one (where this file does NOT exist) is put in. I guess this copy of 
the FAT won't be refreshed again after SD.begin().

2) you can not re-initialize the SD-object again. This would be
necessary (for some SPI magic I guess) if you remove the SDCard from the socket 
and put it back later. There is no SD.close() or something and calling 
SD.begin() a second time will always return FALSE and not initialize the SD 
again.

Issue 1 and 2 could be solved by adding a destructor or a function to
re-initialize the SD object.

Original issue reported on code.google.com by linux-...@o2online.de on 21 Jan 2011 at 2:26

GoogleCodeExporter commented 9 years ago
I had the same issue. Here is the code change that I made to SD.cpp to 
recognize card changes and allow SD.begin to be called many times...

boolean SDClass::begin(uint8_t csPin) {
  /*

    Performs the initialisation required by the sdfatlib library.
    Return true if initialization succeeds, false otherwise.

   */
   //return card.init(SPI_FULL_SPEED, csPin) &&
   //      volume.init(card) &&
   //      root.openRoot(volume);
   cid_t cidbuf;

   if(card.init(SPI_FULL_SPEED, csPin)){
       //check to see if we still have the same card in the slot by comparing SD ID
       card.readCID(&cidbuf);
       if(memcmp(&(card.inslot_),&cidbuf,sizeof(cid_t)) == 0){
            //they match so don't need to init vol or  open root
            return(true);
       }else{
           //they don't match, close root, init volume, open new root
           if(root.isOpen()){
               if(root.close() == false){
                    return(false);  //could be problem if there was cache lying around because
                                    //file was left open when SD card was removed
               }    
            }                 
            if(volume.init(card) && root.openRoot(volume)){
                memcpy(&(card.inslot_),&cidbuf,sizeof(cid_t));
                return(true);
            }           
        }
    }       
    return(false);
}

You will also have to add cid_t inslot_ to SD.h

                -Bennett

Original comment by bennettm...@gmail.com on 22 Sep 2011 at 8:37

GoogleCodeExporter commented 9 years ago
Thanks Bennett,
this solution works for me. I have patch the Sd Library with your code and now 
i can remove and insert the SD Card without error.
Please add this to the Library it es very usefull for a datalogger 
application!!!!!!

Thx Bennett
Matze

Original comment by matthias...@gmail.com on 17 Mar 2013 at 3:10

GoogleCodeExporter commented 9 years ago
Thanks Bennett! This is really helpful. What is the role of "cid_t inslot_"?

Original comment by bdal...@gmail.com on 22 Mar 2013 at 6:30

GoogleCodeExporter commented 9 years ago
Im Test the solution now for some days and it works not stable!
After a random SD-Card changes the file.open gets errors.
I think this solution is not save.

Matze

Original comment by matthias...@gmail.com on 24 Mar 2013 at 10:37

GoogleCodeExporter commented 9 years ago
Bennett, can you please tell me what exactly needs to be modified in SD.h. I 
added cid_t inslot_ to SD.h, but the compiler says error: 'class Sd2Card' has 
no member named 'inslot_'. Maybe I need to make changes to Sd2Card.h.

Original comment by dxdx...@gmail.com on 30 Oct 2013 at 7:10

GoogleCodeExporter commented 9 years ago
..hallo dxdx347

Have you solve it?
where you have modified the SD.h file to successfully compile with new inslot_ 
and cid_t variable?

I have the same problem...

Original comment by alessio3...@alice.it on 8 Nov 2013 at 11:26

GoogleCodeExporter commented 9 years ago
Please where to add cid_t and inslot_ in SD.h?

Original comment by maria.ce...@gmail.com on 27 Mar 2015 at 6:55