Embedded HandbookApps
All sections › VI. Practice
STM32F103, STM32F407, STM32L4 · level intermediate · 13 min read
Open in the interactive handbook

48к. Recovering a bricked board: disabled SWD, sleep and low power, RDP, wrong option bytes, connect under reset, entering the system bootloader

A "brick" is a board the adapter no longer connects to: the tool says "no target", "could not halt the core" or "read error". The chip is almost always intact. It simply cannot be seen: the firmware disabled the debug pins, the core is asleep, protection forbids access or the option bytes make the chip boot from the wrong place.

This section covers: how to tell a real failure from a "brick" in five minutes; a decision tree "symptom → cause → action"; connecting under reset and why without NRST it turns into a lottery; sleep and low power; removing RDP level 1 and how long the erase takes; wrong option bytes; the system bootloader as the last way in; a Python model; pitfalls.

What is not repeated here: the SWD protocol and connecting under reset from the inside, at the line level — 35д; the factory bootloader, BOOT0 and the USART protocol — 48д; adapters, their reference voltage and command-line tools — 48з; option bytes in production and the order of writing them — 48и; why RDP level 1 on F1 does not protect the firmware from extraction — 11в.

Sources (topics only, no retelling of the text): ST reference manuals for STM32F1, F4 and L4 (the sections on the DBGMCU debug block, low-power modes, option bytes and read protection), the ST application note on the system bootloader (tables of entry modes per series), the STM32CubeProgrammer manual (connection and reset modes). The times in the model are assumptions, not measurements.

Terms used in the documentation

Term Meaning
bricked device a "brick", a board without debug access
under-reset attach (hardware reset mode) connecting while NRST is held during entry
hot plug attach connecting to a running target without a reset
debug in low-power mode (DBG_SLEEP, DBG_STOP, DBG_STANDBY) debugging in sleep modes, the DBGMCU_CR bits
SWD pins remapped as GPIO SWD pins reassigned to GPIO by the firmware
RDP regression to level 0 returning protection to level 0 with an erase
nBOOT_SEL, nBOOT1 option bits boot source selection bits in the option bytes
option byte loading (OBL_LAUNCH) reloading the option bytes
empty-flash boot check the empty flash check at startup

48к.1 First rule out a real failure

Before digging into the option bytes, check what breaks physically. Five minutes with a multimeter save an evening.

  1. Power. The voltage on the VDD and VDDA pins is normal; the supply current is reasonable. Hundreds of milliamps on an idle board is a short circuit or a damaged chip, and software methods will not help here.
  2. Adapter reference voltage. The adapter sees the target voltage on its VTref pin (48з). Zero there means the adapter honestly believes there is no target.
  3. NRST. The reset pin is high and does not twitch. Constant pulses on NRST mean the watchdog or BOR is working, and the chip restarts faster than the adapter can connect.
  4. Crystal and clocking. Debugging does not need the crystal clock: SWD runs from the adapter clock, and after reset the core starts from the internal oscillator. So "no crystal" is usually not the cause of a "brick", but it can make the firmware hang.
  5. Wires. Short, with a common ground, the SWD frequency lowered to hundreds of kilohertz. A long ribbon cable at 4 MHz looks exactly like a "brick".

If all of this is fine, the chip is most likely alive. Next, follow the tree.

48к.2 Decision tree

Recovery decision tree: on the left the symptom the tool reports, in the middle the likely cause, on the right the action; "no target" with live power — disabled SWD or sleep, fixed by connecting under reset; "target present, core does not halt" — sleep or the watchdog, also under reset plus the DBGMCU bits; "target present, flash not readable" — RDP level 1, removed by writing level 0 with an erase; "after reset the firmware does not start or the wrong code starts" — wrong boot selection bits, fixed by writing the bytes; if nothing helped — entering the system bootloader via BOOT0; RDP level 2 — cannot be recovered

The tool reports different things, and they already narrow the search.

"No target" (no target, DP not found). The adapter got no reply to the IDCODE read. Causes: the firmware reassigned the SWD pins to GPIO; the core is in Stop or Standby; the wires. Action — connect under reset (48к.3).

"Target found, but the core does not halt". IDCODE reads, but the halt request does not complete or the registers read as zeros. The core goes to sleep or is constantly restarted by the watchdog. Action — under reset, then forbid sleep through DBGMCU (48к.4).

"Target present, flash not readable". The core halts, RAM reads, the flash does not. This is RDP level 1 (48к.5).

"The firmware does not start, although written and verified". The chip boots not from flash: on series with boot selection bits in the option bytes they are set wrong (48к.6).

Nothing helps. The system bootloader via BOOT0 (48к.7). It has its own entry, and the firmware does not affect it.

There is a connection, but any access returns an error, and RDP reads as level 2. It cannot be recovered: debugging is closed forever, the bootloader is disabled. The only option is to replace the chip.

48к.3 Connecting under reset

The most common cause of a "brick" is firmware that at the start of main switches PA13 and PA14 to GPIO or disables debugging through AFIO on F1. While the chip is in reset, the SWD pins are in their default state and the core does not execute a single instruction. Connecting under reset takes advantage of this:

  1. The adapter pulls NRST to ground and holds it.
  2. While NRST is low, the adapter performs an SWD line reset, reads IDCODE and powers up the debug domain.
  3. The adapter sets the "halt on reset vector" bit.
  4. The adapter releases NRST. The core leaves reset and immediately halts — before the first firmware instruction.

Then the flash is erased completely, and new firmware that does not touch PA13 and PA14 solves the problem for good. The internals of this sequence are in 35д.

Connecting under reset needs an NRST wire from the adapter to the board. Without it, the tool in "under reset" mode triggers the reset in software, through a core register, and that no longer works: the core cannot be reached.

STM32_Programmer_CLI -c port=SWD freq=200 mode=UR reset=HWrst -e all

Mode and reset options differ between CubeProgrammer versions — check the --help of your version; the commands require checking.

Without the NRST wire. All that is left is catching the window between leaving reset and the line that disables SWD. Resetting the board with a button or a power cycle and immediately starting a connection works only if the firmware does not disable SWD right away. How much of a lottery this is, the model in 48к.8 calculates.

48к.4 Sleep and low power

Firmware that immediately goes to Stop or Standby looks like a "brick", even though it did not touch the SWD pins. In Stop the core and debug block clocks are stopped, in Standby the core domain is powered off — the adapter has no one to talk to.

What helps.

What to expect. The DBGMCU bits are cleared by a power reset, but not by a reset through NRST — check this for your series in the reference manual. The Stop current with these bits is noticeably higher: when measuring consumption, turn them off and disconnect the adapter.

Debug build versus release build. A good rule: in a debug build the firmware checks whether a debugger is attached and does not go to sleep for the first few seconds after startup. Then even a board without an NRST wire does not become a "brick".

48к.5 RDP level 1: removal with an erase

With RDP level 1 the adapter connects, RAM and registers are accessible, and the flash is not. Removing the protection means writing level 0 (-ob RDP=0xAA). In doing so the chip itself erases the whole flash and only then opens access.

The erase takes long. On small chips it is tens of milliseconds, on chips with 128 KB sectors — seconds. While it runs the adapter gets no replies, and the tool may decide the connection is lost and abort the process on its own timeout. The model in 48к.8 shows how long to wait.

What to do afterwards.

  1. Power cycle the board or reload the option bytes — on some series the new RDP value takes effect only this way.
  2. Connect under reset and check that the bytes read as level 0 and the flash is empty (all bytes 0xFF).
  3. Write the firmware.

If the erase was interrupted halfway, write level 0 again — it starts the erase again. Why level 1 on F1 still does not protect the firmware from extraction — 11в. A board returned from production with level 1 and a serial number — 48и.

Level 2. Writing level 2 is irreversible: debugging is disabled forever, the system bootloader too, the option bytes can no longer be changed. No adapter and no bootloader will bring such a board back.

48к.6 Wrong option bytes

Besides RDP, the option bytes contain settings that make a board "dead" without any protection.

Boot source selection. On the L4, G0, G4 and other series there are the nBOOT0, nBOOT1, nSWBOOT0 and nBOOT_SEL bits. They can replace the BOOT0 pin with a software value. If they say "boot from system memory" and the bootloader found nothing on its interfaces, the chip waits in the bootloader forever — and the firmware does not start, although written.

Empty flash check. On some series the bootloader at startup looks at whether the flash is empty, and if so, stays in itself. The emptiness flag is not updated on every write, but on a power reset or an option byte reload. After writing firmware without a power cycle the chip may keep considering the flash empty.

Watchdog in hardware mode. A bit that enables the watchdog from the moment of reset. If the firmware does not refresh it, the chip restarts every few hundred milliseconds — from the outside it shows as constant pulses on NRST.

BOR with a high threshold. A supply reset threshold above the actual board voltage keeps the chip in reset all the time. A board at 3.0 V and a threshold around 2.9 V is on the edge; when the voltage sags under load it goes into reset.

What to do. Under reset read the option bytes, compare them with a reference (with the factory defaults from the reference manual) and write the correct ones. The order of writing and the reference set for production — 48и.

48к.7 The system bootloader as the last way in

The system bootloader lives in separate memory and is not erased. Its entry depends not on the firmware but on the BOOT0 pin (and on some series on the option bytes), so it is available even when SWD is not reachable at all.

  1. Drive BOOT0 high (a jumper or a button on the board).
  2. Reset the chip. It starts in the system bootloader, not in the firmware.
  3. Connect through USART, USB DFU or another bootloader interface of your series. Through UART — the protocol and tool from 48д; through USB — 48л.
  4. Erase the flash and write working firmware. If RDP level 1 is set — the bootloader can remove it, also with an erase.
  5. Return BOOT0 low and reset.

Through the bootloader you can also connect over SWD: the core in the bootloader does not touch the debug pins. So a BOOT0 pin on a jumper is cheap insurance for any board.

This does not work if BOOT0 is hard-wired to ground without a jumper and the series has no software boot selection, or if RDP level 2 is set.

48к.8 Model: the window without NRST and the erase time

The model model48j.py answers two questions: how realistic it is to catch the core without an NRST wire and how long to wait for the erase when removing RDP.

The window without NRST. The firmware disables SWD some time after leaving reset. The adapter needs 1.0 ms to connect and halt. The moment an attempt starts after a manual reset is random within 20 ms (the button, the program on the computer, USB). The probability of catching it is the share of this interval in which the attempt manages to finish before SWD is disabled.

SWD disabled after Probability of catching Attempts on average Under reset
0.5 ms 0 % never 100 %
2.0 ms 5 % 20 100 %
10.0 ms 45 % 3 100 %
25.0 ms 100 % 1 100 %

If the firmware disables SWD in the first lines of main, before setting up the clocks, this is a fraction of a millisecond — without NRST the board can never be caught. If disabling comes after peripheral initialization, at 10 ms, three attempts on average are enough. Under reset the probability is always 100 %.

The erase when removing RDP. The chip is F4-like: four sectors of 16 KB, one of 64 KB, then 128 KB each; typical sector erase times 0.25 s, 0.55 s and 1.0 s — assumptions. For comparison: the tool's erase timeout of 5 s, also nominal. A chip with small pages (like F1) is erased completely in 40 ms, and there is no problem for it.

Flash size Sectors Erase when removing RDP Fits in 5 s
128 KB 5 1.55 s yes
256 KB 6 2.55 s yes
512 KB 8 4.55 s yes
1024 KB 12 8.55 s no

A 1024 KB chip takes 8.55 s to erase — longer than the nominal timeout. The tool may report an error even though the erase is running. Do not touch the power, wait with a margin (twice the estimate) and connect again.

48к.9 Pitfalls

Removed RDP and switched off the power a second later. The erase was interrupted, the bytes are in an intermediate state. Write level 0 again and wait.

The adapter "flashes", but the board is a brick again after reset. You wrote the same firmware that disables SWD. Fix the source code, not the adapter.

Connecting under reset does not work. The board has no NRST wire to the adapter, or NRST has a capacitor of microfarads, and the adapter cannot discharge it in time. Check with an oscilloscope.

Debugging works only while the adapter is connected. The DBGMCU bits in the debug build keep the clock during sleep; without the adapter the board really sleeps and draws a different current. This is not a brick, it is a difference between builds.

BOOT0 is tied to ground without a jumper. The last way in is closed. A jumper or a pad for one costs pennies.

The tool reports "level 2" on a new chip. More often this is a read error due to a bad contact, not a real level 2. Lower the SWD frequency and reread the bytes before throwing the board away.

After writing the firmware the chip still stays in the bootloader. The empty flash flag was not updated — power cycle the board.

48к.10 Summary

Next: section 48л "Updating over USB DFU: the STM32 system DFU, your own DFU bootloader on a USB device, the DfuSe file, tools, WinUSB drivers".

Open in the interactive handbook