The new RP2040 process on the Raspberry Pi Pico comes with a Programmable IO (PIO) module. This module has many uses to directly control the GPIO of the board at high speed. An example that comes with the Pico shows how to use PIO to capture input on a pin as a logic analyzer. The included example prints out a simple ASCII display of the data and while interesting, it is hard to examine the ASCII printout in detail.
There is an open source project called sigrok that does signal analysis. (SIgnal GROK). Along with sigrok there is a companion project called PulseView that allows you to visually inspect the signal data. These two programs allow you to more easily analyze captured data, adding protocol decoders to see what the data you captured means.
This project aimed to expand the example logic analyzer program to produce data that can be displayed in PulseView and allow the analyzer to be interactively configured.
Overall ArchitectureThe idea behind the project so far is in 3 areas:
- Connect the Pico to the pins on the device to be measured
- Use modified example code to configure the logic analyzer settings and record the signals and output them to the serial device in a CSV format
- Use PulseView to import the CSV file and display the data
Note: sigrok has many hardware drivers built in to read a wide range of analyzers and scopes available. This project is not adding a new driver and just importing data via a CSV file.
The CodeThe following assumes you know how to build a project and install it on a Pico. If you do not please see the excellent documentation that the has been released along with the Pico.
The example code that comes with the Pico was made to demonstrate a PIO program with a DMA transfer. To improve this for use as a logic analyzer several changes were made:
- The program first is in an interactive mode allowing the user to configure settings such as number of pins to capture, number of sample rates, frequency, etc. After initializing the PIO will arm and wait for the trigger signal then capturing the data and writing it in CSV format to the serial output (via USB). This can found found in the function
read_user_input()
. - A new print buffer function was created to write the data out in CSV format. This is found in
print_capture_buf_csv()
. - The built in LED is set to be solid while armed and waiting, and blinking while transferring the data.
The functions logic_analyser_init()
and logic_analyser_arm()
are as is from the original example and are responsible to initialize the PIO program and arm it for the trigger value.
The attached GitHub repository has the code.
Making it Work (An Example)This example will show the steps I took to get a capture of the I2C bus on a second Pico. The second Pico was hooked up to a BME280 sensor using GPIO12 as SDA and GPIO13 as SCL. CircuitPython was installed on the Pico and connected the BME280 via I2C to just continuously print the sensor values.
The logic analyzer Pico is loaded with the code. I used PuTTY to connect to the logic analyzer and configure it. Connect PuTTY to the COM port your Pico connects to at 921, 600 baud. For this example I used the following settings:
- First pin: GPIO17 (for SCL, so GPIO18 is SDA)
- Number of pins: 2
- Frequency: 1 Mhz
- Sample count: 200, 000 (about 50kB of RAM)
- Trigger on first low on SCL
I then connected GPIO13 to GPIO17 and GPIO12 to GPIO18 and connect the grounds. Upon the SCL line going low the Pico will collect 200, 000 samples. At 1 Mhz this will mean a capture of about 0.2 seconds.
You will know the Pico is ready when the on-board LED is on solid green.
To save all the CSV data from the Pico I use a program called plink which is part of the PuTTY installation. Any program that can direct serial input to a file will work. To use plink I used the following command:
plink -serial \\.\COM3 -sercfg 921600,8,1,N,N >
i2c.csv
The COM port should be set to the COM port your Pico connects to. Once this is running you are ready to start your capture.
To commence the capture I powered up the Pico connected to the BME and almost immediately built-in LED begins blinking indicated the data is being written over the serial connection and being saved by plink. While this is happening I stopped the Pico connected to the BME280.
When the Pico's LED is solid the transfer has finished and I stopped the plink command. And started up PulseView. For all the details on PulseView please see their documentation.
In PulseView I chose to import a CSV file and for this example specified 2 logic channels and a samplerate of 1000000 and turned off "Get channel name from first line." The rest of the import settings were left as is. Once loaded you can add a protocol decoder for I2C, assign the SCL to output 0 and SDA to output 1 and view the result. And that's all there is to it.
The sigrock / Pulseview websites have documentation that can help you configure the multitude of options they have.
Next StepsThere is a major improvement that could be added to this project. Write a hardware driver for sigrok. Then everything can be controlled via PulseView through the GUI.
Comments