SDFat32, Mega2560, Ethernet

Hi,

Despite I read just about all posts regarding the SDFat library and Ethernet usage together, I still can’t make it to work.

The code is rather simple, just lists the content of the current directory of the SD card.

I can’t see what I am doing wrong – would someone point it out pls!

Many thanks.

HW:
DFRduino Mega 2560
DRFduino Ethernet Shield V2.0

SW:
arduino-0022
sdfatlib20110902

#include <SdFat.h>
#include <SdFatUtil.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,210 };
byte gateway[] = { 192,168,0,1 };
byte subnet[] = { 255, 255, 255, 0 };

// telnet defaults to port 23
Server server(23);

// SD chip select pin
const uint8_t SD_CARD_SELECT = SS_PIN;
const uint8_t W1500_CHIP_SELECT = 10;
const uint8_t MEGA_SPI = 53;

SdFat sd;
Sd2Card card;
SdVolume vol;

void setup() 
{
  pinMode(W1500_CHIP_SELECT, OUTPUT);
  pinMode(MEGA_SPI, OUTPUT);
  
  // open the serial port
  Serial.begin(9600);
  
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);
  // start listening for clients
  server.begin(); 
  
  //SD card
  Serial.print("Initializing SD card...");
  
  // initialize the SD card 
  if (!sd.init(SPI_FULL_SPEED, SD_CARD_SELECT)) sd.initErrorHalt();

  Serial.print("Volume is FAT ");
  Serial.println(sd.vol()->fatType(), DEC);
  Serial.println();
  
// list file in root with date and size to confirm card is in working order
  PgmPrintln("Files found in root:");
  sd.ls(LS_DATE | LS_SIZE);
  
  Serial.println("Listening");
  Serial.println("==== End Setup ===");
}//End Setup
//**********************************************************************
void loop() 
{  
 // Telnet: wait for a new client:
  Client client = server.available(); 
 
  Serial.println("********** Loop This prints until first or second Telnet message ********************");    
  Serial.println("List LS_R: ");
  sd.ls(LS_R | LS_DATE | LS_SIZE);
  Serial.println("************************************");
   
 if (client) 
 {
    readTelnetClient(client); 
 }
delay(5000); //give time to observe printout
}//End Loop

void readTelnetClient(Client client)
{
  // read the bytes incoming from the client:   
  Serial.println("+++++++++++++++++ readTelnetClient +++++++++++++++++"); 
  
  digitalWrite(SD_CARD_SELECT, HIGH);   // enable SD chip
  digitalWrite(W1500_CHIP_SELECT, LOW); // disable W5100 chip
  
  Serial.print("SD_CHIP_SELECT pin state: "); Serial.println(digitalRead(SD_CARD_SELECT), DEC);
  Serial.print("W1500_CHIP_SELECT pin state: "); Serial.println(digitalRead(W1500_CHIP_SELECT), DEC);
  
  char thisChar = char(client.read()); //discard input. just empty buffer
  client.stop(); 
  
  digitalWrite(W1500_CHIP_SELECT, HIGH); // enable W5100 chip
  digitalWrite(SD_CARD_SELECT, LOW);     // enable SD chip
     
  Serial.print("SD_CHIP_SELECT pin state: "); Serial.println(digitalRead(SD_CARD_SELECT), DEC);
  Serial.print("W1500_CHIP_SELECT pin state: "); Serial.println(digitalRead(W1500_CHIP_SELECT), DEC);
  
  
  Serial.println("+++++++++++++++++ May print first time around +++++++++++++++++");   
  Serial.println("List Directory: "); 
    sd.ls(LS_R);
  Serial.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");
  Serial.println();       
}
  digitalWrite(SD_CARD_SELECT, HIGH);   // enable SD chip
  digitalWrite(W1500_CHIP_SELECT, LOW); // disable W5100 chip
  
  Serial.print("SD_CHIP_SELECT pin state: "); Serial.println(digitalRead(SD_CARD_SELECT), DEC);
  Serial.print("W1500_CHIP_SELECT pin state: "); Serial.println(digitalRead(W1500_CHIP_SELECT), DEC);
  
  char thisChar = char(client.read()); //discard input. just empty buffer
  client.stop();

Turn off the ethernet shield, then read from it. I wonder why more people don't try that.

I can't see what I am doing wrong – would someone point it out pls!

You expect the code to do something. The code does something. If those two somethings were the same thing, I think it is safe to assume that you would not be here asking what was wrong. So, I think it is safe to assume that those two somethings are not the same thing.

What you expect the code to do is not clear. What it actually does is not clear. Therefore, what you are doing wrong is impossible to define.

I would replace this

const uint8_t SD_CARD_SELECT = SS_PIN;

with this

const uint8_t SD_CARD_SELECT = 4;

...and these remarks are backwards:

digitalWrite(SD_CARD_SELECT, HIGH); // enable SD chip digitalWrite(W1500_CHIP_SELECT, LOW); // disable W5100 chip

should be

  digitalWrite(SD_CARD_SELECT, HIGH);   // disable SD chip
  digitalWrite(W1500_CHIP_SELECT, LOW); // enable W5100 chip

LOW is enabled, HIGH is disabled.

Thanks for looking at it.

SurferTim: I would replace this

const uint8_t SD_CARD_SELECT = SS_PIN;

with this

const uint8_t SD_CARD_SELECT = 4;

Sure. The SS_PIN comes from the SdFat library. It is Arduino 4.

SurferTim: ...and these remarks are backwards:

digitalWrite(SD_CARD_SELECT, HIGH); // enable SD chip digitalWrite(W1500_CHIP_SELECT, LOW); // disable W5100 chip

should be

  digitalWrite(SD_CARD_SELECT, HIGH);   // disable SD chip
  digitalWrite(W1500_CHIP_SELECT, LOW); // enable W5100 chip

LOW is enabled, HIGH is disabled.

Whoops! Thanks! The code (here) is correct though. Should work.

Also, these chip-selects are not really necessary here for there is not much activity. The printout lines would give ample time to finish the single byte transmission (of the W5100) before the SdFat lib. needs the bus.

Also, the 'state' remains the same even if there is no more Ethernet input. The sdcard remains inaccessible.

It seems that the W5100 doesn't release the SPI bus. But this is just theorising. Got no idea if this is possible at all.

PaulS:
What you expect the code to do is not clear. What it actually does is not clear. Therefore, what you are doing wrong is impossible to define.

Thank you for taking the time to look at the code.

This is a highly abstracted code that allows to focus on the problem.
Doesn’t do anything more than demonstrates the problem.

Here is some pseudo code what it does, ignoring the declarations etc.

loop
print the current directory on the SD card to Serial
is there an input from the Ethernet?
if yes, print the current directory of the SD card to Serial

The problem is with the SDcard access if there was any Ethernet activity.
In other words, the SDcard is accessible until transmission from the Ethernet was received.
Thereafter the SDcard is inaccessible.
For this to troubleshoot one doesn’t need to know what the received data was.

There are ‘printouts’ inside the code indicating what should happen and what does/doesn’t happen.

A notation/comment error was already found as it can be seen in previous posts.
Quite obviously there is also a code error somewhere for it doesn’t work.

Solved.

I have reloaded the libraries and it solved the issue.

Thanks for everybody trying to help!

Cheers.