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

C12 · Lesson 4 · C for small machines

Fixed-width types, bits, and volatile

Three habits separate firmware C from C written for a desktop. Sizes are spelled out (uint8_t, uint32_t), because an int is 16 bits on some microcontrollers and 32 on others, and a register is exactly as wide as the datasheet says. Single bits are set, cleared and tested with masks, because one register packs many settings. And variables that hardware or an interrupt can change are marked volatile, because an optimizing compiler otherwise assumes nothing changes behind its back, and will happily turn a wait loop into a loop that waits forever.

  • A timer interrupt sets done after 10 ms; main waits for it, then lights the LED. As written, done isn't volatile. Run it: the LED never lights.
  • Open the compiled program and find main's wait: the compiler read done once, before the loop, and the loop jumps to itself. Nothing inside the loop could change done, as far as the compiler can see.
  • Add volatile before int done and run again: now the loop reads done every time, and the LED lights at 10 ms.

Predict

A uint8_t count holds 250. After count = count + 10;, what does it hold?

Name it

Fixed-width types

Type (<stdint.h>)BitsRange
uint8_t, int8_t80 to 255; −128 to 127
uint16_t, int16_t160 to 65,535; −32,768 to 32,767
uint32_t, int32_t320 to 4,294,967,295; about ±2.1 × 10⁹

Arithmetic on anything narrower than int happens in int and is truncated when stored. Unsigned arithmetic wraps modulo 2n; signed overflow is undefined. On this board char is unsigned, as the RISC-V convention says.

Bit operations

GoalC
set bit nx |= 1u << n
clear bit nx &= ~(1u << n)
flip bit nx ^= 1u << n
test bit nif (x & (1u << n))
read a w-bit field at bit s(x >> s) & ((1u << w) - 1)
write field value vx = (x & ~(((1u << w) - 1) << s)) | (v << s)

volatile

volatile says a variable can change, or its accesses matter, outside the program's normal flow. The compiler must then load it every time it's read and store it every time it's written. Use it for:

  • memory-mapped registers (the header's definitions already include it);
  • variables shared between an interrupt handler and the main program;
  • a delay loop's counter, so the loop isn't removed.

It doesn't make an access atomic: a 32-bit increment is still a load, an add and a store, and an interrupt can land between them (lesson 12).

Worked example

A status register holds 0x0000_3A54. Bits 4 to 7 are a "mode" field. What mode is it in?

Show the solution
  1. Shift the field down. Shifting right by 4 moves bit 4 to bit 0: 0x3A54≫4=0x3A5
  2. Mask off the other bits. 0x3A5&0xF=0x5

Answer it is in mode 5.

Worked example

Write mode 0xC into the same field and leave every other bit alone.

Show the solution
  1. Build the mask. ∼(0xF≪4)=0xFFFF_FF0F
  2. Clear the field. 0x3A54&0xFFFF_FF0F=0x3A04
  3. OR in the new value. 0x3A04|(0xC≪4)=0x3A04|0xC0=0x3AC4

Answer the register becomes 0x3AC4.

Worked example

What do 200 + 100 give in a uint8_t, and 100 + 100 in an int8_t?

Show the solution
  1. The uint8_t sum. It wraps modulo 256: 300−256=44
  2. The int8_t sum. An int8_t holds −128 to 127, and 200 doesn't fit; on this machine it wraps to 200−256=−56
  3. Which overflow is defined. Unsigned overflow is defined behavior in C; signed overflow isn't, which is why counters and checksums use unsigned types.

Answer 44 in a uint8_t, and −56 in an int8_t on this machine.

Check

Read the 4-bit field at bit 4 of BEEF. What value is it, in hex?

Check

A register byte holds B9. What does it hold after x &= ~(1u << 0);, in hex?

Check

A uint8_t holds 250. What does it hold after adding 60?

Check

An array of 64 elements, each 1 byte(s) wide (uint8_t, uint16_t or uint32_t): how many bytes of RAM does it use?

Check

What's the largest value a 5-bit unsigned field can hold?

Check

Which variable needs volatile?

Check

This program waits for a timer interrupt and then lights the LED, but the LED never lights. Fix it.

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

X is 48879. Print the 4-bit field that starts at bit S (4), then print X with that field replaced by 12 and every other bit unchanged.

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 register holds 0E97. A field of 4 bits starts at bit 4.

(a)

What is the field's value?

(b)

What does the register hold after the field is cleared?

Check

200 and 100 are added in 8-bit variables.

(a)

What does a uint8_t hold?

(b)

What does the same bit pattern mean as an int8_t?

Explain why

Why can an optimizing compiler turn while (!done) ; into an endless loop when done isn't volatile?

Practice

Keep practicing

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