Pages: [1]   Go Down
Author Topic: Due with SD (SdFat) AND Ethernet Shield Problems  (Read 2631 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Forgot to mention I am using Arduino 1.5.1r2
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1654
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/**
 *  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.
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pages: [1]   Go Up
Jump to: