ATmega644: Page Buffers, RWW, and Why Dumps Come Out Ragged
Flash memory does not accept single bytes. It is erased in blocks and written in pages, and between the two sits a temporary buffer that holds the page while it is being assembled. Every AVR in this family works this way, and the arrangement leaves fingerprints in real device dumps that are routinely mistaken for corruption.
Knowing the mechanism makes those fingerprints readable. It also explains a class of failure that has nothing to do with protection: a device that was interrupted mid-update, and still runs, but from a program that is no longer what the vendor shipped.
The write sequence, in order
A page write on this family is a four-step procedure executed by SPM from the boot section:
- Erase the page. The whole page becomes
FF. - Fill the page buffer, one word at a time, by executing
SPMwith the buffered-write bit set. - Commit: execute
SPMwith the write-page bit set, which transfers the buffer to the array. - Wait for the operation to complete, polling or by interrupt.
Two properties follow from that order. The buffer is volatile — if power is lost before step 3, the buffer contents are gone and the page stays blank. And the erase in step 1 precedes the fill, so a commit that happens with a partially filled buffer writes a page that is part real data and part FF.
That second case is exactly what an interrupted firmware update produces. It is not corruption in the sense of damaged cells; it is a page that was committed before it was fully assembled.
Why half the flash is read-while-write and half is not
The array is split into two sections. The read-while-write section can be read by the CPU while a page in it is being written. The no-read-while-write section cannot — while a page there is being erased or written, the CPU stalls.
| Section | During a page write | Typical contents |
|---|---|---|
| RWW | CPU keeps executing from it | Application code |
| NRWW | CPU halted until the operation finishes | Bootloader, interrupt vectors |
The split exists so that a bootloader living in the NRWW section can service the interface while it writes the application — but the bootloader itself cannot run from the section it is writing. Anyone who has watched an AVR update stall for tens of milliseconds per page has seen the NRWW boundary in action.
For a read attempt, the practical consequence is simpler: reading is a separate operation from writing, and a page write in progress does not block a read through the programming interface. There is no need to wait for an erase to finish before reading a different page, though it is polite to.
Where the ragged dumps come from
Three distinct patterns show up in real dumps and each has a different cause.
A page of FF in the middle of code. The page was erased and never committed, or committed empty. In a device that has been through a failed update, this is a normal artifact.
A page that is half code and half FF. The commit happened with a partially filled buffer. The boundary within the page tells you how many words were buffered before the interruption — which is, incidentally, a fairly precise indicator of how far the update got.
A single word that differs from its neighbours in a repeating pattern. Usually not flash at all, but a read taken at the wrong voltage or too fast, sampled at the point where the sense amplifiers are marginal. Reading the page again at a lower clock rate either reproduces it exactly or makes it vanish.
The third pattern is the one worth checking first, because it costs nothing to re-read and because acting on a misread image wastes far more time than the re-read.
Interrupts and the read-while-write trap
On a part running from flash, an interrupt vector fetch during an NRWW write stalls the core. Firmware that writes the application section from the boot section therefore has to either disable interrupts or accept the stall. Both are common, and both leave a trace: a device that drops characters or misses a deadline during a field update is showing the stall.
This is not a recovery problem, but it is a diagnostic one. If a customer reports that a device “sometimes bricks during update”, the mechanism above is a better explanation than flash wear, and the fix is in the bootloader, not the memory.
Reading order that respects the mechanism
Because page boundaries are the unit of everything, a read is best planned page by page rather than as one long burst:
- Read the page, then read it again. Two identical reads establish that the interface is stable; a difference means voltage or clock, not memory.
- Record the number of non-
FFwords per page. Pages that are entirelyFFare candidates for “never written” or “erased”; pages with a partial tail are candidates for “interrupted”. - Compare the pattern against the vector table. The first page holds the reset vector and the interrupt table; if it is
FF, the device cannot be running the code you think it is.
That last check resolves a surprising number of contradictions. A part that appears to hold an application but power-cycles into the bootloader is usually a part whose application start is elsewhere in the address space, with the vector table redirected by a fuse rather than by the code.
Related notes
- ATmega164 and the boot section lock bits — who is allowed to run
SPM. - ATmega644PA and RSTDISBL — when the reset pin stops being a reset pin.
- ATmega324PA and brown-out reads — the voltage side of the same reliability question.
FAQ
Can a single byte be changed in flash? Not without erasing and rewriting its whole page. The buffer supports partial fill, but the array operation is per page.
Does a stalled CPU mean the flash is busy? It means the CPU is fetching from the NRWW section while that section is being written. The array is busy; the reading interface is not.
Is a page of FF a sign of damage? Usually not. It is a sign of an erased page that was never committed, which is what a normal erase without a following write leaves behind.
Can the page size be changed? No. It is fixed by the die and documented in the programming specification. It is the same for every member of the family with the same flash size.
Conclusion
Page granularity is the organising fact of AVR flash. Erase-then-fill-then-commit explains the partial pages, the FF blocks, and the update stalls, and the read-while-write split explains why a bootloader can update the application but not itself. Once those two mechanisms are clear, a ragged dump stops looking like damage and starts reading like a history.