Embedded Developer

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.

50 min

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 with AND 0xFF: a value of 0x1FF becomes 0xFF, and in Python the ~ of a mask is a negative number that the AND 0xFF repairs.
  • 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"}, then result, changed (sorted names of the registers whose value is not the start value) and n_ops. The checks run apply_ops on 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 toDo thisWhat you see
see what a register really holds on a running chipStop in the debugger, open the peripheral registers view, and read the register in binarythe real bits, to compare with what the code should have produced
find which bits are write-1-to-clearRead the register description in the datasheet and look for the access type of each bitbits marked rc_w1 or w1c, where a read-modify-write can clear a flag by accident
see the generated code for a bit changeCompile with -S or open the disassembly in the IDEwhether 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.

🔒 The debrief unlocks when you close the case. Finish the lab above.

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).

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

📝

Knowledge Check

1 of 4

Which operation sets one bit and leaves the others alone?

Report a problem

About: /lessons/case-the-wrong-bit