PostHeaderIcon ATtiny85: When the Firmware Is a Software USB Stack

An eight-pin part with no USB peripheral is not the obvious place to find a USB device, and yet a large share of ATtiny85 designs in the wild are exactly that. The part has enough pins, enough flash and a fast enough core for a software implementation of low-speed USB, and the reference implementation for it has been copied into thousands of small products.

That fact is worth knowing before opening the disassembly, because it changes the order in which the image should be read. A general-purpose AVR image is read from the reset vector forward, following initialisation into the main loop. A software USB image is read from its timing loops outward, because those loops are the most constrained code in the part and everything else is arranged around them.

What the part actually provides

The ATtiny85 carries 8 KB of flash, 512 bytes of SRAM, 512 bytes of EEPROM, an eight-bit timer with PWM, a second eight-bit timer, a four-channel ten-bit converter, a comparator, a universal serial interface, and six usable I/O pins on the eight-pin package. There is no USB controller anywhere in that list.

Software USB therefore has to bit-bang a 1.5 Mbit/s differential signal on two ordinary port pins, with edge timing accurate to a small fraction of a 0.667 microsecond bit period, while the same core also runs the application. It works because the task is bounded: low-speed USB is slow by modern standards, and an AVR running at 16 MHz has roughly ten instruction slots per bit.

constraint figure consequence in the image
bit period 0.667 µs a tight loop whose cycle count is the specification
core clock used 16 to 16.5 MHz from the internal PLL PLL control written early, before any USB traffic
SRAM available 512 B endpoint buffers, descriptors and application state compete for one page of memory
signal pins two port pins, usually adjacent direction and level written as whole-port operations

The PLL write is the first landmark

The internal oscillator tops out below what the timing needs, so designs enable the phase-locked loop and run the core from it. That is a write to a specific control register in the first few dozen instructions of the program, before timers are configured and long before any pin is touched.

For a reader, that write is the entry point. It says the design intends to run fast, and it dates the code: designs built around the PLL are the ones that can afford software USB, and designs running from the plain 8 MHz oscillator cannot. Finding it also fixes the clock frequency the rest of the analysis assumes, which is the number every delay loop is calibrated against.

The bit engine

The timing-critical code is small, and it looks unlike anything else in the image. It is a loop with a fixed instruction count, entered from a timer interrupt or from a pin-change interrupt, containing shifts, conditional branches on a port bit, and whole-port writes. It has no floating point, no table lookups and no calls into application code, because a call would blow the cycle budget.

Two features make it identifiable even without a symbol table. The first is that it reads a port pin and writes the same port within a handful of instructions. The second is that its branch structure corresponds to the USB line states: idle, start of packet, and the two data states. Once that loop is labelled, the rest of the protocol layer is findable by following its callers.

Descriptors live in flash, not in SRAM

A USB device has to answer requests for its device, configuration and string descriptors. On a part with 512 bytes of SRAM, those tables cannot live in RAM; they are constants in flash, read back with the load-from-program-memory instruction using a pointer pair.

That produces a distinctive pattern in the disassembly: short routines that take a descriptor index, add it to a base address, and copy bytes out of flash one at a time. String descriptors in particular are recognisable because they contain the UTF-16 encoding of a product name, which appears as readable text when the flash image is dumped as bytes. A vendor and product identifier pair stored as two sixteen-bit constants nearby is the second confirmation.

This is also the fastest route to a human-readable answer for a customer. Before any semantic analysis, the descriptors usually say what the product claims to be.

Where the application begins

Everything past the protocol layer is ordinary embedded code: an ADC read for a sensor, a PWM write for a motor or an LED, a state machine driven from the main loop. It is reached from the protocol layer when a control request asks the device to do something, and it is usually the least optimised code in the image because it has slack.

Reading outward from the timing loops therefore has a practical benefit beyond elegance. The parts of the image that are most expensive to reconstruct are the ones that are read first, while attention is fresh, and the parts that are routine are read last.

What a complete deliverable looks like

For an image of this shape the return set is the flash contents as Intel HEX, the EEPROM as a separate file, a disassembly with the PLL and timer configuration annotated, the descriptor tables extracted into readable form, and a map of which routines implement the protocol and which implement the application. The descriptor extraction is worth calling out separately because it is the part of the work most often useful to someone who is not going to read the assembly at all.

The image is also the only place the calibration value can be confirmed. Designs built on the internal oscillator store a per-device calibration byte, and an image restored to a different physical part inherits the donor’s calibration rather than the target’s. That does not stop the code running, but it does move the timing, and on a software USB design timing is the product.

The family’s pin constraints are covered in ATtiny25 flash recovery, and the erase behaviour that decides whether the EEPROM survives in ATtiny85V software recovery. Where the same part appears with more flash and a wider vector table, ATtiny45 heximal extraction covers the identification step.

FAQ

Does this part have hardware USB? No. Everything is done in software on two general-purpose pins, which is why the timing loops are the most constrained code in the image.

Why does the descriptor table matter? It answers what the device claims to be, in text, without requiring the assembly to be understood first.

Is the calibration byte part of the image? It lives in a separate signature row rather than in the flash array, but an image moved to another device carries the donor’s timing unless that row is rewritten.

Conclusion

An eight-pin part with no USB peripheral still produces images that enumerate over USB, and those images have a shape: a PLL write early, a fixed-cycle bit engine, descriptor tables in flash, and application code reached from the protocol layer. Knowing the shape turns an 8 KB disassembly from a wall of instructions into a structure with an obvious first page.

Comments are closed.