🔁

Ring Buffer / DMA Sizing Calculator

Minimum buffer size to absorb data between service intervals.

Input
ℹ Buffer must hold the data arriving while the CPU is busy elsewhere.
Results
Required Bytes—
Next Power of 2—
Fill Time at Throughput—
💡 Usage & Formula

bytes = throughput × latency × safety. Power-of-2 size lets DMA wrap with a cheap mask.

When you need it: Sizing a circular buffer for UART or ADC DMA so it never overruns between the intervals when your code drains it, and picking a power-of-two length for cheap wraparound.

Worked example: A UART at 115200 bps (~11.5 KB/s) serviced every 10 ms must hold at least 0.010 × 11520 ≈ 115 bytes per interval; round up to 256 for margin. Fill level is (head − tail) & (size − 1).

Tips & gotchas:

  • A power-of-two size lets you wrap with & (N−1) instead of an expensive modulo.
  • Keep one slot empty (or a separate count) so you can tell "full" from "empty" — both otherwise show head == tail.
  • Single-producer/single-consumer is lock-free if head and tail are word-aligned volatile and updated last.
  • Size for the worst-case service latency (ISR blocked, higher-priority task), not the average.