---
title: Problem set 9
heading: "Problem set 9: structure, timing, power and storage"
eyebrow: C12 · Problem set 9
type: problems
---

::: hook
These problems use lessons 36 to 44: state machines, superloops and cooperative scheduling, preemptive scheduling, latency, jitter and deadlines, watchdogs, power budgets, keeping data in flash, and bootloaders. The board runs at 10 MHz; it draws 2 mA running, 0.5 mA asleep and 0.002 mA in deep sleep, plus its peripherals and loads.
:::

::: pool
pick: 12

::: check
kind: parts
given: s = 4 | 6 | 12; e = 2 | 5

## A state machine has {s} states and {e} kinds of event.

::: part
answer: ceil(log(s)/log(2))
tolerance: 0

### How many bits does its state need?

::: misses
- [any] $\lceil\log_2 s\rceil$.
:::
:::

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

### How many entries are in its full transition table?

::: misses
- [any] One per (state, event).
:::
:::
:::

::: check
kind: parts
given: g = 1500 | 3000; y = 400; w = 1200 | 2000; r = 500; t0 = 200 | 2500

## A crossing holds green for at least {g} ms, then yellow {y} ms, walk {w} ms and red {r} ms. The button is pressed {t0} ms into green.

::: part
answer: (t0 + g + abs(t0 - g))/2
unit: ms
tolerance: 0

### When does green end?

::: misses
- [any] Whichever comes later: the press or the minimum time.
:::
:::

::: part
answer: (t0 + g + abs(t0 - g))/2 + y + w + r
unit: ms
tolerance: 0

### When does green come back?

::: misses
- [any] Add yellow, walk and red.
:::
:::
:::

::: check
kind: parts
given: c1 = 0.2 | 1; t1 = 4 | 10; c2 = 6 | 25; t2 = 200 | 500

## A superloop runs task 1 ({c1} ms every {t1} ms) and task 2 ({c2} ms every {t2} ms).

::: part
answer: 100*(c1/t1 + c2/t2)
unit: %
tolerance: 0.5%

### What's the utilization?

::: misses
- [any] $\sum C_i/T_i$.
:::
:::

::: part
answer: c2/t1
tolerance: 0.5%

### Task 2's turn can delay task 1 by its whole run time. How many of task 1's periods is that?

::: misses
- [any] Task 2's run time over task 1's period.
:::
:::
:::

::: check
kind: parts
given: W = 120 | 300; p = 1 | 2 | 4

## A task's {W} ms of work is split into {p} ms pieces, one per superloop turn.

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

### How many turns does it take?

::: misses
- [any] W/p.
:::
:::

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

### How long can it now delay another task, at most?

::: misses
- [any] One piece.
:::
:::
:::

::: check
kind: parts
given: lo = 0.1 | 0.3; hi = 0.8 | 2.5; C = 0.2 | 0.5; D = 2 | 3

## A job is started by polling in a loop whose passes take {lo} to {hi} ms; it runs for {C} ms and must finish within {D} ms of its trigger.

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

### What's its worst-case latency?

::: misses
- [any] The longest pass.
:::
:::

::: part
answer: D - hi - C
unit: ms
tolerance: 0.5%

### How much margin is left at worst (negative if it misses)?

::: misses
- [any] D − latency − C.
:::
:::
:::

::: check
kind: parts
given: f = 60 | 1000; A = 1 | 2.5; dt = 5 | 50

## A {f} Hz signal of amplitude {A} V is sampled with {dt} µs of jitter.

::: part
answer: 2*pi*f*A*dt*1e-6*1000
unit: mV
tolerance: 0.5%

### How large can a sample's error be?

::: misses
- [any] $2\pi f A\,\Delta t$.
:::
:::

::: part
answer: 100*2*pi*f*dt*1e-6
unit: %
tolerance: 0.5%

### As a percentage of the amplitude?

::: misses
- [any] Divide by A.
:::
:::
:::

::: check
kind: parts
given: g = 6 | 20; m = 3 | 5

## A main loop's longest normal pass is {g} ms; the watchdog gets a {m}× margin.

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

### What timeout?

::: misses
- [any] g × m.
:::
:::

::: part
answer: g*m*10000
tolerance: 0

### What WDT_LOAD, in cycles?

::: misses
- [any] 10,000 cycles per ms.
:::
:::
:::

::: check
kind: parts
given: w = 80 | 250; T = 30 | 100; s = 1 | 4

## A program hangs {w} ms after each start; its watchdog fires {T} ms after the last kick.

::: part
answer: floor(s*1000/(w + T))
tolerance: 0

### How many resets happen in {s} s?

::: misses
- [any] Whole cycles of w + T.
:::
:::

::: part
answer: 100*w/(w + T)
unit: %
tolerance: 0.5%

### What fraction of the time is it doing useful work?

::: misses
- [any] w out of w + T.
:::
:::
:::

::: check
kind: parts
given: Ia = 2 | 4; Is = 0.05 | 0.5; d = 0.2 | 2 | 20; Q = 220 | 1200

## A device draws {Ia} mA awake ({d}% of the time) and {Is} mA asleep, from a {Q} mAh battery.

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

### What's its average current?

::: misses
- [any] Weight each by its time.
:::
:::

::: part
answer: Q/(d/100*Ia + (1 - d/100)*Is)/24
unit: days
tolerance: 0.5%

### How many days does the battery last?

::: misses
- [any] Capacity over current, in hours, then days.
:::
:::
:::

::: check
kind: parts
given: f = 1 | 20; t = 0.5 | 3; I = 2

## A device wakes {f} times a second for {t} ms each time, drawing {I} mA while awake.

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

### What fraction of the time is it awake?

::: misses
- [any] f × t.
:::
:::

::: part
answer: I*f*t/1000
unit: mA
tolerance: 0.5%

### What do the wake-ups add to the average current?

::: misses
- [any] Current times awake fraction.
:::
:::
:::

::: check
kind: parts
given: I = 3.9 | 10; t = 10 | 50; T = 1 | 30

## A status LED drawing {I} mA flashes for {t} ms every {T} s.

::: part
answer: I*t/1000/T
unit: mA
tolerance: 0.5%

### What does it add to the average?

::: misses
- [any] I × t / T.
:::
:::

::: part
answer: 220/(I*t/1000/T)/24
unit: days
tolerance: 0.5%

### How long would it alone take to drain a 220 mAh cell?

::: misses
- [any] 220 mAh over its average, in days.
:::
:::
:::

::: check
kind: parts
given: k = 3 | 10; r = 100 | 1500

## A fault resets a device {k} times a day; each reset takes {r} ms to recover from.

::: part
answer: k*r/1000
unit: s
tolerance: 0.5%

### How long is it out of action each day?

::: misses
- [any] k × r.
:::
:::

::: part
answer: 100*k*r/1000/86400
unit: %
tolerance: 1%

### What percentage of the day is that?

::: misses
- [any] Over 86,400 s.
:::
:::
:::

::: check
kind: parts
given: C1 = 1 | 2; T1 = 4 | 5; C2 = 5 | 7; let r0 = C1 + C2; let r1 = C2 + ceil(r0/T1)*C1; let r2 = C2 + ceil(r1/T1)*C1; let r3 = C2 + ceil(r2/T1)*C1; let r4 = C2 + ceil(r3/T1)*C1

## Under a preemptive scheduler, task 1 runs {C1} ms every {T1} ms at high priority; task 2 needs {C2} ms.

::: part
answer: r4
unit: ms
tolerance: 0

### What is task 2's worst-case response time?

::: misses
- [C1 + C2] Task 1 may run again before task 2 finishes.
- [any] Iterate $R = C_2 + \lceil R/T_1\rceil C_1$.
:::
:::

::: part
answer: C2
unit: ms
tolerance: 0

### In a superloop that runs task 1 and then task 2, how long can task 1 wait between the end of one run and the start of the next?

::: misses
- [any] Task 2 runs to completion in between.
:::
:::
:::

::: check
kind: parts
given: E = 10000 | 100000; w = 60 | 1440; r = 16 | 32; n = 1 | 4

## A thermostat saves its schedule {w} times a day as {r}-byte records appended to a log over {n} 4 KB flash sector(s) rated for {E} erases.

::: part
answer: E*n*floor(4096/r)/w/365.25
unit: years
tolerance: 0.5%

### How long does the flash last?

::: misses
- [E/w/365.25] Appending records saves many erases.
- [any] $E\,n\lfloor 4096/r\rfloor$ saves.
:::
:::

::: part
answer: E/w/365.25
unit: years
tolerance: 0.5%

### How long if it erased a sector for every save instead?

::: misses
- [any] Endurance ÷ saves per year.
:::
:::
:::

::: check
kind: parts
given: c = 1 | 2; m = 10 | 30

## A high-priority task needs a mutex that a low-priority task holds for {c} ms; a medium-priority task runs for {m} ms.

::: part
answer: c + m
unit: ms
tolerance: 0.5%

### How long can the high task be blocked without priority inheritance?

::: misses
- [c] The medium task can preempt the mutex's holder.
- [any] Critical section plus the medium task.
:::
:::

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

### And with it?

::: misses
- [any] Just the critical section.
:::
:::
:::

::: check
kind: parts
given: F = 256 | 512 | 1024; B = 16 | 32; S = 64 | 150; b = 115200 | 921600

## A device has {F} KB of flash and a {B} KB bootloader, and receives a {S} KB update over a {b}-baud UART.

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

### How large can each A/B slot be?

::: misses
- [F - B] Two slots share the space.
- [any] Not yet. $(F - B)/2$.
:::
:::

::: part
answer: S*1024*10/b
unit: s
tolerance: 0.5%

### How long does the transfer take?

::: misses
- [S*1024*8/b] A UART sends 10 bits per byte.
- [any] Not yet. $10S/\text{baud}$.
:::
:::

::: right
Right. Two slots cost half the space and make an interrupted update harmless.
:::
:::
:::
