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

C8 · Lesson 18 · Pointers and memory-mapped registers

Lighting an LED by writing to an address

How does a program turn on an LED? On almost every microcontroller the answer is a store instruction. The chip's peripherals (GPIO pins, timers, serial ports, ADCs) each have registers that sit at fixed addresses in the memory map. Writing a word to the GPIO output register drives pins high or low; reading the input register returns what the pins see right now. In C, those addresses are reached through pointers. On this board, the LEDs are at 0x20000 and the switches at 0x20004.

Play

  • Press Run, then click the switches. The program copies them to the LEDs over and over.
  • Step it instead and watch t2. Which instruction reads the switches, and which one lights the LEDs?
  • The C uses volatile. What would go wrong without it?

Predict

Without volatile, what is a compiler allowed to do with while (1) { *leds = *switches; }?

Name it

Pointers, device registers and bit manipulation

Pointers. A pointer is a variable holding an address. *p = v stores v at that address (sw); x = *p loads from it (lw). p + k for an int * points k elements, 4k bytes, further on. A pointer to a device register is just an address cast to a pointer type:

volatile unsigned *leds = (unsigned *) 0x20000;
*leds = 0x0F;           // the four low LEDs on

Memory-mapped I/O. Device registers sit in the address space. A store to one doesn't save data for later; it acts, driving pins or starting a transfer. A load returns the device's state at that moment. This board's devices are in the table in lesson 6.

Changing one bit. Device registers pack many controls into one word, one bit each, so firmware changes a single bit without disturbing the others:

CDoesAssembly
r |= (1 << k)sets bit kor with the mask
r &= ~(1 << k)clears bit kand with the inverted mask
r ^= (1 << k)flips bit kxor with the mask
(r >> k) & 1reads bit ksrli, andi 1

Each of the first three is a read-modify-write: load the register, change one bit, store it back. That's three instructions, and if an interrupt (lesson 25) changes the same register between the load and the store, its change is lost, so firmware guards these sequences or uses hardware that sets and clears bits in one write. (Many microcontrollers provide separate set and clear registers for exactly this reason.)

An LED chaser: one lit LED moving along, with a delay loop of 200 instructions between moves. Real firmware would use a timer instead of a delay loop, so the processor can do other work (or sleep) while it waits.

Worked example

The LED register holds 0x35. Firmware sets bit 1, then clears bit 4. What does it hold afterward?

Show the solution
  1. Write it in binary. 0x35 is 0011 0101.
  2. Set bit 1. That gives 0011 0111 = 0x37.
  3. Clear bit 4. That gives 0010 0111 = 0x27.

Answer the register holds 0x27.

Worked example

Read bit 6 of the switches into t1.

Show the solution
  1. Read the switches. li t0, 0x20000, then lw t1, 4(t0).
  2. Move bit 6 to the bottom. srli t1, t1, 6.
  3. Keep only that bit. andi t1, t1, 1.

Answer li t0, 0x20000, lw t1, 4(t0), srli t1, t1, 6, andi t1, t1, 1.

Worked example

A control register holds 0xA5. Its bits 7:4 are a speed field. Set the field to 9 without disturbing the other bits.

Show the solution
  1. Clear the field, then OR in the new value shifted into place. (0xA5&∼0xF0)∣(9≪4)
  2. Work it out. The AND leaves 0x05, the shifted 9 is 0x90, and the result is 0x95.
  3. In assembly. andi t1, t0, -241 (the mask ~0xF0 sign-extends from 12 bits), ori t1, t1, 0x90, then the store.
  4. Why clear first. ORing without clearing first would leave 0xB5, because the old field's 1 bits survive.

Answer the register holds 0x95.

Check

An 8-bit LED register holds 0x29, with LED 1 off. What value turns LED 1 on and leaves the others as they are?

Check

What 8-bit mask do you AND a register with to clear bit 2 and keep the others?

Check

The LEDs already show a pattern. Turn on LED 2 (bit 2) without changing any of the others. Read the pattern from the LED register; don't write it in.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

Light each LED whose switch is off, and turn off each LED whose switch is on. (Read the switches once, then stop.)

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

The switch register reads 0x5B. What is (*switches >> 0) & 1?

Check

What does volatile on a pointer to a device register guarantee?

Check

A register holds 3C. Set its bits 7:4 to 2 and leave the others alone.

(a)

What does it hold afterward?

(b)

What's left after the AND with ~0xF0, before the OR?

Check

Toggle bit a1 of a0 and put the result in a2.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Explain why

To turn on one LED in an 8-bit LED register, why does firmware read the register, change one bit and write it back, instead of just writing a value?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.