Homebuilt underwater robot

 

JTAG for AVR processors

This article provides information about JTAG interface and describes how to create simple JTAG interface. This interface is original design of Aquaticus ROV team.

The interface is compatible with every software that supports JTAG ICE, it works perfectly with AVR Studio and Linux tools. You can program the AVR chip using JTAG interface, but you can not consider this interface as replacement for AVR ISP or any other AVR programmer.

List of supported Atmel micro controllers:
  • AT90CAN128
  • ATmega128
  • ATmega16
  • ATmega162
  • ATmega169
  • ATmega32
  • ATmega323
  • ATmega64
The above list is the same for all JTAG ICE clones.

Once Atmel published schematics of their JTAG interface for AVR processors (JTAG ICE II), you just had to develop PCB board and program it, but... original Atmel AVR JTAG interface is quite complicated – contains antistatic protection, support for wide range of voltages on target device, contains advanced power supply, etc. For amateur purposes it can be simplified which allow it to fit on a quite small PCB board.

JTAG interface: Home designed JTAG interface for AVR chips.JTAG interface: Home designed JTAG interface for AVR chips.

At the end you can find all files needed to create your own PCB.

What's JTAG and how it works?

Simply JTAG interface allows you to look inside a processor and see what's going on there - read registers, alter memory, etc. You can read more about on Wikipedia JTAG article. Atmel AVR JTAG contains some extensions to original JTAG which allows it to program internal memories (FLASH, EEPROM).
Communication between PC computer and AVR JTAG (also called JTAG ICE) is realized using RS232. Communication between AVR JTAG and target device uses 10 wire cable (there are less than 10 active signals). Below figure explains it: Block diagram: JTAG communicationBlock diagram: JTAG communication

General

Schematic below:

Schematic of JTAG interface for AVRSchematic of JTAG interface for AVR

Everything is taken care by ATMega16, originally, in Atmel's application note, there was a different processor, which is actually discontinued, ATMega16 is a 100% replacement for it. You can see also MAX232, which converts voltage levels from TTL to RS232 (PC) standard. Mind that you can use MAX3232, that way your interface will work with lower voltages - 3.3V - additionally some capacitors would have to be exchanged to other values (C3, C4, C5, C6) and C5 connected to ground (not VCC). Mind that there are few versions of MAX3232, capacitor values and connections may differ across different producers (consult datasheet). If you do a 3.3V version don't forget about proper ATMega16 version (see ATMega16 documentation). The rest consist of resistors, capacitors and 2 LEDs. There's one jumper which will be described later on; normally pins 2 and 3 should be short circuited.
The interface can be powered from external 5V supply or from AVR JTAG connector.

Please note that you can not replace ATmega16 with ATmega32 chip. Original Atmel firmware have to be modified (interrupt table) to work with ATmega32. This task can be automatized using special bootloader. You can read JTAG ICE Clone for ATMEGA32 thread on AVRFreaks (you must register to read forum messages) for more info.

Below AVR JTAG connector pinout - it connects AVR JTAG interface and the target device (2x5 pins, IDC10, 10 wires). JTAG connectorJTAG connector

AVR JTAG connector
Pin Name In/Out Details
1 TCK Output Test Clock, clock signal from JTAG ICE to target device
2 GND - Ground
3 TDO Input Test Data Output, data signal from target device to JTAG ICE
4 VTREF Input Voltage level of target device
5 TMS Output Test Mode Select, mode select signal from JTAG ICE to target device
6 NSRST Input/Output Open collector output from JTAG ICE to target device, it is also used as input to detect resets on target device
7 VSUPP Input Power supply for JTAG ICE. This line is cut off when external power supply is attached (see schematics)
8 NTRST Input/NC Not connected, reserved for other JTAG devices (JTAG interface reset)
9 TDI Output Test Data Input, data signal from JTAG ICE to target device
10 GND Ground

How to build – step by step

First you have to make a PCB board (all downloadable things are at the end of article), solder everything. PCB board is 100×46mm in size. It's good to place ATMega16 and RS232 in socket. Be sure that there aren't any shortcuts and elements have proper values. IC2 has to be MAX232A (with A at the end), that way 0.1μF capacitors can be used; you can use normal MAX232, but remember to use proper capacitor values (greater). In case of a mistake rather nothing bad should happen, because there's nothing to break. There's a simple protection against improper power supply polarization - D1 diode.

Now it's time to program ATMega16 - it's done in two stages:

Step 1

First stage it to program the microcontroller with bootloader (download attached file avrboot.hex), which will, later on, be used to load JTAG program into microcontroller. More about bootloader beneath the table. You must also remember about correct configuration of fuse bits and lock bits (these should not be programmed). To flash a bootloader you need a programmer for Atmel AVR processors, here you can find simple programmer which can be assembled in no time (PonyProg): schematics, I used PonyProg to do the programming. Below a picture which bits need to be programmed and a table with more details: Fuse bits settings in PonyProgFuse bits settings in PonyProg

Fuse bits settings - details
Name Value Details
OCDEN 1 Turn off On Chip Debug, if on CPU may use more current in sleep modes, because some clock distribution subsystem are constantly powered
JTAGEN 1 JTAG not used here
SPIEN 0 A must be during SPI programming (you can't set it to 1 (not programmed) in SPI programming mode)
CKOPT 1 This value is due to use of 7,3728 MHz crystal, see ATMega16 doc
EESAVE 1 EEPROM content doesn't have to be preserved between programming - it can be turned off (1)
BOOTSZ1 0 Bootloader size set to 1024 words, ie. 2048 bytes (see documentation for details)
BOOTSZ0 0 Bootloader size set to 1024 words, ie. 2048 bytes (see documentation for details)
BOOTRST 0 After reset jump to bootloader.
BODLEVEL 1 Brown Out Detector level - detection of power failure - not used here - turned off (1)
BODEN 1 Brown Out Detector level - detection of power failure - not used here - turned off (1)
SUT1 1 Start Up Time - set to 65 ms (it doesn't actually matters here)
SUT0 1 Start Up Time - set to 65 ms (it doesn't actually matters here)
CKSEL3 1 This value is due to use of 7,3728 MHz crystal
CKSEL2 1 This value is due to use of 7,3728 MHz crystal
CKSEL1 1 This value is due to use of 7,3728 MHz crystal
CKSEL0 1 This value is due to use of 7,3728 MHz crystal

Bootloader is compliant with avrboot protocol, I compiled it using this project. It works this way: after you power up the processor program flow jumps to special area called bootloader. Bootloader prepares microcontroller to receive control commands and data through serial port (RS232, 19200, 8N1), this way we can send flash ATMega16 using serial port, it's easy and good solution. A disadventage is that you loose some FLASH space for bootloader (in most cases this can be omitted). Important: JP1 jumper must be short-circuited in position 1-2, this way CPU jumps to bootloader, else (2-3 position) normal code (not bootloader) is executed. You can check communication with bootloader with simple terminal connected do serial port, try to send S character or V character. Below you can see the effect, white background means sent character:

Terminal connected to avrboot deviceTerminal connected to avrboot device

Step 2

We have bootloader programmed, remember about jumper (position 1-2). Now we have to load JTAG control program. We start AVRStudio. Examples below are from version 4.0, similar should be with version 3.5. Important: there may be problems with installation of AVRStudio 4.0 using Win XP SP2 (problem is well known, it's about setup program). Choose Tools and AVR Prog menu: Running AVR prog from AVR StudioRunning AVR prog from AVR Studio

AVR Prog window should appear. Remember that JTAG interface should be turned on and connected with computer with serial cable and jumper set in position 1-2, else there will be a message informing that no (avrboot) interface was detected. Note that AVRProg do not recognize serial ports other than COM1 and COM2, so be sure to use one of these ports. This is AVR Prog window, it will show up when everything is OK: AVR prog windowAVR prog window

Click Browse and locate file upgrade.ebn, in standard installation it is located here: C:\Program Files\Atmel\AVR Tools\JTAGICE\Upgrade.ebn. This file contains ciphered version of JTAG ICE control program - AVRStudio takes care of everything, it thinks that our interface is the original one. Click Program (in section Flash). Ready. At the end place jumper in position 2-3 so that bootloader will no longer be invoked.

Short test

To test this interface I build simple device with only ATMega16 (it has JTAG interface, for example ATMega8 doesn't). When you buy a new processor it has the JTAGEN fuse bit programmed, to be sure I programmed it once again along with OCEN bit (this stage may not be necessary). Here's the schematic of the test device: Schematic of test deviceSchematic of test device

We connect signals TCK, TDO, TMS, TDI with appropriate CPU pins, connect VCC to VTREF and optionally NSRST with CPU reset. If you do not power interface externally, connect 5V to VSUPP pin! We connect test circuit with JTAG interface with 10 wire cable. My first JTAG interface almost worked instantly - I only forgot to connect VTREF signal, after correction AVR Studio stopped nagging that the power supply is not connected. And here's how to check the interface: choose Tools -> Program AVR -> Connect: JTAG test step 1: Choose appropriate menu itemJTAG test step 1: Choose appropriate menu item

A window should appear with list of programming devices, we choose JTAG ICE and port number to which our interface is connected: JTAG test step 2: Select JTAG ICEJTAG test step 2: Select JTAG ICE

If everything works properly a window should appear: JTAG test step 3: Ready to work.JTAG test step 3: Ready to work.

One thing that doesn't work the way it supposed to is proper measurement of VTREF voltage, it should be 5V, but the program detects 6.2V. It doesn't affect the proper functionality of JTAG device. Original Atmel JTAG could change voltage levels to work with 3.3V and 5V microcontrollers. This JTAG works with only one voltage level, so voltage divider responsible for proper measurement was not implemented.

The interface was successfully tested under Linux using programs: avarice and avr-insight

Links

AttachmentSize
avrboot.hex3.88 KB
atmel_avr_jtag.pdf85.53 KB
avrjtag_board.pdf49.09 KB
avrjtag_elem.pdf67.01 KB
avrjtag_all.pdf126.91 KB
eagle_project_jtag.zip66.35 KB
avrjtag_schematic.pdf36.73 KB