Subsection 1.4.1 Part 1: From C Code to Hardware
When we write
GPIOA->ODR |= LED_PIN, how does that one line of C actually light an LED? We’ll follow it through three stages, compiling, flashing and executing (Figure 1.4.1). C is a compiled language, not an interpreted one: the compiler processes your entire source file at once and produces a single binary (an .elf file) containing all the machine instructions for the whole program. STM32CubeIDE then flashes that binary to the chip’s 32 KB flash memory in one operation. Once the board resets, the Cortex-M0+ CPU fetches and executes those stored instructions one by one. The source lines you wrote no longer exist on the chip, only the machine instructions they were compiled into.
/slide08_799bfba4.png)
GPIOA->ODR |= LED_PIN compiles to three instructions, a load, an OR and a write; when the write reaches the GPIO Output Data Register it drives the output pin HIGH.
Memory-mapped I/O. The STM32C031C6 has a single 4 GB address space shared by flash, RAM, and every peripheral register (Figure 1.4.2). There is no separate I/O bus and no special I/O instructions: the CPU reads and writes peripheral registers exactly the same way it reads and writes RAM.
GPIOA->ODR is not a variable stored in RAM; it links to the contents of memory address 0x5000 0014, and those contents are Port A’s output register rather than a memory cell. When the CPU writes a value to that address, the GPIO hardware recognizes the address and updates the pins within one clock cycle.
/fullslide_12.png)
0x0800 0000; RAM is at 0x2000 0000. GPIO peripheral registers are in the IOPORT block at 0x5000 0000. Writing to a GPIO register is simply a store to a specific address in this map, no special I/O instruction required.
How the output pin changes voltage. Each GPIO output contains a pair of transistors, a P-MOS on top and an N-MOS on the bottom, connected between the 3.3 V supply and ground (Figure 1.4.3). This is called a push-pull output. Writing a
1 to the ODR bit turns the P-MOS on (connecting the pin to 3.3 V) and turns the N-MOS off. Writing a 0 does the reverse: N-MOS on, P-MOS off, pin driven to 0 V. The transistors switch within nanoseconds of the CPU writing to ODR. Setting MODER to output mode (01) connects this push-pull driver to the pin; in any other mode the driver is disconnected.
/fullslide_14.png)
GPIOA->ODR (bottom-left) passes through output control logic to the P-MOS/N-MOS transistor pair, driving the I/O pin toward the LED. ODR = 1 turns on P-MOS → pin goes HIGH (3.3 V); ODR = 0 turns on N-MOS → pin goes LOW (0 V). GPIOA->MODER (bottom-right) selects input vs. output mode.