C8 · Lesson 27 · Input and output, polling, interrupts and DMA
Three ways to move data between devices and memory
A serial port at 115200 baud delivers a byte about every 87 µs. An audio converter delivers 48,000 samples a second, and a fast ADC a million. Each value has to get from a device register into memory before the next one overwrites it. The processor can keep asking the device whether anything has arrived (polling), let the device interrupt it, or hand the job to a DMA controller that copies the data by itself. Which one works comes down to arithmetic: how often data arrives, and how many cycles each method spends on it.
- A polling loop: it reads the switches and copies them to the LEDs, over and over. Run it and flip some switches; the LEDs follow on the next pass.
- a0 counts the polls. Almost all of them find nothing new, and each one still costs the processor four instructions.
Predict
A UART sends each byte as a start bit, 8 data bits and a stop bit. At 115200 baud (bits per second), how many bytes arrive per second?
11,520: each byte takes 10 bit times, so 115200/10. That's one byte every 86.8 µs, which is the deadline for reading each one before the next arrives (unless the UART has a FIFO to hold a few).
Name it
Polling
The processor reads a status register in a loop until a device is ready, then moves the data. It's simple and reacts within one pass of the loop, but every pass spent waiting is wasted. Polling from a main loop that also does other work reacts within one trip around that loop, so the worst-case latency is the loop's period.
Interrupts
The device raises an interrupt when it has data, and the processor runs a handler (lesson 25). Each event costs the entry and exit overhead (saving and restoring registers, often 20 to 50 cycles on a microcontroller) plus the handler's own work. With events at rate r, c cycles each, on a clock of f,
which has to stay well under 1, leaving time for everything else.
DMA
A DMA controller is a small engine on the bus that copies data between a peripheral and memory by itself, one transfer per request from the device. The processor sets it up (source, destination, count) and gets one interrupt per block of n items, so the interrupt load falls by a factor of n. With double buffering, the DMA fills one buffer while the processor works on the other, so each block must be processed in less time than the next one takes to fill: n/r.
Serial and bus rates
| Link | Rate |
|---|---|
| UART, 1 start + 8 data + 1 stop bit | baud/10 bytes per second |
| a bus w bits wide, f transfers per second | bytes per second |
Worked example
A 48 MHz processor takes an interrupt for each byte from a 115200-baud UART, costing 60 cycles each. What fraction of its time does that use?
Show the solution
- Cycles per second. The UART delivers 11,520 bytes a second:
- The fraction.
Answer 1.44% of its time, which is comfortable.
Worked example
A 16 MHz processor must collect a 1 Msample/s ADC stream. Compare an interrupt per sample (60 cycles) with DMA in blocks of 256.
Show the solution
- An interrupt per sample. against available, a load of 375%: impossible.
- Interrupts with DMA.
- Their cost. a load of 1.5%.
- The deadline. Each block of 256 must be processed within 256 µs.
Answer an interrupt per sample would need 375% of the processor, which is impossible; DMA in blocks of 256 needs 1.5%.
Worked example
A UART receives at 1 Mbaud, 10 bits per byte, into a 16-byte FIFO that interrupts when it holds 12 bytes. How long does the processor have to respond, and how many interrupts a second does it take?
Show the solution
- Byte time. A byte arrives every 10 µs.
- Time to respond. When the interrupt fires there's room for 4 more bytes, so the handler must start emptying the FIFO within 40 µs or a byte is lost.
- Interrupt rate. against 100,000 with an interrupt per byte.
- The trade. A higher threshold means fewer interrupts but less time to respond: the FIFO trades latency tolerance against overhead.
Answer 40 µs to respond, and about 8333 interrupts a second.
Check
How many bytes per second does a 19200-baud UART carry (1 start, 8 data, 1 stop bit)?
Check
A 160 MHz processor takes 11520 interrupts a second, each costing 40 cycles. What percentage of its time do they use?
Check
A main loop checks a button once every 5 ms.
(a)
What is the longest the program can take to notice a press?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
What is the average delay, for presses at random times?
Check
A converter delivers 48000 samples a second through DMA, in blocks of 64 samples with double buffering.
(a)
How many interrupts a second does the processor take?
(b)
How long does the processor have to finish each block?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A bus moves 16 bits per transfer at 8 million transfers a second. What is its bandwidth?
Check
Read the switches (at 0x20004) and show them on the LEDs (at 0x20000) with the low four bits inverted.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A 16 MHz microcontroller must record a 500 kHz ADC stream without losing samples. Each interrupt costs about 50 cycles. Which approach works?
Check
A UART at 1000000 baud (10 bits a byte) fills a 16-byte FIFO that interrupts at 12 bytes.
(a)
How long does the handler have before a byte is lost?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
(b)
How many interrupts a second?
Check
A 500 ksample/s ADC feeds a 100 MHz core; an interrupt costs 80 cycles.
(a)
What load does an interrupt per sample put on the core?
(b)
And DMA with an interrupt per block of 64?
Explain why
Why does DMA make a fast data stream manageable when an interrupt per sample doesn't?
An interrupt has a fixed cost each time, the cycles spent entering and leaving the handler and saving registers, no matter how little data it moves. At a million samples a second that fixed cost alone can exceed the processor's whole cycle budget. A DMA controller moves each sample with its own hardware, using the bus but not the processor, and only interrupts when a whole block is done. The fixed cost is paid once per block instead of once per sample, so the load falls by the block size, and the processor's job becomes processing each block before the next one fills.
Practice
Keep practicing
11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.