j4flmao/agent-skills/bare-metal-rust
Claude Code and agent skill by j4flmao/agent-skills
- community
Checking that the SKILL.md file is still in the repository.
Use this skill when the user says 'embedded rust', 'no_std', 'cortex-m rust', 'embedded-hal', 'PAC', 'svd2rust', 'RTIC', 'Embassy', 'probe-rs', 'defmt', 'rust on microcontroller'. Builds and reviews `#![no_std]` firmware in Rust: crate layers (PAC/HAL/BSP), safe sharing between interrupts and main code, concurrency frameworks (RTIC, Embassy), allocation-free collections, and the flash/debug toolchain. Do NOT use for: general Rust application code (use lang-rust), C firmware (use c-cpp-patterns), RTOS theory (use rtos-fundamentals).
Install
npx vskill@latest install j4flmao/agent-skills/bare-metal-rustSKILL.md
Bare-Metal Rust
Purpose
Rust removes whole classes of firmware bugs (buffer overruns, use-after-free, data races between ISR and main) at compile time while compiling to code comparable with C. This skill shows how to structure a no_std project, share state with interrupts soundly, pick between RTIC, Embassy and a plain superloop, and build/flash/debug with the standard toolchain.
Agent Protocol
Trigger
"embedded rust", "no_std", "cortex-m rust", "embedded-hal", "PAC", "svd2rust", "RTIC", "Embassy", "probe-rs", "defmt".
Input Context
- MCU family and existing HAL crate (e.g.
stm32f4xx-hal,nrf-hal,rp2040-hal/embassy-rp,esp-hal) - Concurrency model wanted (superloop, RTIC, Embassy async)
- Memory budget and whether an allocator is acceptable
Output Artifact
Rust code (main.rs, Cargo.toml, memory.x, .cargo/config.toml) or a review. No file unless requested.
Response Format
1. The no_std Environment
#![no_std]links onlycore(and optionallyallocif you provide a#[global_allocator]). No threads, files, or heap by default.#![no_main]+cortex-m-rt's#[entry]provide the reset handler, vector table andmemory.xlinker layout.- You must supply a panic handler:
panic-halt,panic-probe(with defmt), or your own#[panic_handler].
main returns ! — firmware never returns.
2. Crate Layers
| Layer | What it is | Example | |
|---|---|---|---|
| PAC (Peripheral Access Crate) | Generated by svd2rust from the vendor SVD; register-level, mostly unsafe writes of raw bits are wrapped in typed field APIs | stm32f4, nrf52840-pac | |
| HAL | Safe drivers implementing embedded-hal traits | stm32f4xx-hal, nrf52840-hal, esp-hal | |
embedded-hal 1.0 | Portable traits (digital::OutputPin, spi::SpiDevice, i2c::I2c, delay::DelayNs); embedded-hal-async for async | stable since Jan 2024 | |
| BSP | Board pin mapping on top of the HAL | microbit-v2 | |
| Drivers | Device drivers generic over the traits | sensor crates |
Write drivers against embedded-hal traits so they run on any HAL.
3. Sharing State with Interrupts
static mut requires unsafe for every access, and in the 2024 edition creating references to it is denied by default (static_mut_refs). Use one of:
- critical-section Mutex — portable, short critical sections:
The CriticalSection token proves interrupts are masked (single core) while borrowing. Provide an implementation via cortex-m's critical-section-single-core feature or your HAL.
- Atomics (
core::sync::atomic) for single words — no masking. Cortex-M0/M0+ lacks CAS (fetch_addetc.); useportable-atomicthere. - RTIC resources — compile-time priority-ceiling locking (below).
- Embassy channels/signals (
embassy-sync) between async tasks and interrupts.
4. Concurrency Frameworks
| Option | Model | Choose when | |
|---|---|---|---|
| Superloop + interrupts | loop { wfi } + ISRs | very small programs | |
| RTIC 2 | Hardware tasks bound to interrupts, software tasks, Stack Resource Policy (priority ceiling) — deadlock-free, no runtime scheduler | hard real-time with static priorities; analysable timing | |
| Embassy | async/await executor, async HALs (embassy-stm32, embassy-nrf, embassy-rp), timers, networking (embassy-net), USB, BLE via nrf-softdevice/trouble | I/O-heavy firmware, many concurrent waits, low power | |
| RTOS bindings | FreeRTOS/Zephyr with Rust | mixing with existing C RTOS code |
5. Memory Without a Heap
heapless::{Vec, String, spsc::Queue, pool}— fixed capacity in the type (heapless::Vec<u8, 64>).static_cell::StaticCellto obtain&'static mutbuffers once at startup (common with Embassy).- If
allocis truly needed, useembedded-allocwith a static arena and handle allocation failure. - Check stack usage:
flip-linkputs the stack below.data/.bssso overflow faults instead of silently corrupting statics.
6. Toolchain
rustup target add thumbv7em-none-eabihf;cargo run --releaseflashes and streams logs.probe-rs(flash, RTT, GDB server) replaced most OpenOCD workflows;defmt+defmt-rttgive compact deferred-formatted logging.- Size:
cargo size --release(cargo-binutils); setopt-level = "s"or"z",lto = true,codegen-units = 1,panic = "abort"in the release profile.
7. Anti-Patterns
| Anti-pattern | Problem | Fix | |
|---|---|---|---|
static mut + unsafe everywhere | data races the compiler cannot see | Mutex<RefCell<_>>, atomics, RTIC/Embassy | |
Long work inside critical_section::with | adds to interrupt latency | copy out, process outside | |
unwrap() on fallible HAL calls in production | panic = reset/halt | handle errors, log with defmt | |
| Blocking delays in Embassy tasks | stalls the whole executor | Timer::after(...).await | |
| Writing PAC registers alongside a HAL that owns the peripheral | breaks HAL invariants | let one owner configure each peripheral |
8. Checklist
- [ ]
#![no_std],#![no_main], panic handler chosen deliberately - [ ] Peripherals taken once; ownership moved into drivers/tasks
- [ ] Every ISR-shared value behind a sound primitive (no
static mut) - [ ] No heap, or a bounded allocator with failure handling
- [ ] Release profile optimised; size and stack measured;
flip-linkenabled - [ ] Logs via defmt; flashing/debugging via probe-rs
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/embedded-systems/bare-metal-rust/SKILL.md
Popularity
- Repository stars
- 28
- Repository forks
- 1
- 7-day trend score
- 22
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/j4flmao/agent-skills/bare-metal-rust)