Two SD card / 1 arduino


I would like to use with Arduino Mega the sd slot on the ethernet shield to log events, and another sd shield to log differents datas. Both shields work fine separately, but I can't use them together.
So, first I would like to know if it's possible... I think yes because they use SPI bus and two differents CS pin. But how manipulate the SD library? I tried to create two instances of SD library and it doesn't seem to work.


I tried to create two instances of SD library

How? The SD class has a constructor, but you never (need to) call it. There is an instance of the class created in the header file.

You need to make a number of changes to the class to support multiple instances.

I finally downloaded the library from GitHub - adafruit/SD: fixes & updates to the Arduino SD library - totally in progress. works but in beta.
Indeed, you have to make changes to the class to support multiple instances, but just one at the line 453 in SD.cpp.
I replaced if ( !, filepath, mode)) by if ( !, filepath, mode))
I don't understand why the SD object is used in the class...
It seems to works fine now.

The Adafruit SD.h and the standard SD.h are based on a version of SdFat that does not support multiple cards.

Newer versions of SdFat can support multiple SD cards.

You just need to edit SdFatConfig.h and change this line. Set USE_MULTIPLE_CARDS nonzero:

 * To use multiple SD cards set USE_MULTIPLE_CARDS nonzero.
 * Using multiple cards costs about 200  bytes of flash.
 * Each card requires about 550 bytes of SRAM so use of a Mega is recommended.

See TwoCards.ino in the examples folder for how to use two SdFat objects.

Ok thanks for your help, I will try that.

I'm trying to use the new version of SdFat as fat16lib suggested to me but I got the following error:

undefined reference to 'vtable for SdFile'

I'm using Codeblocks Arduino IDE and I added the SdFat folder to the librairies.
I don't know how to solve this problem... Any ideas?

This is a long shot. I understand Code::blocks may give that error for an undefined virtual destructor.

Try editing SdFatConfig.h and set DESTRUCTOR_CLOSES_FILE non-zero like this

 * Set DESTRUCTOR_CLOSES_FILE nonzero to close a file in its destructor.
 * Causes use of lots of heap in ARM.

I tried to set DESTRUCTOR_CLOSES_FILE nonzero but i still got the error.
Is the error related to Code::blocks ?

Is the error related to Code::blocks ?

Yes, the multiple SD card option works with the Arduino IDE.

A number of software packages have this problem with Code::Blocks. Just google for this:

code:blocks "undefined reference to vtable"

I don't test SdFat with Code:Blocks so I can't help.

Ok, indeed I tried to compile on the Arduino IDE and it works fine.
But I noticed that I have to explicitly include the SdFat library in the ino file (and not through a header file). So I tried to do the same with Code:Blocks, and it works fine... (except for some warnings).
I don't know why we can't include arduino libraries through a header file ...?

Thanks for help !