Add this skill
npx mdskills install sickn33/arm-cortex-expertExceptional embedded systems expertise with comprehensive platform coverage and critical safety patterns
resources/implementation-playbook.md.Target Platforms
Core Competencies
Advanced Topics
CRITICAL: ARM Cortex-M7 has weakly-ordered memory. The CPU and hardware can reorder register reads/writes relative to other operations.
Symptoms of Missing Barriers:
C/C++: Wrap register access with __DMB() (data memory barrier) before/after reads, __DSB() (data synchronization barrier) after writes. Create helper functions: mmio_read(), mmio_write(), mmio_modify().
Rust: Use cortex_m::asm::dmb() and cortex_m::asm::dsb() around volatile reads/writes. Create macros like safe_read_reg!(), safe_write_reg!(), safe_modify_reg!() that wrap HAL register access.
Why This Matters: M7 reorders memory operations for performance. Without barriers, register writes may not complete before next instruction, or reads return stale cached values.
CRITICAL: ARM Cortex-M7 devices (Teensy 4.x, STM32 F7/H7) have data caches. DMA and CPU can see different data without cache maintenance.
Alignment Requirements (CRITICAL):
Memory Placement Strategies (Best to Worst):
DTCM/SRAM (Non-cacheable, fastest CPU access)
__attribute__((section(".dtcm.bss"))) __attribute__((aligned(32))) static uint8_t buffer[512];#[link_section = ".dtcm"] #[repr(C, align(32))] static mut BUFFER: [u8; 512] = [0; 512];MPU-configured Non-cacheable regions - Configure OCRAM/SRAM regions as non-cacheable via MPU
Cache Maintenance (Last resort - slowest)
arm_dcache_flush_delete() or cortex_m::cache::clean_dcache_by_range()arm_dcache_delete() or cortex_m::cache::invalidate_dcache_by_range()Best practice: Validate MMIO addresses in debug builds using is_valid_mmio_address(addr) checking addr is within valid peripheral ranges (e.g., 0x40000000-0x4FFFFFFF for peripherals, 0xE0000000-0xE00FFFFF for ARM Cortex-M system peripherals). Use #ifdef DEBUG guards and halt on invalid addresses.
Many status registers (especially i.MX RT, STM32) clear by writing 1, not 0:
uint32_t status = mmio_read(&USB1_USBSTS);
mmio_write(&USB1_USBSTS, status); // Write bits back to clear them
Common W1C: USBSTS, PORTSC, CCM status. Wrong: status &= ~bit does nothing on W1C registers.
ā ļø Voltage Tolerances:
Teensy 4.x: FlexSPI dedicated to Flash/PSRAM only ⢠EEPROM emulated (limit writes >> = Mutex::new(RefCell::new(None)); // Access: critical_section::with(|cs| STATE.borrow_ref_mut(cs))
**WRONG:** `static mut` is undefined behavior (data races).
**Atomic Ordering:** `Relaxed` (CPU-only) ⢠`Acquire/Release` (shared state) ⢠`AcqRel` (CAS) ⢠`SeqCst` (rarely needed)
---
## šÆ Interrupt Priorities & NVIC Configuration
**Platform-Specific Priority Levels:**
- **M0/M0+**: 2-4 priority levels (limited)
- **M3/M4/M7**: 8-256 priority levels (configurable)
**Key Principles:**
- **Lower number = higher priority** (e.g., priority 0 preempts priority 1)
- **ISRs at same priority level cannot preempt each other**
- Priority grouping: preemption priority vs sub-priority (M3/M4/M7)
- Reserve highest priorities (0-2) for time-critical operations (DMA, timers)
- Use middle priorities (3-7) for normal peripherals (UART, SPI, I2C)
- Use lowest priorities (8+) for background tasks
**Configuration:**
- C/C++: `NVIC_SetPriority(IRQn, priority)` or `HAL_NVIC_SetPriority()`
- Rust: `NVIC::set_priority()` or use PAC-specific functions
---
## š Critical Sections & Interrupt Masking
**Purpose:** Protect shared data from concurrent access by ISRs and main code.
**C/C++:**
```cpp
__disable_irq(); /* critical section */ __enable_irq(); // Blocks all
// M3/M4/M7: Mask only lower-priority interrupts
uint32_t basepri = __get_BASEPRI();
__set_BASEPRI(priority_threshold FPCCR` (clear LSPEN bit) in hard real-time systems or when ISRs always use FPU.
---
## š”ļø Stack Overflow Protection
**MPU Guard Pages (Best):** Configure no-access MPU region below stack. Triggers MemManage fault on M3/M4/M7. Limited on M0/M0+.
**Canary Values (Portable):** Magic value (e.g., `0xDEADBEEF`) at stack bottom, check periodically.
**Watchdog:** Indirect detection via timeout, provides recovery. **Best:** MPU guard pages, else canary + watchdog.
---
## š Workflow
1. **Clarify Requirements** ā target platform, peripheral type, protocol details (speed, mode, packet size)
2. **Design Driver Skeleton** ā constants, structs, compile-time config
3. **Implement Core** ā init(), ISR handlers, buffer logic, user-facing API
4. **Validate** ā example usage + notes on timing, latency, throughput
5. **Optimize** ā suggest DMA, interrupt priorities, or RTOS tasks if needed
6. **Iterate** ā refine with improved versions as hardware interaction feedback is provided
---
## š Example: SPI Driver for External Sensor
**Pattern:** Create non-blocking SPI drivers with transaction-based read/write:
- Configure SPI (clock speed, mode, bit order)
- Use CS pin control with proper timing
- Abstract register read/write operations
- Example: `sensorReadRegister(0x0F)` for WHO_AM_I
- For high throughput (>500 kHz), use DMA transfers
**Platform-specific APIs:**
- **Teensy 4.x**: `SPI.beginTransaction(SPISettings(speed, order, mode))` ā `SPI.transfer(data)` ā `SPI.endTransaction()`
- **STM32**: `HAL_SPI_Transmit()` / `HAL_SPI_Receive()` or LL drivers
- **nRF52**: `nrfx_spi_xfer()` or `nrf_drv_spi_transfer()`
- **SAMD**: Configure SERCOM in SPI master mode with `SERCOM_SPI_MODE_MASTER`
Install via CLI
npx mdskills install sickn33/arm-cortex-expertArm Cortex Expert is a free, open-source AI agent skill. >
Install Arm Cortex Expert with a single command:
npx mdskills install sickn33/arm-cortex-expertThis downloads the skill files into your project and your AI agent picks them up automatically.
Arm Cortex Expert works with Claude Code, Claude Desktop, Cursor, Vscode Copilot, Windsurf, Continue Dev, Codex, Gemini Cli, Amp, Roo Code, Goose, Opencode, Trae, Qodo, Command Code. Skills use the open SKILL.md format which is compatible with any AI coding agent that reads markdown instructions.