C123 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 << 0 is a li, a lui and an sw to 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_SET line runs, and i count up inside delay.

Predict

The loop while (1) GPIO_TOGGLE = 1; compiles to four instructions per pass. At what frequency does P0 square-wave?

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:

WriteEffect
GPIO_SET = mpins in m go high
GPIO_CLR = mpins in m go low
GPIO_TOGGLE = mpins 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 232−1 to 0.

Worked example

How many passes of the 6-cycle delay loop make 1 ms?

Show the solution
  1. Cycles in 1 ms. At 10 MHz, 1 ms is 10,000 cycles.
  2. Passes of the loop. Each pass takes 6 cycles: 100006≈1667passes
  3. 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
  1. Fold the constant. The compiler folds 1 << 3 to 8.
  2. Load the value. li t0, 8 puts the value in a register.
  3. Load the address. lui t1, 0x40000 loads the top of the address.
  4. Store. sw t0, 16(t1) stores the value at 0x4000_0000+16=0x4000_0010
  5. 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
  1. One toggle. 3 instructions at 100 ns: 3×100ns=300ns
  2. One cycle. Two toggles make 600 ns, so f=1600ns≈1.67MHz
  3. Compare with MicroPython. Its cycle takes 80 µs: 80μs600ns≈133 so C is 133 times faster.
  4. 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?

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.