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.
Theory in plain words
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
Use the project to test itself: probe the Pico own PWM or UART pins first.
Wiring
| Pico pin | Use | Note |
|---|---|---|
| GP0 to GP7 | probe inputs 0 to 7 | through 1 kohm series resistors for protection |
| GND (any) | probe ground | connect near the signal source |
| GP15 | test PWM output | loop to GP0 to self-test |
| USB | to PC | streams captured data |
Live capture and decode
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
.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
.wrapuint 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- Generate a known signal: PWM on GP15, wired to GP0.
- Capture 32 KB and print the first samples over USB serial to verify.
- Add a trigger: wait for a rising edge on GP0 before starting DMA.
- Write a Python or sigrok-compatible output so PulseView can open captures (sigrok OLS protocol).
- Decode UART and I2C with PulseView protocol decoders.
Quick quiz
1. Sample rate rule of thumb?
Fewer samples lose edges.
2. Why DMA?
CPU stays free.
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.