EmbeddedEra EmbeddedEra

← Notes from the bench.

Why a firmware update resets mid-swap with a watchdog

Dec 6, 2026 firmwaredesign-for-testsecurity Drafted with AI assistance, reviewed and edited by our engineer.

The update downloads. The image verifies. The device reboots to install it, and then it reboots again, and again. The application watchdog is running, the bootloader is busy erasing flash, and nobody is feeding anything. On the bench with a debugger attached it works, because the debugger can pause the watchdog. In the field it loops.

This is one of the most common ways a good update design fails in practice. The swap logic is correct. The timing around it is not.

What the watchdog is doing during an update

A hardware watchdog counts down and resets the chip unless software refreshes it. Your application feeds it from a healthy main loop or a supervisor task. Then the application asks for an update and resets into the bootloader.

Two things can happen next, depending on the part and the reset type.

  1. The watchdog keeps running across the reset. Some parts do not stop a watchdog on a soft reset. The bootloader inherits a countdown it did not start and may not know about.
  2. The watchdog is cleared by the reset. A pin reset, power cycle or other reset source can leave it stopped. The bootloader then runs unsupervised, which is safe against nuisance resets but means a hung bootloader never recovers on its own.

Nordic’s staff explain the first case on a DevZone thread about MCUboot on an nRF5340: the bootloader does not start the watchdog; the application must, and the watchdog state carries through a soft reset. The bootloader’s feed call only refreshes a watchdog the application already armed, and only if the reset left it armed. Other chip families differ, so read your reference manual’s reset-behavior table for the watchdog before assuming either case.

So you have a timing question with two unknowns: how long the bootloader’s longest silent stretch is, and whether anything is feeding the watchdog during it.

Where the silent stretches come from

A swap-based bootloader moves image data between flash slots in sectors. MCUboot’s design document describes scratch-based, offset-based and move-based swap, each with different erase counts. It also says the swap must survive a reset in the middle, using status records written to flash so the operation can resume.

That resume property is what makes a watchdog reset during a swap survivable in principle. It is also what hides the problem. The device never bricks. It just resumes, gets reset again at the same point, and makes slow or no progress. From outside it looks like an update that never completes.

Flash erase is usually the longest single operation. On the nRF5340, Nordic’s NVMC documentation gives a maximum page erase time of 87.5 ms, and says the CPU is stalled if it executes code from flash while an erase runs. Take that per-page figure and multiply by the pages the bootloader erases between feed points. Your own part will have its own numbers in its own electrical specification, so look them up rather than reusing these.

Other contributors to the silent stretch:

  • Erasing a whole slot in one call. Some code paths erase an entire secondary slot, or a trailer region, before any copy begins. That is one long call with no natural place to feed.
  • Image verification. Hashing and signature checks over a large image take time, and a slower clock or no hardware crypto lengthens them.
  • Reading swap state. Scanning trailers on a large or fragmented layout adds up.
  • External flash. Erase and program times on SPI NOR parts are often longer than internal flash, and the bus adds overhead. Check the part’s datasheet for sector and block erase times.
  • Clock source. A bootloader that runs from the internal oscillator while the application used a crystal changes every timing figure above.

Feeding the watchdog from the bootloader

MCUboot’s Zephyr port has a hook for this. The Kconfig option BOOT_WATCHDOG_FEED enables a MCUBOOT_WATCHDOG_FEED() macro, described as feeding the watchdog while doing a swap. It defaults on when the watchdog driver is enabled, and for Nordic parts.

Three cautions.

The macro feeds; it does not start. If the application never armed the watchdog, or the reset cleared it, the feed is harmless and does nothing. If you want supervision inside the bootloader, you have to start the watchdog there yourself, in a way the build supports.

The feed only helps where it is called. The macro is placed in the swap’s copy loop. A long erase or verification outside that loop is not covered unless the code feeds there too. Check the source version you actually ship. One thing to settle on the bench rather than by reading a forum: is every operation that can run long reached by a feed call, or does the longest one sit between two of them?

Feeding everywhere defeats the point. A watchdog that is fed from inside an erase loop also keeps a stuck erase alive forever if the loop itself is the thing that hangs. Feed once per completed unit of progress, such as one sector, not on a timer.

Setting the timeout without guessing

You have two defensible designs.

Long timeout in the application, shared with the bootloader. Set the application’s watchdog timeout longer than the bootloader’s worst-case silent stretch, with margin. The cost is that a real application hang takes longer to recover from. This is simple, but it is a compromise on field behavior for the sake of update behavior.

Reconfigure at handoff. Where the part allows it, the bootloader starts its own watchdog with a timeout sized for its work. Some watchdogs cannot be reconfigured once started, and some cannot be stopped. The reference manual decides which design is open to you.

Zephyr’s watchdog API shows the same constraint at the software level: timeouts are installed before setup, setup fails if the watchdog is already running, and once running the timeouts must be fed periodically. It also has options to pause the watchdog while the CPU sleeps or is halted by a debugger. Those options are why a bench run with a debugger attached can pass while a free-running device resets. Test the update with the debugger physically disconnected.

A bench procedure that finds this before a customer does

  1. Measure, do not estimate. Toggle a GPIO at the start and end of each bootloader phase, or around each erase, and capture it on a logic analyzer. You are looking for the longest gap between any two feed points.
  2. Test the worst-case image. Use the largest image you will ever ship, not the 50 KB test build. Add margin for the next few releases.
  3. Test every reset path into the bootloader. Soft reset from the application, power cycle, pin reset, brownout. Read the reset-reason register on entry and log it. The watchdog may be armed in one and off in another.
  4. Disconnect the debugger. Run the update free. Repeat with the board warm and cold if the clock source or supply behaves differently.
  5. Interrupt it deliberately. Cut power during the swap and confirm it resumes and completes. Treat this separately from the watchdog test: one proves recovery from a power loss, the other proves the bootloader is not killing itself.
  6. Count boots. Have the bootloader keep a small boot counter in non-volatile memory. If a swap resumes more times than any healthy update would need, stop and report a failure rather than looping forever. A device that loops silently is worse than one that falls back to the old image.

If the loop is already happening in the field, our note on update recovery and rollback covers what the fallback path should look like once you can detect it.

Encrypted and secure-boot images take longer

Image encryption and signature checks add work to the boot path, and some configurations change how the swap proceeds. Nordic DevZone has an unanswered ticket from a user who bench-measured long swap-state reads and long erases on one SDK release with image encryption on. We treat it as a signal to measure, not as a verified defect. If your product enables encryption, re-run every timing test above with it enabled, because the numbers from your unencrypted build do not transfer.

How we approach it

We treat the bootloader as part of the product, with its own timing budget measured on the real hardware and the real image. If you are seeing an update that resets, loops or never completes, send us the part number, the bootloader and SDK versions, the reset-reason log if you have one, and the flash layout, and we will start from the measured gap between feeds.

Your updates work on the bench but reset in the field; we would measure the bootloader's worst-case swap time and fix the watchdog handoff.

ENGINEERING NOTES

One note a week on PCB design, firmware and getting hardware built. No sales email.

Double opt-in. Unsubscribe in one click.