Go Down

Topic: Arduino Ethernet Shield MEGA hack (Read 11 times) previous topic - next topic

nkcelectronics

Hi, I published a hack on how to use the current Arduino Ethernet Shield with the new Arduino MEGA board.

http://mcukits.com/2009/04/06/arduino-ethernet-shield-mega-hack/

Nick Fury

Well, I was on the right track but I couldn't figure out what to edit in the file:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238295170/20

Thank you so much for this.

nkcelectronics

I tried to make the hack work with less pin switches, meaning trying to keep pin SS where it is, in digital 10... but for some reason (that was also discussed long time ago in this forum) it doesn't work.  SS is supposed to be used only when the ATmega is a slave.  In this case it is master, but still SS is hardcoded somewhere.

darudude

Cool!

Quick Question.
What does this code do?
Code: [Select]
#define SPI0_SS_BIT BIT2
...
#define SPI0_SCLK_BIT BIT5
...
#define SPI0_MOSI_BIT BIT3
...
#define SPI0_MISO_BIT BIT4
...
#define IINCHIP_CS_BIT BIT2

and replace them with this code:


#define SPI0_SS_BIT BIT0
...
#define SPI0_SCLK_BIT BIT1
...
#define SPI0_MOSI_BIT BIT2
...
#define SPI0_MISO_BIT BIT3
...
#define IINCHIP_CS_BIT BIT0


What do the different Bit numbers signify?

nkcelectronics

@darudude, the code is part of the pin definitions on the spi.h file, used by Ethernet library.  Each bit definition indicates which pin on the Arduino board is supposed to be wired to the Ethernet shield.  They seem like a small change (BIT2 to BIT0), but physically the pins moved from digital 13-10 on the Duemilanove type board to digital 50-53 on the MEGA board.

It is like each BIT definition, together with the PORT, indicates each physical port on the Arduino board.

darudude

ah okay. I actually dwelled a little bit into the Ethernet library but couldn't make head or tails of what was going on.

Another question:

Why do SPI0_SS_BIT and IINCHIP_CS_BIT share the same pin and port? whats the difference between the two? It seems as the IINchip has something to do with the Wiznet module and the SPI0 is for the Atmega, but why are they set to the same pin?

nkcelectronics

#6
Apr 15, 2009, 04:47 am Last Edit: Apr 15, 2009, 05:22 am by nkcelectronics Reason: 1

Both represented the same pin and it is how you activate the SPI mode on the W5100 chip.  I will try again to move it back to Digital port 10 and see if it works now.  This way, only 3 pins need to be moved for the MEGA hack to work.

nkcelectronics

I fixed the SPI0_SS_BIT issue with the MEGA (and all SPI usage in general, when trying to move the SS bit to a different pin).

What happens is that the SS pin is used by AVR only when acting as a SLAVE, AND also when it is setting the SPCR register (SPI working mode definition register).  SS needs to be high before setting AVR in SPI master mode by setting SPCR bit 4 to HIGH.  If this pin is not high, then it doesn't go into Master mode.  Changing the following line in spi.h:

Code: [Select]
PORTB |= SPI0_SS_BIT; PORTB &= ~(SPI0_SCLK_BIT|SPI0_MOSI_BIT);\


to

Code: [Select]
PORTB |= SPI0_SS_BIT | BIT0; PORTB &= ~(SPI0_SCLK_BIT|SPI0_MOSI_BIT);\


solves the problem and now the Arduino Ethernet Shield can work with the MEGA board by moving only 3 pins, not 4.

The only drawback is that pin SS (IO port 53) will remain high, but any setting using digitalWrite() after Ethernet.begin() will change the value to a desired value, if you ever need to use this port.

I will edit my original hack posting to reflect this new change, that makes the hack simpler to perform.

bioactive712

what if someone wants to use multiple ss pins for pultiple devices...
I want to use the sd on the ethernet shield...
It simply doesn't work with the mosi pin of the spi connected to both wiznet and sd...
is it possible to set the pi 10 hi in order to set arduino as slave and still use diferent pins for cselecting w5100 and sd?

nkcelectronics

MOSI, MISO and SCK pins can be shared, SS or CS cannot be shared and you need a dedicated pin for each SPI slave device.

http://en.wikipedia.org/wiki/File:SPI_three_slaves.svg

noenoe

This seems a bit tricky.

Is anyone able to show some code to show how another SPI device is made to run alongside the ethernet shield.
Will it (apart from the fix explained early in this topic) be very different on the mega in relation to ie. a Duemilanove.

I have a SHT15 SPI temperature and moisture sensor that I would like to run along withe the ethernet-shield on a seeduino mega.

foxkid

This worked great for me.  Thank you.

Jonathan Oxer

Quote
MOSI, MISO and SCK pins can be shared, SS or CS cannot be shared


That's quite correct as far as the SPI spec is concerned, but one problem with the Wiznet W5100 chipset used in the official Ethernet shield (and many others) is that it fails to release the bus properly when it's deselected. That's a real pain. One way around that is to slave the Wiznet's SEN line to the CS line using a MOSFET or similar, but unfortunately the official shield doesn't do that. The new Ethernet Shield I've been working on recently does though: http://www.freetronics.com/ethernet-shield

Cheers

Jon

joker

Thanks (a lot) nkcelectronics.  ;)

I was stuck trying to run multiple SPI modules! :(

Go Up