Domain Embedded

作者 actionbook5c40d3ad7851无许可证1.5K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库6周前更新

Use when developing embedded/no_std Rust. Keywords: embedded, no_std, microcontroller, MCU, ARM, RISC-V, bare metal, firmware, HAL, PAC, RTIC, embassy, interrupt, DMA, peripheral, GPIO, SPI, I2C, UART, embedded-hal, cortex-m, esp32, stm32, nrf, 嵌入式, 单片机, 固件, 裸机

AI 生成的概览

指导 Rust 嵌入式与 no_std 开发,涵盖约束、框架、crate 与硬件模式。

功能
该技能为编写嵌入式与 no_std Rust(例如微控制器固件)提供领域指导。它列出无堆分配、中断安全与外设所有权等约束,并对应到 heapless 集合、临界区与 HAL 单例等 Rust 模式。它还比较框架(RTIC、Embassy、裸机)、列出常用 crate,并给出静态外设代码示例与常见错误。产出的是参考性说明,而非文件或脚本。
适用场景
适用于开发嵌入式或裸机 Rust,包括面向 ARM、RISC-V、STM32、ESP32、nRF 等 MCU 的 no_std 固件。涉及 HAL、PAC、中断、DMA 或外设,或在 RTIC、Embassy 与裸机方案之间做选择时也适用。
运行要求
无需脚本或工具,仅为说明性内容。它假定处于 Rust 嵌入式项目环境,其注入的上下文会在存在时读取 .cargo/config.toml。

Project Context (Auto-Injected)

Target configuration: !cat .cargo/config.toml 2>/dev/null || echo "No .cargo/config.toml found"


Embedded Domain

Layer 3: Domain Constraints

Domain Constraints → Design Implications

Domain RuleDesign ConstraintRust Implication
No heapStack allocationheapless, no Box/Vec
No stdCore only#![no_std]
Real-timePredictable timingNo dynamic alloc
Resource limitedMinimal memoryStatic buffers
Hardware safetySafe peripheral accessHAL + ownership
Interrupt safeNo blocking in ISRAtomic, critical sections

Critical Constraints

No Dynamic Allocation

RULE: Cannot use heap (no allocator)WHY: Deterministic memory, no OOMRUST: heapless::Vec<T, N>, arrays

Interrupt Safety

RULE: Shared state must be interrupt-safeWHY: ISR can preempt at any timeRUST: Mutex<RefCell<T>> + critical section

Hardware Ownership

RULE: Peripherals must have clear ownershipWHY: Prevent conflicting accessRUST: HAL takes ownership, singletons

Trace Down ↓

From constraints to design (Layer 2):

"Need no_std compatible data structures"    ↓ m02-resource: heapless collections    ↓ Static sizing: heapless::Vec<T, N>
"Need interrupt-safe state"    ↓ m03-mutability: Mutex<RefCell<Option<T>>>    ↓ m07-concurrency: Critical sections
"Need peripheral ownership"    ↓ m01-ownership: Singleton pattern    ↓ m12-lifecycle: RAII for hardware

Layer Stack

LayerExamplesPurpose
PACstm32f4, esp32c3Register access
HALstm32f4xx-halHardware abstraction
FrameworkRTIC, EmbassyConcurrency
Traitsembedded-halPortable drivers

Framework Comparison

FrameworkStyleBest For
RTICPriority-basedInterrupt-driven apps
EmbassyAsyncComplex state machines
Bare metalManualSimple apps

Key Crates

PurposeCrate
Runtime (ARM)cortex-m-rt
Panic handlerpanic-halt, panic-probe
Collectionsheapless
HAL traitsembedded-hal
Loggingdefmt
Flash/debugprobe-run

Design Patterns

PatternPurposeImplementation
no_std setupBare metal#![no_std] + #![no_main]
Entry pointStartup#[entry] or embassy
Static stateISR accessMutex<RefCell<Option<T>>>
Fixed buffersNo heapheapless::Vec<T, N>

Code Pattern: Static Peripheral

rust
#![no_std]#![no_main]
use cortex_m::interrupt::{self, Mutex};use core::cell::RefCell;
static LED: Mutex<RefCell<Option<Led>>> = Mutex::new(RefCell::new(None));
#[entry]fn main() -> ! {    let dp = pac::Peripherals::take().unwrap();    let led = Led::new(dp.GPIOA);
    interrupt::free(|cs| {        LED.borrow(cs).replace(Some(led));    });
    loop {        interrupt::free(|cs| {            if let Some(led) = LED.borrow(cs).borrow_mut().as_mut() {                led.toggle();            }        });    }}

Common Mistakes

MistakeDomain ViolationFix
Using VecHeap allocationheapless::Vec
No critical sectionRace with ISRMutex + interrupt::free
Blocking in ISRMissed interruptsDefer to main loop
Unsafe peripheralHardware conflictHAL ownership

Trace to Layer 1

ConstraintLayer 2 PatternLayer 1 Implementation
No heapStatic collectionsheapless::Vec<T, N>
ISR safetyCritical sectionsMutex<RefCell<T>>
Hardware ownershipSingletontake().unwrap()
no_stdCore-only#![no_std], #![no_main]

Related Skills

WhenSee
Static memorym02-resource
Interior mutabilitym03-mutability
Interrupt patternsm07-concurrency
Unsafe for hardwareunsafe-checker

来源与署名

来源:actionbook/rust-skills位于skills/domain-embedded提交5c40d3a

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架