Pages: [1]   Go Down
Author Topic: Changing the SS pin in the Ethernet Library  (Read 879 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working on a project that requires all 12 PWM pins to operate 4 RGB LEDs and I want to use the Ethernet shield. The problem is that the Ethernet shield uses pin 10 (PWM) on the Arduino Mega.
So I thought I could change the CS pin in the Ethernet library to another pin I don't use and rewire it to the shield later.

The changes I made to the s5100.h file:
Code:
#if defined(__AVR_ATmega1280_DFR__)
  inline static void initSS()    { DDRL  |=  _BV(0); };
  inline static void setSS()     { PORTL &= ~_BV(0); };
  inline static void resetSS()   { PORTl |=  _BV(0); };
  #define WORKED
#elif 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); };
  #define N_WORKED
#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
I use the WORKED en N_WORKED defines to verify that my changes work.
PORTL0 is pin 49 if I understand this schematic correctly (http://arduino.cc/en/uploads/Main/arduino-mega-schematic.pdf).

This is the sketch I used to check if it works:
Code:
/*
  DHCP-based IP printer
 
 This sketch uses the DHCP extensions to the Ethernet library
 to get an IP address via DHCP and print the address obtained.
 using an Arduino Wiznet Ethernet shield.
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 12 April 2011
 modified 9 Apr 2012
 by Tom Igoe
 
 */
 
 /*
   By defining __AVR_ATmega1280_DFR__ a other pin then Digital pin 10 is used for Ethenet CS.
   Instead Digital pin 49 is used.
 */
#define __AVR_ATmega1280_DFR__
 

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(19200);
  // this check is only needed on the Leonardo:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  #if defined(WORKED)
    Serial.println("worked0");
  #endif
  #if defined(N_WORKED)
    Serial.println("didn't work0");
  #endif
  
  Serial.println("Connecting...");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    #if defined(WORKED)
      Serial.println("worked1");
    #endif
    #if defined(N_WORKED)
      Serial.println("didn't work1");
    #endif
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
}

void loop() {
}

The problem is that neither WORKED or N_WORKED seems to be defined and the Ethernet shield still only works if I connect pin 10 of the Arduino to the shields CS.

I thought I wasn't that bad in programming C/C++ but I can't see what I'm doing wrong here, can someone help me with this?
Thanks in advanced!

Hardware used:
-Arduino Ethernet shield R3
-DFRobot Arduino Mega1280 compatible
Software used:
-Arduino SDK 1.0.1
-Default Ethernet library
« Last Edit: January 03, 2013, 11:12:10 am by max-232 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[kick]
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It didn't hit either #define.  Perhaps it fell into another case?

First thing I would do is #define another canary in the #else case at the end.  Maybe all the cases.

-br

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You modified a file that your sketch does not include, and you wonder why you don't see the changes. Did I understand that correctly?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS: Sorry, I should have explained. I include the Ethernet library and that includes the w5100.h file.

@billroy: So I did what you proposed and non of the values are defined. See code below
w5100.h
Code:
#if defined(__AVR_ATmega1280_DFR__)
  inline static void initSS()    { DDRL  |=  _BV(0); };
  inline static void setSS()     { PORTL &= ~_BV(0); };
  inline static void resetSS()   { PORTl |=  _BV(0); };
  #define WORKED
#elif 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); };
  #define N_WORKED0
#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); };
  #define N_WORKED1
#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); };
  #define N_WORKED2
#else
  inline static void initSS()    { DDRB  |=  _BV(2); };
  inline static void setSS()     { PORTB &= ~_BV(2); };
  inline static void resetSS()   { PORTB |=  _BV(2); };
  #define N_WORKED3
#endif
sketch (just the changes):
Code:
#if defined(WORKED)
    Serial.println("worked0");
  #endif
  #if defined(N_WORKED0)
    Serial.println("didn't work0");
  #endif
  #if defined(N_WORKED1)
    Serial.println("didn't work1");
  #endif
  #if defined(N_WORKED2)
    Serial.println("didn't work2");
  #endif
  #if defined(N_WORKED3)
    Serial.println("didn't work3");
  #endif
 
  Serial.println("Connecting...");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
 
  #if defined(WORKED)
    Serial.println("worked0");
  #endif
  #if defined(N_WORKED0)
    Serial.println("didn't work0");
  #endif
  #if defined(N_WORKED1)
    Serial.println("didn't work1");
  #endif
  #if defined(N_WORKED2)
    Serial.println("didn't work2");
  #endif
  #if defined(N_WORKED3)
    Serial.println("didn't work3");
  #endif

Am I misinterpreting the code and is the Ethernet SS pin defined somewhere else?

And thank you both for your reply's!
« Last Edit: January 08, 2013, 06:51:48 pm by max-232 » Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sure looks like the file isn't being included. 

Here's a little party trick: put "choke me here" right _after_ the last #endif to force the compile to fail.  If it doesn't fail, perhaps the file is not being compiled after all.

-br

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
@PaulS: Sorry, I should have explained. I include the Ethernet library and that includes the w5100.h file.
Yes, I saw that. The #defines are active, in the ethernet library. But, you didn't include the header file in your sketch, so they are not active there. Compilation units matter. Your sketch is one. The ethernet library is another.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@billRoy:
Ah, now we are getting somewhere!
It chokes on line 337 just after #define N_WORKED0, the part that starts with #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__).
So it is still skipping my added #if defined(__AVR_ATmega1280_DFR__).

The question is why?

Are libraries pre-compiled before the sketch?

And if so is there a way for me make an opt-in alteration that I can enable in the sketch?

@PaulS:
So I included the w5100.h and it makes no difference. Is the order bellow correct?
Code:
#define __AVR_ATmega1280_DFR__
#include <SPI.h>
#include <w5100.h>
#include <Ethernet.h>
« Last Edit: January 08, 2013, 06:56:07 pm by max-232 » Logged

Pages: [1]   Go Up
Jump to: