Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Memory, Registers and Peripherals

Memory-Mapped Registers

Learn how peripherals are controlled through memory-mapped registers: volatile pointers, register structs, base addresses, access types (RW, RO, W1C), and a simulated GPIO block.

A microcontroller peripheral (a GPIO port, a UART, a timer) is controlled by a small group of registers. Registers look like memory: each has an address, and the CPU reads and writes them with ordinary load and store instructions. This design is called memory-mapped I/O. Writing a value to a certain address does not store it in RAM; it makes the hardware do something. Reading another address does not return stored data; it returns the live state of the hardware.

This lesson shows the three ways to access registers in C, then builds the standard solution used by every vendor header (CMSIS): a struct laid over the peripheral's address range. You will also meet the access types that determine whether a read-modify-write is safe, and run a simulated peripheral that reacts to your writes.

Reading the reference manual

For every peripheral the reference manual gives:

  • the base address of the block (for example GPIOA at 0x4002_0000 on an STM32F4),
  • a list of registers with an offset from the base (MODER at 0x00, ODR at 0x14),
  • the reset value after a reset,
  • for each register, the meaning of every bit field and its access type.

The address of a register is base + offset. All registers of a peripheral are typically 32 bits wide and aligned to 4 bytes.

Three ways to touch a register

1. A raw address with a cast:

C
*(volatile uint32_t *)0x40020014u = 0x20u;       // write ODR

2. A named macro for each register:

C
#define GPIOA_ODR  (*(volatile uint32_t *)(0x40020000u + 0x14u))
GPIOA_ODR |= (1u << 5);

3. A struct overlaying the whole block:

C
typedef struct {
    volatile uint32_t MODER;     // 0x00
    volatile uint32_t OTYPER;    // 0x04
    volatile uint32_t OSPEEDR;   // 0x08
    volatile uint32_t PUPDR;     // 0x0C
    volatile uint32_t IDR;       // 0x10
    volatile uint32_t ODR;       // 0x14
    volatile uint32_t BSRR;      // 0x18
} GPIO_TypeDef;

#define GPIOA  ((GPIO_TypeDef *)0x40020000u)

GPIOA->ODR |= (1u << 5);

The third style is what vendor headers (stm32f4xx.h, RP2040.h, nrf52.h) use. The compiler computes each offset from the member order, the struct pointer is a single constant, and the code reads like GPIOA->ODR. Two rules make it work:

  • every member is volatile, so each access happens as written (volatile, const and static);
  • the struct must match the manual exactly, including reserved gaps: uint32_t RESERVED0[2]; to skip two unused words. Protect it with static_assert(offsetof(...)) (compile-time checks).

The same struct pointer works for every instance of a peripheral: USART1, USART2 and USART3 share a type and differ only in base address, so one driver function takes a USART_TypeDef *.

Register access types

The type of each register or bit decides what you may safely do:

Type Meaning Example Note
RW read and write control, data output read-modify-write is fine
RO read only input data, status, device ID writing is ignored or faults
WO write only set/clear registers (BSRR) reading returns zeros or garbage
W1C write 1 to clear interrupt flags write a 1 to clear; a 0 changes nothing; read-modify-write clears other flags!
RC_R cleared by reading some status flags, data register a debug read can lose the flag
RW1S / W1S write 1 to set some enables cannot be cleared by software

Two practical consequences:

  1. Never read-modify-write a W1C register. SR &= ~FLAG or SR |= x does not do what it looks like: SR = FLAG; clears exactly that flag.
  2. Beware of reads with side effects. Reading a UART data register removes the byte from the FIFO. If the debugger displays the register window while you step, it may read it too and eat your data; many tools avoid this for you, but you should know.

A simulated GPIO peripheral

You cannot write to a real address on a PC (the OS would stop the program), but you can overlay the struct on an ordinary variable and add a function that plays the role of the hardware reacting to writes. Notice how BSRR (bit set/reset register) works: writing a 1 to the low half sets that ODR bit; a 1 in the high half clears it. Writing 0s changes nothing, so no read-modify-write is needed, and the update is atomic.

C A register struct and a simulated peripheral

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

In a real chip the "hardware_react" step is silicon, and it happens within a cycle or two of the write.

Side effects and a W1C flag

Next, a timer status register with a write-1-to-clear flag. The "hardware" sets an update flag every few ticks. See what the wrong clearing method (&= ~) does to other flags, compared with writing a 1 to the flag you want to clear.

C Write-1-to-clear: the right and the wrong way

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The second sequence does the opposite of what it looks like: it clears CAPTURE and leaves UPDATE set. The value it wrote back (0b10) contains a 1 at the CAPTURE position, and in a W1C register a 1 means "clear". Real firmware loses interrupt events in exactly this way.

Instances and the base-address pattern

Peripherals of the same kind share a layout. A driver takes a pointer, so the same code serves all of them:

C
void uart_putc(USART_TypeDef *u, char c) {
    while ((u->SR & USART_SR_TXE) == 0) { }
    u->DR = (uint8_t)c;
}
uart_putc(USART1, 'A');
uart_putc(USART2, 'B');

Vendors ship two layers of headers: the register definitions (struct types, base addresses, bit masks) and a HAL or LL driver library. CMSIS-SVD files describe all registers in XML so tools and debuggers can show named registers. You will read and write these headers daily, so it is worth reading one once from top to bottom.

Memory barriers and write buffers

On faster cores a write to a peripheral may be buffered. The next instruction could run before the write reaches the device. Cortex-M normally keeps device memory accesses in order, but when you must be sure (for example after enabling a clock before using the peripheral, or after clearing an interrupt flag at the end of an ISR), insert a barrier (__DSB()) or read the register back. The classic example: after RCC->AHB1ENR |= GPIOAEN; read RCC->AHB1ENR once (or use a short dummy delay) before touching the port, because the clock takes a cycle or two to arrive.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why are all members of a register struct declared volatile?

    Register accesses have side effects and the hardware changes values on its own, so the compiler must not cache or remove accesses.
  2. 2. How do you clear the flag bit FLAG in a write-1-to-clear status register SR?

    Writing a 1 to the flag clears it; writing 0 to other bits leaves them. SR = 0 clears nothing.
  3. 3. What is the advantage of a BSRR-style set/reset register over writing ODR directly?

    Hardware applies the set or reset to exactly the pins named, so an interrupt cannot cause a lost update.
  4. 4. What gives the address of register ODR in the struct overlay GPIOA->ODR?

    The struct pointer holds the base address and the member order determines each offset, so reserved gaps must match the manual.

Summary

  • Peripherals are controlled through registers mapped into the address space; accesses are ordinary loads and stores to special addresses.
  • The standard pattern is a struct of volatile members overlaid on the peripheral's base address, one type for all instances.
  • Know each register's access type (RW, RO, WO, W1C, read-to-clear); never read-modify-write a W1C register.
  • Set/reset registers like BSRR provide atomic single-pin changes without a read.
  • Use static_assert on offsets, and barriers or read-backs when ordering matters.

Next: GPIO in Depth