Confused on SPI.. Any help??

Guys, I'm basically developing a hardware from bare metal... I mean this is not a Arduino shield or boards, but the hardware will be custom designed. The architecture is kind of arduino, actually from the point of view of software. I will use the IDE of Arduino.

Now, At this stage, I'm totally confused on the Arduino SS pin for SPI. Let me come to straight to the point:

1) Hardware will be based on mega2560.

2) It will have SD card and Ethernet both and both will be used via the SPI.

From my understanding, I found these:

1) SD SS pin is D4 for shield. 2) Eth SS pin is D10 for shield. 3) D53 in mega2560 is the hardware SS

After reading and searching I found many info and really confused. Now I'm considering my understanding as follows:

1) Always keep D53 to OUTPUT mode for SPI. 2) When I will need to use the SD card, I need to Put the pin to OUTPUT & LOW of the card's CS pin (and as per the rule of thumb due to library, I can just use the D4) 3) When I will need to use the Ethernet (Wiznet5100/5200), I can do the same, the D10 pin OUTPUT and LOW for the pin of Eth device's CS.

[Question 1] Now, as the device will work for both SD and Eth almost the same time (yeah, not simultaneous), then when i have to use the SD card, I need to first make the D10 (for eth disable) to HIGH. Again when I need to use the Eth, the opposite, D4 to HIGH.

[Question 2] If I'm not wrong, then do I need to make it LOW again once the job is done?? [Coz i found in a thread someone made a D10 high for SD init and then directly did the ethernet init...]

[Question 3] So as it will be a device that will be in a server mode (listening for connections) hence I can just keep the D10 LOW and when only and when it will need to write something in SD, I can temporarily make the D10 HIGH for eth disable and SD enable and then later I can just shift it back again just to keep ethernet continuously running right?

Thanks in advance!! Mishu~

Every SPI device shares the three lines MOSI, MISO and SCK.

And each device must have its own chip-select pin on the Arduino. You do not have to use the Arduino SS pin for chip-select. Any Arduino I/O pin can be used.

However, even if it is not used for chip-select, it is essential to have the Arduino SS pin set at OUTPUT in order to ensure that the Arduino acts as SPI master.

...R

hi, your above reply gives the ans of Q1 and !2 so far I can understand… BBut what about the Q3?

  1. Yes, but not really.

The process of setting the CS line to LOW is a signal to the slave device that communication is about to start. So usually the line is left HIGH all the time unless you have something to say.

I haven't done much with Arduino and Ethernet but I'm pretty sure that the Ethernet library will manage the CS line by itself and you don't need to worry about it.

MorganS: Ethernet library will manage the CS line by itself and you don't need to worry about it.

Does this mean that the same applies for SD card too??? I mean then after initialize in setup, i just can use whenever and whereever i need too use the regular functions???

Yes. That's why we use libraries and standard protocols like SPI. You don't have to re-invent everything for each new sketch.

great!! Then i think problem is solved!!