PIC18F66K80: Restoring an Image Into a Machine, Not Into a Socket
An image that boots on a bench has satisfied one requirement. In the machine it came out of, the same device must also start within the time the system allows, present the right interface to the other participants, and behave correctly when they misbehave. Those requirements are not in the file, and a restoration that ignores them produces a unit that works until it is installed.
The distinction sounds obvious and is routinely lost, because the bench test is easy and the machine test is not.
What the machine expects at power-up
A controller in a machine is usually the first thing to start and the last to be ready. Between those two points, other participants wait for it — or do not, depending on the design.
| Expectation | Where it comes from | Consequence if missed |
|---|---|---|
| Start-up time | Other devices’ timeouts | A participant gives up and faults |
| Interface presence | Bus protocol | The controller is treated as absent |
| Initial state of outputs | Safe-state requirements | Actuators move on power-up |
| Handshake sequence | The system’s own protocol | Operation proceeds with stale state |
The third row is the one with consequences outside the equipment. A restored controller whose outputs default to the wrong state during the first milliseconds moves something it should not, and the failure is intermittent because it depends on how fast the rest of the system comes up.
That behaviour is set by configuration and by the first few instructions after reset — both of which are outside the code image in the sense that matters: configuration is a separate item, and the first instructions are the part most likely to differ in a rebuild.
Configuration as a machine requirement
The oscillator setting is not merely a preference. A device configured for an external crystal, installed on a board that provides one, starts in milliseconds. The same device configured for the internal oscillator starts immediately and runs at a different frequency, changing every timing relationship in the system.
Similarly, the watchdog: enabled with a particular period, it constrains how long any routine may take. A restored device with the watchdog disabled passes every bench test and loses its protection against a lock-up in the field.
The restoration checklist therefore includes configuration as a first-class item, compared field by field against the record taken from the original.
The data array and the machine’s calibration
In an embedded control application the data array frequently holds constants that belong to the machine rather than to the firmware: offsets measured at commissioning, limits set for this particular unit, counters recording its history.
Copying the original’s data array reproduces the original’s calibration, which is correct if the replacement is going into the same machine and wrong if it is going into another. Leaving it blank lets the firmware write defaults, which is correct if the design supports it and produces a unit that measures wrong if it does not.
Neither choice is universally right, and the decision has to be made with knowledge of what the array contains — which is why reading it during recovery is worth the effort even when the program array is the headline item.
Rebuilding versus restoring
For a device whose image is unavailable, the work is a rebuild, and a rebuild has an additional requirement: it must satisfy the machine, not just the bench.
The practical approach is to instrument the machine. Measure the timing of the start-up sequence, capture the bus traffic, record the states of the outputs at each stage, and provoke the fault conditions the system can produce. That gives a specification in machine terms, and a rebuild written to it will install successfully where one written to bench observations will not.
What tends to go wrong after installation
Four failures recur:
- Start-up too slow or too fast, because the oscillator configuration differs and the system’s timeouts are tight.
- Outputs in the wrong state during reset, because the port initialisation order differs from the original.
- A missing handshake, because the rebuild implements the protocol’s steady state and not its opening exchange.
- Watchdog behaviour, because the period differs and a routine that fitted no longer does.
All four are configuration and sequencing rather than logic, and all four are avoidable by recording the original’s behaviour at power-up before it is replaced.
The order that works
- Record the original in the machine: start-up timing, output states at each stage, bus traffic, response to each fault the system can produce.
- Record the configuration and the data array.
- Restore or rebuild.
- Compare at every point from the first, in the machine, with the rest of the system present.
- Only then treat the unit as serviceable.
Step four is the one that distinguishes a restoration from a programming operation, and it takes an afternoon rather than an hour. It is also the only step that finds the four failures above.
Related reading
- PIC18F65K80 and recording the image as a file — what the file does and does not carry.
- PIC18F46K80 and the loader’s verification — why a copied image may refuse to start.
- PIC18F45K80 and reconstruction from measurement — the direction to work in when no image exists.
FAQ
Is a bench-tested restore good enough? It is necessary and not sufficient. The machine imposes timing and sequencing requirements the bench does not.
Which configuration field matters most in a machine? The oscillator, because every timing relationship in the system depends on it.
Should the data array be copied? Only if the replacement is going into the same machine and the original’s calibration applies.
Why do outputs move on power-up after a restore? Because the port initialisation order or the initial values differ from the original’s.
How do I capture what the machine expects? Instrument it: record timing, bus traffic and output states across the full start-up and through each fault condition.
Conclusion
Restoring a controller into a machine is a system problem wearing a firmware costume. The image is the visible part; configuration, data array, sequencing and timing decide whether the unit is serviceable. Recording the original in the machine before removing it is the step that makes everything afterwards possible.