1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Bounded spin-wait helpers.
//!
//! Several HAL drivers busy-wait for hardware to reach a state — reset done,
//! clock stable, PLL locked, FIFO space, peripheral ready, and so on. Without a
//! bound, a misconfigured peripheral (gated clock, held in reset, missing
//! device, wrong DMA request) turns into an unbounded hang.
//!
//! These helpers bound such waits with a *spin count*. The default
//! [`spin_wait`] panics on timeout — a hang-guard for waits the hardware is
//! guaranteed to satisfy once configured. [`try_spin_wait`] reports failure as
//! a `bool` for callers that want to recover instead of aborting, and
//! [`spin_wait_max`] accepts a custom bound per call site for waits whose
//! duration is legitimately variable (e.g. XOSC startup delay).
/// Default maximum spin iterations for [`spin_wait`].
///
/// This is a *hang guard*, not a calibrated timeout: a `spin_loop` iteration
/// takes a few cycles, so 1_000_000 iterations is roughly tens of milliseconds
/// at 150 MHz but several times longer at the boot-time 12 MHz clock. It is
/// sized to sit far above any legitimate hardware settle time while still
/// bounding a stuck peripheral.
///
/// Call sites that need a different bound should use [`spin_wait_max`]
/// instead of relying on this default.
pub const SPIN_TIMEOUT: u32 = 1_000_000;
/// Spin until `cond()` returns `true`, panicking after [`SPIN_TIMEOUT`]
/// iterations.
///
/// `what` is included in the panic message to identify the stalled operation.
///
/// Prefer this for waits the hardware is guaranteed to satisfy once configured
/// (reset done, PLL lock, FIFO space). Use [`try_spin_wait`] when the caller
/// wants to recover from a stuck wait, and [`spin_wait_max`] when the wait can
/// legitimately take far longer than the default bound.
pub
/// Spin until `cond()` returns `true`, returning `false` after
/// [`SPIN_TIMEOUT`] iterations without panicking.
///
/// Use this when the caller wants to handle a stuck wait (report an error,
/// retry, or fall back) instead of aborting the whole program. For example,
/// SPI `transfer`/`read` and UART blocking reads use this to return
/// `Error::Timeout` on a stuck peripheral.
pub
/// Spin until `cond()` returns `true` or `max` iterations elapse, panicking on
/// timeout.
///
/// Use a larger `max` than [`SPIN_TIMEOUT`] for waits whose duration is
/// legitimately variable and can exceed the default bound (e.g. XOSC startup
/// with a raised `STARTUP_DELAY_MULTIPLIER`).
pub