embassy-rs/embassy · error

Invalid watchdog scratch index

Error message

Invalid watchdog scratch index

What it means

`Watchdog::set_scratch` writes to one of the watchdog's 8 scratch registers (indices 0-7); the hardware has no register beyond scratch7, so any other index panics. Scratch registers persist across soft resets and are meant for passing data to the bootloader or across resets.

Solutions

  1. Pass an index in the range 0..=7; verify the call site value
  2. Add a debug_assert or clamp/log for the index in your own wrapper before calling set_scratch
  3. Audit loops/constants generating the index for off-by-one errors

Example fix

// before
watchdog.set_scratch(slot_number, data); // slot_number is 1-based
// after
assert!(slot_number >= 1 && slot_number <= 8);
watchdog.set_scratch(slot_number - 1, data); // hardware index 0..=7
Defensive patterns

Strategy: validation

Validate before calling

fn set_scratch_safe(w: &mut embassy_rp::watchdog::Watchdog<'_>, i: usize, v: u32) {
    assert!(i <= 7, "scratch index must be 0..=7");
    w.set_scratch(i, v);
}

Try / catch

// panic in no_std: bounds-check before the call
if index <= 7 { watchdog.set_scratch(index, value); }

Prevention

When it happens

Trigger: Calling `watchdog.set_scratch(index, value)` with an index outside 0..=7, e.g. set_scratch(8, x) or an index computed from a constant that was off-by-one or 1-based by mistake.

Common situations: Off-by-one when mapping logical slots to scratch indices; 1-based numbering in user config applied directly as the hardware index; loops over 1..=8 instead of 0..=8.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of embassy-rs/embassy@463a07b963 (2026-09-10). Data as JSON: /api/errors/7686bd0989291cd0. Report an issue: GitHub.

Appendix: source

Thrown at embassy-rp/src/watchdog.rs:143

        let watchdog = pac::WATCHDOG;
        watchdog.ctrl().write(|w| {
            w.set_trigger(true);
        })
    }

    /// Store data in scratch register
    pub fn set_scratch(&mut self, index: usize, value: u32) {
        let watchdog = pac::WATCHDOG;
        match index {
            0 => watchdog.scratch0().write(|w| *w = value),
            1 => watchdog.scratch1().write(|w| *w = value),
            2 => watchdog.scratch2().write(|w| *w = value),
            3 => watchdog.scratch3().write(|w| *w = value),
            4 => watchdog.scratch4().write(|w| *w = value),
            5 => watchdog.scratch5().write(|w| *w = value),
            6 => watchdog.scratch6().write(|w| *w = value),
            7 => watchdog.scratch7().write(|w| *w = value),
            _ => panic!("Invalid watchdog scratch index"),
        }
    }

    /// Read data from scratch register
    pub fn scratch(&mut self, index: usize) -> u32 {
        let watchdog = pac::WATCHDOG;
        match index {
            0 => watchdog.scratch0().read(),
            1 => watchdog.scratch1().read(),
            2 => watchdog.scratch2().read(),
            3 => watchdog.scratch3().read(),
            4 => watchdog.scratch4().read(),
            5 => watchdog.scratch5().read(),
            6 => watchdog.scratch6().read(),
            7 => watchdog.scratch7().read(),
            _ => panic!("Invalid watchdog scratch index"),
        }
    }

View on GitHub (pinned to 463a07b963)