---
title: Problem set 6
heading: "Problem set 6: the UART, ring buffers and I²C"
eyebrow: C12 · Problem set 6
type: problems
---

::: hook
These problems use lessons 28 to 30: the UART, ring buffers, and I²C. The UART's rate is $10^7/\text{BAUDDIV}$ baud; its receive register holds one byte. An I²C byte takes 9 clocks with its acknowledge, and the bus needs a pull-up strong enough to rise in time but weak enough for a device to pull low (sinking at most 3 mA to 0.4 V).
:::

::: pool
pick: 12

::: check
kind: parts
given: b = 9600 | 115200 | 921600; n = 64 | 1000; p = 0 | 1

## A UART at {b} baud sends {n} bytes with 8 data bits, {p} parity bit(s) and one stop bit.

::: part
answer: 10 + p
unit: bits
tolerance: 0

### How many bit-times does each byte take?

::: misses
- [8 + p] Count the start and stop bits.
- [any] Start + 8 + parity + stop.
:::
:::

::: part
answer: n*(10 + p)/b*1000
unit: ms
tolerance: 0.5%

### How long do the {n} bytes take?

::: misses
- [any] Bytes × bit-times ÷ baud.
:::
:::
:::

::: check
kind: parts
given: s = 6.5 | 7.5 | 8.5 | 9.5 | 10.5 | 11.5

## A receiver resynchronizes on each start bit and samples the stop bit's center, {s} bit-times later. The transmitter and receiver clocks differ.

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

### What total rate mismatch puts that sample exactly on the stop bit's edge?

::: misses
- [any] Half a bit of drift over {s} bits.
:::
:::

::: part
answer: 0.5/s*100/2
unit: %
tolerance: 0.5%

### If both ends may be off by the same amount in opposite directions, what may each be off by?

::: misses
- [any] Half the total.
:::
:::
:::

::: check
kind: parts
given: b = 9600 | 57600 | 115200

## The UART's receive register holds one byte, and a new byte finishes arriving every byte-time while data streams in at {b} baud.

::: part
answer: 10/b*1e6
unit: µs
tolerance: 0.5%

### How long can the program leave a received byte unread before the next overwrites it?

::: misses
- [any] One byte-time: 10 bits.
:::
:::

::: part
answer: 10/b*1e7
tolerance: 0.5%

### How many 10 MHz cycles is that?

::: misses
- [any] Time × $10^7$.
:::
:::
:::

::: check
kind: parts
given: B = 200 | 1000; b = 57600 | 115200; r = 2000 | 5000

## A burst of {B} bytes arrives at {b} baud while the main loop removes bytes from the ring buffer at {r} bytes per second.

::: part
answer: B*(1 - r/(b/10))
unit: bytes
tolerance: 0.5%

### How many bytes pile up by the burst's end?

::: misses
- [B] The loop removes some while the burst arrives.
- [any] Arrivals less removals over the burst's duration.
:::
:::

::: part
answer: 2^ceil(log(B*(1 - r/(b/10)) + 1)/log(2))
unit: bytes
tolerance: 0

### What power-of-two buffer holds them, keeping one slot empty?

::: misses
- [any] The next power of two above that.
:::
:::
:::

::: check
kind: parts
given: h = 10 | 100; t = 65500 | 65450; N = 256 | 1024

## A ring buffer of {N} bytes uses free-running 16-bit head and tail counters. Head is {h} and tail is {t}.

::: part
answer: h - t + 65536
unit: bytes
tolerance: 0

### How many bytes are waiting?

::: misses
- [h - t] The counters wrap at 65,536; subtract modulo $2^{16}$.
- [any] $(h - t) \bmod 65536$.
:::
:::

::: part
answer: h - N*floor(h/N)
tolerance: 0

### At what index in the array does the next byte go?

::: misses
- [any] head masked with $N - 1$.
:::
:::
:::

::: check
kind: parts
given: b = 115200 | 921600; c = 40 | 100; n = 64 | 256

## A UART at {b} baud receives continuously. Either an interrupt of {c} cycles handles each byte, or DMA moves the bytes and interrupts once per {n}-byte block at 300 cycles.

::: part
answer: b/10*c/1e7*100
unit: %
tolerance: 0.5%

### What share of the 10 MHz processor does the per-byte interrupt use?

::: misses
- [any] Bytes per second × cycles ÷ $10^7$.
:::
:::

::: part
answer: b/10/n*300/1e7*100
unit: %
tolerance: 0.5%

### And the DMA's block interrupt?

::: misses
- [any] Blocks per second × 300 cycles.
:::
:::
:::

::: check
kind: parts
given: a = 72 | 104 | 30

## An I²C device has the 7-bit address {a:hex2}.

::: part
answer: 2*a
tolerance: 0
base: 16
width: 2

### What byte starts a write to it?

::: misses
- [any] Address shifted left one, with a 0 for write.
:::
:::

::: part
answer: 2*a + 1
tolerance: 0
base: 16
width: 2

### And a read?

::: misses
- [any] Address shifted left one, with a 1 for read.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 4 | 8; n = 2 | 6; f = 100 | 400

## {k} sensors are each read once per cycle with a register read of {n} data bytes at {f} kHz (address, register, repeated start, address, data, plus about 3 clocks).

::: part
answer: k*(9*(3 + n) + 3)/f
unit: ms
tolerance: 0.5%

### How long does a full cycle take on the bus?

::: misses
- [any] Per read: $9(3 + n) + 3$ clocks.
:::
:::

::: part
answer: 1000/(k*(9*(3 + n) + 3)/f)
unit: Hz
tolerance: 0.5%

### What is the fastest cycle rate the bus allows?

::: misses
- [any] The reciprocal.
:::
:::
:::

::: check
kind: parts
given: C = 50 | 150 | 400; t = 300 | 1000

## An I²C bus has {C} pF and must rise in {t} ns; devices sink at most 3 mA at 0.4 V.

::: part
answer: (3.3 - 0.4)/3
unit: kΩ
tolerance: 0.5%

### What is the smallest pull-up allowed?

::: misses
- [3.3/3] At 3 mA the line must still be below 0.4 V.
- [any] $(3.3 - 0.4)/3\text{ mA}$.
:::
:::

::: part
answer: t/(0.8473*C)
unit: kΩ
tolerance: 0.5%

### And the largest?

::: misses
- [any] $t_r/(0.8473C)$.
:::
:::
:::

::: check
kind: parts
given: c = 11 | 25 | 85; n = 2 | 3; f = 100 | 400

## A humidity sensor holds the clock low (clock stretching) for {c} ms while it converts, then returns {n} bytes; the read takes $9(3 + n) + 3$ clocks at {f} kHz besides.

::: part
answer: c + (9*(3 + n) + 3)/f
unit: ms
tolerance: 0.5%

### How long is the whole transaction?

::: misses
- [any] Conversion plus the clocks.
:::
:::

::: part
answer: c/(c + (9*(3 + n) + 3)/f)*100
unit: %
tolerance: 0.5%

### What share of it is the bus held idle?

::: misses
- [any] Stretch time over total.
:::
:::
:::

::: check
kind: parts
given: hi = 231 | 255 | 230; lo = 0 | 16 | 128

## A sensor returns {hi} then {lo}: a 12-bit two's-complement reading, left-justified, in sixteenths of a degree.

::: part
answer: floor((hi*256 + lo)/16) - 4096
tolerance: 0

### What signed 12-bit value is it?

::: misses
- [floor((hi*256 + lo)/16)] The top bit is set: subtract 4096.
- [any] Shift right by 4, then subtract 4096 when bit 11 is set.
:::
:::

::: part
answer: (floor((hi*256 + lo)/16) - 4096)/16
unit: °C
tolerance: 0.5%

### What temperature?

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

::: check
kind: parts
given: n = 8 | 32; b = 9600 | 115200; h = 3 | 4

## A message has {h} header bytes, {n} payload bytes and a 2-byte CRC, and is COBS-encoded with a zero delimiter, at {b} baud.

::: part
answer: h + n + 2 + 2
unit: bytes
tolerance: 0

### How many bytes go on the wire (COBS adds one byte for a message this short)?

::: misses
- [h + n + 2] COBS adds a byte, and the delimiter is one more.
- [any] Header + payload + CRC + COBS byte + delimiter.
:::
:::

::: part
answer: (h + n + 4)*10/b*1000
unit: ms
tolerance: 0.5%

### How long does it take?

::: misses
- [any] Bytes × 10 bits ÷ baud.
:::
:::
:::

::: check
kind: parts
given: D = 9600 | 19200 | 230400

## The board needs {D} baud from $10^7/\text{BAUDDIV}$.

::: part
answer: round(1e7/D)
tolerance: 0

### What BAUDDIV?

::: misses
- [any] Nearest whole number to $10^7/b$.
:::
:::

::: part
answer: abs(1e7/round(1e7/D) - D)/D*100
unit: %
tolerance: 2%

### What is the rate's error?

::: misses
- [any] |actual − wanted| ÷ wanted.
:::
:::
:::

::: check
kind: parts
given: b = 9600 | 115200; L = 1 | 5 | 20

## A command line echoes each character: the receive interrupt queues it, the main loop (whose passes take up to {L} ms) handles it, and the echo is sent at {b} baud.

::: part
answer: 10/b*1000 + L + 10/b*1000
unit: ms
tolerance: 0.5%

### What is the worst-case delay from the start of a typed character to the end of its echo?

::: misses
- [L] The character must arrive and the echo must be sent: two byte-times.
- [any] Receive + loop + transmit.
:::
:::

::: part
answer: step(100 - (20/b*1000 + L))
tolerance: 0

### Is it under the 100 ms at which a person notices lag (1 for yes)?

::: misses
- [any] Compare with 100 ms.
:::
:::
:::

::: check
kind: parts
given: n = 4 | 16 | 64; c = 30 | 50

## A receive ISR of {c} cycles copies each byte into a ring buffer; a {n}-byte line arrives back to back at 115,200 baud, and then the main loop parses it in 2000 cycles.

::: part
answer: n*c + 2000
tolerance: 0.5%

### How many cycles does one line cost in all?

::: misses
- [any] Per-byte ISR cost plus the parse.
:::
:::

::: part
answer: (n*c + 2000)/(n*10/115200*1e7)*100
unit: %
tolerance: 0.5%

### What share of the processor is that, over the time the line takes to arrive?

::: misses
- [any] Cycles ÷ (line time × $10^7$).
:::
:::
:::
:::
