9 Replies Latest reply on Jan 4, 2014 2:01 PM by RSavage1

    How to get SD card to initialize?

    RSavage1

      When I try to get the SD card to work on the Galileo I can not get the SD.begin() statement to work. Using IDE 1.5.3 I have upgraded the card's firmware and also confirmed that the SD card works by using the serial port via Linux to verify that I can mount the drive and see a listing of the files on the card. However, when I try the following code I can not get the SD card to initialize. Result is always false regardless if I use chip select 4 or 0 or make the call with empty "()" brackets. The SD card is 8 MB formatted for FAT32 and is inserted in the SD holder on the Galileo. Release notes for version 1.5.3 says that the "begin" statement is supported. Am I missing something or does the SD card have to be mounted first using Linux? Any help on this is highly appreciated.

       

      // Open serial communications

      Serial.begin(9600);

      Serial.print("Initializing SD card...");

       

      if (!SD.begin(4)) {

          Serial.println("initialization failed!");

          return;

        }

      Serial.println("initialization done.");

        • 1. Re: How to get SD card to initialize?
          Intel_Jesus

          Hi RSavage1,

           

          I am able to initiate the SD card using the exact same code you provided. Usisng all the chip select values with no problems(4,3,2,1,0) and empty as well. The only things i added is a 1s delay before to give me time to open the Serial Monitor and the <SD.h>.

           

          Can you give a little more details on how it doesn't work?

          Does it compile? Do you see anything in the Serial Monitor?

           

          Regards,

          Intel_JEspinoza

           


          • 2. Re: How to get SD card to initialize?
            RSavage1

            The code that I am using is cut and past from the Arduino SD ReadWrite example and before running the code example above I do include the "#include <SD.h>" statement. The code complies and loads ok. I then open the IDE serial monitor and press the "reset" button on the Galileo. What I then see is "Initializing SD card...initialization failed!", I never get the response "initialization done".

             

            If I change the code to the following, then the return value of SDTest is always "0". I have tried to add a 2 second delay right after the "SD.begin();" statement with the same result (i.e. initialization fails and SDTest is "0"). Do I need to add a delay right after the "Serial.begin(9600);" statement? I would try this but I will not have access to my test setup for at least the next couple of days.

             

            #include <SD.h>

            Boolean SDTest;

             

            void setup()

            {

            Serial.begin(9600);

            Serial.println("Initializing SD card...");

            SDTest = SD.begin();

            Serial.println(SDTest);

             

            if (!SD.begin(4)) {

                 Serial.println("initialization failed!");

                 return;

               }

            Serial.println("initialization done.");

            }

            • 3. Re: How to get SD card to initialize?
              Intel_Jesus

              RSavage1,

               

              I copy and paste the exact code from the ReadWrite example and my SD card Initialized just fine.

              I formatted my card to FAT32 as well. Have you tired a different SD card?

               

              Regards,

              Intel_JEspinoza

              • 4. Re: How to get SD card to initialize?
                RSavage1

                The SD card that I am using is 8 GB (FAT 32) that was originally formatted for a Raspberry Pi using SD Card Association’s formatting software tool (SD Formatter 4.0 for SD/SDHC/SDXC - SD Association). I originally

                confirmed that the card works by using Galileo's serial port via Linux to verify that I can mount the SD drive and see a listing of files on the card. In the next few days when I get back to the test set I will try a different card as well as re-format the original card using Windows7 to see what happens, I will let you know the results.

                 

                Thanks


                • 5. Re: How to get SD card to initialize?
                  MarcIII

                  Hi,

                   

                  The Arduino IDE relies 100% on the standard Linux drivers to access SD cards; there is no magic there (you can tell from the C++ source code of the SD class). In other words, if this specific SD card works fine from the Linux prompt on Galileo then do not reformat it or change it in any way - you could only make it worse.

                   

                  This looks more like some configuration problem... I would try to add some additional logging and error checking to the C++ code of the SD class (after backing up the original of course). Classes should be automatically re-compiled and re-deployed every time you re-run the sketch.

                   

                  Cheers

                  • 6. Re: How to get SD card to initialize?
                    RSavage1

                    I tried a different SD card formatted for FAT 32 using Windows 7 and verified that the SD card works by mounting the drive via Galileo/Linux serial port and verified that I could see files on the card and could also copy and make directories. But when I tried to run the sketch I get the same results in that the SD.begin() statement still does not work.

                     

                    I then modified the SD.cpp file to see if I could add debug code by including a "Serial.println" command (see code below), result was that when I recompiled the the code and loaded the sketch I never saw the results of the "println" statement. I then added the "#include "Arduino.h" " statement to the SD.cpp file to see if that would make a difference, results was the same. Am I doing something wrong or is the SD.cpp file not being complied, or maybe I have a bad Galileo card?

                     

                    boolean SDClass::begin(uint8_t csPin) {

                       /*

                       Performs the initialization required by the sdfatlib library.

                       Return true if initialization succeeds, false otherwise.

                       */

                      Serial.println("Test to see if Serial print statement works for debug");

                      return card.init(SPI_HALF_SPEED, csPin) &&  volume.init(card) && root.openRoot(volume);

                      }

                     

                    Note, I also tried to re-install the the Ardunio IDE 1.5.3 by coping the zip file to the root directory of my C-Drive and unzipping the contents to "C:\Intel_Galileo_Arduino_SW_1.5.3_on_Windows_v0.7.5", results was the same in that the "SD.begin()" statement did not work.

                    • 7. Re: How to get SD card to initialize?
                      MarcIII

                      Hi,

                       

                      It looks like you edited the non-Intel SD.cpp file. There is another SD.cpp file buried in some other "x86" subdirectory (before someone shouts: they are completely different from each other - extremely little copy/paste :-)

                       

                      In a big and unfamiliar code base, every time I am not quite sure the correct file is the one I am currently looking at I use this very simple and very effective trick: I forcibly break it with some bad code. Whenever this has no effect I know I am in the wrong place.

                       

                      Cheers - Marc.

                      • 8. Re: How to get SD card to initialize?
                        MarcIII

                        Sorry I meant: "I am in the wrong place OR not recompiling OR not linking OR not deploying..." Breaking the code to various degrees helps making the difference between these; I always start by inserting plain English for maximum breakage.

                        • 9. Re: How to get SD card to initialize?
                          RSavage1

                          Problem Solved......Using Marclll's advice I added debug print statements to Galileo's SD.cpp file (see path below) and determined that the SD_MOUNT_PATH (/media/mncblk0p1) was not being found in the "mtab" file.

                           

                          Path to Galileo SD.Cpp File

                          C:\Intel_Galileo_Arduino_SW_1.5.3_on_Windows_v0.7.5\arduino-1.5.3\hardware\arduino\x86\libraries\SD.cpp

                           

                          I then did a "fdisk -l" and got the following results which did not look right in that the SD card was showing that there was 4 partitions.

                           

                          fdisk -l image before reformat.JPG

                           

                          I then used the SD Card Association’s free formatting software tool (SD Formatter 4.0 for SD/SDHC/SDXC - SD Association) to reformat the card on the PC using "Format Type" set to "Full (OverWrite)" and "Format Size Adjustment" set to "On". After I did this I then inserted the SD card back into the Galileo and did another "fdisk -l" command. Results are shown below and show that the SD card now only has one partition.

                           

                          fdisk -l image after reformat.JPG

                           

                          After reformatting the card I then ran the sketch again and this time everything worked like it should!!!

                           

                          Without the help I would not have been able to troubleshoot this....thanks!