Skip to main content
skill CX Codex

interrupts-and-exceptions-baremetal — Claude Code skill

Guides coding agents through Cortex-M NVIC setup, non-blocking ISR design, and HardFault debugging. Useful when building bare-metal firmware without an RTOS.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add mohitmishra786/low-level-dev-skills --skill interrupts-and-exceptions-baremetal -a claude-code
Open on GitHub

What interrupts-and-exceptions-baremetal does

The skill provides guidance for ARM Cortex-M interrupt handling: from configuring NVIC_SetPriority to capturing fault frames. It explains stacking mechanics during exceptions, tail-chaining behavior, and FPU latency.

Included are non-blocking ISR patterns with ring buffers and critical sections managed via PRIMASK or BASEPRI. It also analyzes SCB->CFSR and SCB->HFSR registers to pinpoint memory faults. Cycle counts can be measured directly with DWT->CYCCNT.

Blocking calls inside an ISR are unacceptable. Functions like printf cause priority inversion and data loss, so the skill defers heavy tasks to the main loop.

What it helps with

  • Configure NVIC priorities and priority grouping via CMSIS
  • Implement non-blocking ISRs using ring buffers
  • Decode HardFault causes from stacked registers and fault status flags
  • Protect critical code sections using PRIMASK or BASEPRI

Example prompts for the agent

  • /interrupts-and-exceptions-baremetal Configure NVIC priority for UART2 vs SysTick on STM32F4
  • /interrupts-and-exceptions-baremetal Decode HardFault stacked PC and CFSR register values

How to install interrupts-and-exceptions-baremetal

1Skills CLI (Vercel)

npx skills add mohitmishra786/low-level-dev-skills --skill interrupts-and-exceptions-baremetal -a claude-code

2Manually: put the files in place

~/.claude/skills/interrupts-and-exceptions-baremetal/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/interrupts-and-exceptions-baremetal/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about interrupts-and-exceptions-baremetal

How do I install interrupts-and-exceptions-baremetal in Claude Code?

Run npx skills add mohitmishra786/low-level-dev-skills --skill interrupts-and-exceptions-baremetal -a claude-code. The file installs to ~/.claude/skills/interrupts-and-exceptions-baremetal/SKILL.md.

What does interrupts-and-exceptions-baremetal do?

It instructs agents on bare-metal ARM Cortex-M exception handling, covering NVIC register configuration, latency tuning, and ISR safety rules.

How does the skill help debug a HardFault?

It supplies an assembly wrapper that inspects the active stack pointer to extract the program counter, then analyzes CFSR and HFSR registers in GDB.

Source text

SKILL.md · MIT
---
name: interrupts-and-exceptions-baremetal
description: Bare-metal interrupt and exception skill for Cortex-M NVIC. Use when writing ISRs, configuring priorities, handling HardFault, or measuring interrupt latency. Activates on queries about NVIC, ISR, vector table, HardFault, tail-chaining, or interrupt priority.
---

# Interrupts and Exceptions (Bare-Metal)

## Purpose

Guide agents through bare-metal interrupt handling on ARM Cortex-M: NVIC configuration, ISR writing rules, exception handlers (HardFault, BusFault), priority grouping, nesting, tail-chaining, and latency considerations.

## When to Use

- Configuring peripheral IRQ priorities
- Writing ISRs that must not block
- Debugging HardFault after enabling interrupts
- Sharing data between ISR and main loop
- Optimizing interrupt latency

## Workflow

### 1. NVIC overview (Cortex-M)

```
Exception / IRQ flow
├── NVIC receives IRQ (priority compare with BASEPRI/PRIMask)
├── Stacking: automatic save r0-r3, r12, lr, pc, psr
├── Branch to handler from vector table
├── Handler runs (should be short)
└── Unstack and return — tail-chain if another IRQ pending
```

### 2. Enable and prioritize an IRQ

```c
#include "stm32f4xx.h"  /* CMSIS device header */

void uart_irq_init(void) {
    NVIC_SetPriority(USART2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 2, 0));
    NVIC_EnableIRQ(USART2_IRQn);
}
```

Priority: lower numeric value = higher urgency (on most Cortex-M implementations). Check vendor docs for grouping bits.

### 3. ISR template

```c
void USART2_IRQHandler(void) {
    if (USART2->SR & USART_SR_RXNE) {
        uint8_t b = (uint8_t)USART2->DR;  /* read clears RXNE */
        ringbuf_push(b);
    }
    if (USART2->SR & USART_SR_ORE) {
        (void)USART2->DR;  /* clear overrun */
    }
}
```

ISR rules:
- No blocking calls (`printf`, `malloc`, long loops)
- Minimize work — defer to main via flag/ring buffer
- Clear interrupt flags per datasheet (read-to-clear vs write-1-clear)

### 4. Critical sections

```c
uint32_t primask = __get_PRIMASK();
__disable_irq();
/* atomic section */
__set_PRIMASK(primask);
```

Or raise `BASEPRI` to mask lower-priority IRQs only.

### 5. HardFault handler

```c
void HardFault_Handler(void) {
    __asm volatile(
        "tst lr, #4\n"
        "ite eq\n"
        "mrseq r0, msp\n"
        "mrsne r0, psp\n"
        "b hard_fault_c\n"
    );
}

void hard_fault_c(uint32_t *stack) {
    uint32_t r0  = stack[0];
    uint32_t pc  = stack[6];
    uint32_t psr = stack[7];
    /* log pc — GDB: info registers, bt */
    while (1);
}
```

Decode CFSR/HFSR registers for fault cause:

```c
volatile uint32_t cfsr = SCB->CFSR;
volatile uint32_t hfsr = SCB->HFSR;
volatile uint32_t bfar = SCB->BFAR;
```

### 6. Latency and tail-chaining

| Factor | Impact |
|--------|--------|
| Higher priority IRQ | Preempts lower |
| Tail-chaining | Back-to-back IRQs skip unstack/restack |
| FPU context | Lazy stacking adds latency on M4F/M7 |
| Long ISRs | Starves other IRQs and main |

Measure with GPIO toggle + scope, or DWT cycle counter (`DWT->CYCCNT`).

### 7. Agent usage examples

```
/interrupts-and-exceptions-baremetal Configure NVIC priority for UART vs SysTick
/interrupts-and-exceptions-baremetal Decode HardFault stacked PC with GDB
```

## Common Problems

| Symptom | Cause | Fix |
|---------|-------|-----|
| IRQ never fires | NVIC not enabled or IRQ masked | `NVIC_EnableIRQ`; check `PRIMASK` |
| Spurious re-entry | Flag not cleared | Clear per RM (ORE needs DR read) |
| HardFault in ISR | Stack overflow | Increase `_estack`; check ISR stack |
| Lost bytes | ISR too slow | Ring buffer + higher IRQ priority |
| Priority inversion | Long critical section | Shorten `__disable_irq` window |

## Related Skills

- `skills/baremetal/baremetal-startup` — vector table entries
- `skills/baremetal/uart-serial-baremetal` — UART IRQ handlers
- `skills/embedded/openocd-jtag` — GDB breakpoint in ISR
- `skills/debuggers/gdb` — examine fault stack frame
- `skills/low-level-programming/assembly-arm` — fault handler asm

© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills

Similar skills

All skills for Programming languages →

From the same set90

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor