The LED we are blinking is the green one on the Nucleo board itself, and it is wired to PA5 (Port A, bit 5). To make it blink, the program must do four things in order:
#include "stm32c0xx.h" // All register definitions are here
// Bit 0 of RCC->IOPENR enables the clock for GPIOA
#define GPIOAEN (1U << 0) // 0b...0001 (unsigned 32-bit)
// Bit 5 of GPIOA is wired to the on-board LED
#define LED_PIN (1U << 5) // 0b...0010 0000
int main(void) {
RCC->IOPENR |= GPIOAEN; // Step 1: enable GPIOA clock
GPIOA->MODER |= (1U << 10); // Step 2a: set bit 10 of MODER
GPIOA->MODER &= ~(1U << 11);// Step 2b: clear bit 11 of MODER
// β PA5 is now an output
while (1) {
GPIOA->ODR |= LED_PIN; // Step 3: set bit 5 β LED on
for (int i = 0; i < 100000; i++);
GPIOA->ODR &= ~LED_PIN; // Step 4: clear bit 5 β LED off
for (int i = 0; i < 100000; i++);
}
return 0; // never reached
}
Notice that Steps 3 and 4 are inside while (1), an infinite loop that never terminates. In most programming courses you are told never to write an infinite loop. Embedded programming is the exception. A microcontroller (MCU) is not a general-purpose computer running an operating system; there is no desktop to return to, no shell to hand control back to. From the moment power is applied, the processor must keep executing. The return 0 at the end of main() is unreachable: it is there because main() is declared to return an int, and every program in this course includes it. Any real embedded program has exactly this shape: one-time setup code beforewhile (1), then the work the device does forever inside it.
A peripheral register is a storage location inside a hardware peripheral, such as a GPIO port, that the CPU reads to learn the peripheralβs status and writes to control it. Every peripheral register on the STM32C031C6 is 32 bits wide, and our code controls the hardware by reading and writing those registers. Before we step through each line, here are the C operators used throughout blinky.c. Each of them exists for one reason: a register holds many independent settings at once, so we have to change the bits belonging to one pin and leave every other bit exactly as it was.
Left shift: 1U << n. Writing 1U << n shifts the value 1 left by n bit positions, placing a single 1 at position n and zeros everywhere else. The U suffix makes it an unsigned 32-bit integer, so that the leading bit isnβt interpreted as a way to indicate a negative number, which could lead to unintended shift behavior around that bit.
This 32-bit number with all zeros and a single 1 in the bit we care about is called a bit mask. Giving it a name with #define makes your code more readable: LED_PIN instead of an unexplained (1U << 5).
Setting a bit: OR-assign (|=). To turn the LED on in the while loop we have to set bit 5 of ODR and leave every other bit exactly as it is, and bitwise OR is the operator that does that. Its truth table shows what happens to each bit pair:
When the mask bit is 1, the result is always 1: the register bit is forced high regardless of its current value. When the mask bit is 0, the result equals the register bit: it is left unchanged. So register |= mask sets exactly the bits that are 1 in the mask, leaving all other bits untouched. A plain assignment would overwrite every other bit to zero, potentially disabling things that were already configured, so a bit in a peripheral register needs to be set with |= rather than with a plain assignment.
Clearing a bit: AND-assign (&=) and bitwise NOT (~). To turn the LED back off we need to clear the corresponding bit (that is, set it to 0) in ODR, and that takes two operators. register &= value is shorthand for register = register & value: it AND-assigns any value you give it, exactly like |= is shorthand for OR-assign. The ~ operator is separate: it flips every bit of its operand. In blinky.c these two operators appear together because clearing a bit requires exactly that combination, but they can each be used on their own.
When the mask bit is 0, the result is always 0: the register bit is forced low regardless of its current value. When the mask bit is 1, the result equals the register bit: it is left unchanged. To force a single bit to 0, start with a mask that has a 1 in that position, then apply ~ to invert it, producing 0 at that position and 1s everywhere else:
AND-ing (&) any bit with 0 forces it to 0; AND with 1 leaves it unchanged. So register &= ~mask clears exactly the bits that were 1 in the mask, leaving all others untouched.
Step 1: Enable the GPIOA clock. By default, the clock to each GPIO port is disabled to save power, and we cannot read from or write to any GPIO register until that clock is enabled. The first line of main(), RCC->IOPENR |= GPIOAEN, sets bit 0 of the I/O Port Enable Register and so enables the clock to Port A. A 1 in that bit turns the portβs clock on, which gives the port power so its pins can be used; a 0 turns it off, which saves power but leaves that portβs pins unusable. Step 1 must appear before any access to GPIOA->MODER or GPIOA->ODR.
Step 2: Configure PA5 as an output. The register that controls this is GPIOA->MODER, the Mode Register for Port A. MODER is a 32-bit register that stores the mode of every pin in Port A. Each pin can be in one of four modes: input, output, alternate function (for peripherals like UART), or analog. Because there are four possibilities, two bits are needed to encode the mode of each pin, since a single bit could only represent two states. With 16 pins per port and two bits per pin, MODER is exactly 32 bits wide. The four possible two-bit encodings are:
For pin \(n\text{,}\) MODER uses bits \(2n\) (lower) and \(2n+1\) (upper). For PA5, that is bits 10 and 11. Output mode is encoded as 01: bit 10 set to 1, bit 11 cleared to 0. That is why Step 2 takes two lines: one to set bit 10, one to clear bit 11.
Steps 3 and 4: Drive PA5 high and low. The register here is GPIOA->ODR, the Output Data Register for Port A, which holds whether each pin is HIGH (3.3 V, indicated by a 1 in the corresponding bit) or LOW (0 V, indicated by a 0). Like every register, it is 32 bits wide, but a port has at most 16 pins, so only bits 15 down to 0 are used, one per pin, and bits 31 down to 16 are reserved, which means they are unused. GPIOA->ODR |= LED_PIN sets bit 5 to 1, pulling PA5 to 3.3 V and turning the LED on. GPIOA->ODR &= ~LED_PIN clears bit 5 to 0, pulling PA5 to 0 V and turning the LED off. Inside while(1), these two operations alternate forever, which makes the LED blink.
#include "stm32c0xx.h" // All register definitions are here
#define GPIOAEN (1U << 0) // RCC->IOPENR bit 0: clock for GPIOA
#define LED_PIN (1U << 5) // GPIOA bit 5: the on-board LED
int main(void) {
RCC->IOPENR |= GPIOAEN; // Step 1: enable GPIOA clock
GPIOA->MODER |= (1U << 10); // Step 2a: set bit 10
GPIOA->MODER &= ~(1U << 11); // Step 2b: clear bit 11 β PA5 output
while (1) {
GPIOA->ODR |= LED_PIN; // Step 3: bit 5 = 1 β LED on
for (int i = 0; i < 100000; i++);
GPIOA->ODR &= ~LED_PIN; // Step 4: bit 5 = 0 β LED off
for (int i = 0; i < 100000; i++);
}
return 0; // never reached
}
A peripheral register is a storage location inside a hardware peripheral, such as a GPIO port, that the CPU reads to learn the peripheralβs status and writes to control it.
One register contains many independent settings: for example, the MODER register holds the mode of all 16 pins in a port at once, and a pinβs mode determines whether that pin acts as an input, an output, or something else.
The simplest of blinkyβs registers is GPIOA->ODR, the Output Data Register for Port A, which holds whether a pin is HIGH (3.3 V, indicated by a 1 in the corresponding bit) or LOW (0 V, indicated by a 0). A register is just a row of bits, and in ODR each bit is one pinβs on/off switch (bit n drives pin PAn):
Like every register, ODR is 32 bits wide, but a port has at most 16 pins, so only bits 15 down to 0 are used, and the table shows just those 16. Bits 31 down to 16 are reserved, which means they are unused.
That line uses the left shift operator, <<. Writing 1U << n shifts the value 1 left by n bit positions, leaving a single 1 at position n and zeros everywhere else:
The U makes it an unsigned 32-bit value, so that the leading bit isnβt interpreted as a way to indicate a negative number, which could lead to unintended shift behavior around that bit.
This 32-bit number with all zeros and a single 1 in the bit we care about is called a bit mask. Naming it with #define makes your code more readable than a bare (1U<<5).
To turn the LED on in the while loop, we have to set bit 5 of ODR and leave the other bits exactly as they are, and bitwise OR is the operator that does that. First the operator on its own: | compares two values one column at a time, and the result bit is 1 if either input bit is 1:
What we want is to set one individual bit in a register without touching any of the others. reg |= mask, which is shorthand for reg = reg | mask, does exactly that: it sets the maskβs 1-bits and leaves every other bit alone. Turn PA5 on without disturbing PA2:
To turn the LED back off we need to clear the corresponding bit (that is, set it to 0) in ODR, and that takes two operators. Bitwise & (AND) keeps a 1 only where both inputs are 1, so AND with 0 forces a bit to 0, and AND with 1 leaves it unchanged:
RCC->IOPENR |= GPIOAEN; // GPIOAEN is (1U << 0) - the mask for bit 0
IOPENR (I/O Port Enable Register) is another row of bits, but here each bit is one whole portβs clock: bit 0 = Port A, bit 1 = Port B, bit 2 = Port C.
A 1 in that bit turns the portβs clock on, which gives the port power so its pins can be used. A 0 turns it off, which saves power but leaves that portβs pins unusable.
GPIOA->MODER |= (1U << 10); // Step 2a: set bit 10
GPIOA->MODER &= ~(1U << 11); // Step 2b: clear bit 11 β PA5 output
MODER is Port Aβs Mode Register, and it holds two bits per pin, so 16 pins fill one 32-bit register: pin 0 has bits 0 and 1, pin 1 has bits 2 and 3, and in general pin n has bits 2n and 2n+1. PA5 therefore has bits 10 and 11:
Output mode is 01, so write a 1 into bit 10 and a 0 into bit 11, which is exactly what the two lines above do. Setting bit 10 alone is not enough: Port Aβs MODER resets to 0xEBFF FFFF, so both of PA5βs bits are 1 at reset and the pair reads 11, which is analog mode. Bit 11 has to be cleared explicitly.
while (1) {
GPIOA->ODR |= LED_PIN; // Step 3: bit 5 = 1 β 3.3 V β LED on
for (int i = 0; i < 100000; i++); // delay
GPIOA->ODR &= ~LED_PIN; // Step 4: bit 5 = 0 β 0 V β LED off
for (int i = 0; i < 100000; i++); // delay
}
ODR is the Output Data Register: bit 5 defines PA5βs voltage.