liveBook · Manning

Peripherals::take()

As explained in Embedded Software with Rust

Peripherals::take() is a singleton-acquisition function that returns exclusive access to a device's peripheral handles when they have not already been taken.

`Peripherals::take()` acquires an owning structure of peripheral handles once. Later calls report that the peripherals have already been taken. Move each unique, non-`Copy` handle to the module that needs it; share access through explicit methods or scoped borrows rather than creating another handle. section 1.4.2 section 5.3.3

fn main() -> ! {
    let cp = cortex_m::Peripherals::take().unwrap();
    let mut syst = cp.SYST;
    syst.set_reload(8_000_000);
    syst.clear_current();
    syst.enable_counter();
    syst.enable_interrupt();
    loop {}
}

This SysTick example acquires the core peripherals, then moves the timer handle into `syst` for configuration. section 1.4.2

How Embedded Software with Rust covers it

How deep it goes
Explains one-time acquisition, unique non-Copy handles, ownership transfer, and scoped borrowing.
Examples
Demonstrates acquiring core peripherals for SysTick and device peripherals from an STM32F103 PAC.
What you can do after
Apply singleton acquisition to obtain unique peripheral handles and transfer them to the code that needs them.
How it connects
Connects Rust ownership and borrowing with peripheral access crates and SysTick configuration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.