SPI bus misunderstanding

I'm a little confused about the SPI bus function.
I use mega2560 and ethernet W5500
Is it SPI bus pin 11,12,13 that is used for all 3

Serial consol
Ethernet data
SD drive data

If yes, is it time-division multiplexing ?
The reason I care is that it seem to me I get problem when trouble shooting via Serial.print.
Also having traffic form ethernet chip and the SD drive.
All 3 active close in time.
Like if I alter what get printed on console it affect data from SD and/or from ethernet chip.

So I'm just asking if this is something that I have to consider.

Ha det gött
Bosse

Is it SPI bus pin 11,12,13 that is used for all 3

No. The serial uses the USB connector.

If yes, is it time-division multiplexing ?

No. There must be separate chip select pins for ethernet and SD. Only one chip select can be active at a time, so only one device can transfer data at a time.

Have you considered that your code is not working? Post it in code tags.

Pete

Thanks, that was very useful information for me.
It's been pretty good working, both SD and ethernet
What I have is pin 10 as output HIGH (ether chip active ?)
So when I operate on SD maybe I have confilct with ethernet
If so, depending on timing, console output might effect how problem manifest ... or not.
So it's not clear to me how I activete SD chip and eth chip.
I was thinking pin 10 activate eth and pin 4 SD
But I found out I have to activate pin 10 to get SD working
Is that my problem ? I have eth chip active when doing SD operations.

/////////////////////////////// Setup //////////////////////////////////////////
void setup() {
  pinMode(RELAYPIN, OUTPUT);
  pinMode(ETHPIN, OUTPUT);
  digitalWrite(ETHPIN, HIGH);

  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.print("Initializing SD card...");
  if (!SD.begin(SDPIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // start the Ethernet connection and the server:
  Serial.println(F("Before start"));
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.println(F("server is at "));
  Serial.println(Ethernet.localIP());

  dht.begin();

  Udp.begin(localPort);
  Serial.println("waiting for sync");
  setSyncProvider(getNtpTime);
}

That isn't all your code.

I was thinking pin 10 activate eth and pin 4 SD

That should work fine.

But I found out I have to activate pin 10 to get SD working

Where did you find that out? As long as pin 10 is an OUTPUT, SD will work on other pins.

Pete

If I comment out in Setup

digitalWrite(ETHPIN, HIGH);

The SD.begin will fail every time
Attached my sketch
Function SDread is what giving me problem now
Apart from that all work fine

WebServerSD2560.ino (22.5 KB)

FYI
The function SDread is what giving me problem
Mind still in progress....
As example just now it works with row 220 like this

Serial.println("iloop " + n);

And work not like this ( no output on browser client )

Serial.println("iloop ");

Below current function I have an outcommented SDread that work fine
This one has a lot less SD operations

Although the MEGA has 8kB of sram (whereas an UNO only has 2kB), you make very extensive use of the String class to the point that I suspect that you are running into problems with Strings being overwritten/trashed.
It will be really painful to do but I suggest that you change your code so that it uses only C-style null-terminated strings.

However:

Is that my problem ?

You haven't explained exactly what the problem is. I suspect that you are getting garbled output on the Serial monitor and possibly also in your file on the SD card. If so, then I think a rewrite of all String usage will be necessary (unless someone who knows the String class a lot better than I do can suggest a way out of the problem).

If I comment out in Setup

digitalWrite(ETHPIN, HIGH);

The SD.begin will fail every time

That illustrates what I said: "As long as pin 10 is an OUTPUT, SD will work on other pins". The corollary being that if you don't set pin 10 to be an output (by commenting that statement), SD will fail no matter which pin it is on.

Pete

sidhabo:
I use mega2560
...
Is it SPI bus pin 11,12,13

The hardware SPI pins on the Mega are on 51, 50, 52. See the connections chart here:
https://www.arduino.cc/en/Reference/SPI
If you're using a well designed shield you will notice that it works on an Uno style board (which has the SPI pins on 11, 12, 13) as well as a Mega. The reason is that the SPI connection is actually made through the ICSP header, which has a standardized pinout and is in the same place on both boards.

It's also possible to do software SPI on any pins but it's preferable to use hardware SPI if possible.

Ok, so Strings are piling up on sram …
Still I have over 5 kB free from start…
Is it like every time I set a value to a String it will take more space in sram ?
Like if I set 4 bytes first time and 8 bytes next time I will end up using 12 bytes or 8 bytes in sram ?
Everybody tell me NOT to use Strings so maybe it’s time to work on that.
I just have a hard time to figure it out with buffers and pointers.
Like can I pass a text buffer by buffer name just like I pass a String ?
Thanks for effort guys :slight_smile:

el_supremo:
That illustrates what I said: "As long as pin 10 is an OUTPUT, SD will work on other pins". The corollary being that if you don't set pin 10 to be an output (by commenting that statement), SD will fail no matter which pin it is on.

Pete

Just so I don't misunderstand this Pete. You say "as long as pin 10 is OUTPUT"
But I found I ALSO have to set pin 10 HIGH to get SD working.
Even though I have SD SS pin set to 4.
I just commented the write statement out.
But maybe that's what you also was saying ?
Pin 10 HIGH seems to me like I have both SS active at the same time .....
Which got me worried.

pinMode(ETHPIN, OUTPUT);
//digitalWrite(ETHPIN, HIGH);

SS is active LOW. So when you set SS HIGH you are deselecting the device connected to that pin.

Thanks Pert, now it make sense :slight_smile: