liveBook · Manning

Critical-section

As explained in Embedded Software with Rust

A Rust crate that provides a portable abstraction for entering and executing code within critical sections.

critical-section provides a portable Rust abstraction for entering a critical section and running code while shared state is protected from concurrent interrupt access. Its API supplies a temporary permission token; shared data can be accessed only while that token remains in scope. section 1.4.4 section 4.6.6

Use a critical section when:

  • Main code and an interrupt handler access the same peripheral or state. section 1.4.4
  • A peripheral must be moved into global storage once and later borrowed mutably from multiple contexts. section 4.6.5
  • Direct interrupt disabling would expose unsafe restoration behavior; the abstraction scopes access and restores the permission boundary when the closure ends. section 4.5.5 section 1.4.4

Sharing a timer peripheral

Store the peripheral as Mutex<RefCell<Option<SYST>>>. During initialization, enter a critical section and replace the empty Option with the owned peripheral. Later, enter another critical section before borrowing and mutating it from an interrupt handler. This prevents double initialization, undefined access, and races. section 4.6.5

use cortex_m::interrupt::{Mutex, free};
use core::cell::RefCell;
use cortex_m::peripheral::SYST;
use core::ops::DerefMut;

static G_SYST: Mutex<RefCell<Option<SYST>>> = Mutex::new(RefCell::new(None));

fn main() -> ! {
    let dp = cortex_m::Peripherals::take().unwrap();

    // Transfer ownership into the static global using .replace()
    in a critical section
    free(|cs| {
        G_SYST.borrow(cs).replace(Some(dp.SYST));
    });

    loop {
        cortex_m::asm::wfi();
    }
}

#[exception]
fn SysTick() {
    free(|cs| {
        if let Some(ref mut syst) = G_SYST.borrow(cs).borrow_mut()
        .deref_mut() {
            let _ = syst.has_wrapped();
        }
    });
}

Shows a complete shared-peripheral pattern, including ownership transfer into static storage and protected access from SysTick. section 4.6.5

[dependencies.cortex-m]
version = "*"
features = ["critical-section-single-core"]

Shows the Cortex-M Cargo feature needed to provide the single-core critical-section implementation. section 4.6.6

How Embedded Software with Rust covers it

How deep it goes
Explains scoped permission and peripheral-sharing patterns, while deferring broader framework APIs and advanced use cases.
Examples
Uses shared SYST storage, Mutex<RefCell<Option<T>>>, interrupt masking, and a Cortex-M Cargo feature configuration.
What it assumes
Assumes basic Rust ownership and interrupts, while introducing synchronized shared access and scoped permissions.
What you can do after
Apply it via scoped critical-section APIs to share peripherals safely between firmware contexts.
How it connects
Connects interrupts, ownership, mutexes, interior mutability, atomic critical sections, and async or task frameworks.

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.