MODULE 9 · LESSON 9.6 · THEORY
Memory Mapped Registers in Embedded C: Volatile Pointers, Register Structures, BSRR and Bit-Banding
How peripherals appear as addresses on the STM32F407, volatile register pointers, struct overlays like GPIO_TypeDef, bit set/clear/toggle patterns, read-modify-write hazards, BSRR, clock enabling in RCC and bit-banding. Keep this in mind whenever you work with memory mapped registers in embedded C.
Intermediate → Advanced
Level
What You Will Learn
✓ How peripherals appear as ordinary addresses in the 4 GB memory map
✓ Why a register pointer must be volatile
✓ Building a register with a cast: (*(volatile uint32_t *)ADDR)
✓ Describing a peripheral with a structure (GPIO_TypeDef) and checking offsets
✓ Set, clear, toggle and field-update bit patterns, and why read-modify-write is dangerous
✓ Atomic alternatives: BSRR and bit-banding
✓ Enabling a clock in RCC_AHB1ENR before touching a peripheral
✓ Simulating registers on a PC so you can test driver logic
Key Terms
Memory-mapped I/ORegistervolatilePeripheral base addressGPIO_TypeDefRead-modify-writeBSRRRCC AHB1ENRBit-bandingBit maskReserved bitsOffsetStruct overlayAtomic accessCMSIS
1. Registers Are Just Addresses
On the STM32F407 (and every ARM Cortex-M) the CPU has one address space. Flash, SRAM and the control registers of every peripheral all sit in it. A memory-mapped register is a location in that space which is not storage but a hardware control or status word: storing to 0x40020C14 does not save a number, it changes the output pins of port D. The C language needs no special I/O instructions; a load or store through a pointer is enough, and this is why pointers (this module) and registers belong together. Many bugs with memory mapped registers in embedded C start here.
STM32F407 memory map (32-bit address space)
0x08000000 – Flash (1 MB), also aliased at 0x00000000
0x10000000 – CCM data RAM (64 KB)
0x20000000 – 0x2001FFFF – SRAM (128 KB)
0x40000000 – Peripherals: APB1, APB2, AHB1 (GPIO, RCC, timers, USART …)
0xE0000000 – Cortex-M4 core peripherals: SysTick, NVIC, SCB
| Peripheral | Base address | Bus | Registers you meet first |
|---|
| GPIOA | 0x40020000 | AHB1 | MODER, IDR, ODR, BSRR |
| GPIOD | 0x40020C00 | AHB1 | The four LEDs on PD12–PD15 |
| RCC | 0x40023800 | AHB1 | AHB1ENR (clock gates) |
| USART2 | 0x40004400 | APB1 | SR, DR, BRR, CR1 |
| SysTick | 0xE000E010 | Core | CTRL, LOAD, VAL |
Each register has an offset from its peripheral base. The reference manual lists them; the register address is base + offset. For example GPIOD_ODR is 0x40020C00 + 0x14 = 0x40020C14. This is the core rule for memory mapped registers in embedded C.
2. Accessing a Register with a Pointer
The classic idiom is a macro that casts the integer address to a pointer to a volatile unsigned integer and immediately dereferences it. The result is an lvalue, so it can be read or assigned like a variable. Reviewers check memory mapped registers in embedded C closely.
noc.c
#include <stdint.h>
void f(void)
{
uint32_t *odr = 0x40020C14; /* integer assigned to a pointer, no cast */
*odr = 1u << 12;
}
The cast needs to be explicit
noc.c: In function ‘f’:
noc.c:5:21: warning: initialization of ‘uint32_t *’ {aka ‘unsigned int *’} from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
5 | uint32_t *odr = 0x40020C14; /* integer assigned to a pointer, no cast */
| ^~~~~~~~~~
Here is the idiom, run on a PC. A real address such as 0x40020C14 is not mapped on a PC (the program would crash), so the program builds a small array of words that stands in for the GPIOD register block and applies the same macros to it. On the microcontroller only the one line defining the base address changes. Interviewers test memory mapped registers in embedded C on exactly this point.
reg_macro.c
#include <stdio.h>
#include <stdint.h>
/* — simulated hardware: on the board this would be #define GPIOD_BASE 0x40020C00u — */
static volatile uint32_t sim_gpiod[10]; /* 40 bytes: MODER … AFR[1] */
#define GPIOD_BASE ((uintptr_t)sim_gpiod)
/* register = base + offset, cast to a volatile pointer, dereferenced */
#define REG32(addr) (*(volatile uint32_t *)(addr))
#define GPIOD_MODER REG32(GPIOD_BASE + 0x00u)
#define GPIOD_ODR REG32(GPIOD_BASE + 0x14u)
int main(void)
{
GPIOD_MODER = 0x55000000u; /* PD12..PD15 as outputs (01 in each 2-bit field) */
GPIOD_ODR = 1u << 12; /* PD12 high: green LED */
GPIOD_ODR |= 1u << 14; /* PD14 high as well: read-modify-write */
printf(“MODER = 0x%08X\n”, (unsigned)GPIOD_MODER);
printf(“ODR = 0x%08X\n”, (unsigned)GPIOD_ODR);
printf(“sizeof(*(volatile uint32_t *)0) = %zu bytes\n”, sizeof(volatile uint32_t));
return 0;
}
Output (verified by compiling and running)
MODER = 0x55000000
ODR = 0x00005000
sizeof(*(volatile uint32_t *)0) = 4 bytes
Why the pointer must be volatile
The compiler assumes that memory only changes when the program changes it. A status register, however, changes by itself, and a write to a control register has an effect beyond storing a value. volatile tells the compiler that every access in the source is an observable event: it must not be removed, merged, cached in a CPU register or reordered with other volatile accesses. Without it, a loop such as while (!(USART2_SR & (1u << 5))); may be compiled into an endless loop on a value read once. Module 14 treats volatile in depth; for now the rule is simple: every register pointer is volatile. Master memory mapped registers in embedded C by practising this case.
| Macro form | Meaning | Verdict |
|---|
(*(volatile uint32_t *)0x40020C14) | Dereferenced volatile pointer, the usual style | Correct |
(volatile uint32_t *)0x40020C14 | The pointer only; you must write * each time | Correct, noisier |
(*(uint32_t *)0x40020C14) | No volatile | Bug: accesses may be optimised away |
(*(volatile uint16_t *)0x40020C14) | Half-word access to a 32-bit register | Wrong width unless the manual allows it |
(*(volatile int *)...) | Signed type: shifts and masks become fragile | Avoid; use unsigned fixed-width types |
Width matters: Use the width the reference manual specifies. Most STM32 registers are 32 bits, and a few (for example the older USART_DR in practice) accept narrower accesses. A wrong width can write only part of a register, or fault on some buses.
3. Describing a Peripheral with a Structure
One macro per register becomes unmanageable. Registers of a peripheral are laid out at consecutive offsets, so a struct whose members have the same size and order overlays them exactly. A single base pointer then reaches every register as a member: GPIOD->ODR. This is precisely what ST’s CMSIS header stm32f407xx.h does with GPIO_TypeDef. Remember this when you review memory mapped registers in embedded C in code reviews.
gpio_struct.c
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#define __IO volatile /* CMSIS: read/write register */
#define __I volatile const /* CMSIS: read-only register */
typedef struct
{
__IO uint32_t MODER; /* 0x00 mode register */
__IO uint32_t OTYPER; /* 0x04 output type */
__IO uint32_t OSPEEDR; /* 0x08 output speed */
__IO uint32_t PUPDR; /* 0x0C pull-up / pull-down */
__I uint32_t IDR; /* 0x10 input data (read only) */
__IO uint32_t ODR; /* 0x14 output data */
__IO uint32_t BSRR; /* 0x18 bit set / reset */
__IO uint32_t LCKR; /* 0x1C configuration lock */
__IO uint32_t AFR[2]; /* 0x20, 0x24 alternate function low / high */
} GPIO_TypeDef;
_Static_assert(offsetof(GPIO_TypeDef, ODR) == 0x14, “ODR offset”);
_Static_assert(offsetof(GPIO_TypeDef, BSRR) == 0x18, “BSRR offset”);
_Static_assert(sizeof(GPIO_TypeDef) == 0x28, “GPIO block size”);
int main(void)
{
printf(“MODER 0x%02zx OTYPER 0x%02zx OSPEEDR 0x%02zx PUPDR 0x%02zx\n”, offsetof(GPIO_TypeDef, MODER), offsetof(GPIO_TypeDef, OTYPER), offsetof(GPIO_TypeDef, OSPEEDR), offsetof(GPIO_TypeDef, PUPDR));
printf(“IDR 0x%02zx ODR 0x%02zx BSRR 0x%02zx LCKR 0x%02zx AFR 0x%02zx\n”, offsetof(GPIO_TypeDef, IDR), offsetof(GPIO_TypeDef, ODR), offsetof(GPIO_TypeDef, BSRR), offsetof(GPIO_TypeDef, LCKR), offsetof(GPIO_TypeDef, AFR));
printf(“sizeof(GPIO_TypeDef) = %zu\n”, sizeof(GPIO_TypeDef));
/* On the board: #define GPIOD ((GPIO_TypeDef *)0x40020C00UL) — a pointer constant to the real block */
GPIO_TypeDef sim; /* a RAM stand-in for the register block */
GPIO_TypeDef *GPIOD = ∼
GPIOD->MODER = 0u;
GPIOD->ODR = 0x5000u;
printf(“GPIOD->ODR = 0x%04X at offset %td from the base\n”, (unsigned)GPIOD->ODR, (char *)&GPIOD->ODR – (char *)GPIOD);
return 0;
}
Output (verified by compiling and running)
MODER 0x00 OTYPER 0x04 OSPEEDR 0x08 PUPDR 0x0c
IDR 0x10 ODR 0x14 BSRR 0x18 LCKR 0x1c AFR 0x20
sizeof(GPIO_TypeDef) = 40
GPIOD->ODR = 0x5000 at offset 20 from the base
Three details make this overlay safe. First, the members are all uint32_t, so no padding is inserted and the offsets equal the manual’s. Second, _Static_assert proves the offsets at compile time: if a member is added or removed in the wrong place the build stops. Third, reserved gaps in a peripheral are filled with placeholder members (uint32_t RESERVED0[2];) to keep every later register at its correct offset. Every driver for the STM32F407 depends on memory mapped registers in embedded C being understood.
One pointer, many registers: GPIOD = (GPIO_TypeDef *)0x40020C00
Why const and volatile together on read-only registers
__I expands to volatile const: the program may not assign to IDR (the compiler rejects it), yet each read still reaches the hardware. This turns a hardware rule (“input data register is read-only”) into a compile-time check.
ro.c
#include <stdint.h>
typedef struct { volatile const uint32_t IDR; volatile uint32_t ODR; } PORT;
void f(PORT *p)
{
p->ODR = 1;
p->IDR = 1; /* IDR is volatile const */
}
Writing to a read-only register is a compile error
ro.c: In function ‘f’:
ro.c:8:12: error: assignment of read-only member ‘IDR’
8 | p->IDR = 1; /* IDR is volatile const */
| ^
4. Bit Manipulation Patterns for Registers
A register usually packs several independent controls. Four operations cover almost all driver code. Always use an unsigned constant (1u) so that shifts never touch the sign bit.
| Operation | Expression | Effect |
|---|
| Set bit n | REG |= (1u << n); | Bit becomes 1, others unchanged |
| Clear bit n | REG &= ~(1u << n); | Bit becomes 0, others unchanged |
| Toggle bit n | REG ^= (1u << n); | Bit flips |
| Test bit n | if (REG & (1u << n)) | True if set |
| Write field | REG = (REG & ~MASK) | ((val << POS) & MASK); | Replace a multi-bit field only |
bitops.c
#include <stdio.h>
#include <stdint.h>
#define BIT(n) (1u << (n))
#define SET_BIT(reg, n) ((reg) |= BIT(n))
#define CLEAR_BIT(reg, n) ((reg) &= ~BIT(n))
#define TOGGLE_BIT(reg, n) ((reg) ^= BIT(n))
#define READ_BIT(reg, n) (((reg) >> (n)) & 1u)
/* replace the field described by MASK/POS with val, leave every other bit alone */
#define MODIFY_FIELD(reg, mask, pos, val) ((reg) = ((reg) & ~(mask)) | (((uint32_t)(val) << (pos)) & (mask)))
int main(void)
{
uint32_t moder = 0xA8000000u; /* GPIOA_MODER reset value (PA13, PA14, PA15 are debug pins) */
printf(“reset : 0x%08X\n”, (unsigned)moder);
/* make PD12 an output in a fresh MODER: field MODER12 occupies bits 25:24, value 01 = output */
uint32_t d = 0;
MODIFY_FIELD(d, 3u << 24, 24, 1u);
printf(“PD12 output : 0x%08X\n”, (unsigned)d);
MODIFY_FIELD(d, 3u << 24, 24, 2u); /* switch the same field to alternate function (10) */
printf(“PD12 alt-func : 0x%08X\n”, (unsigned)d);
uint32_t odr = 0;
SET_BIT(odr, 12); SET_BIT(odr, 15);
printf(“set 12,15 : 0x%04X\n”, (unsigned)odr);
CLEAR_BIT(odr, 12);
printf(“clear 12 : 0x%04X\n”, (unsigned)odr);
TOGGLE_BIT(odr, 13);
printf(“toggle 13 : 0x%04X bit13=%u bit12=%u\n”, (unsigned)odr, READ_BIT(odr, 13), READ_BIT(odr, 12));
return 0;
}
Output (verified by compiling and running)
reset : 0xA8000000
PD12 output : 0x01000000
PD12 alt-func : 0x02000000
set 12,15 : 0x9000
clear 12 : 0x8000
toggle 13 : 0xA000 bit13=1 bit12=0
Notice the MODER fields: each pin uses two bits (pin n sits in bits 2n+1:2n), so a single-bit set would be wrong; the field macro first clears both bits and then inserts the new value. Forgetting the clear step is the most frequent reason a pin shows the wrong mode when its previous value was non-zero.
5. Read-Modify-Write and Why It Can Break
ODR |= (1u << 12); looks like one operation but is three: read the register, OR the bit, write the register back. If an interrupt service routine changes another bit of the same register between the read and the write, its change is silently overwritten.
Lost update on a shared register (read-modify-write)
main: reads ODR = 0x0000
step 1
↓
ISR: sets bit 15, ODR = 0x8000
interrupt
↓
main: ORs bit 12 into the OLD value and writes 0x1000
step 2
↓
Result 0x1000: bit 15 lost
bug
lost_update.c
#include <stdio.h>
#include <stdint.h>
static uint32_t odr; /* shared “register” */
/* what the interrupt would do while main is between its read and its write */
static void isr_sets_bit15(void) { odr |= 1u << 15; }
int main(void)
{
odr = 0;
uint32_t tmp = odr; /* main: READ */
isr_sets_bit15(); /* the interrupt strikes here */
tmp |= 1u << 12; /* main: MODIFY */
odr = tmp; /* main: WRITE (overwrites the ISR’s work) */
printf(“read-modify-write result : 0x%04X (expected 0x9000)\n”, (unsigned)odr);
return 0;
}
Output (verified by compiling and running)
read-modify-write result : 0x1000 (expected 0x9000)
Three ways to avoid the problem
- Use set/clear registers. GPIO has
BSRR: bits 0–15 set the matching ODR bits, bits 16–31 reset them, and bits written as 0 change nothing. One 32-bit store, no read, no lost update. - Use bit-banding (below), which turns a single-bit write into one atomic store.
- Disable interrupts around the sequence (
__disable_irq() / __enable_irq()) when no atomic register exists. This increases interrupt latency, so keep the region short.
BSRR in detail
bsrr.c
#include <stdio.h>
#include <stdint.h>
/* Model of the GPIO output stage: a write to BSRR acts only on the bits written as 1 */
static uint32_t odr;
static void write_bsrr(uint32_t v)
{
uint32_t set = v & 0xFFFFu; /* BSx: bits 0..15 -> set ODRx */
uint32_t reset = v >> 16; /* BRx: bits 16..31 -> reset ODRx */
odr = (odr | set) & ~reset; /* if both are 1 for the same pin, set wins */
}
#define PIN_SET(n) (1u << (n))
#define PIN_RESET(n) (1u << ((n) + 16))
int main(void)
{
odr = 0x0003;
write_bsrr(PIN_SET(12));
printf(“set PD12 : 0x%04X\n”, (unsigned)odr);
write_bsrr(PIN_RESET(0) | PIN_SET(14));
printf(“reset 0 + set 14 : 0x%04X\n”, (unsigned)odr);
write_bsrr(PIN_RESET(12) | PIN_RESET(14) | PIN_SET(15));
printf(“reset 12,14 + set 15 : 0x%04X\n”, (unsigned)odr);
write_bsrr(0);
printf(“write 0 (no effect) : 0x%04X\n”, (unsigned)odr);
return 0;
}
Output (verified by compiling and running)
set PD12 : 0x1003
reset 0 + set 14 : 0x5002
reset 12,14 + set 15 : 0x8002
write 0 (no effect) : 0x8002
Model versus hardware: The program imitates what the GPIO hardware does with a BSRR write; in your driver you simply write GPIOD->BSRR = (1u << 12); and the hardware performs the atomic set. Per the reference manual, if the same pin has both its set and reset bit written as 1, the set action has priority.
6. Enabling the Peripheral Clock First
On the STM32 every peripheral is clock-gated to save power, and all gates are off after reset. Until the clock is enabled, writes to the peripheral registers are ignored and reads return 0. The gates are in the RCC (Reset and Clock Control) block, which sits at 0x40023800. GPIOD is on the AHB1 bus, so its enable bit GPIODEN is bit 3 of RCC_AHB1ENR, at offset 0x30.
| AHB1ENR bit | Name | Peripheral |
|---|
| 0 | GPIOAEN | GPIOA |
| 1 | GPIOBEN | GPIOB |
| 2 | GPIOCEN | GPIOC |
| 3 | GPIODEN | GPIOD (the LED port) |
| 4 | GPIOEEN | GPIOE |
led_init.c
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#define __IO volatile
typedef struct { __IO uint32_t MODER, OTYPER, OSPEEDR, PUPDR, IDR, ODR, BSRR, LCKR, AFR[2]; } GPIO_TypeDef;
typedef struct { __IO uint32_t CR, PLLCFGR, CFGR, CIR, AHB1RSTR, AHB2RSTR, AHB3RSTR, RESERVED0,
APB1RSTR, APB2RSTR, RESERVED1[2], AHB1ENR; } RCC_TypeDef;
_Static_assert(offsetof(RCC_TypeDef, AHB1ENR) == 0x30, “AHB1ENR must be at 0x30”);
/* RAM stand-ins; on the board: #define RCC ((RCC_TypeDef *)0x40023800UL) and GPIOD ((GPIO_TypeDef *)0x40020C00UL) */
static RCC_TypeDef rcc_block;
static GPIO_TypeDef gpiod_block;
#define RCC (&rcc_block)
#define GPIOD (&gpiod_block)
#define RCC_AHB1ENR_GPIODEN (1u << 3)
#define LED_GREEN 12
#define LED_ORANGE 13
#define LED_RED 14
#define LED_BLUE 15
static void led_init(void)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; /* 1. clock on */
for (int pin = LED_GREEN; pin <= LED_BLUE; pin++) { /* 2. MODERx = 01 (output) for PD12..PD15 */
GPIOD->MODER &= ~(3u << (2 * pin));
GPIOD->MODER |= (1u << (2 * pin));
}
}
int main(void)
{
led_init();
printf(“RCC->AHB1ENR = 0x%08X (bit 3 = GPIODEN)\n”, (unsigned)RCC->AHB1ENR);
printf(“GPIOD->MODER = 0x%08X\n”, (unsigned)GPIOD->MODER);
GPIOD->ODR = (1u << LED_GREEN) | (1u << LED_BLUE);
printf(“GPIOD->ODR = 0x%08X\n”, (unsigned)GPIOD->ODR);
printf(“offset of AHB1ENR = 0x%zX, sizeof(RCC_TypeDef) = %zu\n”, offsetof(RCC_TypeDef, AHB1ENR), sizeof(RCC_TypeDef));
return 0;
}
Output (verified by compiling and running)
RCC->AHB1ENR = 0x00000008 (bit 3 = GPIODEN)
GPIOD->MODER = 0x55000000
GPIOD->ODR = 0x00009000
offset of AHB1ENR = 0x30, sizeof(RCC_TypeDef) = 52
After enabling the clock, ST’s manual recommends a short read-back (or a couple of instructions) before the first access to the peripheral, because the enable takes effect one or two bus cycles later. The simulated run above only shows the register arithmetic; the clock gating itself exists only on the chip.
7. Bit-Banding: Atomic Access to a Single Bit
The Cortex-M3 and Cortex-M4 provide bit-band regions. The first 1 MB of SRAM (0x20000000–0x200FFFFF) and the first 1 MB of the peripheral space (0x40000000–0x400FFFFF) each have a 32 MB alias region (0x22000000 and 0x42000000) in which every bit of the original region has its own 32-bit word. Writing 1 or 0 to that word sets or clears exactly that bit with a single atomic bus transaction. The alias address is:
Bit-band alias address formula
alias_base
0x42000000 (peripherals) or 0x22000000 (SRAM)
+byte_offset × 32
(addr − region_base) × 32
+bitband.c
#include <stdio.h>
#include <stdint.h>
#define PERIPH_BASE 0x40000000u
#define PERIPH_BB_BASE 0x42000000u
#define SRAM_BASE 0x20000000u
#define SRAM_BB_BASE 0x22000000u
/* alias address of bit ‘bit’ of the word at ‘addr’ */
#define BB_ALIAS(addr, region, region_bb, bit) \
((region_bb) + (((addr) – (region)) * 32u) + ((bit) * 4u))
int main(void)
{
uint32_t odr_addr = 0x40020C00u + 0x14u; /* GPIOD_ODR */
printf(“GPIOD_ODR bit 12 alias = 0x%08X\n”, (unsigned)BB_ALIAS(odr_addr, PERIPH_BASE, PERIPH_BB_BASE, 12u));
printf(“GPIOD_ODR bit 15 alias = 0x%08X\n”, (unsigned)BB_ALIAS(odr_addr, PERIPH_BASE, PERIPH_BB_BASE, 15u));
printf(“RCC_AHB1ENR bit 3 alias = 0x%08X\n”, (unsigned)BB_ALIAS(0x40023830u, PERIPH_BASE, PERIPH_BB_BASE, 3u));
printf(“SRAM 0x20000010 bit 0 alias = 0x%08X\n”, (unsigned)BB_ALIAS(0x20000010u, SRAM_BASE, SRAM_BB_BASE, 0u));
return 0;
}
Output (verified by compiling and running)
GPIOD_ODR bit 12 alias = 0x424182B0
GPIOD_ODR bit 15 alias = 0x424182BC
RCC_AHB1ENR bit 3 alias = 0x4247060C
SRAM 0x20000010 bit 0 alias = 0x22000200
On the target you would write *(volatile uint32_t *)0x424182B0 = 1; (this is the alias printed above for bit 12) and bit 12 of GPIOD_ODR is set without a read-modify-write. The alias addresses are computed by the program only; the computation runs on the PC, but the aliasing itself exists only in the Cortex-M3/M4 hardware. Bit-banding is absent on the Cortex-M0/M0+ and has been dropped from the Cortex-M7, so portable drivers use BSRR or the vendor’s HAL instead. It also does not exist for every register (registers outside the first 1 MB of peripheral space are not bit-banded, and some peripherals on the STM32F4 buses behave differently for byte or word accesses).
8. Rules for Safe Register Access
- Declare every register
volatile; declare read-only status registers volatile const. - Use unsigned fixed-width types and unsigned constants (
1u, 0xFFu) in masks and shifts. - Enable the peripheral clock before the first access, and configure the peripheral before enabling it.
- Preserve reserved bits: read-modify-write fields rather than assigning a whole register unless the manual says reset values must be restored.
- Know the access type of each bit: rw, r, w, rc_w1 (cleared by writing 1; writing 0 does nothing), rc_r (cleared by reading). A read-modify-write on a status register with rc_w1 bits can clear flags you never meant to touch.
- Reading some registers has side effects (for example reading
USART_DR consumes a received byte). Do not read registers just to look at them in a debugger watch expression without knowing this. - Prefer one named structure per peripheral (CMSIS) to scattered address constants.
- Prove the layout with
_Static_assert(offsetof(...)). - Wrap hardware in a thin layer so the logic above it can be unit-tested on a PC with simulated registers, as this lesson does.
Key Takeaways — Lesson 9.6
- A memory-mapped register is an address in the peripheral region; a pointer dereference reads or writes it.
- The register pointer type is
volatile uint32_t *; read-only registers add const. - A structure of same-sized members overlays a peripheral;
GPIOD->ODR is 0x40020C14. - Use
|=, &= ~, ^= for bits, and clear-then-insert for multi-bit fields. - Read-modify-write can lose an update when an interrupt touches the same register;
BSRR and bit-banding avoid it. - Enable the clock (
RCC_AHB1ENR bit 3 for GPIOD) before using a peripheral.
Frequently Asked Questions
What is a memory-mapped register?
A hardware control or status word that occupies an address in the processor’s normal address space, so ordinary load and store instructions (pointer dereferences in C) access the peripheral.
Why must register pointers be volatile?
Registers change without the program writing them, and writes have side effects. volatile forces the compiler to perform every access exactly as written, without caching, removing or reordering it.
How do I turn an address into a register in C?
Cast the address to a pointer to a volatile unsigned integer and dereference it: #define GPIOD_ODR (*(volatile uint32_t *)0x40020C14UL). Or overlay a structure such as GPIO_TypeDef on the base address.
What is BSRR and why use it?
The bit set/reset register of a GPIO port. Writing a 1 to bit n sets pin n, writing a 1 to bit n+16 resets it, and zeros do nothing, so a pin can be changed atomically without a read-modify-write.
Which RCC register enables the GPIOD clock on the STM32F407?
RCC_AHB1ENR at offset 0x30 from 0x40023800; bit 3 (GPIODEN) must be set before GPIOD registers can be used.
Ready to practise?
Next: worked examples that configure LEDs, build a register driver, decode status flags, test logic on a PC and compute bit-band aliases.
2 Comments