Due with SD (SdFat) AND Ethernet Shield Problems

Hey,

I have a project I am moving to the Due and am trying to get what used to work on the Mega 2560 working on the Due. Specifically I have a Ethernet Shield R3 and am trying to use both the SD card and Ethernet interface in one sketch.

Unfortunately I am having some problems getting the ethernet to work after initializing the SD card. If I use the stock SD library (not SdFat) it seems to be able to initialize both, I used to use the stock SD library but switched to SdFat as I needed some of the extra functionality plus I know the SD library uses (or used to use for avr) SdFat internally so i'm not sure why now SD works and SdFat doesn't (to be fair SdFat for Due is in beta, I am using the version from the 12/3/2012).

Let me show you some code (not full sketches):

SdFat sd;

// disable w5100 SPI while setting up SD
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
if (!sd.begin(4, SPI_HALF_SPEED))
{
    sd.initErrorHalt();		
    Serial.println("Failed to init SD card");
    return;
}
else
    Serial.println("SD init done");


// now disable SD card CS and enable ethernet CS
digitalWrite(4, HIGH);
digitalWrite(10, LOW);
if (Ethernet.begin(mac) == 0) 
{
    Serial.println("Failed to configure Ethernet using DHCP");
    return;
}

As you can see I enable the SD card CS and disable the ethernet one before initializing the SD card and then reverse the procedure for initializing the ethernet (ala Arduino Mega 2560 ethernet/sd shield issue. - #2 by SurferTim - Networking, Protocols, and Devices - Arduino Forum).

When it gets to initializing the ethernet interface the code hangs and this point and the "Failed to configure" line is not printed.

If I omit the digitalWrite statements, SD inits ok and the ethernet fails and returns immediately. I'm pretty sure I have my pin numbers correct for the two CS lines, they seem to be the same as the mega (Arduino Forum).

The following code using the stock SD library works:

if (!SD.begin(4)) {
    Serial.println("SD initialization failed!");
    return;
}
Serial.println("SD initialization done.");

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    return;
}

If anyone has gotten this working (SD card using SdFat AND Ethernet) please let me know, or if anyone has any ideas as to what I'm doing or not doing wrong :slight_smile:

If fat16lib or any of the other SdFat guys have any ideas that would be great. Happy to help debugging the problem given SdFat for Due is beta.

Cheers,
JayCee

Forgot to mention I am using Arduino 1.5.1r2

It appears that the problem is that SdFat uses SPI DMA by default to achieve high performance.

The Ethernet driver doesn't seem reinitialize the SPI bus for the mode it uses before each access.

Edit SdFatConfig.h and at about line 50 make this change to use the Arduino SPI library.

/**
 *  Force use of Arduino Standard SPI library if USE_ARDUINO_SPI_LIBRARY
 * is nonzero.
 */
#define USE_ARDUINO_SPI_LIBRARY 1

This seems to fix the problem.

Hello,
I use the DUE with an ethernet Shield R3 and shifted now from the SD library to the SdFat library.
So that the ethernet functions generally, I must set the parameter

#define USE_ARDUINO_SPI_LIBRARY 1

After this, the SD card and also the Ethernet works.

In my sketch, I transfer a file per FTP to the SD card. But the speed is in the DUE now about 3x slower as on the MEGA (same sketch, same SD card). In the MEGA, i use a buffer of 255byte, on the DUE i tested 255, 1024, 4096byte but the speed is the same).

Is there a trick of side of the SdFat library to optimize the interaction with the ethernet library on the DUE? Why is it 3x faster on the MEGA?

btw: the original SD-library is even slower on the MEGA and DUE as the powerfull SdFat.

Thank you very much.
paulinchen

Hi All,
I hope the mentioned problem is sorted in your project, but just in case for future reference I'd like to share a experience I had few days ago.

I'm had a crisis where I couldn't get my Shield to work with DUE on SDFat libary for a WebServer, but the exact code works fine on Mega with the same Shield. After few runs and help of @SurferTim, he narrowed that the MISO line is not getting released by the SD module (using SDFat library, I hope @fat16lib will be able to shed a light oh this).

Then I went to do the exact same thing the OP did, but it was a failure. No matter what I do once the SD card reader is connected on the SPI (with or without SD card), only the SD reader is usable.

while looking at the schematic I found that,

The generic micro SD card reader available in site like AliExpress (0.5USD) has a QUAD Tri-State buffer on the SPI line (74LVC125). its EN pins are grounded and active all the time. (after reading on better SPI designs Better SPI Bus Design | Hackaday) I tried removing the EN pin (13) for MISO and manually connecting to 3.3V.
then the W5100 ethernet module worked and SD module didn't as expected.

While trying to tie that pin to CSS line the pin broke of the SMD rendering the SD card reader useless. Now waiting to design an external SPI buffer between the DUE and the SPI modules, that will use the CSS line to activate the buffer.

Anyways, I might not know about potential performance impact by this but I'll be further doing some testing and will post it in a new thread later...

BR

https://forum.arduino.cc/index.php?topic=465101.0