Subsection 1.6.2 Memory-Mapped I/O
The STM32C031C6 uses memory-mapped I/O: peripheral registers are located at specific addresses in the same 4 GB address space as flash and RAM (Figure 1.4.2). From the CPU’s perspective, writing to a GPIO register looks identical to writing to a RAM variable, but instead of storing a value in a memory cell, it changes the state of a hardware circuit.
The GPIOA base address is
0x5000 0000. ODR is at offset 0x14 within the GPIO structure, giving absolute address 0x5000 0014. In the STM32 header file, GPIOA is a C pointer cast to the structure’s base address:
// Inside stm32c031xx.h (simplified):
#define GPIOA ((GPIO_TypeDef *) 0x50000000UL)
typedef struct {
uint32_t MODER; /* offset 0x00 */
uint32_t OTYPER; /* offset 0x04 */
uint32_t OSPEEDR; /* offset 0x08 */
uint32_t PUPDR; /* offset 0x0C */
uint32_t IDR; /* offset 0x10 */
uint32_t ODR; /* offset 0x14 */
/* ... */
} GPIO_TypeDef;
The
#define line is read from the inside out. 0x50000000UL is the base address written as a number, where the UL makes it an unsigned long integer, and (GPIO_TypeDef *) is a cast that turns that number into the address of a GPIO_TypeDef structure. The typedef struct below it defines GPIO_TypeDef as a structure whose fields are the port’s registers, in the order in which they are located in memory. Each field is a 32-bit uint32_t, four bytes wide, which is where the offsets 0x00, 0x04, 0x08 and so on come from.
So
GPIOA->ODR = value is exactly equivalent to writing to address 0x5000 0014. The hardware detects that address on the bus and routes the write to the GPIO output latch rather than to RAM.
/fullslide_15.png)
GPIOA->ODR in the address space. The IOPORT block (left, green) expands to show each GPIO port’s 1 KB range; GPIOA occupies 0x5000 0000 to 0x5000 03FF (green box, right table). Within GPIOA, the register offset table shows ODR at offset 0x14 (green box). The blue callout explains the bus write: the processor places data, address, and a write signal on the bus; only the peripheral whose address matches activates and latches the data. The address calculation is shown at the bottom: 0x5000 0000 + 0x14 = 0x5000 0014.