Ethernet Shield change CS in library

Hi,

I have one of these shields https://www.arduino.cc/en/Main/ArduinoEthernetShield.

I have a customised Arduino PCB and I've connected the Ethernet shield to it, and have it working. I connected the ICSP and pin 10 to my Arduino. However, I'm wondering whether I cannot change the pin 10 to another pin. I'm using the Ethernet library. Is this possible?

Thanks

However, I'm wondering whether I cannot change the pin 10 to another pin.

No, you can't. Pin 10 must be an OUTPUT pin, to make the Arduino an SPI master.

You can, but as PaulS said, you must set D10 as OUTPUT to set the SPI bus to master mode. This is easier to do with a Mega 2560.

You can, but as PaulS said, you must set D10 as OUTPUT to set the SPI bus to master mode. This is easier to do with a Mega 2560.

The customised PCB I'm using is based on the Mega 2560. What exactly do I need to change in the Ethernet library?

Cheers

I had code at one time that you could select the CS pin for the ethernet shield, but it doesn't function very well with IDE v1.6.5 because of the new Due.

This code is from /libraries/Ethernet/src/utility/w5100.h

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

You must select a pin that you want to use for the slave select from this list. It uses D10 by default (PortB pin 4).

Then change this section of the code. This is for the Mega.

#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); };

The easiest is to change it to D11. That is PortB pin 5. Like this:

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  inline static void initSS()    { DDRB  |=  _BV(5); };
  inline static void setSS()     { PORTB &= ~_BV(5); };
  inline static void resetSS()   { PORTB |=  _BV(5); };

Thanks a lot SurferTim.

If I wanted CS to be digital pin 29, does the following look correct?

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  inline static void initSS()    { DDRA  |=  _BV(7); };
  inline static void setSS()     { PORTA &= ~_BV(7); };
  inline static void resetSS()   { PORTA |=  _BV(7); };

Cheers

That looks correct.

That looks correct.

Working fine, thanks.