C12 · Lesson 3 · Blinking an LED in C
Registers, pointers and the instructions they become
In MicroPython, led.on() hides a register write behind a method call. In C there's nothing to hide behind: a peripheral register is a memory address, and the program writes to it through a pointer. GPIO_SET = 1 << 0; turns into three machine instructions, which take 300 ns on this board, against MicroPython's 40 µs for the same job. That speed, and seeing exactly what the processor does, is why C is the language of firmware. The cost is that the program has to manage everything itself, including how long a delay lasts.
- The C program blinks the LED on P0 with a delay loop that just counts. Open The compiled program to see the RISC-V instructions it became:
GPIO_SET = 1 << 0is ali, aluiand answto address 0x4000_0010. - Each pass of the delay loop takes 6 cycles, so
delay(16667)wastes about 100,000 cycles: 10 ms. Halve the number and run again. - Press Debug and step through it a line at a time: watch the LED change when the
GPIO_SETline runs, andicount up insidedelay.
Predict
The loop while (1) GPIO_TOGGLE = 1; compiles to four instructions per pass. At what frequency does P0 square-wave?
1.25 MHz. Each pass toggles once and takes 4 cycles (400 ns), so a full period, high then low, takes 8 cycles: MHz. The same loop in MicroPython toggles once every 40 µs, a 12.5 kHz square wave: a hundred times slower.
Name it
A register is a pointer
board.h defines each register as a dereferenced pointer to its address:
#define GPIO_SET (*(volatile uint32_t *)0x40000010)
Reading the right to left: the number 0x4000_0010, cast to a pointer to a 32-bit unsigned integer, then dereferenced, so GPIO_SET = x stores x at that address and y = GPIO_IN loads from one. volatile tells the compiler every access must really happen (lesson 4).
SET, CLR and TOGGLE
GPIO_OUT holds every pin's level at once. Changing one pin by reading OUT, changing a bit and writing it back takes three steps, and an interrupt between them can lose another pin's change. The write-only registers act on just the bits written as 1:
| Write | Effect |
|---|---|
GPIO_SET = m | pins in m go high |
GPIO_CLR = m | pins in m go low |
GPIO_TOGGLE = m | pins in m flip |
with the mask m = 1 << n for pin n, or several ORed together: (1 << 0) | (1 << 3).
Delays by counting
A busy-wait loop burns cycles. Its length is the cycles per pass times the passes, so it has to be measured or counted from the compiled code (6 cycles per pass here) and changes with the compiler. A delay that reads a clock is more robust:
void wait_cycles(uint32_t n) {
uint32_t start = SYS_CYCLES;
while (SYS_CYCLES - start < n) ; // correct even when the counter wraps
}
The subtraction of unsigned values gives the elapsed count even across the counter's wrap from to 0.
Worked example
How many passes of the 6-cycle delay loop make 1 ms?
Show the solution
- Cycles in 1 ms. At 10 MHz, 1 ms is 10,000 cycles.
- Passes of the loop. Each pass takes 6 cycles:
- The call's own cost. The call itself adds a few cycles, which only matters for very short delays.
Answer about 1667 passes make 1 ms.
Worked example
What does GPIO_SET = 1 << 3; compile to, and how long does it take?
Show the solution
- Fold the constant. The compiler folds
1 << 3to 8. - Load the value.
li t0, 8puts the value in a register. - Load the address.
lui t1, 0x40000loads the top of the address. - Store.
sw t0, 16(t1)stores the value at - Time it. Three instructions take 300 ns.
Answer it compiles to li t0, 8, lui t1, 0x40000 and sw t0, 16(t1), three instructions that take 300 ns.
Worked example
A C loop toggles a pin with a store, then branches back: about 3 instructions per toggle. What frequency comes out, against MicroPython's 12.5 kHz?
Show the solution
- One toggle. 3 instructions at 100 ns:
- One cycle. Two toggles make 600 ns, so
- Compare with MicroPython. Its cycle takes 80 µs: so C is 133 times faster.
- A better way. Even so, a hardware timer or PWM channel is the better way to make a frequency, because it keeps running steadily while the processor does other work.
Answer about 1.67 MHz, 133 times faster than MicroPython's 12.5 kHz.
Check
A busy-wait loop takes 4 cycles per pass. About how many passes make 5 ms on the 10 MHz board?
Check
A loop toggles a pin once per pass, and each pass takes 3 cycles. What's the frequency of the square wave?
Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.
Check
What mask, in hex, selects pins P1 and P3 together?
Check
A GPIO register sits at offset 20 (decimal) from the GPIO base, 0x4000_0000. What address does the C header cast to a pointer, in hex?
Check
A register write takes 4 instructions in C (100 ns each) and 40 µs in MicroPython. How many times faster is C?
Check
Another part of the program can change other pins of GPIO_OUT from an interrupt. Which line turns P2 on safely?
Check
LEDs are wired to P1 and P3 (the program can use the constants A and B). Make both pins outputs and turn both LEDs on.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
Blink P0 with HALF (10) ms on and 10 ms off, timing it with SYS_CYCLES (10 cycles per microsecond).
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
A delay loop takes 4 cycles per pass on the 10 MHz board.
(a)
How many passes make 1 ms?
(b)
How long is one pass?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
A C loop toggles a pin every 3 instructions of one cycle each.
(a)
What frequency comes out?
Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.
(b)
How many times faster than MicroPython's 12.5 kHz is that?
Explain why
Why does a register appear in C as *(volatile uint32_t *)0x40000010, and what does each part do?
The number 0x40000010 is the register's address from the datasheet. Casting it to uint32_t * turns it into a pointer to a 32-bit unsigned value, matching the register's width, so loads and stores through it move whole 32-bit words. The leading * dereferences the pointer, so the whole expression behaves like a variable: assigning to it stores to the register and reading it loads from the register. volatile tells the compiler that this location can change, or has effects, outside the program's view, so it must perform every read and write exactly as written instead of caching the value in a register or removing writes that look redundant.
Practice
Keep practicing
12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.