agentleFS
botirkhaltaevbotirkhaltaev/botir-skills/skills/rust-embedded/SKILL.md

rust-embedded

Skill4 starsChanged 3 months agoApache-2.0View on GitHub

Embedded Rust development for bare-metal and no_std environments. Use when targeting microcontrollers, writing HAL drivers, working with peripherals, handling interrupts, or using RTIC/Embassy for real-time systems.

Files
SKILL.md

Rust Embedded Development

Based on The Embedded Rust Book, The Embedonomicon, and Embedded Rust on Espressif.

When to Use This Skill

  • Writing #![no_std] applications for microcontrollers
  • Working with HAL (Hardware Abstraction Layer) crates
  • Configuring linker scripts and memory layouts
  • Handling interrupts and DMA
  • Using RTIC or Embassy for concurrency
  • Targeting ARM Cortex-M, RISC-V, or ESP32

Project Structure

my-firmware/
├── .cargo/config.toml     # target, linker settings
├── Cargo.toml
├── memory.x               # memory layout (flash + RAM regions)
├── build.rs               # optional: extra link args
└── src/
    └── main.rs            # #![no_std] #![no_main]

Minimal no_std Binary

#![no_std]
#![no_main]

use panic_halt as _;  // panic handler (halt on panic)
use cortex_m_rt::entry;

#[entry]
fn main() -> ! {
    // Initialize peripherals
    let dp = pac::Peripherals::take().unwrap();
    let mut led = dp.GPIOA.pin(5).into_push_pull_output();

    loop {
        led.toggle();
        cortex_m::asm::delay(8_000_000);
    }
}

Memory Layout (memory.x)

MEMORY
{
    FLASH : ORIGIN = 0x08000000, LENGTH = 256K
    RAM   : ORIGIN = 0x20000000, LENGTH = 64K
}

.cargo/config.toml

[target.thumbv7em-none-eabihf]
runner = "probe-rs run --chip STM32F303VCTx"

[build]
target = "thumbv7em-none-eabihf"

[env]
DEFMT_LOG = "info"

HAL Layer Architecture

┌─────────────────────────────┐
│  Application Code           │
├─────────────────────────────┤
│  HAL (e.g. stm32f3xx-hal)  │  ← high-level, safe API
├─────────────────────────────┤
│  PAC (e.g. stm32f3)        │  ← auto-generated register access
├─────────────────────────────┤
│  Hardware                   │
└─────────────────────────────┘
  • PAC (Peripheral Access Crate): Generated from SVD files, provides raw register access
  • HAL: Type-safe wrappers implementing embedded-hal traits
  • BSP (Board Support Package): Board-specific pin mappings and initialization

embedded-hal Traits

Portable driver abstraction:

use embedded_hal::digital::OutputPin;
use embedded_hal::i2c::I2c;
use embedded_hal::spi::SpiDevice;
use embedded_hal::delay::DelayNs;

// Write a portable driver
pub struct Sensor<I2C> {
    i2c: I2C,
    addr: u8,
}

impl<I2C: I2c> Sensor<I2C> {
    pub fn read_temp(&mut self) -> Result<f32, I2C::Error> {
        let mut buf = [0u8; 2];
        self.i2c.write_read(self.addr, &[0x00], &mut buf)?;
        Ok(i16::from_be_bytes(buf) as f32 * 0.0625)
    }
}

Interrupts

use cortex_m::peripheral::NVIC;
use stm32f3xx_hal::pac::interrupt;

#[interrupt]
fn TIM2() {
    // Timer interrupt handler
    // Access shared state via critical section or RTIC resources
    cortex_m::interrupt::free(|cs| {
        if let Some(timer) = TIMER.borrow(cs).borrow_mut().as_mut() {
            timer.clear_interrupt(Event::Update);
        }
    });
}

RTIC (Real-Time Interrupt-driven Concurrency)

#[rtic::app(device = stm32f3xx_hal::pac, dispatchers = [SPI1])]
mod app {
    use super::*;

    #[shared]
    struct Shared { led: Led }

    #[local]
    struct Local { timer: Timer }

    #[init]
    fn init(cx: init::Context) -> (Shared, Local) {
        // Setup peripherals
        (Shared { led }, Local { timer })
    }

    #[task(binds = TIM2, local = [timer], shared = [led])]
    fn tick(cx: tick::Context) {
        cx.local.timer.clear_interrupt();
        cx.shared.led.lock(|led| led.toggle());
    }
}

Key Crates

CratePurpose
cortex-mLow-level ARM Cortex-M access
cortex-m-rtRuntime (vector table, entry)
panic-haltPanic handler: infinite loop
defmtEfficient logging for embedded
probe-rsFlashing and debugging tool
embedded-halHardware abstraction traits
rticReal-time concurrency framework
embassyAsync embedded framework

Reference Map

  • references/no-std-basics.md — no_std setup, panic handlers, alloc
  • references/peripherals-hal.md — GPIO, timers, I2C, SPI, UART
  • references/interrupts-concurrency.md — NVIC, critical sections, RTIC, Embassy

Key References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.