Case File: Which Bit Did I Just Flip
A firmware change that was meant to switch on one pin switched off seven other things, because a whole register was written where one bit was meant. Apply a list of set, clear, toggle and write operations to 8-bit control registers with masks, keep the result to 8 bits, ignore registers that do not exist, and read multi-bit fields with a shift and a width. Run it on requests that you cannot see. The registers are generated for you alone.
Sabi Logistics · Firmware 1
Which Bit Did I Just Flip
After one line of firmware was changed, the locker door lamp came on and the serial port stopped working
Tunde Hardware lead ·
Tunde. A colleague changed one line of firmware to turn on the door lamp, and now the lamp is on, but the serial port is dead and the ADC no longer samples. The line was PORTB = 0x20;.
I know what happened, but I want it understood by everybody and checked by a machine. Here are the control registers of the chip, the operations that the firmware performs on them in order, and the fields that I want to read at the end. Please apply the operations bit by bit the right way, tell me what every register holds in binary, which registers changed, and what the fields say.
The brief
What you need to know first.
- A register is one byte. Each bit controls one thing: a pin, a timer mode, a serial port switch. To change one thing without touching the others you work with a mask that has a 1 for each bit that you mean.
- The four operations.
set:reg = reg OR mask.clear:reg = reg AND (NOT mask).toggle:reg = reg XOR mask.write:reg = value(the whole byte, which is the dangerous one). Keep only the low 8 bits after every operation withAND 0xFF: a value of 0x1FF becomes 0xFF, and in Python the~of a mask is a negative number that theAND 0xFFrepairs. - Unknown registers. An operation on a register that is not in the list is ignored (the chip has no such register).
- Fields. A group of bits inside a register:
(reg >> shift) AND ((1 << width) - 1). For example the 3 lowest bits of the ADC control register set the clock divider. - Fill in
apply_ops(req)returning{"regs", "bits" (8-character binary text), "fields"}, thenresult,changed(sorted names of the registers whose value is not the start value) andn_ops. The checks runapply_opson requests that you cannot see: a full clear, a double toggle, a write over 255, an unknown register, and no operations.
The lab gives you the data for this case, invented and generated for you alone.
Preparing your data…
On the bench
The lab checked the rule with a script. These are the tools and measurements that show the same thing on a real board.
| You want to | Do this | What you see |
|---|---|---|
| see what a register really holds on a running chip | Stop in the debugger, open the peripheral registers view, and read the register in binary | the real bits, to compare with what the code should have produced |
| find which bits are write-1-to-clear | Read the register description in the datasheet and look for the access type of each bit | bits marked rc_w1 or w1c, where a read-modify-write can clear a flag by accident |
| see the generated code for a bit change | Compile with -S or open the disassembly in the IDE | whether the compiler used a single-instruction set or clear, or a read-modify-write |
Work at low voltage only, and never test firmware on a device that controls something dangerous.
Tunde: “We fixed it with a set instead of a write. Another colleague says that an interrupt could still undo the change at the wrong moment. Is that possible?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Tunde: “So even a correct line can be unsafe.”
What a senior firmware developer would do next. A set or clear on a register is a read-modify-write: the chip reads the byte, changes one bit and writes it back. If an interrupt changes the same register between the read and the write, its change is lost. On the small 8-bit chips there are single-instruction versions for the low registers, and many chips have atomic set and clear registers (write a 1 to a SET or CLR register and only that bit changes), or a bit-banding area. Otherwise disable interrupts around the change, or keep the shared state in a variable that only one place writes. Use named masks (1 << ADEN) and never magic numbers like 0x20 in the code, wrap them in small functions with a name that says what they do, and read the datasheet bit table for the reset value and for bits that are write-1-to-clear, where a read-modify-write can clear a flag that you did not mean to touch. When you debug, read the register back over the debugger and look at it in binary.
The trap. reg = value for one bit. Everything else in that byte is overwritten with zeros or with whatever the value had.
Write the register note
Write the note for the hardware lead: how set, clear, toggle and write work with a mask and why only the low 8 bits are kept, what the operations in the case did and which registers changed, what the fields say, why writing a whole byte switched off the serial port and the ADC, and how to avoid it (named masks, small functions, reading the datasheet, atomic set and clear registers, disabling interrupts for a read-modify-write, reading the register back in the debugger).
🧪 Want to practise this on your own computer? See how to set up your lab →