Posts Tagged ‘mcu locked heximal recovery’

PostHeaderIcon Recovery MCU PIC16C662 Heximal: Reading the Board Back from the Port Writes

Forty pins means a device that spends a good part of its time driving things. On a smaller package the code is dominated by decision logic; here a substantial fraction of the instruction stream is writes to ports, in patterns that repeat at fixed intervals. Those patterns are a description of the board, and reading them is often more informative than reading the arithmetic.

The PIC16C662 is a forty-pin OTP part with 4096 by 14 bits of program memory and the five ports that the package allows. It appears in appliance controllers, instrument front panels and industrial timers — designs where the microcontroller is the whole user interface.

Four port patterns that identify hardware

Pattern in the image External circuit How to confirm
one port written with a changing pattern, another port cycling one bit high multiplexed display the cycle period matches a refresh rate
a port driven with a walking one, another read immediately after keypad matrix the read follows the drive within a few instructions
a single pin toggled at a fixed period with data on another pin clocked serial device the period is a round number of microseconds
a port written once at start-up and never again static output or latch the value encodes the initial state of several outputs

The first row is the most common on a device of this size, and it is also the most rewarding to read, because a multiplexed display carries the product’s entire visible output in the code.

The multiplex, in detail

A multiplexed display works by driving one digit at a time and relying on persistence of vision. The code therefore has a loop that runs at a fixed rate, and inside it, for each digit: write the segment pattern to one port, enable the digit on another, wait a short interval, disable, move to the next digit.

Three facts can be extracted from that loop.

The number of digits is the number of iterations, which is visible as the loop counter’s limit. The segment encoding is the set of values written to the segment port, which is usually a table indexed by the digit value — so the table can be decoded into a seven-segment map, and the order of the table tells you whether the display is common-anode or common-cathode. And the refresh rate is the loop period divided by the digit count, which should land in the range a human eye integrates.

A design with four digits refreshing at a hundred hertz has a loop period of forty milliseconds and a per-digit dwell of ten. That number matching a plausible display is a strong sign the loop has been read correctly.

The segment table is also where character faults become visible. A product that displays a colon, a decimal point or a small set of letters has extra entries in the table, and their bit patterns identify which segments are wired.

The keypad scan

A matrix keypad is read by driving one row at a time and reading the columns. The code is a loop over rows: write a walking-one pattern to the row port, read the column port, and if any bit is low, decode which one.

Element What to look for What it tells you
row drive a value with one bit clear, rotated each pass how many rows, and their order
column read a port read immediately after the drive how many columns
debounce a delay loop inside the branch the switch quality assumed
repeat a counter that re-triggers after a hold whether the product supports auto-repeat

The fourth element is a product decision rather than a hardware fact: a keypad with auto-repeat is a value entry field, one without is a set of function keys. Reading it tells you what kind of interface the product has before you read a single label.

The clocked serial line

When a pin toggles at a fixed period with data changing on another pin, the design is bit-banging a serial protocol rather than using a peripheral. On this part that is common, because the peripheral pins may be needed for the display.

The bit period converted through the oscillator frequency gives the rate, and the rate identifies the protocol family: a few kilohertz is a slow sensor bus, hundreds of kilohertz is a memory interface, and a rate that matches a standard baud divisor is an asynchronous line with a start bit.

Reading the bit order — least significant first or most — is the other half. It shows up as whether the loop shifts left or right, and getting it wrong reverses every byte in the analysis without changing anything else.

Where the port map becomes a schematic

The end product of this reading is a table: pin, direction, function, timing, external device class. On a device with five ports that table has between thirty and forty rows, and it is the closest thing to a schematic that exists for a product whose drawings are lost.

Two cautions about confidence. A pin that is configured as an output and written once may be a latch, a power enable, or a signal to another processor — the code cannot distinguish those. And a pin that is never touched may be unused, or may be an input whose value is never read because the feature was removed, which is common on a part whose firmware went through several revisions.

Where the analysis stays honest, the table distinguishes what is read from what is inferred, and the inferred rows are marked.

Why heximal rather than an image

The plural in the request is worth honouring on a part of this size: the program array is eight kilobytes of file, and it is usually accompanied by a decode of the configuration word and a table of the port assignments. Delivering those as separate items rather than as one long file keeps the derived information visible.

Where a smaller part shows the same port-reading technique with far fewer pins to work with, PIC12C509A binary covers the constraint. Where a forty-pin sibling in the same generation has a parallel bus instead of a display, PIC16F747 code reads that interface. And where the ports drive an analogue front end rather than a display, PIC16C717 program follows the constants.

The short version

On a forty-pin part the port writes are the schematic. Look for a multiplex loop, a keypad scan and a bit-banged clock, extract the counts and the tables, and write the result as a pin table with the inferred rows marked as inferred.