0

It has been mentioned here http://arduino.cc/en/Main/ArduinoEthernetShield that corresponding libraries take care of chip select between SD and W5100 in Arduino Ethernet shield. Does it take care properly all the time? What happens if the Chipselect is not properly managed by libraries? Does that cause blocking of code?

1
  • Without the Chip Select the SPI device will just ignore all date on the SPI lines. I think you can trust most libraries to properly handle the CS pins.
    – Gerben
    Oct 7, 2014 at 12:18

1 Answer 1

2

Here we have a nice example of a single Shield requiring TWO libraries. The Ethernet.h and SD.h where the later appears as an after thought and is not demonstrated by the Example. As it is done so in the SD examples. However, their styles are slightly different.

The Ethernet libary as mentioned (provided within) the IDE is hardcoded to implement the Chip Select for the W5100 on corresponding pin. When using the typical purchased Ethernet Shield, there is no need to configure it. Unlike the SD library, which requires the pin to configured using the SD.begin(4) member function.

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

Where again on the typical Ethernet shield the SD card is at pin D4.

Note being hardcode that the Ethernet library selects the matching pin to corresponding shield.

private:
#if defined(ARDUINO_ARCH_AVR)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  inline static void initSS()    { DDRB  |=  _BV(4); };
  inline static void setSS()     { PORTB &= ~_BV(4); };
  inline static void resetSS()   { PORTB |=  _BV(4); };
#elif defined(__AVR_ATmega32U4__)
  inline static void initSS()    { DDRB  |=  _BV(6); };
  inline static void setSS()     { PORTB &= ~_BV(6); };
  inline static void resetSS()   { PORTB |=  _BV(6); }; 
#elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
  inline static void initSS()    { DDRB  |=  _BV(0); };
  inline static void setSS()     { PORTB &= ~_BV(0); };
  inline static void resetSS()   { PORTB |=  _BV(0); }; 
#else
  inline static void initSS()    { DDRB  |=  _BV(2); };
  inline static void setSS()     { PORTB &= ~_BV(2); };
  inline static void resetSS()   { PORTB |=  _BV(2); };
#endif
#endif // ARDUINO_ARCH_AVR

And in the case of the UNO (or the else case) Port B Bit 2 is D10.


worth noting that SD library has an alternate method of initializing and using the SD card

if (!card.init(SPI_HALF_SPEED, chipSelect)) {
  Serial.println("initialization failed. Things to check:");

where chipSelect would be D10 in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.