C12 · Problem set 6
Problem set 6: the UART, ring buffers and I²C
These problems use lessons 28 to 30: the UART, ring buffers, and I²C. The UART's rate is 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).
Check
A UART at 115200 baud sends 64 bytes with 8 data bits, 1 parity bit(s) and one stop bit.
(a)
How many bit-times does each byte take?
(b)
How long do the 64 bytes take?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A receiver resynchronizes on each start bit and samples the stop bit's center, 8.5 bit-times later. The transmitter and receiver clocks differ.
(a)
What total rate mismatch puts that sample exactly on the stop bit's edge?
(b)
If both ends may be off by the same amount in opposite directions, what may each be off by?
Check
The UART's receive register holds one byte, and a new byte finishes arriving every byte-time while data streams in at 57600 baud.
(a)
How long can the program leave a received byte unread before the next overwrites it?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
(b)
How many 10 MHz cycles is that?
Check
A burst of 200 bytes arrives at 57600 baud while the main loop removes bytes from the ring buffer at 5000 bytes per second.
(a)
How many bytes pile up by the burst's end?
(b)
What power-of-two buffer holds them, keeping one slot empty?
Check
A ring buffer of 1024 bytes uses free-running 16-bit head and tail counters. Head is 10 and tail is 65500.
(a)
How many bytes are waiting?
(b)
At what index in the array does the next byte go?
Check
A UART at 115200 baud receives continuously. Either an interrupt of 40 cycles handles each byte, or DMA moves the bytes and interrupts once per 256-byte block at 300 cycles.
(a)
What share of the 10 MHz processor does the per-byte interrupt use?
(b)
And the DMA's block interrupt?
Check
An I²C device has the 7-bit address 68.
(a)
What byte starts a write to it?
(b)
And a read?
Check
4 sensors are each read once per cycle with a register read of 2 data bytes at 400 kHz (address, register, repeated start, address, data, plus about 3 clocks).
(a)
How long does a full cycle take on the bus?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
What is the fastest cycle rate the bus allows?
Check
An I²C bus has 150 pF and must rise in 300 ns; devices sink at most 3 mA at 0.4 V.
(a)
What is the smallest pull-up allowed?
Write units the way you normally would: 1.5 kΩ, 1500 Ω and 0.0015 MΩ are the same answer.
(b)
And the largest?
Check
A humidity sensor holds the clock low (clock stretching) for 25 ms while it converts, then returns 2 bytes; the read takes clocks at 400 kHz besides.
(a)
How long is the whole transaction?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
What share of it is the bus held idle?
Check
A sensor returns 255 then 0: a 12-bit two's-complement reading, left-justified, in sixteenths of a degree.
(a)
What signed 12-bit value is it?
(b)
What temperature?
Check
A message has 4 header bytes, 8 payload bytes and a 2-byte CRC, and is COBS-encoded with a zero delimiter, at 9600 baud.
(a)
How many bytes go on the wire (COBS adds one byte for a message this short)?
(b)
How long does it take?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
The board needs 19200 baud from .
(a)
What BAUDDIV?
(b)
What is the rate's error?
Check
A command line echoes each character: the receive interrupt queues it, the main loop (whose passes take up to 1 ms) handles it, and the echo is sent at 9600 baud.
(a)
What is the worst-case delay from the start of a typed character to the end of its echo?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
Is it under the 100 ms at which a person notices lag (1 for yes)?
Check
A receive ISR of 30 cycles copies each byte into a ring buffer; a 16-byte line arrives back to back at 115,200 baud, and then the main loop parses it in 2000 cycles.
(a)
How many cycles does one line cost in all?
(b)
What share of the processor is that, over the time the line takes to arrive?
Have paper and a calculator ready. Work each problem out by hand, then enter the answer. Your first try on each one counts toward your score.
Practice
Keep practicing
15 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.