PIC18F25K22: Restoring a Part Means Reproducing a State, Not Just Code
A hex file is a description of memory contents. A working device is a state: a program, a configuration, a data array, a calibration, and a physical part with a particular silicon revision. Programming the first into a replacement part reproduces one fifth of the second.
That gap is where restorations fail quietly. The unit boots, runs, responds on its bus, passes the test that was written against the old firmware — and then behaves differently under conditions the test did not cover, because something outside the array was never reproduced.
The five things that must match
| Item | Where it lives | In a hex file |
|---|---|---|
| Program code and tables | Program array | Yes |
| Configuration words | Config registers | Sometimes, as records |
| Data EEPROM contents | Data array | Usually not |
| Analog and oscillator trim | Factory area | No |
| Silicon revision | The die | No |
Items one and two are usually handled. Item three is forgotten often enough to be the most common cause of a restored instrument reading slightly wrong. Items four and five are properties of the replacement part and cannot be programmed at all.
Configuration: the part that changes behaviour silently
Configuration decides the oscillator, the watchdog, the brown-out threshold, the programming interface and the protection state. A restored part with the right code and the wrong watchdog setting runs until it does not.
The reason configuration is omitted is mechanical: many tools treat configuration as a separate operation from programming the array, and a hex file exported from the array may or may not include the configuration records depending on how the export was performed.
The check is simple: after programming, read the configuration back and compare it field by field against the source device’s recorded values. A comparison of the raw bytes is better than a comparison of the tool’s display, since the display merges fields and hides reserved bits that may differ.
Data EEPROM: the part that is different on every unit
Calibration values, settings and counters live in the data array. On a restored part there are three choices:
- Copy the source device’s data array. Reproduces the original’s calibration, including any drift it had accumulated.
- Leave it blank. The firmware writes defaults on first run, which may be correct and may not.
- Restore from a recorded value. Correct when the calibration is documented and reproducible.
The first choice is right when the replacement must behave exactly like the original, including its errors. The second is right when the calibration is a property of the hardware rather than the unit. Deciding between them requires knowing what the data array contains, which is why reading it during recovery matters even when the program array is the headline item.
Trim, and why the copy cannot be perfect
Every die carries analog trim written at test. A replacement part has its own. For most digital work the difference is irrelevant; for anything measuring an analog quantity it is not.
A restored instrument that reads a consistent offset from the original is showing trim difference, not a code error. If the offset is unacceptable, the correct fix is to recalibrate the restored unit against a reference — which is a procedure, not a firmware change.
Revision, and the compatibility question
Silicon revisions fix errata, and errata include peripheral behaviour that firmware may depend on. A replacement part of a different revision may behave differently in exactly the corner the original firmware was written to avoid.
Revision is visible in the device identification area and, sometimes, from behaviour. Recording it at recovery time is cheap; discovering it months later is not.
A restoration procedure that holds
- Record the source device: configuration bytes, data array, identification area, behaviour.
- Erase the replacement completely, including configuration.
- Program the code.
- Program the configuration explicitly, do not assume the file carried it.
- Write the data array, or leave it blank by decision rather than by omission.
- Read everything back and compare against the record.
- Run the functional test, then run the corner cases the original was known to handle.
Steps two and four are where most failures are introduced. An incomplete erase leaves configuration from a previous life, and a program that omits configuration inherits whatever was there.
When restoration is not the right goal
Two situations call for something else:
- The source device is unique and its behaviour is undocumented. Then restoration is a research project, and the output is a specification rather than an image.
- The replacement part is a different revision or a different variant. Then the code may run and the behaviour may differ, and the honest description of the result is “a compatible replacement”, not “a copy”.
Saying which of those applies, before the work starts, is worth more than a perfect procedure applied to the wrong problem.
Related reading
- PIC18F23K22 and the two protection gates — what has to be cleared before a restore is possible.
- PIC18F24K20 and the data array — why the second array is separate.
- PIC18F26K22 and the 64K image — where configuration handling becomes more error-prone.
FAQ
Does a hex file include the configuration words? Sometimes, depending on the export. Treat it as unknown and program configuration explicitly.
Will a restored part have the same calibration? Only if the data array was copied. Otherwise it has defaults or the replacement’s own.
Can trim be copied between devices? It can be written, but it belongs to the die. Copying it makes the replacement measure like the original’s silicon, which is not the same as measuring correctly.
Why does a restored unit pass tests and fail in the field? Because the test covered function and not the corner the original handled through configuration or calibration.
Is a different silicon revision a problem? It can be. Record the revision at recovery time so the difference is known rather than discovered.
Conclusion
Restoring a device is reproducing a state: code, configuration, data array, trim and revision. Only the first is reliably in the file, and the other four decide whether the restored unit behaves like the original. The procedure is not difficult; it is just longer than the part of it people usually do.