domain-embedded

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Project Context (Auto-Injected)

项目上下文(自动注入)

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

目标配置: !
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

领域规则设计约束Rust 实现要求
无堆内存栈分配heapless、禁止使用Box/Vec
无标准库仅使用Core库#![no_std]
实时性可预测时序禁止动态分配
资源受限最小化内存占用静态缓冲区
硬件安全性安全的外设访问HAL + 所有权机制
中断安全中断服务程序(ISR)中禁止阻塞原子操作、临界区

Critical Constraints

关键约束

No Dynamic Allocation

禁止动态分配

RULE: Cannot use heap (no allocator)
WHY: Deterministic memory, no OOM
RUST: heapless::Vec<T, N>, arrays
RULE: 不能使用堆内存(无分配器)
WHY: 内存使用可确定,避免内存不足(OOM)
RUST: heapless::Vec<T, N>、数组

Interrupt Safety

中断安全

RULE: Shared state must be interrupt-safe
WHY: ISR can preempt at any time
RUST: Mutex<RefCell<T>> + critical section
RULE: 共享状态必须保证中断安全
WHY: ISR可随时抢占主线程
RUST: Mutex<RefCell<T>> + 临界区

Hardware Ownership

硬件所有权

RULE: Peripherals must have clear ownership
WHY: Prevent conflicting access
RUST: HAL takes ownership, singletons

RULE: 外设必须有明确的所有权
WHY: 防止冲突访问
RUST: HAL获取所有权、单例模式

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

从约束到设计(第二层):
"需要兼容no_std的数据结构"
    ↓ m02-resource: heapless集合
    ↓ 静态大小:heapless::Vec<T, N>

"需要中断安全的状态管理"
    ↓ m03-mutability: Mutex<RefCell<Option<T>>>
    ↓ m07-concurrency: 临界区

"需要外设所有权管理"
    ↓ m01-ownership: 单例模式
    ↓ m12-lifecycle: 硬件的RAII机制

Layer Stack

层级栈

LayerExamplesPurpose
PACstm32f4, esp32c3Register access
HALstm32f4xx-halHardware abstraction
FrameworkRTIC, EmbassyConcurrency
Traitsembedded-halPortable drivers
层级示例用途
PACstm32f4、esp32c3寄存器访问
HALstm32f4xx-hal硬件抽象
框架RTIC、Embassy并发处理
特征embedded-hal可移植驱动

Framework Comparison

框架对比

FrameworkStyleBest For
RTICPriority-basedInterrupt-driven apps
EmbassyAsyncComplex state machines
Bare metalManualSimple apps
框架风格最佳适用场景
RTIC基于优先级中断驱动型应用
Embassy异步式复杂状态机
裸机手动控制简单应用

Key Crates

核心依赖库

PurposeCrate
Runtime (ARM)cortex-m-rt
Panic handlerpanic-halt, panic-probe
Collectionsheapless
HAL traitsembedded-hal
Loggingdefmt
Flash/debugprobe-run
用途依赖库
运行时(ARM)cortex-m-rt
Panic处理程序panic-halt、panic-probe
集合类型heapless
HAL特征embedded-hal
日志defmt
闪存/调试probe-run

Design Patterns

设计模式

PatternPurposeImplementation
no_std setupBare metal
#![no_std]
+
#![no_main]
Entry pointStartup
#[entry]
or embassy
Static stateISR access
Mutex<RefCell<Option<T>>>
Fixed buffersNo heap
heapless::Vec<T, N>
模式用途实现方式
no_std环境搭建裸机开发
#![no_std]
+
#![no_main]
入口点启动程序
#[entry]
或 Embassy
静态状态ISR访问
Mutex<RefCell<Option<T>>>
固定缓冲区无堆内存
heapless::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();
            }
        });
    }
}

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

错误违反的领域规则修复方案
使用Vec堆内存分配改用heapless::Vec
未使用临界区与ISR竞争Mutex + interrupt::free
ISR中阻塞错过中断延迟到主循环处理
不安全的外设访问硬件冲突使用HAL的所有权机制

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]

约束第二层模式第一层实现
无堆内存静态集合heapless::Vec<T, N>
ISR安全临界区Mutex<RefCell<T>>
硬件所有权单例模式take().unwrap()
no_std仅使用Core库#![no_std]、#![no_main]

Related Skills

相关技能

WhenSee
Static memorym02-resource
Interior mutabilitym03-mutability
Interrupt patternsm07-concurrency
Unsafe for hardwareunsafe-checker
场景参考
静态内存m02-resource
内部可变性m03-mutability
中断模式m07-concurrency
硬件相关不安全代码unsafe-checker