Home / Learn Chip Programming / Project 11: Mini Logic Analyzer on RP2040

Project 11: Mini Logic Analyzer on RP2040

Use the Pico PIO and DMA to sample 8 pins at up to 100 MHz and send captures to the PC.

In shortUse the Pico PIO and DMA to sample 8 pins at up to 100 MHz and send captures to the PC.

Theory in plain words

Capture path8 input pinsPIO samples each clockDMA to RAM buffertrigger and pre-triggerUSB to PC viewer
Capture path

What it is

The PIO state machine reads 8 input pins every clock into a FIFO, DMA copies them to RAM without the CPU, a trigger starts the capture, and the Pico streams the buffer over USB so the PC can show waveforms and decode UART or I2C.

Why we use it

You will build the tool you use for debugging, and learn PIO, DMA and USB data streaming.

How it works

Choose a sample rate by dividing the system clock. Capture N samples after the trigger edge. Compress runs of identical samples to save RAM.

Where it is used

  • Hardware debugging
  • Protocol decoding
  • Learning DMA and PIO

Equipment

What you need: equipment list with costTry it Yourself

Use the project to test itself: probe the Pico own PWM or UART pins first.

Wiring

Pico pinUseNote
GP0 to GP7probe inputs 0 to 7through 1 kohm series resistors for protection
GND (any)probe groundconnect near the signal source
GP15test PWM outputloop to GP0 to self-test
USBto PCstreams captured data

Live capture and decode

Sample a UART frame at different ratesTry it Yourself

At 200 kHz sampling a 115200 baud signal has fewer than 2 samples per bit and the decode fails. That is exactly why analyzers have a maximum rate.

PIO program

capture.pio
.program capture
    ; sample 8 pins into the RX FIFO every clock cycle, autopush each 32 bits (4 samples)
    in pins, 8
.wrap_target
    in pins, 8
.wrap
capture.c
uint offset = pio_add_program(pio, &capture_program);
pio_sm_config c = capture_program_get_default_config(offset);
sm_config_set_in_pins(&c, 0);                         // GP0..GP7
sm_config_set_in_shift(&c, true, true, 32);           // shift right, autopush at 32 bits
sm_config_set_clkdiv(&c, 125e6f / sample_rate);       // sample rate = 125 MHz / divider
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX);
pio_sm_init(pio, sm, offset, &c);
for (int i = 0; i < 8; i++) pio_gpio_init(pio, i);
dma_channel_config d = dma_channel_get_default_config(chan);
channel_config_set_read_increment(&d, false); channel_config_set_write_increment(&d, true);
channel_config_set_dreq(&d, pio_get_dreq(pio, sm, false));
dma_channel_configure(chan, &d, buf, &pio->rxf[sm], BUF_WORDS, true);   // CPU is now free
pio_sm_set_enabled(pio, sm, true);
dma_channel_wait_for_finish_blocking(chan);                              // then send buf over USB
  1. Generate a known signal: PWM on GP15, wired to GP0.
  2. Capture 32 KB and print the first samples over USB serial to verify.
  3. Add a trigger: wait for a rising edge on GP0 before starting DMA.
  4. Write a Python or sigrok-compatible output so PulseView can open captures (sigrok OLS protocol).
  5. Decode UART and I2C with PulseView protocol decoders.
Tip:The Pico has 264 KB of RAM: at 8 channels one byte per sample you can store about 200,000 samples, which is 2 ms at 100 MHz or 2 seconds at 100 kHz.

Quick quiz

1. Sample rate rule of thumb?

2. Why DMA?

Common questions

Sample rate rule of thumb?

At least 4 times the signal rate. Fewer samples lose edges.

Why DMA?

Moves data without CPU. CPU stays free.