Posts Tagged ‘microcontroller locked eeprom recoverying’

PostHeaderIcon Recover Microcontroller PIC16F874 Eeprom: Finding the Parameter Block

A hundred and twenty-eight bytes is not much storage, but it is enough for a complete set of operating parameters, and that is what it holds on almost every design built around this part. The bytes are not scattered: they are a block with an order, an order that the program imposed when it wrote them, and the routine that reads them is the map.

The PIC16F874 is the forty-pin member of the family: 4096 by 14 bits of program memory, 128 bytes of data EEPROM, five ports. It appears in equipment that has settings — controllers, timers, dosing equipment, environmental chambers — which means the EEPROM content is the difference between one installation and the next.

How a parameter block gets its structure

The program does not write the EEPROM byte by byte at random addresses. It writes a sequence, usually from a buffer in RAM, and it reads the sequence back the same way. Both operations use a pointer that starts at an address and advances, and that pointer’s start address and limit define the block.

So the block is discoverable from the code, in three steps.

The first is to find the EEPROM read routine. It is the only code that touches the EEPROM address and data registers, and it will be called from a small number of places.

The second is to find the loop that calls it repeatedly. That loop has a counter with a limit, and the limit is the block size.

The third is to find the RAM buffer the values land in. The addresses used after the load are the parameter slots, and their use identifies their meaning.

Reading the parameter slots

Once the buffer is known, each slot can be read by following its uses.

Usage pattern Parameter type Typical encoding
compared against a conversion result a threshold raw counts
multiplied or shifted before use a calibration factor fixed point
copied to a timer reload a period timer counts
used as a loop limit a count of items or repeats integer
copied to an output port at start-up a configuration word for external hardware bit flags
compared against zero only an enable flag boolean

The fifth row is worth looking for specifically. On a forty-pin part with five ports, a byte in EEPROM that is written to a port at start-up is a hardware configuration: which relays are enabled, which outputs are active-low, which display mode is selected. Those bytes explain unit-to-unit differences that otherwise look like faults.

Why the order tells you about the product

Parameters are stored in the order the design uses them, and that order is a description of the product’s feature set.

A block that begins with four thresholds and then a calibration factor belongs to a device with four setpoints and one sensor. A block that begins with a mode byte, then a table of times, belongs to a device with selectable programs. A block with a large run of similar values is a curve — a temperature profile, a dosing schedule, a set of segment times.

Reading the block layout before reading the code often gives a faster understanding of the product than the control flow does, because the layout is a summary of what the product can be told to do.

The map, the checksum and the blank case

Three details recur on this part and each has a diagnostic value.

A map byte or a length byte. Many designs store the block’s own size or a version at the start, so that a firmware update can migrate old contents. If the first byte is a small number that matches the block size, that is what it is.

A checksum over the block. Designs that care about data integrity store a checksum, and a device restored with a blank EEPROM fails its own check at start-up. This is the most common reason a correctly programmed device appears not to work.

An all-blank array. A 128-byte array of 0xFF means the EEPROM was never written, which on a production unit usually means the settings were defaults held in the program. In that case the parameter block exists only as default values in the code, and those are worth tabulating too.

Observation Meaning Action
all 0xFF never written, defaults in code read the defaults from the initialisation
first byte equals block length versioned block record the version
a byte makes the sum zero checksum present include it in any reconstruction
non-uniform from a known offset per-unit calibration preserve exactly, do not normalise

Reconstructing a block from behaviour

When the EEPROM is unreadable — protected, or the device is damaged — the block can sometimes be rebuilt from observed behaviour: set a parameter through the product’s interface, watch what changes. On equipment with a keypad and a display this is feasible, though slow.

The result is a block that is correct in the parameters the interface exposes and default in the rest. That distinction belongs in the report, because a block that is 80 percent right behaves in ways that are 100 percent wrong for the parameters that were not exercised.

Delivering the block as a documented object

The bytes, raw, at their addresses. A table of slots with their roles and, where the arithmetic allows, their values in engineering units. The version or checksum byte identified. And a note on which slots were inferred from the interface rather than read from the device.

Where the same family’s structure work is the subject rather than the data, PIC16F873 software covers the conversion to maintainable source. Where the protection matrix decides whether the block is readable at all, PIC16F876 flash sets out the combinations. And where an eight-pin part has sixteen bytes instead of a hundred and twenty-eight and the same problem at smaller scale, PIC12CE518 binary recovery covers it.

The short version

Follow the load routine to the block, follow the block to its uses, and the product’s configurability becomes explicit. Preserve the bytes exactly, document the slots, and record which values came from behaviour rather than from the device.