Posts Tagged ‘read avr chip binary file’

PostHeaderIcon Read AVR Chip Firmware: ISP and JTAG Are Not the Same Door

AVR microcontrollers usually present two ways in: a synchronous serial programming interface on a handful of pins, and a JTAG test access port on four. Both can read program memory. They are not interchangeable, and choosing the wrong one is a common reason for a slow, unreliable or impossible read.

The differences are mechanical. One is a shift register clocked by the programmer, with a simple command structure. The other is a state machine defined by an industry standard, with an instruction register and a chain of data registers. The first is fast and fragile at speed. The second is slower per byte and much more robust, and it can do things the first cannot.

The two interfaces compared

Property Serial programming JTAG
Signal count Four, including reset Four, plus optional reset
Protocol Command followed by data, clocked TAP state machine, instruction then data
Clock source Programmer Programmer, through TCK
Typical throughput Limited by clock and command overhead Lower per byte, more per transaction
Access to other devices None Chain of devices, if present
Works when lock is set Read refused Read refused, but other registers may answer

The sixth row is worth noting: neither interface defeats the lock. But JTAG reaches registers that serial programming does not, including boundary scan cells attached to the device’s pins, which is a way to observe the device without reading its memory at all.

Why serial programming is fast and fragile

The serial interface shifts data in and out under a clock the programmer generates, and the device samples that clock against its own internal clock. The relationship between the two is the constraint.

Constraint Value Consequence
Maximum clock frequency A fraction of the device’s clock Slower device clock means slower programming clock
Minimum clock frequency None in theory Very slow reads are possible
Signal integrity limit Cable length and termination Long cables force a lower clock
Reset timing Setup before the first clock Violations cause silent failures

The third row is the practical limit on a bench. A short ribbon cable to a socketed device runs at full speed; the same interface brought out to a header through thirty centimetres of wire needs the clock reduced, and a read at too high a clock returns plausible data with occasional errors.

Why JTAG is slow and robust

A JTAG transaction has more overhead per byte, because each access is framed by an instruction and a state transition. The compensation is that the protocol is stateful and self-checking in a way the serial interface is not.

Operation Overhead Benefit
Read a byte Instruction plus data register shift Deterministic state machine
Read a block One instruction, many shifts Efficient for bulk
Access a scan chain Device selection by instruction Works with other devices present
Halt the CPU Debug registers Can stop the device mid-program

The fourth row is the capability that serial programming lacks entirely: JTAG on these devices is a debug interface, not only a programming interface. It can halt the core, read registers, and single-step — provided the debug system is enabled.

The debug enable bit

This is the detail that decides whether JTAG is useful on a production device. Debug access is gated by a configuration bit, and production devices typically have it turned off.

Debug state JTAG programming JTAG debugging
Enabled Works Works
Disabled Programming may work Refused
Disabled by lock Refused Refused

The second row is common: programming through JTAG still functions, while the debug registers do not. That combination means the port is present and reading memory, but not capable of halting the device.

Choosing the interface

The choice depends on what the read needs to accomplish.

Task Better interface Reason
Read the whole flash Serial Higher throughput
Read a small region Either JTAG has more overhead
Verify a written image Serial Same reason
Observe pins while running JTAG Boundary scan
Halt and inspect JTAG Only option
Read a device in a chain JTAG Chain addressing
Read with minimal pins Serial Four pins, no chain

The sixth row matters on boards where the AVR shares a scan chain with other devices, which is common on anything designed for testability. Selecting the AVR in the chain is straightforward but requires knowing the instruction register length, which is a per-device fact.

The lock’s effect on both

Both interfaces are subject to the same lock, and both fail in the same way.

Lock state Serial read JTAG read
Unlocked Full read Full read
Read protected Refused, or fixed pattern Refused, or fixed pattern
Read and write protected Refused Refused

Neither interface offers a way around the lock, and this is the point at which the work becomes physical. What the two interfaces still provide on a locked device is access to the areas the lock does not cover, which are treated in attacking ATmega AVR programs.

Speed and reliability in practice

The practical advice for a bulk read is unglamorous and effective.

Practice Effect
Start at a low clock and increase Finds the reliable maximum for the setup
Read twice and compare Detects marginal timing errors
Keep the cable short and ground returns adequate Reduces ringing
Read in blocks, verifying each Localises a failure
Record the clock used Makes the result reproducible

The second row is the single most valuable habit in this work. A read that is not compared against a second read has an unknown error rate, and the errors it might contain are exactly the kind that produce a plausible image that does not run. The specific failure modes at high clock are covered in reading AVR firmware at speed, and the identity check that should follow any read is in reading Atmel AVR firmware.

Services at this boundary

Work covers interface selection, clock characterisation for a given fixture, bulk readout with double-read verification, JTAG chain identification, and boundary scan where pin-level observation is needed. It does not cover reading a locked array through either interface, because both honour the lock.

The useful framing is that the two interfaces are two doors into the same room, and neither one is a key.