Arduino Wiznet5100 ethernet shield interfacing to atmega1284p

Hello Folks,

I am trying to get an off the shelf Arduino Ethernet shield to interface to the atmighty (atmega1284p) With the Maniacbug Optiboot boot-loader installed.

I have got the integrated SD card to work without issues however I cannot get the DHCP demonstration sketch to work.It just return that it has failed.

To get the SD car reader working I connected the SD SS pin(4) to D0 on the 1248 and set that as the select pin in the sd init call and also connected the Ethernet SS Pin(10) to the SS (D4,PB4,pin5) on the 1248.

In order to get the SPI to work I also had to set the port direction of the MISO,MOSI and SCK in code and set SS as outputs.

Does anyone have experience of actually getting this combination to work. Any other idea would be gratefully received.

Cheers Pete.

If you got the SD to work, the SPI data lines are correct. The ethernet library wants the w5100 slave select on D10. The w5100 and the SD card don't work well together if they are not started correctly. Try this startup and see if it works for you. Compile and upload, then power down the Arduino for a few seconds, then power it up again. You shouldn't have to do that all the time, just when you had problems with them working together.

void setup() {
  Serial.begin(9600);
  
  // disable w5100 SPI while starting SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print(F("Starting SD..."));
  if(!SD.begin(4))  Serial.println(F("failed"));

  Serial.print(F("Starting w5100..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(Ethernet.localIP());

  // rest of your setup code
}

If you do not disable or start the SD before starting the w5100 dhcp, it will fail most of the time. The same happens the other way around. If you do not disable the w5100 before starting the SD, it will fail about half the time.

Hi Tim,

Thanks for your suggestion. I have in fact tried getting it to work without the SD card code as well. I only mentioned it to, as you have pointed out, demonstrate that the wiring is correct. On the 328 pin 10 is the SS. However this is pin 4 on the 1284. I have wired pin 4 on the 1284 to pin 10 on the Ethernet shield. I have also pulled pin 4,on the Ethernet shield high to disable the SD card. Still no luck.

Are you suggesting that the Ethernet library uses the mapped digital pin 10 and not the mapped SS pin in all MCUs ?

Are you suggesting that the Ethernet library uses the mapped digital pin 10 and not the mapped SS pin in all MCUs ?

Yes. On my Mega2560, the w5100 uses D10 as the SS, even though the default SPI SS is D53.

Hmm on the 1284 using the standard pin mapping (rather than the Bobduino varient) SS is in fact D4 is is the same internal pin allocation as the 328 I.E. PortB4 with Port B5/B6/B7 being MOSI/MISO/CLK If you look at the mappings for the 2560 you will see the port to pin allocations are the same

// ATMEL ATMEGA1284P
//
//                   +---\/---+
//           (D 0) PB0  1|        |40  PA0 (AI 0 / D24)
//           (D 1) PB1  2|        |39  PA1 (AI 1 / D25)
//      INT2 (D 2) PB2  3|        |38  PA2 (AI 2 / D26)
//       PWM (D 3) PB3  4|        |37  PA3 (AI 3 / D27)
//    PWM/SS (D 4) PB4  5|        |36  PA4 (AI 4 / D28)
//      MOSI (D 5) PB5  6|        |35  PA5 (AI 5 / D29)
//  PWM/MISO (D 6) PB6  7|        |34  PA6 (AI 6 / D30)
//   PWM/SCK (D 7) PB7  8|        |33  PA7 (AI 7 / D31)
//                 RST  9|        |32  AREF
//                 VCC 10|        |31  GND 
//                 GND 11|        |30  AVCC
//               XTAL2 12|        |29  PC7 (D 23)
//               XTAL1 13|        |28  PC6 (D 22)
//      RX0 (D 8)  PD0 14|        |27  PC5 (D 21) TDI
//      TX0 (D 9)  PD1 15|        |26  PC4 (D 20) TDO
// RX1/INT0 (D 10) PD2 16|        |25  PC3 (D 19) TMS
// TX1/INT1 (D 11) PD3 17|        |24  PC2 (D 18) TCK
//      PWM (D 12) PD4 18|        |23  PC1 (D 17) SDA
//      PWM (D 13) PD5 19|        |22  PC0 (D 16) SCL
//      PWM (D 14) PD6 20|        |21  PD7 (D 15) PWM
//                   +--------+
//

Any way round you put it however if I ignore the SS pin hard wire the Pin10 on the Shield to GND and Pin4 to Vcc it should be causing the Ethernet to be permanently enabled as a slave device. doing this I should only need MOSI/MISO/CLK for my interface. This however does not seem to want to work either.

Cheers Pete.

Any way round you put it however if I ignore the SS pin hard wire the Pin10 on the Shield to GND and Pin4 to Vcc it should be causing the Ethernet to be permanently enabled as a slave device. doing this I should only need MOSI/MISO/CLK for my interface. This however does not seem to want to work either.

That will not work. The w5100 and SD use the slave select as the start and stop markers for communication. You can't hard wire it. It is the HIGH-to-LOW transition of the SS that indicates the start of a read/write, and the LOW-to-HIGH transition that indicates the end.

SurferTim:

Any way round you put it however if I ignore the SS pin hard wire the Pin10 on the Shield to GND and Pin4 to Vcc it should be causing the Ethernet to be permanently enabled as a slave device. doing this I should only need MOSI/MISO/CLK for my interface. This however does not seem to want to work either.

That will not work. The w5100 and SD use the slave select as the start and stop markers for communication. You can't hard wire it. It is the HIGH-to-LOW transition of the SS that indicates the start of a read/write, and the LOW-to-HIGH transition that indicates the end.

I beg to differ doing this (SS to GND) works fine with the SD card ... is this method unique to the Ethernet card ?

In fact WIKI agrees with me Serial Peripheral Interface - Wikipedia

Here is the basic stuff on the SPI bus.

If the SD card works under that situation, it is a variation from the norm. Here is the datasheet for the SanDisk brand. I use them.
http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Components/General/SDSpec.pdf
Page 5-1 (page 91 on a pdf reader) has the SPI stuff for the SD card.

Similar to the SD Bus protocol, the SPI messages are built from command, response and
data-block tokens. The host (master) controls all communication between host and cards.
The host starts every bus transaction by asserting the CS signal, low.

Page 62 here shows the w5100 SPI communication.

Hmm interesting .. I have several (brands of) SD card breakout board and they all do the same thing ..

work with the SS held low ....

I am under the impression that the SS is used to set the SPI (MISO) data pins (of the slave device) into tristate (open collector) so that other devices on the bus can use them. whilst I see the SS pin being held low during the read.write cycle of the data I dont see it clocking anything just enabling the SPI pins.

OK so the SS debate aside it is still not working connected either to pin D10 or pin D4. any other ideas please :slight_smile:

Cheers Pete.

Baines, I have the 1284 working simultaneously with ethernet, SD, RFM12 radio, and the ColorLCD shield - and which all use the SPI port pins D11..D13. I use a similar pinout to the Bobuino, but slightly different. CS is probably not your problem. Try this - from the issues page I cooked up for the 1284.

See what I call "Fix #1" in the upper part, and how it apples to Ethernet in the lower part.

1. Lack of 1284(P) Support in Arduino Libraries

For some reason, Arduino central command didn't include support for ATmega1284(P) chips in the libraries that come packaged with the Arduino IDE. These libraries can be seen by opening the IDE "File > Examples" menu, and include: EEPROM, Ethernet, LiquidCrystal, SD, Servo, on and on.

The fix for this problem is "relatively" simple, because the 1284 and 1284P chips have a similar port assignment to the ATmega2560 and 1280 chips. So, the procedure is:

find the specific Arduino library in the IDE directory, eg in path:
   ".\arduino-1.0.5-windows\arduino-1.0.5\libraries\Ethernet".

look through the source files in the directory, and also any subdirectories, for lines such as:

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

and simply add the 1284P extension (call this [glow=yellow,2,300]Fix #1[/glow]):

#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560) || defined(AVR_ATmega1284P)
   inline static void initSS()    { DDRB  |=  _BV(4); };
   inline static void setSS()     { PORTB &= ~_BV(4); };
  inline static void resetSS()   { PORTB |=  _BV(4); };

So, anytime one of the Arduino IDE libraries is used, or a 3rd party library is installed for use with the 1284(P) chips, all of the .cpp and .h source files in the library should be immediately examined to see whether they need the change described here.

Furthermore,

10. Misc IDE Files and Libraries Checked for ATmega1284(P) Support

The following files in Arduino IDE version 1.0.5 have been checked for ATmega1284(P) support. There may also be some support in earlier versions of the IDE, but those are not dealt with here. These files are in the ArduinoCore ".\hardware\arduino\cores\arduino" subdirectory in the Arduino IDE.

  • "Arduino.h" = ok ... since 1284(P) support has been included in IDE v1.0.5.
  • "Client.h", "IPAddress.h", and "IPAddress.cpp" files = ok ... no direct references to cpu types.

NOTE - the "Arduino.h" file indicated above has support for several different ATmega1284(P) and ATmega644(P) chips, so consult that file for a more comprehensive version of Fix #1.

Also, the very end of "Arduino.h" has a < #include "pins_arduino.h" > statement, so it will automatically handle different board variants with different pinouts.

We also checked the following standard Arduino libraries, found in IDE subdirectory "\libraries" :

  • SPI library = ok ... since SPI.cpp references only "pins_arduino.h", the SPI pin callouts are taken
       care of in the specific 1284 variant being used.
  • EEPROM library = ok ... no cpu references.
  • SoftwareSerial library = ok ... no cpu references.
  • Wire library = ok ... references only "Arduino.h" and "pins_arduino.h".
    [color=red][b]
  • Ethernet library = needs fixing ... as of IDE v.1.0.5, file "w5100.h" still requires
       Fix #1 listed above.
    [/b][/color]
  • Servo library = needs fixing ... file "Servo.h" requires Fix #1 listed above
  • SD library = needs fixing ... both "S2dCard.h" and "Sd2PinMap.h" require Fix #1 listed
       above, plus the latter file requires more serious attention.

We've also checked the following 3rd party libraries for ATmega1284P support:

  • Xively library = ok.

  • HttpClient library (used with Xively) = ok ... references "Arduino.h", "Client.h", and "IPAddress.h",
       which are all ok, per checks listed above.

  • Twitter library = ok ... references "Ethernet.h" and "EthernetDNS.h", which are ok, as long
         as  "w5100.h" is fixed as indicated above.

  • Time library (used by Twitter) = ok.

  • Sha library = ok.

  • LowPower Labs RF12 library (used with Hope RFM12 modules) = works ... includes
         Sanguino/644 support (but totally NFG for Bobuino variant).

  • Mikem RFM22 library (used with Hope RFM12 modules).

  • maniacbug nRF2401 library = ok ... uses only references to "Arduino.h" and "SPI.h".

  • ElecFreaks ColorLCD library = needs fixing ... file "ColorLCDShield.cpp" requires Fix #1
         listed above (note - this fix also applies to the Sparkfu ColorLCDShield library).

  • Jeelib library (used with Hope RFM12 modules) = has so many bugs we don't use it.

I included all the other stuff here, just to show you there are many other issues with trying to use the 1284.

Hi Oric_Dan,

Thanks for this input. I have already tried the suggested addition to the ws5100.h file.

Could you explain how you managed to get the ws5100 working, what hardware you used (Ethernet variant) and what version of libs, IDE and bootloader on the 1284P you got it working on. you say you use port pins D11-D13, what is the SS PIN and what version of pin mapping are you using on the 1284P to get this mapping for the SPI bus.

I am using the "Standard" atmega1284P using Optoboot pulled down from

and using the "Mighty 1284p 16MHz using Optiboot"

This one maps D4 to SS, D5 to MOSI, D6 to MISO and D7 to SCK.

Cheers Pete.

  1. I don't think you mentioned which 1284 board you are using. ---> ?????

Thanks for this input. I have already tried the suggested addition to the ws5100.h file.

  1. where did you learn to do this?

  2. I am using a standard ethernet board, using the default pins and the ethernet library in the IDE. Standard stuff.

  3. also, every time I access any of the 4 SPI devices in my system, I call a routine that specifically disables all of the other CS lines, to ensure they are disabled.

  4. I also have maniac1284 optiboot burned into the chips.

I think you've been through the following:

  1. if you're compiling using "Mighty 1284p 16MHz using Optiboot", boards.txt says

mighty_opt.name=Mighty 1284p 16MHz using Optiboot
....
mighty_opt.bootloader.file=optiboot_atmega1284p.hex
...
mighty_opt.build.variant=standard

  1. so you should be looking at the following pins_arduino.h from the variants\standard directory

// ATMEL ATMEGA1284P
//
// +---/---+
// (D 0) PB0 1| |40 PA0 (AI 0 / D24)
// (D 1) PB1 2| |39 PA1 (AI 1 / D25)
// INT2 (D 2) PB2 3| |38 PA2 (AI 2 / D26)
// PWM (D 3) PB3 4| |37 PA3 (AI 3 / D27)
// PWM/SS (D 4) PB4 5| |36 PA4 (AI 4 / D28)
// MOSI (D 5) PB5 6| |35 PA5 (AI 5 / D29)
// PWM/MISO (D 6) PB6 7| |34 PA6 (AI 6 / D30)
// PWM/SCK (D 7) PB7 8| |33 PA7 (AI 7 / D31)
// RST 9| |32 AREF
// VCC 10| |31 GND
// GND 11| |30 AVCC
// XTAL2 12| |29 PC7 (D 23)
// XTAL1 13| |28 PC6 (D 22)
// RX0 (D 8 ) PD0 14| |27 PC5 (D 21) TDI
// TX0 (D 9) PD1 15| |26 PC4 (D 20) TDO
// RX1/INT0 (D 10) PD2 16| |25 PC3 (D 19) TMS
// TX1/INT1 (D 11) PD3 17| |24 PC2 (D 18) TCK
// PWM (D 12) PD4 18| |23 PC1 (D 17) SDA
// PWM (D 13) PD5 19| |22 PC0 (D 16) SCL
// PWM (D 14) PD6 20| |21 PD7 (D 15) PWM
// +--------+
//[/quote]
So, there are the pins to hook to. It's straightforward, it should work. You obviously need to worry more about the chip pins than the Dx bit, if you hacked your own 1284 board.

I had no problems whatsoever, after I did item #1.

Thanks to Dan and others on this...

I finally got it working by changing my Ethernet board. I had another one in my toy box and so out of desperation i thought i would try it ... voila it works

Now what is REALLY odd is this board that refuses to work with the 1248 works fine attached to an UNO.

Dan .. as a completely side question it the Oric part of your handle anything to do with that personal PC that came out in the 80s.. does that give my age away :slight_smile:

Cheers Pete.

edit I added the following to the ws5100.h file for the 1284P, this will actually use the pins defined as SS for the CPU version

#elif  defined(__AVR_ATmega1284P__)
  inline static void initSS()    { pinMode(SS, OUTPUT); };
  inline static void setSS()     { digitalWrite(SS, LOW); };
  inline static void resetSS()   { digitalWrite(SS, HIGH); };

Dan .. as a completely side question it the Oric part of your handle anything to do with that personal PC that came out in the 80s.. does that give my age away

I've heard about that in the past, but no.

Glad it works now. My ethernet board works fine on UNO, Mega, and my 1284 board too, just as long as I fixed w5100.h and got the correct pins, so ???

oric_dan:
My ethernet board works fine on UNO, Mega, and my 1284 board too, just as long as I fixed w5100.h and got the correct pins, so ???

Yes my second board does too. It was just my luck to have pulled this one out of the draw first.

I have a second problem to do with the software serial not working but i wont hijack this thread i will start another one.