C827 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

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,

CPU load=r⋅cf

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

LinkRate
UART, 1 start + 8 data + 1 stop bitbaud/10 bytes per second
a bus w bits wide, f transfers per secondw⋅f/8 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
  1. Cycles per second. The UART delivers 11,520 bytes a second: 11,520×60=691,200
  2. The fraction. 691,20048×106=1.44%

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
  1. An interrupt per sample. 106×60=6×107cycles/s against 1.6×107 available, a load of 375%: impossible.
  2. Interrupts with DMA. 106256=3906a second
  3. Their cost. 3906×60≈234,000cycles a load of 1.5%.
  4. 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
  1. Byte time. A byte arrives every 10 µs.
  2. 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.
  3. Interrupt rate. 10512≈8333a second against 100,000 with an interrupt per byte.
  4. 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?

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.