banner
News center
Affordable prices for quality goods.

Dmitry Grinberg's Raspberry Pi Pico PIO Code Runs a 2.8" Touchscreen Display with "No CPU Cycles"

Jun 01, 2023

Developer Dmitry Grinberg has put the Raspberry Pi RP2040's Programmable Input/Output (PIO) blocks to work, driving an ST7789/XPT2046-based color touchscreen display module whose stock software was — in his own words — "beyond bad."

"I was searching for a display with at least 160×160 resolution and a resistive touch screen that would mate easily to Raspberry Pi Pico for my rePalm project," Grinberg explains, referring to his ongoing efforts to bring back the Palm palmtop and Palm OS software. "There were not many, actually. But I did come across this display at Waveshare: a 2.8" 320×240 full-color LCD. Waveshare provides sample code for this device, [but] it is beyond bad."

Among the issues Grinberg had with the sample code provided was the assumption that the user would be actively drawing individual primitives to the display. "I have no idea who'd actually do this sort of thing. It is pure insanity both in terms of code size and in terms of speed," Grinberg complains. "The various articles I found from others on using this display did equally insane things, like DMA [Direct Memory Access]-ing a line of data at a time, and using the CPU to set up the next xfer."

Grinberg's solution, though, avoids using the CPU at all — by taking advantage of the Programmable Input/Output (PIO) blocks on the Raspberry Pi Pico's RP2040 microcontroller to create state machines which handle the display entirely independently of what the two Arm Cortex-M0+ cores are doing.

"Presented here is a very fast driver for this display supporting 1, 2, and 4 bpp [bits per pixel] greyscale, 8-bit indexed color, and 16-bit full color modes on the whole display or any rectangular subset of it," Grinberg writes of the result. "Touch data is copied to a memory location for your perusal at your leisure automatically, and if you wish, you can even do tear-free page flipping and get VSync [Vertical Synchronization] interrupts. No CPU cycles are used at all!"

The full technical write-up on the project is available on Grinberg's website, along with the source code of the driver under the permissive BSD 2-clause license. "I am too lazy (and disgusted) to turn this into some sort of an Arduino or MicroPython library," Grinberg writes, "but I am sure someone else will."

Main article image courtesy of Waveshare.