PIC18F44K20: The Order of Operations in a Restoration
The sequence most people use is the sequence the task suggests: erase the device, write the code, set the configuration. It works often enough to become a habit, and it fails in a specific and confusing way — a device that is half programmed, refuses further writes, and reports nothing useful.
The failure has a cause, and it is the same cause that makes the correct order counter-intuitive: configuration is not a setting applied to the device, it is content that governs access to the device.
What the order determines
Configuration decides which regions are write-protected, which are read-protected, and how the device may be entered. Once write-protection is in place for a region, that region cannot be programmed.
So writing configuration first means the remaining programming operations run against bits that may forbid them. Whether the result is a device that works depends on whether the configuration happened to leave the rest writable — which is a coin flip depending on the source’s protection state.
| Order | Outcome |
|---|---|
| Erase → code → configuration | Works, provided configuration is written last |
| Erase → configuration → code | Works only if configuration leaves everything writable |
| Erase → configuration with protection → code | Fails partway, device needs a second erase |
The third row is the one that produces a confusing support case, because the tool reports success on the configuration write and then fails on the first code write, which reads as a tool problem.
The full sequence that holds
- Erase. Full erase, including configuration. A partial erase leaves configuration from a previous life, and the new program inherits it.
- Verify erased. Read a few addresses across the array and confirm the erased value. An incomplete erase is invisible until programming fails.
- Program code. The application image, at its addresses.
- Program the data array, if it is being restored, or decide deliberately to leave it blank.
- Program configuration, last, with every field compared against the recorded source values.
- Verify. Read back code, data and configuration, and compare against records.
Step five last is the rule. Step two is the one people skip, and it is the one that catches a device whose erase did not complete — usually because the programming voltage was marginal.
Why configuration deserves its own step
Configuration is often treated as a checkbox in a programmer’s interface. On this family it is fourteen or so bytes with fields that decide oscillator, watchdog, brown-out, programming interface and protection.
Each of those changes behaviour, and each is invisible in the code image. A restoration that gets eleven of them right produces a device that works in the lab and fails in the field, typically on the watchdog or the brown-out setting.
The discipline is to read the configuration from the source device, record it as bytes, and compare it byte for byte after writing — not field by field through a tool’s display, which merges and hides reserved bits.
The data array decision
This family has a separate data array, separately protected. During a restoration it must be an explicit choice:
- copy the source’s contents, reproducing its calibration and accumulated state;
- leave it blank, letting the firmware initialise defaults;
- write known values from documentation.
Whichever is chosen, the choice belongs in the record. “The data array was left as erased” is a statement that answers questions later; silence does not.
Verification, and what it must include
Reading back and comparing is the only step that proves the restoration. It must cover:
- the code, word for word across the programmed range;
- the configuration, byte for byte;
- the data array, if it was written;
- the behaviour, on a bench, against the recorded behaviour of the source.
The fourth is the one that catches a correct file and a wrong configuration, because a wrong oscillator shows in timing long before it shows in logic.
When a restoration fails
Three failure signatures and what they mean:
Programming fails partway with no obvious cause. Erase incomplete, or configuration written too early.
Device programs and does not run. Configuration points at a clock source the board does not provide, or the reset vector is not where the program puts it.
Device runs and behaves differently. Configuration fields other than the clock — watchdog, brown-out — or the data array.
All three are answered by comparing against the record, which is why the record is made before the source device is disturbed.
Related reading
- PIC18F43K20 and the smaller array — where the same sequence applies with less to lose.
- PIC18F45K20 and the four protection blocks — the configuration that makes ordering matter.
- PIC18F24K20 and the data array — the decision that belongs in the record.
FAQ
Why is configuration written last? Because it governs write access to the array. Written early, it can forbid the writes that follow.
Does a partial erase matter? Yes. Leftover configuration applies to the new program and produces behaviour nobody intended.
Should the data array be written? Only by decision. Leaving it blank is acceptable; leaving it undecided is not.
How is configuration compared? Byte by byte against the source’s recorded values, not through a tool’s field display.
What if the device programs and does not run? Usually the oscillator setting, or a reset vector that the program does not place where the configuration expects.
Conclusion
Restoration has an order because configuration is access control as well as content. Erase fully, verify the erase, program code and data, then write configuration and compare it byte for byte. The sequence is short, the failure it prevents is expensive, and the record made beforehand is what makes any of it checkable.