Arduino Ethernet Shield MEGA hack

April 6th, 2009

The Arduino MEGA was announced officially on March 26th, 2009. The MEGA kept the odd pin header spacing to make it compatible with most Arduino shields. But unfortunately, some pins had to be moved and this movement made some shields that use SPI incompatible. One of the most popular shields, the Arduino Ethernet shield is one of the incompatible shields, as it relies on SPI for Arduino to Ethernet communication. The good news is that it is possible to make it work with the MEGA and here is the procedure:

Ingredients

  • Arduino MEGA board
  • Arduino Ethernet shield
  • 4 x male2male jumper wires
ingredients

ingredients

First the Hardware hack

The SPI signals SCK, MISO, MOSI and SS are located in pins 13, 12, 11 and 10 on the Arduino Diecimila/Duemilanove or compatible boards like freeduino and seeeduino.
These signals moved to pins 52, 50, 51 and 53 on the Arduino MEGA.
Signals SCK, MISO and MOSI are available in the ICSP 2×3 pin header also, but signal SS is missing from this header, and only available on pin 53.

As the Arduino Ethernet shield expects to get these signals from pins 13 to 10, we need to re-wire them to pins 50 to 53.

First, we need to disconnect pins 13 to 10 in the Arduino Ethernet Shield:

these4pins

these4pins

Bend them slightly to the outside:

these4pinsside

these4pinsside

And plug the Arduino Ethernet shield to the Arduino MEGA, so these 4 pins remains unplugged:

plug

plug

Now, how are we going to get the SPI signals? From pins 50 to 53… following the next mapping:

MEGA pin 50 (MISO) to Arduino Ethernet Shield pin 12.
MEGA pin 51 (MOSI) to Arduino Ethernet Shield pin 11.
MEGA pin 52 (SCK) to Arduino Ethernet Shield pin 13.
MEGA pin 53 (SS) to Arduino Ethernet Shield pin 10.

wires1

wires1

wires2

wires2

wires3

wires3

Now the Hardware hack is complete, but there is one more change we need to do, as the original Ethernet Library included with the Arduino IDE has hardcoded the SPI signals. We need to change these hardcoded signals to match the new position in the Arduino MEGA.

Software Hack

Locate the file spi.h in the hardware/libraries/Ethernet/utility directory, under your Arduino 0015 installation.

Find and replace the following 5 lines:


#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

These 5 lines are in a non-consecutive order in the spi.h file.

After you save the edited spi.h file, remove all .o files in the utility and Ethernet directory.

Open the Arduino 0015 IDE (The Arduino MEGA requires Arduino 0015), and load your preferred Ethernet sketch or try this example that I use (You need to change the IP address to reflect the values in your network):

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 50 }; // Change this parameters to reflect your network values
byte server[] = { 64, 233, 187, 99 }; // Google

Client client(server, 80);

void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);

Serial.println("connecting...");

if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}

void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}

Compile and upload the sketch. Activate the Serial Monitor, set baud to 9600 and you should see the Google search result, in html format, like in the following screen capture:

ide

ide

And the complete hack while getting information from Google:

working

working

This concludes the Arduino Ethernet Shield MEGA hack.

You can purchase the Arduino MEGA here and the Arduino Ethernet Shield here

April 14th, 2009 UPDATE
The previous hack requires moving 4 signals: SCK, MOSI, MISO and SS. As SS is used by AVR only when working SPI in SLAVE mode, I decided to try a new simpler hack, and move only 3 signals: SCK, MOSI and MISO, and use digital pin 10 as SS. This way, only 3 pins need to be bended: 13, 12 and 11.

At the beginning this seemed to be a simple modification to the original hack, but mysteriously it didn’t work. Assigning SPI0_SS_BIT and IINCHIP_CS_BIT to BIT4 (corresponding to digital pin 10 on the Arduino MEGA), the Arduino Ethernet shield couldn’t be initialized, so the sketch didn’t work (It never returned from Ethernet.begin()). After doing some research, I found that the SS pin is also used when setting AVR in SPI master mode, but only before setting bit 4 of register SPCR (Master mode) required this pin SS to be HIGH. So I tricked some more code to make it work (force SS HIGH before setting bit 4 in SPCR register to HIGH).

Hardware hack

Follow hardware hack instructions above, but only bend pins 13, 12 and 11. Wire the pins as instructed, except for the 4th wire from Arduino MEGA pin 53 to Ethernet Shield pin 10 (as this pin is not bended in this new hack).

Software hack

Forget all the changes suggested above, and follow this new changes:
Find and replace the following 6 lines:


#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
...
PORTB |= SPI0_SS_BIT; PORTB &= ~(SPI0_SCLK_BIT|SPI0_MOSI_BIT);\

and replace them with this code:


#define SPI0_SS_BIT BIT4
...
#define SPI0_SCLK_BIT BIT1
...
#define SPI0_MOSI_BIT BIT2
...
#define SPI0_MISO_BIT BIT3
...
#define IINCHIP_CS_BIT BIT4
...
PORTB |= SPI0_SS_BIT | BIT0; PORTB &= ~(SPI0_SCLK_BIT|SPI0_MOSI_BIT);\

By adding BIT0, we force pin SS to be HIGH when the SPCR register is set for AVR to behave like SPI master device.

I hope you find the new addition simpler to execute than the original hack.

If there is an Arduino MEGA, then there should be a MEGAshield

March 16th, 2009

Hi, I just saw the picture of the Arduino MEGA featured in hack-a-day

If there is an Arduino MEGA, then for sure you need a MEGAshield. Here are some pictures of the NKC MEGAShield:

MEGAshield PCB

MEGAshield 1

MEGAshield 2

MEGAshield 3

MEGAshield 4

Half Stackable MEGAShield = Monster MEGAShield (As called by ladyada)

I installed long legged 6-pin and 8-pin headers to make the Arduino MEGAshield stackable on the left half side (legacy Arduino side?). Here are some pictures:

MEGAshield 5

MEGAshield 6

MEGAshield 7

MEGAshield 8

Assembling the NKC Electronics XBee Shield V3.0 KIT

March 12th, 2009

Assembling the NKC Electronics XBee Shield V3.0 KIT

A new version of this kit is available as V4

SCHEMATICS (click on images to enlarge)
xbee shield v3.0 schematics

The NKC Electronics XBee Shield V3.0 KIT is an enhanced version of the original Arduino XBee Shield. It is sold in a DIY kit format and it comes with all the components required to assemble a full XBee Shield that is pin-compatible with all Arduino format compliant boards (Arduino, Freeduino, Seeeduino, etc).
First, unpack the kit

Xbee Shield V3.0 KIT

and start with the PCB.

Let’s start with the power portion of the schematic using the following parts:

IC1 TLV2217-33 Voltage Regulator (TO-220 package)
C1 100nF ceramic capacitor
C2, C3 10uF electrolytic capacitor

xbee shield v3.0 KIT

Next continue with the transistor, LEDs and other discrete components:

xbee shield v3.0 KIT

R1 10K resistor
R2 15K resistor
R3, R4 1K resistor
R5 330ohm resistor
RSSI 3mm LED
ASSOCIATE 3mm LED blue (transparent)
T1 BC547 transistor
reset tactile switch

Solder the sockets and pin headers:

xbee shield v3.0 KIT

Next step: Insert the jumpers:

xbee shield v3.0 KIT

There are 4 jumpers. J1 and J2 are for upgrading the firmware on the XBee module. Leave open for normal operation (both J1 and J2 open).

xbee shield v3.0 KIT

Pay special attention to the alignment of the female headers. The 2×3 female socket must be placed with the female portion facing down. This board takes some signals from the ICSP connector, so this socket is mandatory.

And this is the final picture of the XBee Shield V3.0 assembled and ready to use. XBee module is not included in the kit and must be purchased separately.

xbee shield v3.0 KIT

http://www.nkcelectronics.com/

atxmega128a1 DAC is nonlinear and inaccurate when reference is above 2.4V or Vcc – 0.6V

March 12th, 2009

I started experimenting with the new ATMEL atxmega128a1 chip, when I noticed a strange noise in the signal generated by the DAC example (atmel application note AVR1301: Using the XMEGA DAC). I captured the signal with a Rigol VS5042 Digital Storage Oscilloscope:

XMEGA DAC problem

You can clearly see the noise at the bottom of the sawtooth signal.

The setup:

  • STK600 (Target Voltage 3.6V)
  • TQFP100 Package
  • ATMEGA128A1
  • AVR Studio 4.15
  • WINAVR 20080610
  • Rigol VS5042 Digital Storage Oscilloscope
  • AVR1301 example in C

I thought there was a mistake in the example program, and was about to review it, when I found the following note in the Errata section of the atmega128a1 preliminary datasheet:

“DAC is nonlinear and inaccurate when reference is above 2.4V or Vcc – 0.6V”

So I went back to AVR Studio and set the target voltage of the STK500 (and the target device) to 2.0V, and the problem does not appear:

DAC solved

I assume the VREF used by the example program is internal, so lowering the voltage of the target device is the setup that fixes the problem. I will change the example program to use external VREF and see if it is possible to power the target with 3.3V and lower VREF to 2.0V and see if the problem can be fixed as well. The ATMEL documentation says that there is no workaround to this problem, and they recommend using VREF below 2.4V or Vcc – 0.6V

The XMEGA is a very advanced and interesting device. The only disadvantage is that it is very difficult to get good documentation, user experiences, etc. So I will be preparing different settings and publishing the results.

Update: I checked the source code and VREF was set to AVCC, and AVCC = Target Voltage. That is why the only way to change VREF to 2.0V was to lower the complete target board (STK600) voltage to 2.0V. I modified the source code to use external VREF for DAC channel A, and the result is that I can set the target board voltage to 3.5V and VREF to 2.0V and now the example works ok, without noise in the SAWTOOTH signal.

DAC external AREF

DSO channel 1 (yellow) is the sawtooth signal output, with Vmax = 2.0V and channel 2 (blue) is VTarget, with Vmax = 3.5V.

Assembling the NKC Electronics JTAG ICE Clone Board (Rev. B)

March 12th, 2009

Assembling the NKC Electronics JTAG ICE CLONE Board (Rev. B)

by NKC Electronics

SCHEMATICS (right click –> view image)

jtag schematic

JTAG ICE Clone board is an implementation of the Aquaticus JTAG ICE clone. The Kit includes the PCB and all the parts requiered to build a fully functional clone of AVR JTAG ICE. It can even be upgraded using AVR-STUDIO when a new firmware is released by Atmel.

This guide covers the assembly process of the JTAG ICE clone Rev B kit (marked Rev B in the PCB)

First, unpack the kit and start with the PCB.

The JTAG ICE clone board has all the component values printed on the PCB, making the use of the schematic almost unnecessary.

jtag ice clone kit

We will install the passive components (resistors, capacitors, etc) first.

jtag ice passive

Start by soldering the resistors R1 to R7, C3 to C9 ceramic capacitors, C10 electrolytic capacitor and D2 diode

R1 10 K resistor
R2, R3, R4, R6 1 K resistor
R5, R7 4.7 K resistor
C3, C4, C5, C6, C7, C8, C9 0.1uF ceramic capacitor
D1 1N4148 diode
C10 10uF electrolytic capacitor

jtag ice clone kit

Next identify and separate the 2 22pF ceramic capacitors, 2 LEDs and the crystal

C1, C2 22pF ceramic capacitor
Q1 7.3728 crystal
PWR, JTAG 3mm LED

jtag ice clone kit

Now separate the 16-pin IC socket, 40-pin IC socket (wide), 10-pin male header, 5-pin male header, DB9 female PCB connector. Cut the 5-pin male header in one 3-pin header and one 2-pin header.

X1 DB9 female PCB connector
JTAG 2×5 male header
IC1 40-pin DIP socket
IC2 16-pin DIP socket
JP1 3-pin male header
JP2 2-pin male header

jtag ice clone kit

We are done with the soldering. You need to install the MCU and the RS232 (ICL3232, MAX3232, ST3232) driver in the sockets. The large chip is the ATMEGA16 Microcontroller. It is already programmed with the latest release of the JTAG ICE firmware, and the bootloader. Please, be very careful with the pins while inserting the ICs.

jtag ice clone kit

Insert the shunt shorting positions 2-3 of the 3-pin MODE header. The JTAG ICE clone board has two modes of operation:

  1. Programming / Upgrade mode
  2. Normal operation mode

Position 2-3 is the Normal operation mode (board is ready to connect to target board and start debugging)

jtag normal

Position 1-2 is the Programming mode. This mode is used to program or upgrade the JTAG ICE firmware. The firmware is distributed by Atmel with updates on the AVR Studio IDE. In the operation guide you will find the manual firmware upgrade process, explained in detail.

jtag program

This is how the JTAG ICE clone board looks ready to use with the 10-wire cable for the target board.
jtag ice clone kit

The target board must supply the power to the JTAG ICE clone board, using the standard JTAG connector. The board expects the power from the target board (2.7V to 5.0V) in the VTarget (VCC) pin. It is recommended to supply also the target voltage to the VTref pin (Use the provided JP2 and jumper to supply power to the board from the target device). The JTAG ICE board does not have voltage leveling circuit, so if you supply VTref, it must be the same as VTarget.

Testing the board:

  1. Start AVR Studio
  2. Verify mode jumper is in 2-3 Normal
  3. Connect JTAG port to target board. Supply VCC. At this point, you only need to supply VCC to the JTAG ICE clone board. No real circuit with target MCU is needed
  4. Both LEDs are on
  5. Select Connect to the Selected AVR Programmer avr studio connect
  6. You should see the following message: jtag avrstudio ocd error

This message means that AVR Studio detected the JTAG ICE clone board, but was not able to identify the target MCU (either it is not installed, or the installed MCU does not support JTAG).

The JTAG ICE clone board is now assembled and tested. Now you need a real target board to start debugging.

An important reminder: JTAG ICE requieres the JTAG fuse in the target MCU set: JTAG Interface Enabled [JTAGEN=0]. The setting looks like this in AVR Studio:

jtag avrstudio fuses

IMPORTANT NOTE to AVRStudio 4.13 sp2 users: There seems to be a bug in AVRStudio 4.13 sp2 that generates an error trying to read fuses using the JTAG interface. There is a fix posted in Atmel Norway website: http://www.atmel.no/beta_ware/as4/413sp2/stk500Dll.zip