Posts Tagged ‘break ic source file’

PostHeaderIcon ATtiny261: The Calibration Constants Are the Product

Most small microcontrollers are chosen for their pins or their price. This one is usually chosen for its converter: a ten-bit successive-approximation unit with a differential input stage and a selectable gain ahead of it, which lets a part that costs very little measure a bridge, a thermocouple or a shunt without any external amplifier.

That has a direct consequence for a recovery. The code in such a device is mostly plumbing — timer, serial port, a state machine — and the part that a competitor or a repair engineer actually wants is a small set of constants that convert raw counts into physical units. Those constants are not a secret algorithm. They are the product.

What the converter offers

The array is 2 KB, the SRAM and EEPROM are 128 bytes each, and the package has twenty pins. Within that budget the analogue side is unusually complete: eleven single-ended channels, differential pairs selected by the input multiplexer, two gain settings ahead of the converter, and an internal reference selectable in software.

feature setting where it shows in the image
input selection single-ended or differential pair channel bits written to the multiplexer register
gain unity or a high fixed gain the same register, adjacent bits
reference internal or an external pin reference bits written during initialisation
conversion mode single shot or free running the enable and start bits in the control register

The differential gain setting is the one worth understanding. A bridge output is a few millivolts sitting on a common-mode level of half the excitation, and unity gain would spend most of the converter’s range on the offset. The gain stage multiplies the difference before conversion, which is why a design can resolve microvolts with a ten-bit converter and why the same converter is useless for that job without it.

How a conversion sequence reads in assembly

The register-level pattern is short and repetitive: select the channel, start a conversion, wait for the completion flag, read the result register pair, and either use the value or accumulate it.

Two details distinguish a careful design from a rushed one, and both are visible after the fact. The first is whether the code discards the first conversion after a channel change. The sample-and-hold circuit needs time to settle when the multiplexer moves, and the first result after a switch is systematically wrong by an amount that depends on the source impedance. Designs that discard it read cleanly; designs that do not have a fixed offset on every channel change.

The second is whether the reference is allowed to settle. Switching the reference and converting immediately produces a first reading that is low, and on a device that samples once per second that error is invisible in testing and present in every unit.

The constants that carry the model

After the raw counts come the arithmetic. A conversion is turned into a physical value by an expression of the form offset plus gain times the count, sometimes with a second-order correction term, and on a part this small the arithmetic is fixed point because there is no floating-point unit and no room for a library.

That makes the constants readable. A sixteen-bit gain term, a signed offset, a shift count that implements the division — those three values state the sensor’s sensitivity, its zero point and the scaling the designer chose. For a bridge sensor the gain term is proportional to the excitation voltage and the gauge factor; for a thermocouple it corresponds to the Seebeck coefficient over the reference junction range; for a current shunt it is the reciprocal of the shunt resistance.

A disassembly that labels those constants is therefore more useful than one that labels the control flow, because the control flow is generic and the constants are specific to the product.

Averaging, and what it says about the noise budget

A single conversion on this converter has a noise floor set by the reference and the sample time. Designs that need better resolution do not change hardware; they average. The number of samples averaged appears in the image as a loop count, and the loop count states the designer’s noise budget directly.

Four samples buys one bit. Sixteen buys two. A design averaging sixty-four conversions per reading is telling you that it needed three more bits than the converter could give and was willing to spend the time. That number also predicts the sample rate, which is often the specification the customer cares about and never has documentation for.

samples averaged resolution gained typical use
1 none monitoring a supply rail
4 1 bit a slow temperature reading
16 2 bits a bridge with moderate precision
64 or more 3 bits or more a scale or a precision instrument

What the deliverable contains

For a code-level recovery the return set is the flash image, the EEPROM as a separate file, the fuse and lock state as read, a disassembly with the converter configuration annotated, and — the part customers use — a table of the calibration constants with the arithmetic they feed, written out as an equation that reproduces the device’s output from a raw count.

That last item is often the whole engagement. A customer with a failed unit and no source code does not need the state machine; they need to know what the number on the display means.

The wider vector table of this part’s larger sibling is covered in ATtiny261 vector table notes, and the marginal-array read problem in ATtiny261V code recovery. For a device where the analogue front end is external and the constants therefore live in a separate part, see ATtiny4313 code recovery.

FAQ

Is the gain stage internal? Yes, it sits ahead of the converter and is selected through the same register that chooses the input pair.

Why discard the first conversion after a channel change? The sample-and-hold needs to settle. The first result after a multiplexer change carries an error proportional to the source impedance.

Can the calibration constants be recovered without the code? They can be measured from the device by applying known inputs, but reading them from the image is faster and exact.

Conclusion

On a converter-centric part the firmware is mostly scaffolding and the value is in a handful of numbers. Reading the multiplexer configuration says what is measured; reading the averaging count says how precisely; reading the gain and offset constants says what the numbers mean. Those three together reconstruct the product’s measurement model without needing a single line of the original source.

PostHeaderIcon ATtiny2313: Self-Programming and the Half-Written Page It Leaves Behind

Most images have one author: whatever programmer wrote them at the factory or on the line. Some images have two, because the firmware can rewrite its own flash, and when that is the case the array is not a snapshot of a build — it is the current state of a process that may have stopped in the middle.

On a twenty-pin part with 2 KB of flash that distinction is unusually tractable. The array is only thirty-two pages. An interrupted self-update leaves evidence in one of them, and finding which one is a matter of arithmetic rather than archaeology.

The mechanism, in the order it happens

Self-programming on AVR is not a store instruction. It is a sequence of writes to a control register, each one triggering a hardware operation that runs to completion after the instruction retires:

  1. Erase the target page. The whole page goes to 0xFF, not just the bytes about to be written.
  2. Fill the page buffer, one word at a time, through a temporary register pair.
  3. Commit the buffer to the page in a single write.

Two properties follow from that sequence and both matter during a recovery. The first is that a page is the unit of destruction: rewriting four bytes costs the other sixty. The second is that the sequence has intermediate states, and a power loss between any two of them leaves a page that is neither old nor new.

interruption point page state what it looks like in the image
before the erase old contents intact nothing to see
after the erase, before the commit all 0xFF a hole in the middle of otherwise dense code
during the buffer fill, before the commit still all 0xFF same as above; the buffer is volatile
after the commit, before verification new contents, unverified valid code that may be truncated

The middle two rows are the reason a hole of erased bytes inside a region of dense code is worth investigating rather than dismissing. On a part that was programmed once and never updated, erased bytes appear only at the top of the array. Anywhere else, they are a scar.

Why 2 KB makes this cheap to analyse

Thirty-two pages of sixty-four bytes means the page map fits on one line. Walk the array page by page and classify each one as dense, sparse or blank, and the layout of the firmware becomes visible without reading a single instruction:

  • the vector table occupies the first page, and possibly part of the second;
  • initialisation and the main loop follow in dense pages;
  • string and table data tend to occupy pages with a regular repeating structure;
  • blank pages appear at the top, and anywhere else they are candidates for an interrupted write.

That classification takes minutes and it answers a question that otherwise gets answered badly: whether the image is a clean build or the survivor of a failed field update. The difference matters commercially, because the second case usually means the customer has a second unit that was updated successfully, and that unit is a better source than the damaged one.

The vector table as a fixed cost

Every interrupt source needs an entry, and on this family each entry is a two-word relative jump. The count of entries is set by the peripheral set: two timers, a USART with its separate receive, transmit and data-register-empty sources, an analogue comparator, an external interrupt pair, a pin-change group, a watchdog, and the EEPROM-ready source.

That works out to a table of roughly thirty to forty words, which is between one and two percent of a 2 KB array. Small in absolute terms, and large enough that it constrains what fits: a 2 KB part running a USART protocol stack has very little left for application code, and that is why images on this part are usually written in assembly with attention to every byte.

It also gives a verification tool. The table is at a known address, its entries are a known instruction form, and any deviation is either an unused entry left as a jump-to-self or the mark of a build that disabled an interrupt. Both are readable, and a table that does not decode at all means the image starts at the wrong base address.

The USART register writes as a chronology

Because the part is normally chosen for its serial port, almost every image contains a USART initialisation block: baud rate registers, frame format, enable bits. That block is a timestamp of sorts. The baud divisor is computed from the clock frequency, so the pair of constants written to the baud registers states the clock the build assumed, and comparing it against the board’s actual oscillator resolves the most common «restored image runs at the wrong speed» complaint.

The frame format register says something else: whether the design used the port for a simple debug channel, in which case the configuration is minimal and the transmit path is a blocking loop, or for a real protocol, in which case there are receive interrupts, buffers and state machines. The second kind of image is longer, denser and usually the one worth reconstructing in full.

What is delivered

The return set for a code-level recovery on this part is the flash image as Intel HEX, the EEPROM as a separate file, the fuse and lock state as read, a disassembly with the SPM sequence and the USART configuration annotated, and a page map marking any region that looks like an interrupted write.

The page map is the part of the deliverable that customers did not ask for and consistently use. It says whether the image in hand is the original build or the result of an update that stopped, and those two answers lead to different next steps.

Where the same package carries twice the flash, ATtiny4313 code recovery covers what changes when code is moved between them. The variant with revised silicon is discussed in ATtiny2313A heximal recovery, and the wider vector table of the next part up in ATtiny261 code recovery.

FAQ

Can self-programming be detected from the image alone? Yes. The control-register sequence is distinctive and appears in a small number of routines that are then callable from the update path.

Does an erased page always mean a failed update? No. It can also be an unused page inside a build, but on this part the linker packs from the bottom, so a hole in the middle deserves a second look.

Is the page buffer readable after a power loss? No. It is volatile, which is why an interrupted fill leaves the page erased rather than partially written.

Conclusion

A part that can rewrite its own flash produces images that record their own history. The page is the unit of destruction, the sequence has three steps with a gap between each, and a 2 KB array is small enough to classify page by page in a few minutes. That classification is the difference between reporting a build and reporting the state of an update that failed.

PostHeaderIcon ATtiny85V: What EESAVE Decides Before Anyone Erases Anything

Every AVR recovery that involves clearing protection runs into the same physical fact: the operation that removes the lock bits also erases the array. There is no separate unlock command. The erase is the unlock.

That fact is usually stated as bad news and left there. On this family it is more interesting than that, because the erase is not a single action covering a single memory. It covers three, and one bit in the fuse configuration decides whether the smallest of them is included. That bit is read without disturbing anything, it takes a second to obtain, and it determines which of two quite different procedures is safe.

Three memories, two erase behaviours

The device holds flash for code, SRAM for working state, and EEPROM for data that has to survive a power cycle. Of these, two are non-volatile and both are affected by a chip erase — but not identically.

memory contents survives chip erase endurance
flash program and constants no about 10,000 cycles
EEPROM calibration, counters, settings depends on one fuse bit about 100,000 cycles
SRAM working state irrelevant, gone at power-off unlimited

The fuse is called EESAVE and it is programmed rather than cleared to preserve. In the state where it is programmed, a chip erase clears the flash and the lock bits and leaves the EEPROM contents untouched. In the other state, the EEPROM is erased along with everything else.

Why this changes the order of work

Consider a part whose lock bits prevent reading the flash but whose EEPROM is the thing the customer actually needs — a unit counter, a calibration table, a serial number that was never written down. Two procedures exist and they lead to different places:

  • If EESAVE is programmed, running the erase clears protection and flash, and the EEPROM can then be read through the ordinary interface. The code is gone, the data is not. That is often an acceptable trade, and it costs nothing but a reflash of an image that probably exists in the customer’s source control anyway.
  • If EESAVE is not programmed, the same erase destroys both memories. The job then has to go to the silicon, which is a different service at a different price.

The entire decision rests on one bit that can be read before anything is touched. Skipping that read is the single most expensive shortcut available on this part, and it is taken routinely by operators who have learned that «chip erase unlocks the chip» and stopped there.

Reading the fuse state first

The fuse bytes are readable through the interface on a part whose lock bits are set, as long as verification has not been disabled entirely. That is the first operation, before any erase, before any attempt at the flash array:

  1. Read the signature. If it does not answer, stop; the problem is electrical and no fuse will help.
  2. Read the lock bytes. They say which operations the part will accept.
  3. Read the fuse bytes. They say what an erase will destroy.
  4. Only now decide whether an erase is part of the plan.

Steps 1 to 3 are non-destructive and take under a minute. Their output is the entire basis for the choice in step 4, and it also documents what state the part was in when it arrived — which matters when a customer later claims the part was working.

What the EEPROM contents reveal

When the EEPROM does survive, its contents are worth more than a data dump. The endurance figures are the reason: flash in this class is rated around ten thousand write cycles and EEPROM around a hundred thousand, an order of magnitude apart. A page that has been rewritten on every power-up shows a different wear signature from one written once at the factory.

In practice that shows up as data that is still valid but sits in a page whose neighbours are inconsistent, or as a structure that looks like a rotating log — a sequence of records walking through the address space, each with a sequence number that only ever increases. Recovering such a structure is a matter of ordering records, not of reading bytes, and the last valid record is the answer the customer wants.

When the fuse says the data is gone

If EESAVE was left in its erase-inclusive state, the data page is not recoverable by any interface operation, and the honest answer is that the erase that would unlock the flash will also clear the EEPROM. The work then proceeds at die level, where the EEPROM array is read directly out of the silicon before any electrical erase is applied.

That path is slower and more expensive, but it is not speculative. The EEPROM array on this family is a distinct physical structure with its own row address decoder, and reading it does not depend on the flash lock bits in any way. What it does depend on is the package: an eight-pin plastic part is a straightforward decapsulation, and the array is small enough that the read is quick once access exists.

The deliverable, and the note that matters

The return set is the flash image, the EEPROM contents as a separate file because it is a separate address space, the fuse and lock bytes exactly as read on arrival, and a statement of which of the two paths was taken and why. That last item is not administrative. A customer who is told «the EEPROM was preserved because EESAVE was set» learns something about their own production programming that they did not know, and the same customer who is told the opposite learns that the setting needs changing in the fixture.

Protection semantics on this variant are otherwise identical to the rest of the family, and what the same die looks like when the firmware turns out to be a software USB stack is covered in ATtiny85 flash reverse engineering. The low-voltage electrical constraints are covered in ATtiny45V program recovery, and the identification of the three pin-compatible members in ATtiny45 heximal extraction. For images where the EEPROM is used as a rotating log rather than a settings page, the same reasoning appears in ATmega1280 EEPROM recovery.

FAQ

Can the EEPROM be read while the flash is still locked? Not through the array read, which the lock bits cover. The fuse bytes are readable, and they decide whether a later erase will spare the data.

Is EESAVE programmed by default? It is not, which is the reason an unplanned chip erase so often takes the data page with it.

Does preserving the EEPROM mean the code can be recovered afterwards? No. The erase that clears the lock bits clears the flash regardless of EESAVE. Only the data survives.

Conclusion

«Chip erase unlocks the part» is true and incomplete. It is one operation with two possible scopes, and a single fuse bit selects between them. Reading the fuse bytes before touching anything costs a minute and decides whether the customer gets a data page back or a quotation for decapsulation.

PostHeaderIcon ATtiny45: Three Parts, One Pinout, and a Dump Length That Tells Them Apart

Three part numbers, one eight-pin body, one pinout, one peripheral list. A designer who runs short of program space in an ATtiny25 moves to an ATtiny45 without touching the board, and a designer who runs short again moves to an ATtiny85 the same way. The BOM changes, the copper does not. That convenience is the reason a recovery on this family begins with a question that has nothing to do with reading: which of the three is physically in the socket.

The answer is not on the silkscreen in any reliable way. It is in the signature, in the initialisation code, and in how the programming interface behaves when asked for more words than the part contains.

What actually differs between the three

The core is the same AVR eight-bit machine across all three, and the peripheral set is close to identical: an eight-bit timer with PWM, a second eight-bit timer, a four-channel ten-bit converter, an analogue comparator, a universal serial interface, and the same six usable I/O pins on the eight-pin package.

ATtiny25 ATtiny45 ATtiny85
flash 2 KB 4 KB 8 KB
SRAM 128 B 256 B 512 B
EEPROM 128 B 256 B 512 B
words of flash 1024 2048 4096
pinout 8-pin 8-pin 8-pin

Everything that changes is a quantity. Nothing that changes is a feature. That is exactly the situation in which a swapped part goes unnoticed for a year and then surfaces during a recovery, when the image comes back the wrong length and nobody can say whether the read failed or the part is simply smaller than the drawing says.

Why the dump length is the first test, and why it is not enough

The interface is asked for a word count. On a 4 KB member the correct answer is 2048 words. Ask the same interface for 4096 words from a 2 KB member and one of three things happens, depending on the adapter: it wraps and returns the first 1024 words a second time, it returns 0xFF for everything past the end, or it reports a verify failure partway through. All three look like a fault to somebody who has not seen the pattern before, and only the first is diagnostic, because a wrap is a fingerprint.

A wrap is easy to detect and worth checking for explicitly. Compare the second half of the buffer against the first. Identical halves on a part that should not be able to produce them means the read exceeded the array, which means the part is smaller than assumed. That single comparison resolves a surprising number of «the dump is corrupt» cases without any further work.

0xFF past the end is the useless answer, because erased flash is also 0xFF. A tail of 0xFF is equally consistent with a small part, an unprogrammed region of a large part, and an adapter that failed silently and filled its buffer with the erased value. The pattern carries no information on its own.

The stack pointer initialisation as an identifier

Here is the test that does work, and it comes from the code rather than the interface. Every AVR program built with a normal toolchain starts by loading the stack pointer with the top of SRAM. That value is a constant compiled into the image, and it differs between the three members because their SRAM differs: a 128-byte part, a 256-byte part and a 512-byte part do not share a RAMEND.

So the first few instructions of a valid image contain an immediate that identifies the member. Load it out, look at the value, compare against the three possible RAMEND figures, and the question «which part is this» is answered from the image itself. It costs one minute and it does not depend on the adapter, the cable or the operator.

The same logic works in reverse, which is where it earns its keep. If the initialisation constant says 256 bytes of SRAM and the BOM says ATtiny85, the image did not come from the part the customer believes is on the board. That happens more often than it should, usually because a prototype was reworked and the documentation was not.

Reading order that avoids rework

The sequence that keeps a job from being repeated:

step what it establishes cost of skipping
read the signature the interface works at all, and the family hours spent on a wiring fault blamed on protection
read the word count the part should have expected image length a wrap mistaken for corruption
check for a repeated half whether the part is smaller than assumed a wrong part number in the report
extract the stack pointer constant which member, independently a report that contradicts the BOM
read the full array the image —

The first two steps take under a minute and settle the majority of disputes about whether a read succeeded.

Lock bits on this family

The lock bits here are the two-bit arrangement common to the small AVR parts: one setting disables further programming of the flash and EEPROM while leaving verification available, the next disables verification as well. The distinction matters because the first state still permits a read through the ordinary interface, and a customer who reports a «locked» part is often describing the first state rather than the second.

The erase that clears those bits also clears the flash array. There is no state in which the protection is removed and the contents survive, so a request framed as «unlock it and keep the code» has no solution at the interface level. The contents have to come from the silicon.

What the deliverable contains

A complete job on this family returns the flash image as an Intel HEX file with the correct length for the identified member, a disassembly annotated with the peripheral addresses actually referenced, the EEPROM contents as a separate file because it is a separate address space, the fuse and lock state as read, and a short note recording the signature and the stack pointer constant that established the identity. The last item is not padding. It is the evidence that the image belongs to the part the report names.

Where the sibling with more flash needs the same treatment, ATtiny85 software recovery continues with the cases that only appear at 8 KB. The smallest member’s pin constraints are covered in ATtiny25 flash recovery, and the low-voltage variant’s electrical limits in ATtiny45V program recovery.

FAQ

Can the three be told apart without reading the code? Yes, from the signature, which the interface reports on request and which does not consume flash addresses. The stack pointer constant is the independent second opinion.

Is a dump that ends in 0xFF incomplete? Not necessarily. Erased flash reads as 0xFF, so a tail of 0xFF is consistent with several different situations and proves nothing either way.

Does a locked part still respond to the interface? It responds, and it refuses the specific operations the lock state disables. Silence is a wiring or level problem, not a protection problem.

Conclusion

On a pin-compatible family the part number is a claim and the image is the evidence. The signature says which family answered, the requested word count says whether the interface respected the array boundary, and the stack pointer constant says which member the code was built for. Three cheap checks, in that order, and the expensive ambiguity is gone before any work begins.

PostHeaderIcon ATtiny84: What a Complete Image Looks Like on a Fully Used Array

Small families are built by putting several memory sizes in the same package, and the smaller members occupy only part of the address map. That leaves an unimplemented tail which reads as FF — a harmless artefact once recognised, and a persistent source of confusion before it is.

This member is the largest of the fourteen-pin group, so its array is fully populated. There is no unimplemented tail. Every address in the map corresponds to real cells, which means a large FF region is now a claim about content rather than about the die.

The map, and why it matters

Region Content Consequence of a gap
Vector table Reset and interrupt entries Device cannot be running the code you think
Start-up Clock and peripheral configuration Behaviour differs from the source you reconstruct
Application The bulk of the logic Missing functions, unresolvable calls
Tail FF if the build is smaller than the part Normal, not a fault

The fourth row is the one that changes meaning on a fully populated part. On a smaller sibling, FF at the top is expected. Here, FF at the top means the build did not use the space, which is common and worth stating explicitly in a report so that nobody later interprets it as truncation.

Four checks that establish completeness

The end-of-file marker. In a hex file, a missing terminator means truncation. Cheap to check, and it catches a partial write of the file itself rather than a partial read of the device.

The instruction encoding at the boundary. A file padded to a round length ends in FF, which is not a valid instruction. A file that ends in valid-looking code is telling you the real length is shorter than the file.

The vector table’s shape. On this family the reset vector is a jump, and the interrupt vectors follow at a fixed spacing. A table whose entries decode as valid jumps is aligned; a table that decodes as garbage is off by a byte or two.

Page-level double reads. Every page read twice, compared. A page that differs between passes was read at an inadequate margin, and its content is a hypothesis rather than data.

The four are independent, which is why doing all four is worth the few minutes. Each catches a different class of error, and a file that passes all four is defensible.

Why this family has no boot section

There is no self-programming path on these parts: no boot section, no boot lock bits, no SPM-based field update. The entire protection model is the two general lock bits.

That simplification is genuinely useful. There is no second container to reason about, no boot reset fuse to check, and no possibility that part of the program is privileged code in a separate region. The questions collapse to: is the array readable, and if not, what does the erase cost.

It also means a device cannot update itself. Any field update happens through the programming interface, which is why products built on this family tend to have a header on the board.

Lock state and what it changes

The four-state encoding applies as it does everywhere in the family: unlocked, mode 1 blocking programming, mode 2 and 3 blocking verification as well. In mode 1 the array is readable and the protection only prevents reflashing — a distinction worth making, because “protected” is often assumed to mean “unreadable” and does not.

For a device in mode 2, the only documented operation that opens it is the chip erase, which clears the array. There is no partial path, no page-level unlock, and no interface that behaves differently.

Reading the part properly

Power at the design rail, measured at the socket. Read signature and fuses before memory, because a part configured for a crystal that is not fitted will not run and therefore will not answer. Read the array in pages, twice each. Disassemble the vector table and confirm the reset vector lands where the code begins.

That sequence is unremarkable and it is also the one that produces a file nobody has to re-examine later. On a small, fully populated array the whole thing takes a few minutes, and the alternative — discovering three weeks later that the image was never verified — costs considerably more.

Related notes

FAQ

Is FF at the end of the image a fault? On a fully populated part it means the build did not use the space. That is normal and should be stated in the report.

Does this part support a bootloader? No. There is no boot section and no self-programming path.

Can a mode 1 part be read? Yes. Mode 1 blocks programming, not verification.

Why check the vector table instead of the whole file? Because it is the only region with a known structure that fails visibly when misaligned.

Conclusion

Completeness is a claim that has to be tested, not assumed from file size. On a fully used array the tests are the terminator, the boundary encoding, the vector table, and page-level double reads — four independent checks that together turn a plausible file into a verified one.