chipKIT® Development Platform

Inspired by Arduino™

Using SD card and SPI in parallel avoiding interference

Created Tue, 18 Mar 2014 07:56:53 +0000 by WRainer


WRainer

Tue, 18 Mar 2014 07:56:53 +0000

Hello I have seen quite a few posts also for Arduino struggling using SD card library and SPI for other devices in the same code. I expierenced the same problem and found for my project (using chipkit Uno) that the chip select is causing the problem which is not cleared or set or assigned according to the used device.

I have solved this as follows:

#include <SPI.h> #include <SD.h> File myFile; Sd2Card card; boolean SdLoop=true;

void setup() { // no SPI.begin() or SD.begin(#number) here }

void loop() { callSPIEEPROM(); callSDCard(); }

void callSPIEEPROM() { SPI.begin(); digitalWrite(chipSelectPinEEPROM, LOW); //do what ever you want with the EEPROM or any other SPI device digitalWrite(chipSelectPinEEPROM, HIGH); SPI.end(); }

void callSDCard(); { if (SdLoop) { if (!SD.begin(8)) //the chip select pin is in my case 8 { Serial.print("fail"); } SdLoop=false; }

if (!card.init(SPI_FULL_SPEED,8)) { Serial.print("fail"); }

myFile = SD.open("/"); // do what ever you want with SD card myFile.close(); }

You may argue why SD.begin and card.init? I wanted to use the more comfortable wrapper of the SDFAT library and this is available by calling SD.begin(8). But you can call this only once otherwise it hangs up (in my case). That's why I have build in this boolean to ensure SD.begin is called only once. The card.init does not hang up when called twice. It also allows to set the speed to FULL. SD.begin is default HALF speed (as long I found it in the library). But most important, it assigns the chip select pin new for each and any call of this function and this has solved my problem with using SPI and SD in the same code. If you ask why have you not put SPI.begin and SD.begin in the setup? This has caused hang up in my case. It may be possible to have SD.begin or SPI.begin in the setup but I have not tested it further as above has worked.

Rainer


Jacob Christ

Sun, 23 Mar 2014 00:06:34 +0000

We (at PONTECH) are using SD and other SPI devices in a chipKIT project without a problem. I don't know if anything funny was done to make it work, but if this is required it sounds like something is broke and needs to be fixed.

Jacob


WRainer

Wed, 26 Mar 2014 13:58:22 +0000

Hello

Maybe you use the SDFat library only without the wrapper from SD library?

In case: http://forum.arduino.cc/index.php/topic,153447.0.html

This was described as a solution as well: " ... Using the SdFat library solved all of our problems, and it only took a few modifications to the original programs. The SdFat library is the original basis of the SD library ...."

Rainer