//! Top-level PAC-style bring-up entry for {{ ir.board.name }}.
//!
//! Call [`init`] once from your application reset handler before touching
//! any peripheral. It performs the following in order:
//!
//! 1. [`super::clocks::init`] — ungate `SYSTEM` clocks for used peripherals
//!    and release them from reset.
//! 2. [`super::io_mux::init`] — route each board pin via IO MUX or the GPIO
//!    matrix per the `db/boards/{{ board_stem }}.yaml` pin table.
//! 3. [`super::peripherals::init`] — per-peripheral init (UART0 real when
//!    it is the console, others stubbed).
//!
//! The generated code uses the `{{ ir.chip.pac_crate }}` PAC crate for all
//! register access; add it as a dependency of the consuming crate. Sibling
//! references use `super::` so this module can be either a crate root
//! (when consumed directly) or a child module of the host crate.

#[allow(unused_imports)]
pub use {{ ir.chip.pac_crate }}::*;

{% if ir.chip.pac_vintage == "modern" %}
#[allow(unused_imports)]
use {{ ir.chip.pac_crate }} as pac;

/// Local aggregate `Peripherals` shim for `pac_vintage: modern` chips.
///
/// `{{ ir.chip.pac_crate }}` is generated by svd2rust 0.37+, which dropped
/// the upstream `pub struct Peripherals { ... }` aggregate in favour of
/// per-peripheral `pac::FOO::steal()`. This shim re-creates the legacy
/// aggregate shape so `clocks.rs` / `io_mux.rs` / `peripherals.rs` can
/// keep their `let p = unsafe {{ '{' }} Peripherals::steal() {{ '}' }};`
/// + `p.UART0.foo()` field-access pattern unchanged across vintages.
///
/// The field set is computed at generation time from the chip IR
/// (`IO_MUX`, `GPIO`, every peripheral the board uses, plus every
/// register-block referenced by a used peripheral's clock gate).
#[allow(non_snake_case, missing_docs)]
pub struct Peripherals {
    {% for inst in shim_instances %}
    pub {{ inst }}: pac::{{ inst }},
    {% endfor %}
}

impl Peripherals {
    /// Construct a `Peripherals` shim by stealing each component instance.
    ///
    /// # Safety
    ///
    /// Must obey the same invariants as each underlying
    /// `pac::FOO::steal()` — no aliasing live `&mut` references to the
    /// same MMIO register block, and (per svd2rust) the caller is
    /// responsible for coordinating with any other code that may have
    /// stolen the same peripheral instance.
    #[inline]
    pub unsafe fn steal() -> Self {
        unsafe {
            Self {
                {% for inst in shim_instances %}
                {{ inst }}: pac::{{ inst }}::steal(),
                {% endfor %}
            }
        }
    }
}
{% endif %}

/// Bring up the board.
///
/// Safety: must be called exactly once from a single-threaded reset handler
/// before any other code touches `{{ ir.chip.pac_crate }}::Peripherals`.
pub fn init() {
    super::clocks::init();
    super::io_mux::init();
    super::peripherals::init();
}
