---
title: Problem set 7
heading: "Problem set 7: communication"
eyebrow: C12 · Problem set 7
type: problems
---

::: hook
These problems use lessons 28 to 33: the UART, ring buffers and interrupt-driven serial, I²C, SPI, and serial protocols. The clock is 10 MHz; a UART byte is 10 bits on the wire; an I²C byte is 9 clocks; an SPI byte is 8.
:::

::: pool
pick: 12

::: check
kind: parts
given: b = 9600 | 57600 | 115200; n = 16 | 80

## A UART runs at {b} baud and sends {n} bytes back to back.

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

### How long is one bit?

::: misses
- [any] $1/\text{baud}$.
:::
:::

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

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

::: misses
- [any] 10 bits per byte.
:::
:::
:::

::: check
kind: parts
given: b = 38400 | 115200 | 460800

## The board must run its UART at {b} baud.

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

### What BAUDDIV?

::: misses
- [any] $f_\text{clk}/\text{baud}$, rounded.
:::
:::

::: part
answer: 100*(1e7/round(1e7/b) - b)/b
unit: %
tolerance: 1%

### How far off is the actual rate, in percent?

::: misses
- [any] Compare $f_\text{clk}/\text{BAUDDIV}$ with {b}.
:::
:::
:::

::: check
kind: parts
given: b = 115200 | 921600; T = 3 | 15

## Bytes stream in at {b} baud; the main loop may be away for {T} ms.

::: part
answer: b/10*T/1000
tolerance: 1%

### How many bytes arrive meanwhile?

::: misses
- [any] $(b/10) T$.
:::
:::

::: part
answer: 2^ceil(log(b/10*T/1000)/log(2))
tolerance: 0

### What's the smallest power-of-two ring buffer that holds them?

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

::: check
kind: parts
given: h = 5003 | 260 | 70; t = 4990 | 250 | 66; N = 16 | 32

## A {N}-slot ring buffer has head = {h} and tail = {t}.

::: part
answer: h - t
tolerance: 0

### How many bytes are waiting?

::: misses
- [any] head − tail.
:::
:::

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

### Which slot does the next byte go in?

::: misses
- [any] head modulo {N}.
:::
:::
:::

::: check
kind: parts
given: c = 30 | 80; b = 115200 | 460800

## A receive ISR takes {c} cycles per byte at {b} baud.

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

### What share of the processor does it use?

::: misses
- [any] Cycles per byte × bytes per second ÷ 10 MHz.
:::
:::

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

### How much time is there between bytes?

::: misses
- [any] 10 bits per byte.
:::
:::
:::

::: check
kind: parts
given: a = 0x19 | 0x3C | 0x48 | 0x77

## An I²C device answers at address {a:hex2}.

::: part
answer: 2*a
base: 16

### What byte starts a write to it, in hex?

::: misses
- [any] Address shifted left, direction 0.
:::
:::

::: part
answer: 2*a + 1
base: 16

### And a read?

::: misses
- [any] Direction bit 1.
:::
:::
:::

::: check
kind: parts
given: f = 100 | 400; n = 3 | 5

## A register read over I²C at {f} kHz sends {n} bytes in all, plus about two clocks for the start, repeated start and stop.

::: part
answer: (9*n + 2)*1000/f
unit: µs
tolerance: 1%

### About how long does it take?

::: misses
- [any] 9 clocks per byte, plus two.
:::
:::

::: part
answer: 1e6/((9*n + 2)*1000/f)
tolerance: 1%

### About how many such reads fit in a second?

::: misses
- [any] One second over one read.
:::
:::
:::

::: check
kind: parts
given: hi = 0x1F | 0xE7 | 0xF9; lo = 0x40 | 0xC0

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

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

### What's the 12-bit pattern, as an unsigned number?

::: misses
- [any] Shift the 16 bits right by 4.
:::
:::

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

### What's the temperature?

::: misses
- [any] Subtract 4096 if bit 11 is set, then divide by 16.
:::
:::
:::

::: check
kind: parts
given: t = 300 | 1000; C = 80 | 250

## An I²C bus has {C} pF and needs rise times under {t} ns.

::: part
answer: t*1e-9/(0.8473*C*1e-12)/1000
unit: kΩ
tolerance: 1%

### What's the largest pull-up?

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

::: part
answer: 3.3/(t*1e-9/(0.8473*C*1e-12))*1000
unit: mA
tolerance: 1%

### What current flows through that pull-up when a device holds the line low?

::: misses
- [any] 3.3 V over the resistor.
:::
:::
:::

::: check
kind: parts
given: CLKDIV = 1 | 5 | 25; n = 2 | 6

## The SPI runs with CLKDIV = {CLKDIV} (clock $f_\text{clk}/(2 \times \text{CLKDIV})$) and sends {n} bytes.

::: part
answer: 10/(2*CLKDIV)
unit: MHz
tolerance: 0.5%

### What's the SPI clock?

::: misses
- [any] $10\text{ MHz}/(2 \times \text{CLKDIV})$.
:::
:::

::: part
answer: n*8*2*CLKDIV/10
unit: µs
tolerance: 0.5%

### How long do the bytes take?

::: misses
- [any] 8 clocks per byte.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 3 | 5; f = 1 | 4

## {k} shift registers are daisy-chained on SPI at {f} MHz.

::: part
answer: 8*k
tolerance: 0

### How many outputs do they drive?

::: misses
- [any] 8 each.
:::
:::

::: part
answer: 8*k/f
unit: µs
tolerance: 0.5%

### How long does a full update take on the wire?

::: misses
- [any] One byte per register.
:::
:::
:::

::: check
kind: parts
given: a = 150 | 220; b = 140 | 90; c = 33 | 200; h = 2 | 3

## A frame has {h} header bytes, then the payload {a}, {b}, {c}, then a checksum byte.

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

### What's the payload's sum, modulo 256?

::: misses
- [any] Add, then mod 256.
:::
:::

::: part
answer: 100*3/(3 + h + 1)
unit: %
tolerance: 0.5%

### What fraction of the frame is payload?

::: misses
- [any] 3 bytes of the whole.
:::
:::
:::

::: check
kind: parts
given: d = 4 | 8; f = 1 | 4; r = 50 | 200

## A display of {d} seven-segment digits is multiplexed: each refresh sends 16 bits over SPI at {f} MHz to light one digit, cycling through all digits {r} times a second.

::: part
answer: d*r*16/(f*1e6)*100
unit: %
tolerance: 0.5%

### What share of the SPI bus does refreshing take?

::: misses
- [any] Bits per second ÷ bit rate.
:::
:::

::: part
answer: 100/d
unit: %
tolerance: 0.5%

### What fraction of the time is each digit lit?

::: misses
- [any] One digit at a time: $1/d$.
:::
:::
:::

::: check
kind: parts
given: n = 4 | 12; b = 9600 | 115200; e = 1 | 4

## A command `SET LED n` plus newline ({n} characters in all) is sent at {b} baud; a line with a bad checksum is resent, and {e} in 100 lines are bad.

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

### How long does one line take?

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

::: part
answer: n*10/b*1000/(1 - e/100)
unit: ms
tolerance: 0.5%

### What is the average time per delivered line, counting resends?

::: misses
- [any] Divide by the fraction that get through.
:::
:::
:::

::: check
kind: parts
given: c = 8 | 16 | 32; f = 2 | 10

## A 24-bit ADC on SPI at {f} MHz is read with 8 extra command bits each time, and the CPU spends {c} cycles setting up each transfer.

::: part
answer: 32/f
unit: µs
tolerance: 0.5%

### How long is each transfer on the wire?

::: misses
- [24/f] Count the command bits too.
- [any] 32 bits ÷ clock rate.
:::
:::

::: part
answer: 1/(32/f*1e-6 + c/1e7)
unit: readings/s
tolerance: 0.5%

### What is the most readings a second it can make, back to back?

::: misses
- [any] One reading every transfer + setup time.
:::
:::
:::
:::
