As a Rust firmware lead, how do you choose between bare interrupt handlers with critical sections, RTIC 2 and an Embassy async executor for a new product?
answer
- latency budget first
- who schedules: NVIC or executor
- compile-time ceilings versus await points
- HAL ecosystem and team fluency
- one sanctioned set of primitives
basics
~20 sChoose by latency budget, I/O complexity and ecosystem. Bare handlers suit small firmware; RTIC 2 suits hard priorities, with hardware-scheduled tasks and compile-time lock ceilings; Embassy suits I/O-heavy products, with async drivers but cooperative scheduling within each executor.
solid answer
~50 sI start from the worst-case latency each interrupt must meet and how much of the firmware is sequential I/O. Bare `#[interrupt]` handlers with atomics and critical sections fit a small device with a few events, but every new shared value is another review item and every critical section delays all interrupts. RTIC 2 maps tasks onto NVIC priorities and computes lock ceilings at compile time, so preemption, race freedom and deadlock freedom are proved by the macro; it relies on external HALs and assumes a single core, and RTIC 2 adds async software tasks. Embassy makes multi-step protocols plain `async fn`s on async HAL drivers, but tasks on one executor are cooperative, so latency is bounded by the longest stretch between `.await` points unless you add `InterruptExecutor`s. Then I set one rule set: no module-level `static mut`, short handlers, a written priority map.
go deeper
Recall the three options: bare interrupt handlers with critical sections, RTIC tasks on interrupt priorities, and Embassy async tasks on an executor.
Explain who schedules in each model: the NVIC alone, the NVIC with compile-time ceilings, or a cooperative executor that sleeps on WFE.
Show how latency is bounded in each model, by priority ceilings in RTIC and by await points in Embassy, and how each one's typical bugs show up.
Own the trade-off: latency budget, I/O shape, HAL ecosystem and team fluency, then the written rules and measured numbers that make the choice reviewable.
## The decision a lead actually owns Every firmware product has to answer one question early: **who decides what runs next?** In Rust there are three common answers, and each changes how the team writes, reviews and tests code for the life of the product. - **Bare handlers.** `#[interrupt]` and `#[exception]` functions do the urgent work; a main loop or superloop does the rest. Shared state goes through atomics or `critical_section::Mutex`. The hardware's interrupt controller is the only scheduler. - **RTIC 2.** Tasks are interrupt handlers with static priorities. The `#[rtic::app]` macro knows every task's priority and resources, computes each resource's ceiling at compile time and generates `lock` implementations that raise BASEPRI (or mask NVIC lines on Armv6-M). This is the Stack Resource Policy: race-free, deadlock-free, one shared stack. RTIC 2 also runs `async fn` software tasks on executors it generates on spare interrupts. - **Embassy.** Tasks are `async fn`s on a cooperative executor that sleeps the core with WFE when idle. Embassy's HAL crates provide async drivers, so "send a command, wait for the reply, time out after 10 ms" is straight-line code. Priorities come from running more executors, such as an `InterruptExecutor` per level. ## Criteria, in the order I weigh them 1. **Worst-case latency.** If a control loop must react within microseconds, I want hardware preemption with bounded blocking. RTIC gives that by construction: a lock blocks only tasks at or below its ceiling. In Embassy, a ready task waits for the running task's next `.await`, so its latency is the longest stretch of code any task on that executor runs without yielding. Time-critical work goes into a higher-priority executor or a hand-written handler, which Embassy supports alongside its own. 2. **Shape of the work.** Firmware that is mostly multi-step I/O (radio stacks, USB, sensor protocols with timeouts) is far shorter as async code. Firmware that is mostly periodic control with a few events fits RTIC hardware tasks or bare handlers. 3. **Ecosystem.** Embassy's HALs for its supported chip families come with async drivers and a time driver. RTIC is HAL-agnostic and relies on the chip's PAC and an external HAL. Bare handlers need only the PAC. 4. **Team fluency and review cost.** Async Rust brings its own failure modes: a blocking call that stalls an executor, a `Signal` that coalesces events, a raw mutex chosen for the wrong context. RTIC moves mistakes into compile errors (E0614 for an unlocked shared resource, E0728 for awaiting inside a lock), at the price of a macro DSL. Bare handlers have the least machinery and the most manual reasoning. 5. **Cores and power.** RTIC models a single-core application. A dual-core part needs a multicore critical-section implementation in any design. Both can sleep when idle: the Embassy thread-mode executor through WFE, an RTIC app from its `idle` task. | | Bare handlers | RTIC 2 | Embassy | |---|---|---|---| | Scheduling | NVIC plus your main loop | NVIC, static priorities | cooperative executors | | Shared state | atomics, critical sections | `#[shared]` plus `lock`, `#[local]` | `embassy_sync` primitives with a raw mutex | | Guarantees | what review finds | SRP checked at compile time | Rust's type checks plus discipline about blocking | | Fits | small, event-light devices | hard priorities, control loops | I/O-heavy, protocol-heavy products | ## Rules I set whichever model wins - **No module-level `static mut`** for shared state; handler-local `static mut`, which `cortex-m-rt` turns into `&mut`, is allowed. - **One sanctioned set of primitives** per model, written down: which raw mutex in Embassy, when `#[lock_free]` is acceptable in RTIC, which atomics exist on the chosen core. - **Short handlers and short critical sections**, with the slow work outside. - **A priority map** in the repository: every interrupt, its priority, its latency budget, and what it shares. - **Logic tested on the host** by keeping protocol and control code free of interrupt glue, so the concurrency layer stays thin. ## When to revisit The choice is expensive to reverse, so I record the measured numbers behind it: worst-case handler latency, idle current and code size. A missed deadline, a move to a dual-core part or a protocol stack that only exists in async form are the triggers for reopening it, rather than taste.
- When is a superloop with bare handlers still the right choice?When the device has a handful of events, little sequential I/O and a small team that can review every shared static. It needs no framework, the code size is minimal and the behaviour is easy to trace on a logic analyser. The price is manual discipline: each new shared value needs an atomic or a critical section, and long critical sections quietly add latency everywhere.
- How do you keep interrupt latency bounded in an Embassy design?Keep handlers tiny and make them only signal or enqueue. Keep long computations off the shared executor or break them up with awaits, put time-critical tasks on a higher-priority `InterruptExecutor`, and use a hand-written handler for the few paths that cannot wait for an executor at all. Measure the worst case on hardware rather than trusting averages.
- Async firmware always has lower latency than priority-based interrupt scheduling.
- RTIC is a time-slicing RTOS kernel with a software scheduler.
- Critical sections are free on a single-core chip, so wrap everything in them.
- Embassy cannot coexist with hand-written interrupt handlers.
- Choosing RTIC rules out async code entirely.