/interrupts-and-exceptions-baremetal
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.
$ npx -y skills add mohitmishra786/low-level-dev-skills --skill interrupts-and-exceptions-baremetal --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/interrupts-and-exceptions-baremetal
Context preview
The summary Claude sees to decide when to auto-load this skill.
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.
SKILL.md
interrupts-and-exceptions-baremetal.SKILL.mdname: 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
#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
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
uint32_t primask = __get_PRIMASK();
__disable_irq();
/* atomic section */
__set_PRIMASK(primask);
Or raise `BASEPRI` to mask lower-priority IRQs only.
5. HardFault handler
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:
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
Read more
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
#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
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
uint32_t primask = __get_PRIMASK(); __disable_irq(); /* atomic section */ __set_PRIMASK(primask);
Or raise `BASEPRI` to mask lower-priority IRQs only.
5. HardFault handler
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:
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
A curated suite of AI agent skills for systems and low-level programming — C/C++, Rust, Zig, GPU, bare-metal firmware, Linux kernel/driver development, computer architecture, compiler internals, HPC, and more.
Repo: mohitmishra786/low-level-dev-skills
Other skills on low-level-dev-skills.
- /custom-allocators
Custom allocator skill for memory allocation strategies. Use when implementing pool/slab/arena allocators, tuning jemalloc/mimalloc, writing Rust GlobalAlloc, or benchmarking allocator performance. Activates on queries about jemalloc, mimalloc, tcmalloc, arena allocator,
Open skill - /numa-programming
NUMA programming skill for multi-socket memory locality. Use when detecting NUMA topology, binding processes with numactl, using libnuma API, building NUMA-aware data structures, or measuring remote access penalties. Activates on queries about numactl, libnuma, NUMA topology,
Open skill - /af-xdp
AF_XDP skill for high-performance XDP sockets. Use when creating AF_XDP sockets, configuring UMEM and XSK rings, XDP_REDIRECT programs, copy vs zero-copy mode, or comparing with DPDK. Activates on queries about AF_XDP, xsk_umem, XDP_REDIRECT, libbpf xsk, or zero-copy XDP.
Open skill - /dpdk
DPDK skill for userspace packet I/O. Use when initializing EAL, configuring PMD drivers, using mbuf pools and rte_ring, setting up huge pages, RSS, or testpmd validation. Activates on queries about DPDK, EAL, rte_eth_rx_burst, hugepages, PMD, or testpmd.
Open skill - /io-uring
io_uring skill for Linux async I/O. Use when building high-performance servers with liburing, multi-shot operations, provided buffers, fixed files, zero-copy send, or tokio-uring. Activates on queries about io_uring, SQE/CQE, liburing, IORING_OP_PROVIDE_BUFFERS, or io_uring vs
Open skill - /adc-dac-baremetal
Bare-metal ADC and DAC skill. Use when configuring analog sampling, DMA-driven ADC, calibration, or DAC output on MCUs. Activates on queries about ADC bare-metal, sampling time, DMA ADC, or DAC channel setup.
Open skill

