Skip to main content
skill CX Codex

mmio-and-bit-manipulation — Claude Code skill: MMIO and bit handling

mmio-and-bit-manipulation — a skill for safe peripheral memory access: volatile semantics, read-modify-write patterns, bit masks, and alignment. Essential when writing bare-metal drivers without a HAL.

MIT 392 installs ★ 298 updated 2026-06-27

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

What mmio-and-bit-manipulation does

Teaches the agent correct register access through memory-mapped I/O. Shows how to use volatile to prevent compiler caching, safe bit manipulation via RMW macros without triggering races with ISRs, and why C bitfields are unreliable—replaced by explicit masks. Covers endianness on different MCU architectures, word alignment (unaligned uint32_t access faults on ARMv7-M+), and 8-bit register access patterns.

Includes macro templates for register read, write, set, clear, and toggle operations; a reference table of common symptoms (optimized-away reads, bit flips from ISR interference, HardFault on access) and their fixes; and pointers to related skills on GPIO bare-metal patterns and linker script memory layout.

What it helps with

  • safely update register bits while ISR touches other bits in the same register
  • prevent compiler from optimizing away peripheral register accesses
  • debug random bit flips and hangs when working with MMIO
  • port a register driver across architectures with different endianness
  • understand why volatile is mandatory in all register code

Example prompts for the agent

  • Show me a safe way to set bit 3 in an MMIO register without affecting other bits from an ISR context
  • Why do peripheral pointers need to be volatile?
  • Write a REG_SET macro for atomic bit setting if hardware BSRR atomic registers are unavailable

How to install mmio-and-bit-manipulation

1Skills CLI (Vercel)

npx skills add mohitmishra786/low-level-dev-skills --skill mmio-and-bit-manipulation -a claude-code

2Manually: put the files in place

~/.claude/skills/mmio-and-bit-manipulation/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/mmio-and-bit-manipulation/

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

Questions about mmio-and-bit-manipulation

How do I install mmio-and-bit-manipulation in Claude Code?

Run npx skills add mohitmishra786/low-level-dev-skills --skill mmio-and-bit-manipulation -a claude-code, then open ~/.claude/skills/mmio-and-bit-manipulation/SKILL.md.

What does the mmio-and-bit-manipulation skill do?

It guides the agent on safe memory-mapped I/O coding: using volatile correctly, avoiding read-modify-write races with interrupts, replacing unpredictable C bitfields with explicit masks, and accounting for endianness and address alignment.

Which platforms support this skill?

The skill works with Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, and OpenCode. Each platform has its own install command.

Source text

SKILL.md · MIT
---
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.

```c
#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

```c
#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:

```c
#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:

```c
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

```c
/* 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:

```c
#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

```c
#define REG8(addr)  (*(volatile uint8_t *)(addr))
```

### 5. Memory barriers (when needed)

```c
/* 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

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

Similar skills

All skills for Programming languages →

From the same set140

The whole set →
bootloaders-embedded bootloaders-embedded is a skill for Cortex-M bootloader development. It covers image validation, safe handoff to an app, vector table relocation, and updates over UART or USB DFU. CX Codex +1 395installs kernel-debugging This Claude Code skill guides Linux kernel debugging with tracing, debuggers, and crash-dump analysis. It activates for questions about kgdb, kdb, ftrace, kprobes, dyndbg, kdump, or dmesg levels. CX Codex +1 395installs qemu-kvm qemu-kvm walks the agent through QEMU VMs with KVM acceleration: flags, virtio devices, VFIO passthrough and booting custom kernels. CX Codex +1 395installs baremetal-startup baremetal-startup is a Claude Code skill that guides coding agents through Cortex-M and RISC-V initialization from reset to main(). CX Codex +1 394installs dma-baremetal dma-baremetal is a Claude Code skill that assists with register-level DMA setup for bare-metal microcontrollers, moving UART, SPI, or ADC data away from the CPU. CX Codex +1 392installs bus-drivers-i2c-spi bus-drivers-i2c-spi walks an agent through Linux client drivers for I2C and SPI sensors and PMICs: registration, regmap, device tree binding. CX Codex +1 392installs uart-serial-baremetal uart-serial-baremetal configures bare-metal UART and USART drivers for serial debug consoles, baud calculation, and interrupt I/O. CX Codex +1 391installs kernel-concurrency kernel-concurrency is a Claude Code skill that helps select a Linux kernel synchronization primitive: spinlock, mutex, RCU, seqlock, or completion. CX Codex +1 391installs

Project sponsors

Companies that keep this analytics open