Go Down

Topic: Due with SD (SdFat) AND Ethernet Shield Problems (Read 4547 times) previous topic - next topic



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):
Code: [Select]

SdFat sd;

// disable w5100 SPI while setting up SD
if (!sd.begin(4, SPI_HALF_SPEED))
    Serial.println("Failed to init SD card");
    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");

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 http://arduino.cc/forum/index.php/topic,86837.msg650948.html#msg650948).

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 (http://arduino.cc/forum/index.php?PHPSESSID=93a36a30afd8b1aa250d55dbc311426f&topic=132130.0).

The following code using the stock SD library works:
Code: [Select]

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

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

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  :)

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.



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.
Code: [Select]

*  Force use of Arduino Standard SPI library if USE_ARDUINO_SPI_LIBRARY
* is nonzero.

This seems to fix the problem.


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


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.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131