Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI_SS Pins in Arduino MEGA #19

Closed
hood171 opened this Issue Feb 22, 2012 · 26 comments

Comments

Projects
None yet
9 participants
@hood171
Copy link

hood171 commented Feb 22, 2012

I use Arduino MEGA1280, who has SPI pins in: 50, 51, 52 (and 53 as CS).
I changed manually the code in enc28j60.cpp at lines 247 to use these pins, but if I change the CS pin (SPI_SS) to another, like 49, because I have the SD card module, they don't work.

I see recently the ether.begin support a third parameter, but it is limited at pins 8, 9, and 10, so this not help-me.
Is possible expand the CS pin to all digital pins, for who use Arduino MEGA can use this library too?

Best Regards

@levicm

This comment has been minimized.

Copy link

levicm commented Mar 12, 2012

It would be nice to put those pins constants in header file in a form dependent of Arduino type, like this:
https://github.com/thiseldo/EtherShield/blob/master/enc28j60.h#L33

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Oct 6, 2012

@levicm why don't you create a fork, make changes, and submit a pull request...?

@jcw

This comment has been minimized.

Copy link
Collaborator

jcw commented Oct 6, 2012

Just applied a change, but more is probably needed. The point is that SPI_SS needs to be a fixed hardware-specific pin, regardless of which pin is actually used to control the EtherCard's slave select.

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Oct 7, 2012

@jcw, @thiseldo ... in response to "more is probably needed" ... I'm thinking about gutting references to selectBit in enc28j60.cpp and adopting a new variable called selectPin and using pinMode/digitalWrite with that variable instead... do you know why it wasn't done that way to begin with rather than directly manipulating PORTB and restricting to D8, D9, and D10 for the chip select?

SPI_SS is a different beast, representing the ATMega's chip select for slave mode, right? Basically that should not be set to an input under any circumstances in the context of EtherCard (or more generally cases where the ATMega is intended to behave as an SPI master). As long as it remains set to an output, my impression is that it is viable for users to do with as they wish without impacting EtherCard functionality.

@jcw

This comment has been minimized.

Copy link
Collaborator

jcw commented Oct 7, 2012

Might be a good idea - digitalWrite() is a lot slower, but it probably won't affect things much in this context.

Yes, SPI_SS needs to be an output to use the SPI hardware in master mode, Don't have to use it as select, but it does need to be defined as an output pin.

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Oct 7, 2012

@jcw OK, I'll start working this change, but would rather separate it from my current pull request which I think is good to go now

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Oct 7, 2012

@jcw I think the arduino core also provides some definitions that we can use instead of defining / managing our own definitions SPI pins, by way of the hardware/arduino/variants/standard/pins_arduino.h file...

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Oct 7, 2012

@jcw I've got an update that refers to the SPI pins with arduino defines and uses digitalWrite / pinMode to manipulate the ENC28J60 chipSelect. I can push to my fork's master and submit a pull request for it once my previous pull request is integrated and closed.

@pizzro

This comment has been minimized.

Copy link

pizzro commented Oct 10, 2012

I have the same problem. If I use ether.begin(xxx,xxx,other_cs_pin),the program isnt work. I use 2560Mega.

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Oct 10, 2012

@pizzro please elaborate. Are you saying it doesn't work with the current version (i.e. commit 0c4c1a6 or later)? Are you using the Arduino 1.0.1 IDE? If you have selected a variant board that points to the mega, the SPI definitions should come from the variants/mega folder:

static const uint8_t SS   = 53;
static const uint8_t MOSI = 51;
static const uint8_t MISO = 50;
static const uint8_t SCK  = 52;

Are you sure whatever Ethernet shield you are using is hardware compatible with the Mega in this regard?

@pizzro

This comment has been minimized.

Copy link

pizzro commented Oct 10, 2012

I just download the newest version of jcw.

@pizzro

This comment has been minimized.

Copy link

pizzro commented Oct 10, 2012

my IDE is 1.0 ,not 1.0.1

@pizzro

This comment has been minimized.

Copy link

pizzro commented Oct 10, 2012

pinMode(15, OUTPUT);
pinMode(4, OUTPUT); //SD 's CS_PIN
digitalWrite(4, HIGH);
Serial.begin(57600);
Serial.println("\n[getStaticIP]");

if (ether.begin(sizeof Ethernet::buffer, mymac,15) == 0)
Serial.println( "Failed to access Ethernet controller");

ether.staticSetup(myip, gwip);

result 1# :

[getStaticIP]

sometime.
result 2#:
[getStaticIP]
Failed to access Ethernet controller
Server: 96.46.10.64

@pizzro

This comment has been minimized.

Copy link

pizzro commented Oct 10, 2012

it don't work .

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Oct 10, 2012

@pizzro did you know you can edit an existing comment rather than adding new ones?

Can you try any of the library examples and confirm you get this hanging behavior? Also please can you download the latest Arduino IDE and confirm the problem is not in the IDE? Finally please can provide a reference to the shield that you are using and how you are attaching it to your Arduino Mega?

@pizzro

This comment has been minimized.

Copy link

pizzro commented Oct 10, 2012

it has two results.
so I comment in two comments.

@pizzro

This comment has been minimized.

Copy link

pizzro commented Oct 10, 2012

attaching it to my Arduino Mega:
enc28j60 <-> arduino2560
mosi <-> 51
miso <-> 50
sck <-> 52
ss <-> 15
rst <-> reset
int <->
CLK <->
Vcc <-> 5v
gnd <-> gnd

if I attach the 'SS' of enc28j69 to mega's 53 pin. everything is ok.

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Oct 10, 2012

Can anyone else confirm this with Mega hardware? I haven't got one to try.

@absorbingtechnologies

This comment has been minimized.

Copy link

absorbingtechnologies commented Oct 19, 2012

Hi, I have Arduino Mega and ENC28J60 board (http://www.geeetech.com/wiki/index.php/Arduino_ENC28J60_Ethernet_Module) and can confirm an issue with CS. It's wired according to the wiki link...

I am consistently having this line hang / freeze operation:
ether.begin(sizeof Ethernet::buffer, mymac) == 0)

In Arduino IDE v1.0.1, no EtherCard examples would work out-of-the-box, so I had to really play around. After much experimenting, I finally had to add the 3rd option to "ether.begin" routine, namely for the CS pin. I had read everywhere that I could put "8" (default), "9", or "10" only, but on a whim I put "53" and it FINALLY worked.

The end result:
ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)

Needless to say, this wasted quite a few hours of my time so hopefully it can be made more clear in a later revision of JCW EtherCard, as it's a fantastic library.

@pizzro

This comment has been minimized.

Copy link

pizzro commented Oct 22, 2012

I use 8pin as CS. it 's OK.

2012/10/19 absorbingtechnologies notifications@github.com

Hi, I have Arduino Mega and ENC28J60 board (
http://www.geeetech.com/wiki/index.php/Arduino_ENC28J60_Ethernet_Module)
and can confirm an issue with CS. It's wired according to the wiki link...

I am consistently having this line hang / freeze operation:
ether.begin(sizeof Ethernet::buffer, mymac) == 0)

In Arduino IDE v1.0.1, no EtherCard examples would work out-of-the-box, so
I had to really play around. After much experimenting, I finally had to add
the 3rd option to "ether.begin" routine, namely for the CS pin. I had read
everywhere that I could put "8" (default), "9", or "10" only, but on a whim
I put "53" and it FINALLY worked.

The end result:
ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)

Needless to say, this wasted quite a few hours of my time so hopefully it
can be made more clear in a later revision of JCW EtherCard, as it's a
fantastic library.

¡ª
Reply to this email directly or view it on GitHubhttps://github.com//issues/19#issuecomment-9586528.

Ïæ³ÈÍøÂç
×ܾ­Àí Åíìã
Tel:15675293071
email: pizzro@gmail.com Email%3Apizzro@gmail.com
web:www.suncent.com.cn

@rawouter

This comment has been minimized.

Copy link

rawouter commented Nov 1, 2012

@vicatcu: On my Mega shield there is an SD card reader. I need to disable it to allow access to the enc28j60, if this is not done it will indeed loop at bootup. I'm using:

diff EtherCard.cpp

349,357d348
<
< /Disabling SD card on the MEGA shield/
< #if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
< pinMode(4, OUTPUT);
< digitalWrite(4, HIGH);
< #endif
<

With this code in EtherCard::begin, the lib is working just fine after I pass 53 as csPin in the begin function.

On a side not, I had to add an include for pins_arduino.h, but I guess if this is an issue with my environment?

@vicatcu

This comment has been minimized.

Copy link
Collaborator

vicatcu commented Nov 1, 2012

@RaphYot I'm glad to hear you have found a solution, but this does not belong in the library in my opinion. It sounds like a hardware compatibility issue with a particular class of boards. If you have SPI devices that is not in use, you still have to ensure their chip select pins are properly controlled or they will interfere with the SPI bus. That should be handled in the sketch, or with a board specific library / initialization routine.

@jcw

This comment has been minimized.

Copy link
Collaborator

jcw commented Nov 1, 2012

Agreed - adjusting I/O pins for other devices is not a task for the EtherCard library, so I'm closing the issue for now.

@jcw jcw closed this Nov 1, 2012

@rawouter

This comment has been minimized.

Copy link

rawouter commented Nov 1, 2012

Indeed, and I just realized pizzro did the same.
So, I can confirm this works on Mega for me, thanks!

@brunnels

This comment has been minimized.

Copy link

brunnels commented Apr 29, 2013

So glad I found this:

ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)

This is still an issue with the latest git version. By adding the 53 my board started working immediately.

@LemarinelNet

This comment has been minimized.

Copy link

LemarinelNet commented Nov 8, 2016

Hi

I was stuck since a few hours with an ethernet shield on a MEGA board.
The problem is that I can't use pin 53 for CS since my shield is hard-wired to pin 10.
I've tried to set CS pin on BEGIN function with no success...
Then, I've found an interresting thing in arduino wiki (https://www.arduino.cc/en/Main/ArduinoEthernetShield) . It says :
"On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won't work. "
So, even if the CS pin is 10, you MUST set pin 53 to OUTPUT !
I've tried, and... IT WORKS !!!

Hope this can help, and perhaps some modification to code should be done ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.