ATmega162: When the Firmware Runs From Memory That Is Not On the Chip
The ATmega162 is a 40-pin megaAVR with 16 KB of flash, 1 KB of SRAM, 512 bytes of EEPROM, 35 I/O pins, two USARTs, a JTAG interface and — the feature that makes it different from most of its relatives — a full external memory interface. With the XMEM interface enabled, the part can address up to 64 KB of memory outside the package, and that external space can hold data, instructions, or both.
Most discussions of AVR protection treat the internal flash as the program. On this part that is an assumption, and it is worth separating the assumption from the lock bit mechanism before drawing any conclusion.
What the ATmega162 actually contains
| Item | ATmega162 |
|---|---|
| Core | 8-bit AVR, up to 16 MHz |
| Flash | 16 KB (8K × 16) |
| SRAM | 1 KB |
| EEPROM | 512 bytes |
| I/O pins | 35 |
| USARTs | 2 |
| Timers | 2 × 8-bit, 2 × 16-bit |
| External memory | up to 64 KB via XMEM (ALE, RD, WR, A8–A15) |
| Debug | JTAG, OCD support |
| Packages | 40-pin PDIP, 44-pin TQFP, 44-pin MLF |
| Programming | ISP, JTAG, high-voltage parallel |
The pin count is the tell. Thirty-five I/O on a 40-pin device leaves almost nothing over, so designs that use the external bus give up PORT A and PORT C to the multiplexed address/data bus and the control strobes. A board with a 32-pin SRAM or a latch sitting next to the ATmega162 is a board using XMEM.
Lock bits, in inverted logic
The AVR lock bits are a two-bit field with inverted polarity: an erased device reads 1, and protection is applied by programming zeros. Three meaningful modes exist, and the distinctions between them matter because they describe different barriers.
| Mode | LB1 | LB0 | Effect |
|---|---|---|---|
| 1 | 1 | 1 | No memory lock; programming and verification both allowed |
| 2 | 0 | 1 | Further programming of flash and EEPROM disabled; verification still allowed |
| 3 | 0 | 0 | Further programming and verification disabled |
Mode 2 is the interesting one, and it is frequently mislabelled as “unprotected”. Verification is still possible in mode 2 — the device can be read over ISP or JTAG — but it cannot be written again. This exists so a manufacturer can program a device, verify it, and then prevent the end user from changing it. A part found in mode 2 is readable; a part found in mode 3 is not.
On devices with boot loader support, a second field appears: the BLB bits, which restrict self-programming from the boot section and can block either writing to the application section or reading it via the boot loader’s own read path. The ATmega162 has a boot section, so BLB applies. This produces a configuration that is common in fielded equipment: application section unreadable from outside, boot section able to update the application section, and no path by which the boot loader can export what it is writing.
Chip erase, and why it is the whole argument
AVR lock bits are not one-way in the sense that matters. The chip erase operation — whether issued over ISP, JTAG or the parallel programmer — erases the flash array and the EEPROM array and returns the lock bits to their unprogrammed state.
That single sentence resolves the question that generates most of the confusion about AVR parts:
A chip erase clears the lock bits and the memory in the same operation.
There is no sequencing in which the lock bits are cleared first and the memory is preserved for a subsequent read. The erase is what clears them, so the memory is gone by the time the bits are clear. This is the AVR equivalent of the Microchip clause about removing code protection erasing program memory, and it leads to the same conclusion from the opposite direction — the AVR stores protection in the same non-volatile array as the data it protects, so it cannot be separated.
XMEM and the completeness problem
Here the ATmega162 diverges from the rest of the family, and it does so in a way that no amount of lock bit analysis addresses.
When the XMEM interface is enabled, the AVR can fetch instructions from external memory. The mechanism is straightforward: the external bus occupies the data space above the internal SRAM, and the SRE bit in the MCU control register makes that space accessible. Code that executes from external memory is linked to addresses in that space, and the corresponding instructions are not in the internal flash at all.
Three configurations appear in real designs:
- Flash only. XMEM enabled for data (an external SRAM for buffers), instructions entirely internal. A flash dump is the complete program.
- Flash plus external data. Same as above, but the recovered image references external addresses that no longer exist on the bench. The code runs, but only alongside the original board’s memory.
- Instructions in external memory. Part of the program — often a large table-driven routine, a font, a waveform generator or a protocol stack — lives in external ROM or SRAM. The internal flash holds the entry point and jumps out.
The third case is the one worth flagging in advance. It is rare, and it is not detectable by reading lock bits or the configuration bytes — the SRE bit is a runtime register write, not a fuse. It is detectable from the board: a 28- or 32-pin memory device, an address latch, and ALE routed to it.
What a complete image contains
- 0000h–3FFFh — 16 KB application and boot flash
- 1E00h–1FFFh — the boot section boundary, programmable within the BOOTSZ settings
- 512 bytes of EEPROM in a separate address space, protected by the same lock bits
- lock bits and fuse bytes — low fuse (clock source, start-up, boot size), high fuse (JTAG enable, boot reset vector, SPIEN), extended fuse (brown-out)
- optionally, external memory contents, which are not part of the device
The fuse bytes deserve a mention of their own. The low fuse selects the clock source — internal RC, external crystal, external clock — and the CKDIV8 bit divides it by eight. Firmware recovered without the fuse settings is firmware whose timing base is unknown, and on a part running from the internal RC oscillator at a calibrated frequency, “unknown” is not a recoverable state: every delay loop and baud rate divisor was computed against a number that only exists in the fuse byte.
Related notes
The pin-compatible ATmega162L uses the same lock bit mechanism at a lower voltage range, and the ATmega162A is the later die revision of the same part number. The larger ATmega16L shares the XMEM interface and has the same completeness question, while the ATmega169P shows the same lock bits on a part with an LCD controller instead.
FAQ
What does lock bit mode 2 actually allow? Verification. Flash and EEPROM can still be read over ISP or JTAG; further programming is blocked. Mode 3 blocks both.
Does a chip erase unlock the device? It clears the lock bits, but the same operation erases flash and EEPROM. The memory is not preserved across the unlock.
Can the ATmega162 execute code from external memory? Yes. With XMEM enabled, the external data space can hold instructions. When that is done, the internal flash is not the whole program.
Which fuse matters most for reproducing behaviour? The low fuse. It selects the clock source and the CKDIV8 divider, which set the timing base for every delay and every baud rate in the firmware.
Does JTAG change the protection analysis? No. JTAG is an access path to the same lock bits and the same chip erase; it does not bypass them.
Conclusion
The ATmega162 is a reminder that protection analysis answers a narrower question than it appears to. Lock bits tell you whether the internal arrays can be read; they say nothing about whether the internal arrays are where the program lives. On a part with an external memory interface, those are two different questions, and the second one is answered by looking at the board rather than at the fuses.