Skip to main content

Subsection 1.2.3 Part 3: Inside blinky.c

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:
  1. Turn on the clock to GPIO Port A (the peripheral is off by default).
  2. Set PA5 to output mode so we can drive it HIGH or LOW.
  3. Set bit 5 HIGH to turn the LED on.
  4. Clear bit 5 LOW to turn the LED off, then repeat.
Each step is one or two lines of C. Here is the complete blinky.c source; we’ll go through it line by line once we have the operators it uses.
#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 before while (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.
1U       = 0b 0000 0000 0000 0000 0000 0000 0000 0001   (bit  0 is 1)
1U <<  5 = 0b 0000 0000 0000 0000 0000 0000 0010 0000   (bit  5 is 1)
1U << 10 = 0b 0000 0000 0000 0000 0000 0100 0000 0000   (bit 10 is 1)
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:
A (register bit) B (mask bit) A | B
0 0 0
0 1 1
1 0 1
1 1 1
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.
A (register bit) B (mask bit) A & B
0 0 0
0 1 0
1 0 0
1 1 1
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:
LED_PIN  = 0b 0000 0000 0000 0000 0000 0000 0010 0000   (bit 5 is 1)
~LED_PIN = 0b 1111 1111 1111 1111 1111 1111 1101 1111   (bit 5 is 0, rest are 1s)
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.
With those operators in hand, each step of blinky.c is straightforward to read.
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:
Table 1.2.6. MODER bit-pair encoding
Bits [2n+1 : 2n] Pin mode
00 Input
01 General-purpose output
10 Alternate function
11 Analog
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.

Aside: Why two lines?

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.

Activity 1.2.3. Bit Manipulation Practice.

Work through each task before moving to the next.

(a)

Write out the 32-bit binary value of 1U << 10. Which bit position is the 1? What is the value in hexadecimal?

(b)

Based on what you just read about &= and ~: what does GPIOA->MODER &= ~(1U << 11) do to bit 11? Explain in one sentence.

(c)

Compute 0b00001111 | 0b10100000 by hand (8-bit example). Write the result in binary. Which bits are 1 in the result, and why?

(d)

Starting from 0b11111111, apply & 0b11110111. What is the result? Which bit changed, and in which direction?