---
title: Problem set 11
heading: "Problem set 11: languages, testing and whole-system design"
eyebrow: C12 · Problem set 11
type: problems
---

::: hook
These problems use lessons 49 and 50, comparing MicroPython and C and testing embedded code, and then draw on the whole course for design questions. MicroPython hardware calls take about 40 µs; C runs one instruction per cycle at 10 MHz; the board draws about 2 mA awake.
:::

::: pool
pick: 12

::: check
kind: parts
given: n = 50 | 500; k = 10 | 40; f = 1 | 10

## A job makes {n} hardware calls, {f} times a second. In MicroPython each call takes 40 µs; in C each takes {k} instructions. The board draws 2 mA at 3.3 V while awake.

::: part
answer: 3.3*2e-3*n*40e-6*f*1e6
unit: µW
tolerance: 0.5%

### What average power does the MicroPython version spend awake?

::: misses
- [any] $VI \times$ awake time per second.
:::
:::

::: part
answer: 40/(k*0.1)
tolerance: 0.5%

### How many times less awake time does C need?

::: misses
- [any] 40 µs ÷ ({k} × 0.1 µs).
:::
:::
:::

::: check
kind: parts
given: F = 128 | 512 | 1024; R = 8 | 32 | 128

## A chip has {F} KB of flash and {R} KB of RAM. MicroPython needs about 256 KB of flash and 16 KB of RAM before any program.

::: part
answer: step(F - 256)*step(R - 16)
tolerance: 0

### Can it run MicroPython (1 for yes)?

::: misses
- [any] Both must be large enough.
:::
:::

::: part
answer: step(F - 256)*step(R - 16)*(R - 16)
unit: KB
tolerance: 0

### If so, how much RAM is left for the program and its heap (0 if not)?

::: misses
- [any] RAM less 16 KB.
:::
:::
:::

::: check
kind: parts
given: F = 256 | 512; b = 16 | 32; s = 4 | 8

## A chip with {F} KB of flash keeps a {b} KB bootloader and two equal firmware slots (A and B) for safe updates; {s} KB is reserved for a settings log.

::: part
answer: (F - b - s)/2
unit: KB
tolerance: 0.5%

### How large can the firmware image be?

::: misses
- [F - b - s] Two slots share the space.
- [any] (flash − bootloader − log) ÷ 2.
:::
:::

::: part
answer: (F - b - s)/2*1024/256*0.5 + (F - b - s)/2/4*30
unit: ms
tolerance: 0.5%

### How long does writing a full image take, at 0.5 ms per 256-byte page plus 30 ms per 4 KB sector erased?

::: misses
- [any] Pages × page time + sectors × erase time.
:::
:::
:::

::: check
kind: parts
given: N = 8 | 12 | 16; t = 5 | 50

## A conversion function takes an {N}-bit input; each test on the board takes {t} µs.

::: part
answer: 2^N*t/1e6
unit: s
tolerance: 0.5%

### How long would testing every input take?

::: misses
- [any] $2^N$ tests.
:::
:::

::: part
answer: 6
tolerance: 0

### How many tests are 0, 1, the two values around the middle, and the two at the top?

::: misses
- [any] Six boundary values.
:::
:::
:::

::: check
kind: parts
given: p = 0.01 | 0.05 | 0.2; c = 0.99 | 0.999

## An intermittent bug shows up in each test run with probability {p}.

::: part
answer: ceil(ln(1 - c)/ln(1 - p))
tolerance: 0

### How many runs give a {c} chance of seeing it at least once?

::: misses
- [any] Solve $1 - (1 - p)^n \ge c$.
:::
:::

::: part
answer: 1/p
tolerance: 0.5%

### How many runs does it take on average to see it the first time?

::: misses
- [any] The mean of a geometric distribution, $1/p$.
:::
:::
:::

::: check
kind: parts
given: q = 8 | 12 | 16; s = 1 | 2 | 3

## A test checks a Q-format result (with {q} fraction bits) against a floating-point reference after {s} rounding step(s), each off by at most half an LSB.

::: part
answer: s*0.5/2^q
tolerance: 0.5%

### What tolerance should the test allow?

::: misses
- [any] {s} × half of $2^{-q}$.
:::
:::

::: part
answer: s*0.5
unit: LSB
tolerance: 0.5%

### In units of the last bit?

::: misses
- [any] Half an LSB per rounding.
:::
:::
:::

::: check
kind: parts
given: b = 300 | 800; m = 10 | 25; c = 250 | 700

## An ISR's cycle budget is {b} cycles, and the team wants {m}% margin. A test measures it at {c} cycles.

::: part
answer: step(b*(1 - m/100) - c)
tolerance: 0

### Does it pass (1 for yes)?

::: misses
- [any] It must stay under the budget less the margin.
:::
:::

::: part
answer: b*(1 - m/100)
unit: cycles
tolerance: 0.5%

### What is the most it may take?

::: misses
- [any] Budget × (1 − margin).
:::
:::
:::

::: check
kind: parts
given: p = 0.5 | 0.8 | 0.95; s = 20 | 100

## In a MicroPython program, a fraction {p} of the time is spent in one loop, which is rewritten in C and runs {s} times faster.

::: part
answer: 1/((1 - p) + p/s)
tolerance: 0.5%

### What is the whole program's speedup?

::: misses
- [s] Only part of the program got faster.
- [any] $1/((1 - p) + p/s)$.
:::
:::

::: part
answer: 1/(1 - p)
tolerance: 0.5%

### What is the most it could ever gain, however fast the C?

::: misses
- [any] $1/(1 - p)$.
:::
:::
:::

::: check
kind: parts
given: r = 1 | 10; b = 8 | 16; d = 30 | 365; F = 1 | 4

## A logger stores {b}-byte records {r} times a second in {F} MB of flash.

::: part
answer: F*1048576/(b*r)/86400
unit: days
tolerance: 0.5%

### How many days does the flash hold?

::: misses
- [any] Bytes ÷ (bytes per record × records per second) ÷ 86,400.
:::
:::

::: part
answer: step(F*1048576/(b*r)/86400 - d)
tolerance: 0

### Does it hold {d} days without wrapping (1 for yes)?

::: misses
- [any] Compare with {d} days.
:::
:::
:::

::: check
kind: parts
given: a = 0.01 | 0.03; c = 0.5 | 2; p = 1 | 5; u = 0.2 | 2

## A sensor event's path: acquisition ({a} ms), conversion and filtering ({c} ms), a main loop that may be away for up to {p} ms, and a UART report ({u} ms).

::: part
answer: a + c + p + u
unit: ms
tolerance: 0.5%

### What is the worst-case latency from event to report?

::: misses
- [any] Add the stages.
:::
:::

::: part
answer: p/(a + c + p + u)*100
unit: %
tolerance: 0.5%

### What share of it is the main loop's wait?

::: misses
- [any] Wait ÷ total.
:::
:::
:::

::: check
kind: parts
given: s = 4 | 6; e = 3 | 5; t = 1 | 10

## A state machine with {s} states and {e} events is tested off the board in a simulation that runs each transition test in {t} µs, against 20 ms each on the board.

::: part
answer: s*e
tolerance: 0

### How many transition tests are there?

::: misses
- [any] States × events.
:::
:::

::: part
answer: 20000/t
tolerance: 0.5%

### How many times faster is the simulation?

::: misses
- [any] 20 ms ÷ {t} µs.
:::
:::
:::

::: check
kind: parts
given: n = 100 | 1000; g = 2 | 8; d = 4 | 16

## A MicroPython program allocates {n} small objects a second; each garbage collection pauses it for {g} ms and happens every {d} KB of allocations, each object taking 32 bytes.

::: part
answer: n*32/(d*1024)
unit: collections/s
tolerance: 0.5%

### How often does it collect?

::: misses
- [any] Bytes allocated per second ÷ bytes between collections.
:::
:::

::: part
answer: g
unit: ms
tolerance: 0.5%

### What jitter does that add to a periodic task?

::: misses
- [any] One collection's pause.
:::
:::
:::

::: check
kind: parts
given: N = 100 | 500; f = 0.5 | 2; h = 100 | 1000

## {N} devices in the field each reset by a rare bug at a rate of {f} per 1000 device-hours.

::: part
answer: N*f/1000*24
unit: resets/day
tolerance: 0.5%

### How many resets happen across the fleet each day?

::: misses
- [any] Devices × rate × 24 h.
:::
:::

::: part
answer: 1 - exp(-f/1000*h)
tolerance: 0.5%

### What is the chance a single test device shows it within {h} hours?

::: misses
- [any] $1 - e^{-\lambda t}$.
:::
:::
:::

::: check
kind: parts
given: t = 2 | 8; n = 200 | 1000; c = 0.5 | 2

## A regression suite has {n} tests. On the board each takes {t} s, including flashing and reset; in a simulator each takes {c} ms.

::: part
answer: n*t/60
unit: min
tolerance: 0.5%

### How long does the suite take on the board?

::: misses
- [any] Tests × time.
:::
:::

::: part
answer: n*c/1000
unit: s
tolerance: 0.5%

### And in the simulator?

::: misses
- [any] Tests × time.
:::
:::
:::

::: check
kind: parts
given: Q = 1000 | 2400; d = 1 | 5; Is = 5 | 20; Ia = 3 | 8; I = 100 | 500

## A battery-powered data logger with {Q} mAh must last a year. It sleeps at {Is} µA and is awake at {Ia} mA for {d}% of the time; its sensor draws {I} µA continuously.

::: part
answer: (Is/1000*(1 - d/100) + Ia*d/100 + I/1000)
unit: mA
tolerance: 0.5%

### What is its average current?

::: misses
- [any] Sleep, awake and sensor, each weighted by its time.
:::
:::

::: part
answer: step(Q/(Is/1000*(1 - d/100) + Ia*d/100 + I/1000) - 8766)
tolerance: 0

### Does it last a year (1 for yes)?

::: misses
- [any] Battery life must exceed 8766 hours.
:::
:::
:::
:::
