/mmio-and-bit-manipulation
MMIO and register access skill for bare-metal firmware. Use when accessing memory-mapped peripherals with volatile, bit masks, RMW patterns, or endianness concerns. Activates on queries about MMIO, volatile register, bit manipulation, read-modify-write, or register alignment.
$ npx -y skills add mohitmishra786/low-level-dev-skills --skill mmio-and-bit-manipulation --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
/mmio-and-bit-manipulation
Context preview
The summary Claude sees to decide when to auto-load this skill.
MMIO and register access skill for bare-metal firmware. Use when accessing memory-mapped peripherals with volatile, bit masks, RMW patterns, or endianness concerns. Activates on queries about MMIO, volatile register, bit manipulation, read-modify-write, or register alignment.
SKILL.md
mmio-and-bit-manipulation.SKILL.mdname: mmio-and-bit-manipulation
description: MMIO and register access skill for bare-metal firmware. Use when accessing memory-mapped peripherals with volatile, bit masks, RMW patterns, or endianness concerns. Activates on queries about MMIO, volatile register, bit manipulation, read-modify-write, or register alignment.
MMIO and Bit Manipulation
Purpose
Guide agents through safe memory-mapped I/O: `volatile` semantics, read-modify-write patterns, bitfield pitfalls, alignment and endianness, and portable register access macros for bare-metal drivers.
When to Use
- Writing peripheral register drivers without HAL
- Fixing intermittent register corruption or stale reads
- Replacing C bitfields with explicit masks
- Porting drivers between little-endian MCUs
- Auditing ISR vs main-line register access
Workflow
1. MMIO fundamentals
Peripheral registers live at fixed addresses in the CPU memory map. The compiler must not cache reads/writes.
#include <stdint.h>
#define PERIPH_BASE 0x40000000U
#define GPIOA_MODER (*(volatile uint32_t *)(PERIPH_BASE + 0x20000U))
| Qualifier | Effect | |-----------|--------| | `volatile` | Forces load/store each access — required for hardware | | `const volatile` | Read-only hardware (rare) | | Plain `uint32_t *` | **Wrong** — compiler may optimize away |
2. Read-modify-write macros
#define REG32(addr) (*(volatile uint32_t *)(addr))
#define REG_SET(addr, mask) (REG32(addr) |= (mask))
#define REG_CLR(addr, mask) (REG32(addr) &= ~(mask))
#define REG_TOGGLE(addr, mask) (REG32(addr) ^= (mask))
#define REG_WRITE(addr, val) (REG32(addr) = (val))
#define REG_READ(addr) (REG32(addr))
**Good** — atomic intent for single-bit updates when register supports it:
#define GPIOA_BSRR REG32(0x40020018U)
GPIOA_BSRR = (1U << 5); /* set PA5 */
GPIOA_BSRR = (1U << (5+16)); /* reset PA5 — STM32 BSRR pattern */
**Bad** — non-atomic RMW on interrupt-shared registers:
uint32_t v = REG_READ(GPIOA_MODER);
v |= (1U << 10);
REG_WRITE(GPIOA_MODER, v); /* ISR may interleave — lost update */
Fix: disable IRQ briefly, use hardware set/clear registers, or LL atomic bitband if available.
3. Bitfield pitfalls
/* Bad — layout is implementation-defined, not portable */
typedef struct {
uint32_t mode : 2;
uint32_t type : 1;
uint32_t speed : 2;
} gpio_moder_bits_t;Prefer explicit masks:
#define GPIO_MODER_MODE0_SHIFT 0
#define GPIO_MODER_MODE0_MASK (3U << GPIO_MODER_MODE0_SHIFT)
#define GPIO_MODER_MODE0_VAL(n) ((n) << GPIO_MODER_MODE0_SHIFT)
REG32(GPIOA_MODER) = (REG32(GPIOA_MODER) & ~GPIO_MODER_MODE0_MASK)
| GPIO_MODER_MODE0_VAL(1); /* output */4. Endianness and alignment
- Cortex-M and most MCUs: **little-endian** — `uint32_t` MMIO at word-aligned addresses
- Unaligned `uint32_t` access may fault on ARMv7-M+
- 8-bit registers: use `volatile uint8_t` with correct byte lane address
#define REG8(addr) (*(volatile uint8_t *)(addr))
5. Memory barriers (when needed)
/* After configuring peripheral before first use */
__DSB();
__ISB();
/* After DMA setup, before enabling channel */
__DMB();
Use CMSIS barriers (`core_cm4.h`) on Cortex-M.
6. Agent usage examples
/mmio-and-bit-manipulation Safe pattern to set bit 3 without affecting other bits in ISR context
/mmio-and-bit-manipulation Why must peripheral pointers be volatile?
Common Problems
| Symptom | Cause | Fix | |---------|-------|-----| | Register write ignored | Wrong address/clock gated | Enable peripheral clock first | | Random bit flips | RMW race with ISR | BSRR-style atomic regs or critical section | | HardFault on access | Unaligned or protected bus | Match access width to datasheet | | Optimized-away read | Missing `volatile` | Use `volatile uint32_t` | | Bitfield wrong value | Compiler packs unexpectedly | Use shift/mask macros |
Related Skills
- `skills/baremetal/peripherals-from-datasheet` — extracting register maps
- `skills/baremetal/gpio-baremetal` — GPIO register patterns
- `skills/low-level-programming/assembly-arm` — inline asm barriers
- `skills/embedded/linker-scripts` — peripheral memory map regions
Read more
name: mmio-and-bit-manipulation description: MMIO and register access skill for bare-metal firmware. Use when accessing memory-mapped peripherals with volatile, bit masks, RMW patterns, or endianness concerns. Activates on queries about MMIO, volatile register, bit manipulation, read-modify-write, or register alignment.
MMIO and Bit Manipulation
Purpose
Guide agents through safe memory-mapped I/O: `volatile` semantics, read-modify-write patterns, bitfield pitfalls, alignment and endianness, and portable register access macros for bare-metal drivers.
When to Use
- Writing peripheral register drivers without HAL
- Fixing intermittent register corruption or stale reads
- Replacing C bitfields with explicit masks
- Porting drivers between little-endian MCUs
- Auditing ISR vs main-line register access
Workflow
1. MMIO fundamentals
Peripheral registers live at fixed addresses in the CPU memory map. The compiler must not cache reads/writes.
#include <stdint.h> #define PERIPH_BASE 0x40000000U #define GPIOA_MODER (*(volatile uint32_t *)(PERIPH_BASE + 0x20000U))
| Qualifier | Effect | |-----------|--------| | `volatile` | Forces load/store each access — required for hardware | | `const volatile` | Read-only hardware (rare) | | Plain `uint32_t *` | **Wrong** — compiler may optimize away |
2. Read-modify-write macros
#define REG32(addr) (*(volatile uint32_t *)(addr)) #define REG_SET(addr, mask) (REG32(addr) |= (mask)) #define REG_CLR(addr, mask) (REG32(addr) &= ~(mask)) #define REG_TOGGLE(addr, mask) (REG32(addr) ^= (mask)) #define REG_WRITE(addr, val) (REG32(addr) = (val)) #define REG_READ(addr) (REG32(addr))
**Good** — atomic intent for single-bit updates when register supports it:
#define GPIOA_BSRR REG32(0x40020018U) GPIOA_BSRR = (1U << 5); /* set PA5 */ GPIOA_BSRR = (1U << (5+16)); /* reset PA5 — STM32 BSRR pattern */
**Bad** — non-atomic RMW on interrupt-shared registers:
uint32_t v = REG_READ(GPIOA_MODER); v |= (1U << 10); REG_WRITE(GPIOA_MODER, v); /* ISR may interleave — lost update */
Fix: disable IRQ briefly, use hardware set/clear registers, or LL atomic bitband if available.
3. Bitfield pitfalls
/* Bad — layout is implementation-defined, not portable */
typedef struct {
uint32_t mode : 2;
uint32_t type : 1;
uint32_t speed : 2;
} gpio_moder_bits_t;Prefer explicit masks:
#define GPIO_MODER_MODE0_SHIFT 0
#define GPIO_MODER_MODE0_MASK (3U << GPIO_MODER_MODE0_SHIFT)
#define GPIO_MODER_MODE0_VAL(n) ((n) << GPIO_MODER_MODE0_SHIFT)
REG32(GPIOA_MODER) = (REG32(GPIOA_MODER) & ~GPIO_MODER_MODE0_MASK)
| GPIO_MODER_MODE0_VAL(1); /* output */4. Endianness and alignment
- Cortex-M and most MCUs: **little-endian** — `uint32_t` MMIO at word-aligned addresses
- Unaligned `uint32_t` access may fault on ARMv7-M+
- 8-bit registers: use `volatile uint8_t` with correct byte lane address
#define REG8(addr) (*(volatile uint8_t *)(addr))
5. Memory barriers (when needed)
/* After configuring peripheral before first use */ __DSB(); __ISB(); /* After DMA setup, before enabling channel */ __DMB();
Use CMSIS barriers (`core_cm4.h`) on Cortex-M.
6. Agent usage examples
/mmio-and-bit-manipulation Safe pattern to set bit 3 without affecting other bits in ISR context /mmio-and-bit-manipulation Why must peripheral pointers be volatile?
Common Problems
| Symptom | Cause | Fix | |---------|-------|-----| | Register write ignored | Wrong address/clock gated | Enable peripheral clock first | | Random bit flips | RMW race with ISR | BSRR-style atomic regs or critical section | | HardFault on access | Unaligned or protected bus | Match access width to datasheet | | Optimized-away read | Missing `volatile` | Use `volatile uint32_t` | | Bitfield wrong value | Compiler packs unexpectedly | Use shift/mask macros |
Related Skills
- `skills/baremetal/peripherals-from-datasheet` — extracting register maps
- `skills/baremetal/gpio-baremetal` — GPIO register patterns
- `skills/low-level-programming/assembly-arm` — inline asm barriers
- `skills/embedded/linker-scripts` — peripheral memory map regions
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

