David Johnson-Davies' NeoPixel Driver Runs Happily on a Microchip AVR Microcontroller
Lightweight driver makes use of the chip's SPI peripheral to stream data at the right rate for WS2812 control.
Developer David Johnson-Davies has written a driver for WS2812 programmable LED strips, including Adafruit's NeoPixel boards, which runs entirely on an eight-bit Microchip AVR microcontroller — making use of its SPI bus to get the performance up to snuff.
"WS2812 displays, nicknamed NeoPixels by Adafruit, are a popular chainable type of RGB LED display now available in a wide variety of formats," Johnson-Davies explains by way of background. "They use a non-standard protocol consisting of a serial stream of pulses, with the width of each pulse determining whether it is a '0' or a '1'. However the pulses are very short: a zero is defined as having a width of 350ns, which is just 8.4 cycles on a 24MHz CPU. Most NeoPixel libraries therefore use hand-crafted assembler routines tailored to each processor for the low-level pulse generation."
Johnson-Davies, though, decided on an alternative approach. Inspired by existing projects to use the programmable input/output (PIO) capabilities of the Raspberry Pi RP2040 and RP2350 microcontroller families to drive WS2812 LED strips, Johnson-Davies decided to see if it was possible to do something similar on a Microchip AVR microcontroller — an eight-bit family that, in the AVR128DA28 picked for the project, runs at just 24MHz.
"This AVR NeoPixel driver uses the SPI peripheral to generate the serial stream of bits," Johnson-Davies explains, "a Timer/Counter to generate waveforms with the appropriate pulse widths for the '0' and '1' bits, and the Configurable Custom Logic (CCL) to combine these signals into a single stream of pulses encoding the colors. This application would probably work using most members of the AVR DA, DB, and DD families."
The project, which uses only one of the ADV128DA28's two SPI peripherals, is documented in full on Johnson-Davies' website, complete with source code under the Creative Commons Attribution 4.0 International license. "We could go one stage further, and use the AVR Events System to eliminate most of the interconnections between the pins on the […] circuit without affecting its operation," the developer notes. "I'll describe how to do this in a subsequent article."
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.