Arduino, AVRStudio, JTAG etc. A report on progress

I have been experimenting with Arduino, AVRStudio and JTAG interfaces, with
mixed success, and I thought I would summarise my experiences for others.

The Arduino and Arduino IDE is a wonderful, easy to use system, allowing you
to get off the ground very quickly, but I wonted to expand the envelop a bit
and try AVRStudio. Im also interested in JTAG for programming FPGAs etc. JTAG
is an interesting protocol implmented by many non-trivial chips to test and
debug in-circuit.

AVRStudio is a very full featured IDE, debug, system development environemnt
(but not as easy to set up or learn as Arduino). With appropriate hardware it
can be used to support and program any Atmel chips.

My initial goal was to use AVRStudio to inspect and debug an Arduino using
JTAG. To this end I got a Chinese AVR-USB-JTAG board for $19 from Hong Kong
(inc shipping to Australia). This board has USB (an FTDI serial port) on one
side and JTAG on the other. It supposedly implments the Atmel JTAG-ICE
protocol, which is supported by AVRStudio.

Arduino uses the Atmel processors. Most Atmel processors (Mega etc) support
JTAG, however, the standard preprogrammed Atmel processor in an Ardiuno has
JTAG disabled. So the first step is to enable JTAG. This involves changing the
'fuse' bits to enable JTAG (which disables the Arduino analog pins A4, A5, A6
and A7). The fuse bits can NOT be set through the Arduino IDE, or from within
a program you might download to the target Arduino. They can only be set
through JTAG or the ICSP interface.

But I could not use the JTAG interface, because the JTAG interface is by
default disabled in an Arduino, so I had to use the ICSP interface. Most
Arduinos have a 6 pin ICSP header plug. If you connect to this through an
appropriate ICSP programmer you can change the fuse bits, lock bits, program
flash, program eeprom etc, all using avrdude.

You can make an ICSP programmer using another Arduino. Burn ArduinoISP sketch,
included by default in Arduino-0018 into a spare Arduino (I used a
Diecimila). Then pins 10 to 13 of the ArduinoISP become the ICSP outputs to
program the target Arduino. Connect the pins thus:

ArduinoISP Target Arduino ICSP header pins
10 5 RESET
11 4 MOSI
12 1 MOSI
13 3 SCK
GND GND
5V 5V

Plug the ArduinoISP into your development machine (where avrdude is
installed). Something like this:

Development machine
USB
|
USB
Arduino with ArduinoISP sketch installed)
pins 10, 11, 12, 13, GND, 5V
|
ICSP Header pins
Target Arduino

Then you can use avrdude to access the programs and fuses etc of the target
arduino (ie the one connected to the ArduinoISP through its ICSP pins) like
this (examples from Linux):

Get the fuse values:
avrdude -C/mnt/disk2/src/arduino-0018/hardware/tools/avrdude.conf -q -q -patmega1280 -cavrisp -P/dev/ttyUSB5 -b19200 -v -U efuse:r:efuse.txt:h -U hfuse:r:hfuse.txt:h -U lfuse:r:lfuse.txt:h

reprogram Arduino-Mega bootloader:
avrdude -C/mnt/disk2/src/arduino-0018/hardware/tools/avrdude.conf -q -q -patmega1280 -cavrisp -P/dev/ttyUSB5 -b19200 -v -Uflash:w:/usr/local/src/arduino-0018/hardware/arduino/bootloaders/atmega/ATmegaBOOT_168_atmega1280.hex:i

etc....

Note that avrdude (5.4) that comes with arduino-0018 on Linux did not work for me on
OpenSuSE 11.1. However the avrdude (5.5) downloaded from OpenSuSe repository
and the avrdude 5.9 from the avrdude sourceforge site and compiled from source
both worked fine.

So, then I used this command to set the JTAG enable flag in the Arduino Mega:
avrdude -C/mnt/disk2/src/arduino-0018/hardware/tools/avrdude.conf -q -q -patmega1280 -cavrisp -P/dev/ttyUSB5 -b19200 -v -U lfuse:w:0xff:m -U hfuse:w:0x9a:m -U efuse:w:0xfd:m

You can use the wonderful fuse calculator at
AVR® Fuse Calculator – The Engbedded Blog to calculate the right fuse settings for
your target arduino. For my Mega, the defaults were ff da fd, which are
changed by the above avrdude line to ff 9a fd (the only change is the JTAG
enable bit)

You can set the fuse bits back to the original shipped Mega values with:
avrdude -C/mnt/disk2/src/arduino-0018/hardware/tools/avrdude.conf -q -q -patmega1280 -cavrisp -P/dev/ttyUSB5 -b19200 -v -U lfuse:w:0xff:m -U hfuse:w:0xda:m -U efuse:w:0xfd:m

So now I had an Arduino Mega with JTAG enabled.

I then connected the AVR-USB-JTAG board to the JTAG enabled Mega like this:
DIP header on AVR-USB-JTAG Arduino
1 ADC4 (TCK)
2 GND
3 ADC6 (TDO)
4
5 ADC5 (TMS)
6
7 5v on target (to sense connection)
8
9 ADC7 (TDI)
10

Then the AVR-USB-JTAG board was connected to a Windows PC running AVRStudio
4.4 SP2. During conmnection to the When AVRStudion was asked to connect to the
programmer, I specified a Platform (programmer) of JTAG ICE, the appropriate
COM port and left the baud rate at 19200 (default). AVRStudion was then able
to connect to the target Arduino with JTAG, and interrogate the fuses etc
using JTAG.

However, when I tried to write a new program (a hex file created with arduino
IDE) to the target Arduino it failed. AVRStudio apparently cleared the flash
program just fine, it thought it had written the new program to flas bu the
verification failed at the first byte (it read by a 00 expecting it to be 0C,
the first byte of the downloaded program).

Further, I discovered that when AVRStudio is connected to a JTAG ICE
proogrammer, it is very limited as to what Tmel procesors it supports. Only
about 6 of the nearly 100 processors were enabled for selection when the JTAG
ICE interface is selected (I dont know why this is). At any rate, I was not
able to use AVRStudio to debug a Atmel Mega 1280 (the processor on an Arduino
Mega).

Further I discoverd that AVRStudio is not able to download software to an
Arduino because although the standard Arduino bootloader nominally implements
the STK500 programmer interface, it doent implement enough of it to let
AVRStudio work with it.

At any rate, I was left with an Arduino Mega with a flash of all 0s (ie no
bootloader either). I was able to recover from this by going back to the
ArduinoISP and avrdude and using them to reinstall the Arduino bootloader
with:

avrdude -C/mnt/disk2/src/arduino-0018/hardware/tools/avrdude.conf -q -q -patmega1280 -cavrisp -P/dev/ttyUSB5 -b19200 -v -Uflash:w:/usr/local/src/arduino-0018/hardware/arduino/bootloaders/atmega/ATmegaBOOT_168_atmega1280.hex:i

So, there was only partial success with all this.
I think the best answer if you want to use AVRStudio with an Arduino (or any
other Atmel processor) is to get an AVRISPmkII programmer (US$35 at various
outlets)