PostHeaderIcon Copy Microcontroller PIC18F4220 Binary: Three Things Travel, Not One

The word copy hides an assumption. It assumes the device holds one thing, and that taking that one thing away reproduces the device. On an 8-bit part with a single array that assumption is close enough to true. On this member of the PIC18F family it is not, because three separate pieces of non-volatile state decide how the product behaves, and only one of them is the program.

The distinction matters in practice because the failure it produces is quiet. A board loaded with program memory alone will start, run its initialisation, and then do something subtly or grossly wrong — hold the wrong calibration, refuse to communicate on the right address, brown out at the wrong voltage. Nothing in the bench test says the copy was incomplete.

What actually sits in the device

Three regions, three different access rules, three different meanings.

Region Approximate size What it decides Read through the programming interface
Program memory a few thousand instruction words the algorithm yes, until code protection is set
Data EEPROM 256 bytes calibration, counters, identity yes, independent of code protection
Configuration bytes 8 bytes at the top of the config space oscillator mode, watchdog, brown-out, code protection itself yes, with separate protection bits

The third row is the one that surprises people. Configuration bytes are not code and are not covered by the same protection bit. On many parts they remain readable even when the program array has been locked, which makes them the first thing worth reading on a device that refuses everything else.

Program memory is the easy part

Reading the program array is a mechanical operation once the device is unlocked: the interface walks the address space and returns instruction words, and the result is a flat image whose length is fixed by the density of the part. There is no structure to reconstruct, no log to replay, no wear levelling to undo. What comes back is what the linker produced, minus whatever the protection scheme withheld.

Two details are worth checking rather than assuming. The first is the reset and interrupt vectors at the bottom of the array — if they are erased, the image is not a working program regardless of what the rest contains. The second is the tail: unused space in a partially filled array reads as the erased pattern, and a customer comparing two images will see a long run of identical words that means nothing.

Data EEPROM carries the production story

The 256-byte data array is where a product keeps what it learned after the firmware was written. Serial numbers, calibration constants measured on the test fixture, hour counters, user settings, the last known state of a mechanical assembly — all of it lands here, and none of it is in the program image.

This is why a copy that omits it fails in a way that looks like a firmware bug. The replacement controller runs the same instructions on different numbers. On a product where the calibration is a gain and an offset applied to an analogue input, the error is proportional and obvious. On a product where the settings page is a packed bitfield, the error is a device that comes up in the wrong mode and appears to have lost its configuration.

Reading it is a separate operation from reading code, and it is worth doing first on a device whose program array is locked, because data EEPROM protection is a separate bit and is frequently left unset even when code protection is engaged.

Configuration bytes decide whether the copy can run at all

Eight bytes control the physical behaviour of the part before a single instruction executes. The oscillator selection decides what the CPU runs on and whether an external crystal is required. The brown-out threshold decides at what supply voltage the device resets. The watchdog decides whether an unattended product recovers from a hang. The low-voltage programming bit decides whether the programming pin can be used as an input afterwards.

A copy that gets the program and the data right and leaves the configuration at its factory default will, on many boards, fail to start. The most common instance is an oscillator setting: a product designed to run from its internal oscillator is copied onto a part configured for an external crystal input, and the replacement sits there producing nothing.

The configuration space also holds the protection bits themselves. That creates a trap on rework: writing the configuration bytes of a protected original onto a fresh device re-locks the fresh device. The order of operations has to be read and data first, configuration last, and only after the image has been verified.

The identity region is a fourth thing

Above the configuration bytes sits a small read-only area, programmed by the manufacturer and not erasable by the user. It carries a device identifier and a revision code. It cannot be copied and does not need to be, but it does need to be recorded, because it tells you which silicon revision produced the behaviour you are trying to reproduce.

That revision code has real consequences. Errata are tied to it, and a copy made onto a different revision may behave differently in the exact corner the original was designed around. Recording the identifier and the revision alongside the three copied regions is what makes a copy auditable a year later.

What a complete deliverable looks like

A job described as a binary copy should end with four files and one page of notes.

  • the program image, with its start and end address stated
  • the data EEPROM image, as a flat 256-byte block
  • the configuration bytes, decoded into named fields rather than left as hex
  • the identifier and revision code, as read
  • a note stating which regions were read and which were inferred

The note is not paperwork. When a customer later reports that the replacement unit behaves differently, the note is what distinguishes a copy that was always incomplete from a copy that has since been corrupted.

Reading order when time is short

On a device with unknown history, the order that produces the most information per minute is fixed.

  1. Read the identifier and revision. This confirms what the part is and which errata apply.
  2. Read the configuration bytes. This tells you whether the array is locked, and how the part is meant to start.
  3. Read the data EEPROM. It is usually unprotected and it is often the real goal of the job.
  4. Read the program array last, because it is the largest operation and the one most likely to be blocked.

If step four is refused, steps one to three have still produced a usable result for a repair or a re-calibration, and they have told you precisely why step four failed. See PIC18F4220 binary boundaries for what the image looks like when the array does come out, and PIC16F88 data recovery for a smaller device where the same three-region split applies with different proportions.

Where the three-region model breaks

It is worth knowing the exception, because it changes the shape of the job. On some products the data EEPROM is not used at all and the parameters are compiled into the program image as constants. The tell is that the data array reads as a uniform pattern while the product clearly remembers settings across power cycles — which means the store is elsewhere, either in an external serial memory or in a reserved block of the program array.

Conversely, some products keep almost nothing in the program array and treat it purely as an interpreter, with all behaviour driven by the data array. On those, copying the program alone yields a device that runs a generic engine against empty settings.

Deciding which model a product follows takes one observation: read both arrays, then compare each against the erased pattern. Whichever array shows structure is where the product keeps its state. On this device, the answer is usually both — which is exactly why a single-file copy is the wrong shape for the job. For the same reasoning applied to a larger part with more peripherals, the PIC18F14K22 flash readout covers how peripheral configuration migrates into the copy.

Comments are closed.