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
doneafter 10 ms;mainwaits for it, then lights the LED. As written,doneisn't volatile. Run it: the LED never lights. - Open the compiled program and find
main's wait: the compiler readdoneonce, before the loop, and the loop jumps to itself. Nothing inside the loop could changedone, as far as the compiler can see. - Add
volatilebeforeint doneand run again: now the loop readsdoneevery time, and the LED lights at 10 ms.
Predict
A uint8_t count holds 250. After count = count + 10;, what does it hold?
- The addition happens in
int(C promotes small types first), giving 260, but storing it back into 8 bits keeps only the low 8: . Unsigned types wrap like this by definition; signed overflow, by contrast, is undefined in C, which is one more reason firmware uses unsigned types for counters and registers.
Name it
Fixed-width types
Type (<stdint.h>) | Bits | Range |
|---|---|---|
uint8_t, int8_t | 8 | 0 to 255; −128 to 127 |
uint16_t, int16_t | 16 | 0 to 65,535; −32,768 to 32,767 |
uint32_t, int32_t | 32 | 0 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 ; signed overflow is undefined. On this board char is unsigned, as the RISC-V convention says.
Bit operations
| Goal | C |
|---|---|
| set bit n | x |= 1u << n |
| clear bit n | x &= ~(1u << n) |
| flip bit n | x ^= 1u << n |
| test bit n | if (x & (1u << n)) |
| read a w-bit field at bit s | (x >> s) & ((1u << w) - 1) |
| write field value v | x = (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
- Shift the field down. Shifting right by 4 moves bit 4 to bit 0:
- Mask off the other bits.
Answer it is in mode 5.
Worked example
Write mode 0xC into the same field and leave every other bit alone.
Show the solution
- Build the mask.
- Clear the field.
- OR in the new value.
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
- The
uint8_tsum. It wraps modulo 256: - The
int8_tsum. Anint8_tholds −128 to 127, and 200 doesn't fit; on this machine it wraps to - 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?
The compiler reasons only about the code it can see. Inside the loop nothing writes to done and nothing calls a function that might, so as far as the C language is concerned done can't change while the loop runs. It's therefore free to load done once before the loop and test the copy in a register on every pass, or to decide the loop either exits immediately or never exits. An interrupt handler changes done from outside that view, so the cached copy never updates. Declaring done volatile tells the compiler the value can change at any moment, which forces a fresh load from memory on every pass.
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.