skills/baremetal/mmio-and-bit-manipulation/SKILL.md
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 skillsauth add mohitmishra786/low-level-dev-skills mmio-and-bit-manipulationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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.
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 |
#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.
/* 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 */
uint32_t MMIO at word-aligned addressesuint32_t access may fault on ARMv7-M+volatile uint8_t with correct byte lane address#define REG8(addr) (*(volatile uint8_t *)(addr))
/* After configuring peripheral before first use */
__DSB();
__ISB();
/* After DMA setup, before enabling channel */
__DMB();
Use CMSIS barriers (core_cm4.h) on Cortex-M.
/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?
| 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 |
skills/baremetal/peripherals-from-datasheet — extracting register mapsskills/baremetal/gpio-baremetal — GPIO register patternsskills/low-level-programming/assembly-arm — inline asm barriersskills/embedded/linker-scripts — peripheral memory map regionsdevelopment
QEMU/KVM skill for virtualization and kernel development. Use when running qemu-system-x86_64 with KVM, configuring virtio devices, VFIO passthrough, QMP monitor, libvirt, or booting custom kernels. Activates on queries about QEMU, KVM, virtio, VFIO, virsh, virt-install, or -kernel -append.
development
Hardware virtualization internals skill for Intel VT-x and AMD-V. Use when studying VMCS/VMCB, EPT/NPT page tables, VMEXIT handling, APIC virtualization, or building minimal hypervisors. Activates on queries about VMX, SVM, VMCS, EPT, NPT, VMEXIT, or type-1 hypervisor.
testing
Linux containers internals skill for namespaces, cgroups, and OCI. Use when understanding clone/unshare namespaces, cgroups v2 limits, overlayfs, runc, seccomp profiles, capabilities, or escape mitigations. Activates on queries about namespaces, cgroups, overlayfs, runc, seccomp-bpf, OCI spec, or container escape.
tools
Reverse engineering skill for binary analysis. Use when decompiling with Ghidra, analyzing with radare2, scripting RE tools, triaging with strings/file/xxd, or diffing binaries. Activates on queries about Ghidra, radare2, r2, decompiler, Binary Ninja, Diaphora, or stripped binary analysis.