---
title: Problem set 1
heading: "Problem set 1: the board, blinking, bits and memory"
eyebrow: C12 · Problem set 1
type: problems
---

::: hook
These problems use lessons 1 to 5: the board and its datasheet, blinking in MicroPython and in C, the C habits of fixed-width types, bit operations and volatile, and budgeting flash, RAM and the stack. The board runs at 10 MHz, one instruction per cycle; each MicroPython call into the hardware takes about 40 µs.
:::

::: pool
pick: 12

::: check
kind: parts
given: Vf = 1.9 | 2.1 | 3; I = 2 | 5 | 8

## An LED with a {Vf} V forward voltage must carry {I} mA from a 3.3 V pin.

::: part
answer: (3.3 - Vf)/(I*1e-3)
unit: Ω
tolerance: 1%

### What series resistor does it need?

::: misses
- [any] $(3.3 - V_f)/I$.
:::
:::

::: part
answer: floor(50/I)
tolerance: 0

### Each on its own pin, how many such LEDs fit within the 50 mA package limit?

::: misses
- [any] Whole LEDs within 50 mA.
:::
:::
:::

::: check
kind: parts
given: t = 50 | 400 | 2500; k = 4 | 6 | 7

## A delay must last {t} µs, built from a loop of {k} cycles per pass.

::: part
answer: t*10
tolerance: 0.5%

### How many clock cycles is that?

::: misses
- [any] 10 cycles per µs.
:::
:::

::: part
answer: t*10/k
tolerance: 0.5%

### About how many passes does the loop need?

::: misses
- [any] Cycles over {k}.
:::
:::
:::

::: check
kind: parts
given: base = 0x40000000 | 0x40002000 | 0x40005000; off = 4 | 12 | 16

## A peripheral starts at {base:hex8}; a register sits at offset {off} (decimal).

::: part
answer: base + off
base: 16

### What's the register's address, in hex?

::: misses
- [any] Base plus offset.
:::
:::

::: part
answer: floor((base + off - 1073741824)/4096)
tolerance: 0

### Counting from 0x4000_0000 in 4 KiB blocks, which block number is the peripheral in?

::: misses
- [any] Each peripheral has a 0x1000-byte block.
:::
:::
:::

::: check
kind: parts
given: a = 0 | 2 | 3; b = 5 | 13 | 15

## A program drives P{a} and P{b}.

::: part
answer: 2^a + 2^b
base: 16

### What mask selects both pins, in hex?

::: misses
- [any] One bit for each pin.
:::
:::

::: part
answer: 65535 - 2^a - 2^b
base: 16

### What's `~mask & 0xFFFF`, the mask of every other pin, in hex?

::: misses
- [any] Flip the 16 bits.
:::
:::
:::

::: check
kind: parts
given: k = 3 | 4 | 6; n = 2 | 5

## A C loop toggles a pin once per pass, {k} cycles per pass; a MicroPython loop toggles it with {n} hardware calls per pass.

::: part
answer: 10000/(2*k)
unit: kHz
tolerance: 0.5%

### What frequency does the C loop make?

::: misses
- [any] Two passes per period: $10\text{ MHz}/(2k)$.
:::
:::

::: part
answer: 1000/(2*n*0.04)
unit: Hz
tolerance: 0.5%

### And the MicroPython loop?

::: misses
- [any] Each pass is {n} × 40 µs; two passes per period.
:::
:::
:::

::: check
kind: parts
given: f = 1 | 4 | 10; D = 10 | 30 | 50

## An LED blinks at {f} Hz with a {D}% duty cycle.

::: part
answer: 1000*D/100/f
unit: ms
tolerance: 0.5%

### How long is it on each period?

::: misses
- [any] $D/f$.
:::
:::

::: part
answer: 1000*(1 - D/100)/f
unit: ms
tolerance: 0.5%

### And off?

::: misses
- [any] The rest of the period.
:::
:::
:::

::: check
kind: parts
given: s = 100 | 250 | 500; n = 3 | 10 | 40

## A fixed-sleep loop sleeps {s} ms and makes {n} hardware calls (40 µs each) per pass.

::: part
answer: s + n*0.04
unit: ms
tolerance: 0.1%

### How long is each pass?

::: misses
- [any] The sleep plus the calls.
:::
:::

::: part
answer: 3600*1000/(s + n*0.04)*n*0.04/1000
unit: s
tolerance: 1%

### Compared with an exact {s} ms grid, how far behind is it after an hour of passes?

::: misses
- [any] Passes in an hour times the extra time per pass.
:::
:::
:::

::: check
kind: parts
given: a = 180 | 230 | 255; b = 90 | 140

## A `uint8_t` holds {a}; {b} is added to it.

::: part
answer: a + b - 256*floor((a + b)/256)
tolerance: 0

### What does it hold?

::: misses
- [any] Modulo 256.
:::
:::

::: part
answer: a + b
tolerance: 0

### What would a `uint16_t` hold?

::: misses
- [any] No wrap below 65,536.
:::
:::
:::

::: check
kind: parts
given: x = 0x5A3C | 0xC3F0 | 0x1E87; s = 4 | 8

## A register holds {x:hex4}.

::: part
answer: floor(x/2^s) - 16*floor(x/2^(s + 4))
base: 16

### What's the 4-bit field at bit {s}, in hex?

::: misses
- [any] Shift right {s}, keep 4 bits.
:::
:::

::: part
answer: x - (floor(x/2^s) - 16*floor(x/2^(s + 4)))*2^s + 9*2^s
base: 16

### What's the register after writing 9 into that field, in hex?

::: misses
- [any] Clear the field, then OR in 9 shifted by {s}.
:::
:::
:::

::: check
kind: parts
given: w = 6 | 10 | 12; v = 1 | 3

## A field is {w} bits wide.

::: part
answer: 2^w - 1
tolerance: 0

### What's the largest value it holds?

::: misses
- [any] $2^w - 1$.
:::
:::

::: part
answer: v*(2^w - 1)
tolerance: 0

### What's that value times {v}?

::: misses
- [any] Multiply.
:::
:::
:::

::: check
kind: parts
given: n = 12 | 50 | 200; s = 2 | 4

## A table holds {n} values of {s} bytes each, and the board has 60 KiB of RAM.

::: part
answer: n*s
unit: bytes
tolerance: 0

### How many bytes does it take?

::: misses
- [any] Count times size.
:::
:::

::: part
answer: 100*n*s/61440
unit: %
tolerance: 0.5%

### What percentage of the RAM is that?

::: misses
- [any] 60 KiB is 61,440 bytes.
:::
:::
:::

::: check
kind: parts
given: t = 5 | 20 | 100; c = 3 | 4

## A C register write takes {c} instructions; a MicroPython one takes 40 µs. A task makes {t} thousand writes.

::: part
answer: t*1000*c*0.1/1000
unit: ms
tolerance: 0.5%

### How long does it take in C?

::: misses
- [any] Writes × {c} × 100 ns.
:::
:::

::: part
answer: t*1000*0.04
unit: ms
tolerance: 0.5%

### And in MicroPython?

::: misses
- [any] Writes × 40 µs.
:::
:::
:::

::: check
kind: parts
given: R = 8 | 16 | 32; d = 1 | 3; b = 4 | 6; n = 10 | 20; f = 48 | 96

## A chip has {R} KB of RAM. The program has {d} KB of .data and {b} KB of .bss; its deepest call chain is {n} frames of {f} bytes, plus one 64-byte interrupt frame.

::: part
answer: n*f + 64
unit: bytes
tolerance: 0

### How much stack does it need?

::: misses
- [n*f] An interrupt can land on the deepest point.
- [any] Frames plus the interrupt frame.
:::
:::

::: part
answer: R*1024 - d*1024 - b*1024 - (n*f + 64)
unit: bytes
tolerance: 0

### How much RAM is left over?

::: misses
- [any] RAM less .data, .bss and stack.
:::
:::
:::

::: check
kind: parts
given: n = 128 | 512 | 2048; w = 2 | 4

## A lookup table of {n} entries of {w} bytes is declared without `const`.

::: part
answer: n*w
unit: bytes
tolerance: 0

### How much RAM does it waste?

::: misses
- [0] Without `const` it's copied into RAM at startup.
- [any] The whole table.
:::
:::

::: part
answer: n*w*2
unit: bytes
tolerance: 0

### How much memory does it occupy in all, counting its copy in flash?

::: misses
- [any] Flash for the initial values, RAM for the copy.
:::
:::
:::

::: check
kind: parts
given: d = 4 | 8 | 20; b = 64 | 256

## A recursive function puts a {b}-byte buffer on the stack and recurses {d} levels deep; each frame also holds 16 bytes of saved registers.

::: part
answer: d*(b + 16)
unit: bytes
tolerance: 0

### How much stack does the recursion take?

::: misses
- [any] Levels × (buffer + saved registers).
:::
:::

::: part
answer: b + d*16
unit: bytes
tolerance: 0

### How much if the buffer is made `static`?

::: misses
- [any] One buffer in .bss, plus the frames.
:::
:::
:::
:::
