Using STM32 SPI for LED Strip WS2812B
Introduction
This project uses STM32 SPI to drive LED strip with WS2812B(another name is Neopixel LEDs). If use DMA option, the STM32 MCU is only have to setup and is free to do another task.
Source Code Repository
The repo is at the following link.
https://bitbucket.org/mtien888/stm32-for-ws2812b-using-spi/src/
You can get the soource code using
git clone https://bitbucket.org/mtien888/stm32-for-ws2812b-using-spi/src/ .\neopixel
Hardware
The demo program use Nucleo F070RB. The concept can port to other STM32 CPU.
SPI Baudrate
The baudrate should adust to 3M bit/s. For this Nucleo F070RB board, I configure SYSCLK to 48Mhz and SPI prescaler to 16.
The generated SPI baud rate is 3Mbits/s. Each SPI clock width is 0.33 us.
WS2812B Encoding
Each bit of W2812B is encoded using 4 SPI bits. W2812B bit 0 is encoded as SPI bit 0100. WS2812B bit 1 is encoded as SPI bits 0111.
Generated Mapping Table
For fast mapping value(R, G, B) to SPI bits, a table is generated to map each value(0-255) to 3 bytes.
const uint8_t leddata[256*4] = { // size = 256 * 4
0X44 , 0X44 , 0X44 , 0X44 , // 0
0X44 , 0X44 , 0X44 , 0X47 , // 1
0X44 , 0X44 , 0X44 , 0X74 ,
0X44 , 0X44 , 0X44 , 0X77 ,
0X44 , 0X44 , 0X47 , 0X44 ,
0X44 , 0X44 , 0X47 , 0X47 ,
0X44 , 0X44 , 0X47 , 0X74 ,
0X44 , 0X44 , 0X47 , 0X77 ,
0X44 , 0X44 , 0X74 , 0X44 ,
0X44 , 0X44 , 0X74 , 0X47 ,
0X44 , 0X44 , 0X74 , 0X74 ,
0X44 , 0X44 , 0X74 , 0X77 ,
0X44 , 0X44 , 0X77 , 0X44 ,
0X44 , 0X44 , 0X77 , 0X47 ,
0X44 , 0X44 , 0X77 , 0X74 ,
0X44 , 0X44 , 0X77 , 0X77 ,
0X44 , 0X47 , 0X44 , 0X44 ,
0X44 , 0X47 , 0X44 , 0X47 ,
0X44 , 0X47 , 0X44 , 0X74 ,
0X44 , 0X47 , 0X44 , 0X77 ,
0X44 , 0X47 , 0X47 , 0X44 ,
0X44 , 0X47 , 0X47 , 0X47 ,
0X44 , 0X47 , 0X47 , 0X74 ,
0X44 , 0X47 , 0X47 , 0X77 ,
0X44 , 0X47 , 0X74 , 0X44 ,
0X44 , 0X47 , 0X74 , 0X47 ,
0X44 , 0X47 , 0X74 , 0X74 ,
0X44 , 0X47 , 0X74 , 0X77 ,
0X44 , 0X47 , 0X77 , 0X44 ,
0X44 , 0X47 , 0X77 , 0X47 ,
0X44 , 0X47 , 0X77 , 0X74 ,
0X44 , 0X47 , 0X77 , 0X77 ,
0X44 , 0X74 , 0X44 , 0X44 ,
0X44 , 0X74 , 0X44 , 0X47 ,
0X44 , 0X74 , 0X44 , 0X74 ,
0X44 , 0X74 , 0X44 , 0X77 ,
0X44 , 0X74 , 0X47 , 0X44 ,
0X44 , 0X74 , 0X47 , 0X47 ,
0X44 , 0X74 , 0X47 , 0X74 ,
0X44 , 0X74 , 0X47 , 0X77 ,
0X44 , 0X74 , 0X74 , 0X44 ,
0X44 , 0X74 , 0X74 , 0X47 ,
0X44 , 0X74 , 0X74 , 0X74 ,
0X44 , 0X74 , 0X74 , 0X77 ,
0X44 , 0X74 , 0X77 , 0X44 ,
0X44 , 0X74 , 0X77 , 0X47 ,
0X44 , 0X74 , 0X77 , 0X74 ,
0X44 , 0X74 , 0X77 , 0X77 ,
0X44 , 0X77 , 0X44 , 0X44 ,
0X44 , 0X77 , 0X44 , 0X47 ,
0X44 , 0X77 , 0X44 , 0X74 ,
0X44 , 0X77 , 0X44 , 0X77 ,
0X44 , 0X77 , 0X47 , 0X44 ,
0X44 , 0X77 , 0X47 , 0X47 ,
0X44 , 0X77 , 0X47 , 0X74 ,
0X44 , 0X77 , 0X47 , 0X77 ,
0X44 , 0X77 , 0X74 , 0X44 ,
0X44 , 0X77 , 0X74 , 0X47 ,
0X44 , 0X77 , 0X74 , 0X74 ,
0X44 , 0X77 , 0X74 , 0X77 ,
0X44 , 0X77 , 0X77 , 0X44 ,
0X44 , 0X77 , 0X77 , 0X47 ,
0X44 , 0X77 , 0X77 , 0X74 ,
0X44 , 0X77 , 0X77 , 0X77 ,
0X47 , 0X44 , 0X44 , 0X44 ,
0X47 , 0X44 , 0X44 , 0X47 ,
0X47 , 0X44 , 0X44 , 0X74 ,
0X47 , 0X44 , 0X44 , 0X77 ,
0X47 , 0X44 , 0X47 , 0X44 ,
0X47 , 0X44 , 0X47 , 0X47 ,
0X47 , 0X44 , 0X47 , 0X74 ,
0X47 , 0X44 , 0X47 , 0X77 ,
0X47 , 0X44 , 0X74 , 0X44 ,
0X47 , 0X44 , 0X74 , 0X47 ,
0X47 , 0X44 , 0X74 , 0X74 ,
0X47 , 0X44 , 0X74 , 0X77 ,
0X47 , 0X44 , 0X77 , 0X44 ,
0X47 , 0X44 , 0X77 , 0X47 ,
0X47 , 0X44 , 0X77 , 0X74 ,
0X47 , 0X44 , 0X77 , 0X77 ,
0X47 , 0X47 , 0X44 , 0X44 ,
0X47 , 0X47 , 0X44 , 0X47 ,
0X47 , 0X47 , 0X44 , 0X74 ,
0X47 , 0X47 , 0X44 , 0X77 ,
0X47 , 0X47 , 0X47 , 0X44 ,
0X47 , 0X47 , 0X47 , 0X47 ,
0X47 , 0X47 , 0X47 , 0X74 ,
0X47 , 0X47 , 0X47 , 0X77 ,
0X47 , 0X47 , 0X74 , 0X44 ,
0X47 , 0X47 , 0X74 , 0X47 ,
0X47 , 0X47 , 0X74 , 0X74 ,
0X47 , 0X47 , 0X74 , 0X77 ,
0X47 , 0X47 , 0X77 , 0X44 ,
0X47 , 0X47 , 0X77 , 0X47 ,
0X47 , 0X47 , 0X77 , 0X74 ,
0X47 , 0X47 , 0X77 , 0X77 ,
0X47 , 0X74 , 0X44 , 0X44 ,
0X47 , 0X74 , 0X44 , 0X47 ,
0X47 , 0X74 , 0X44 , 0X74 ,
0X47 , 0X74 , 0X44 , 0X77 ,
0X47 , 0X74 , 0X47 , 0X44 ,
0X47 , 0X74 , 0X47 , 0X47 ,
0X47 , 0X74 , 0X47 , 0X74 ,
0X47 , 0X74 , 0X47 , 0X77 ,
0X47 , 0X74 , 0X74 , 0X44 ,
0X47 , 0X74 , 0X74 , 0X47 ,
0X47 , 0X74 , 0X74 , 0X74 ,
0X47 , 0X74 , 0X74 , 0X77 ,
0X47 , 0X74 , 0X77 , 0X44 ,
0X47 , 0X74 , 0X77 , 0X47 ,
0X47 , 0X74 , 0X77 , 0X74 ,
0X47 , 0X74 , 0X77 , 0X77 ,
0X47 , 0X77 , 0X44 , 0X44 ,
0X47 , 0X77 , 0X44 , 0X47 ,
0X47 , 0X77 , 0X44 , 0X74 ,
0X47 , 0X77 , 0X44 , 0X77 ,
0X47 , 0X77 , 0X47 , 0X44 ,
0X47 , 0X77 , 0X47 , 0X47 ,
0X47 , 0X77 , 0X47 , 0X74 ,
0X47 , 0X77 , 0X47 , 0X77 ,
0X47 , 0X77 , 0X74 , 0X44 ,
0X47 , 0X77 , 0X74 , 0X47 ,
0X47 , 0X77 , 0X74 , 0X74 ,
0X47 , 0X77 , 0X74 , 0X77 ,
0X47 , 0X77 , 0X77 , 0X44 ,
0X47 , 0X77 , 0X77 , 0X47 ,
0X47 , 0X77 , 0X77 , 0X74 ,
0X47 , 0X77 , 0X77 , 0X77 ,
0X74 , 0X44 , 0X44 , 0X44 ,
0X74 , 0X44 , 0X44 , 0X47 ,
0X74 , 0X44 , 0X44 , 0X74 ,
0X74 , 0X44 , 0X44 , 0X77 ,
0X74 , 0X44 , 0X47 , 0X44 ,
0X74 , 0X44 , 0X47 , 0X47 ,
0X74 , 0X44 , 0X47 , 0X74 ,
0X74 , 0X44 , 0X47 , 0X77 ,
0X74 , 0X44 , 0X74 , 0X44 ,
0X74 , 0X44 , 0X74 , 0X47 ,
0X74 , 0X44 , 0X74 , 0X74 ,
0X74 , 0X44 , 0X74 , 0X77 ,
0X74 , 0X44 , 0X77 , 0X44 ,
0X74 , 0X44 , 0X77 , 0X47 ,
0X74 , 0X44 , 0X77 , 0X74 ,
0X74 , 0X44 , 0X77 , 0X77 ,
0X74 , 0X47 , 0X44 , 0X44 ,
0X74 , 0X47 , 0X44 , 0X47 ,
0X74 , 0X47 , 0X44 , 0X74 ,
0X74 , 0X47 , 0X44 , 0X77 ,
0X74 , 0X47 , 0X47 , 0X44 ,
0X74 , 0X47 , 0X47 , 0X47 ,
0X74 , 0X47 , 0X47 , 0X74 ,
0X74 , 0X47 , 0X47 , 0X77 ,
0X74 , 0X47 , 0X74 , 0X44 ,
0X74 , 0X47 , 0X74 , 0X47 ,
0X74 , 0X47 , 0X74 , 0X74 ,
0X74 , 0X47 , 0X74 , 0X77 ,
0X74 , 0X47 , 0X77 , 0X44 ,
0X74 , 0X47 , 0X77 , 0X47 ,
0X74 , 0X47 , 0X77 , 0X74 ,
0X74 , 0X47 , 0X77 , 0X77 ,
0X74 , 0X74 , 0X44 , 0X44 ,
0X74 , 0X74 , 0X44 , 0X47 ,
0X74 , 0X74 , 0X44 , 0X74 ,
0X74 , 0X74 , 0X44 , 0X77 ,
0X74 , 0X74 , 0X47 , 0X44 ,
0X74 , 0X74 , 0X47 , 0X47 ,
0X74 , 0X74 , 0X47 , 0X74 ,
0X74 , 0X74 , 0X47 , 0X77 ,
0X74 , 0X74 , 0X74 , 0X44 ,
0X74 , 0X74 , 0X74 , 0X47 ,
0X74 , 0X74 , 0X74 , 0X74 ,
0X74 , 0X74 , 0X74 , 0X77 ,
0X74 , 0X74 , 0X77 , 0X44 ,
0X74 , 0X74 , 0X77 , 0X47 ,
0X74 , 0X74 , 0X77 , 0X74 ,
0X74 , 0X74 , 0X77 , 0X77 ,
0X74 , 0X77 , 0X44 , 0X44 ,
0X74 , 0X77 , 0X44 , 0X47 ,
0X74 , 0X77 , 0X44 , 0X74 ,
0X74 , 0X77 , 0X44 , 0X77 ,
0X74 , 0X77 , 0X47 , 0X44 ,
0X74 , 0X77 , 0X47 , 0X47 ,
0X74 , 0X77 , 0X47 , 0X74 ,
0X74 , 0X77 , 0X47 , 0X77 ,
0X74 , 0X77 , 0X74 , 0X44 ,
0X74 , 0X77 , 0X74 , 0X47 ,
0X74 , 0X77 , 0X74 , 0X74 ,
0X74 , 0X77 , 0X74 , 0X77 ,
0X74 , 0X77 , 0X77 , 0X44 ,
0X74 , 0X77 , 0X77 , 0X47 ,
0X74 , 0X77 , 0X77 , 0X74 ,
0X74 , 0X77 , 0X77 , 0X77 ,
0X77 , 0X44 , 0X44 , 0X44 ,
0X77 , 0X44 , 0X44 , 0X47 ,
0X77 , 0X44 , 0X44 , 0X74 ,
0X77 , 0X44 , 0X44 , 0X77 ,
0X77 , 0X44 , 0X47 , 0X44 ,
0X77 , 0X44 , 0X47 , 0X47 ,
0X77 , 0X44 , 0X47 , 0X74 ,
0X77 , 0X44 , 0X47 , 0X77 ,
0X77 , 0X44 , 0X74 , 0X44 ,
0X77 , 0X44 , 0X74 , 0X47 ,
0X77 , 0X44 , 0X74 , 0X74 ,
0X77 , 0X44 , 0X74 , 0X77 ,
0X77 , 0X44 , 0X77 , 0X44 ,
0X77 , 0X44 , 0X77 , 0X47 ,
0X77 , 0X44 , 0X77 , 0X74 ,
0X77 , 0X44 , 0X77 , 0X77 ,
0X77 , 0X47 , 0X44 , 0X44 ,
0X77 , 0X47 , 0X44 , 0X47 ,
0X77 , 0X47 , 0X44 , 0X74 ,
0X77 , 0X47 , 0X44 , 0X77 ,
0X77 , 0X47 , 0X47 , 0X44 ,
0X77 , 0X47 , 0X47 , 0X47 ,
0X77 , 0X47 , 0X47 , 0X74 ,
0X77 , 0X47 , 0X47 , 0X77 ,
0X77 , 0X47 , 0X74 , 0X44 ,
0X77 , 0X47 , 0X74 , 0X47 ,
0X77 , 0X47 , 0X74 , 0X74 ,
0X77 , 0X47 , 0X74 , 0X77 ,
0X77 , 0X47 , 0X77 , 0X44 ,
0X77 , 0X47 , 0X77 , 0X47 ,
0X77 , 0X47 , 0X77 , 0X74 ,
0X77 , 0X47 , 0X77 , 0X77 ,
0X77 , 0X74 , 0X44 , 0X44 ,
0X77 , 0X74 , 0X44 , 0X47 ,
0X77 , 0X74 , 0X44 , 0X74 ,
0X77 , 0X74 , 0X44 , 0X77 ,
0X77 , 0X74 , 0X47 , 0X44 ,
0X77 , 0X74 , 0X47 , 0X47 ,
0X77 , 0X74 , 0X47 , 0X74 ,
0X77 , 0X74 , 0X47 , 0X77 ,
0X77 , 0X74 , 0X74 , 0X44 ,
0X77 , 0X74 , 0X74 , 0X47 ,
0X77 , 0X74 , 0X74 , 0X74 ,
0X77 , 0X74 , 0X74 , 0X77 ,
0X77 , 0X74 , 0X77 , 0X44 ,
0X77 , 0X74 , 0X77 , 0X47 ,
0X77 , 0X74 , 0X77 , 0X74 ,
0X77 , 0X74 , 0X77 , 0X77 ,
0X77 , 0X77 , 0X44 , 0X44 ,
0X77 , 0X77 , 0X44 , 0X47 ,
0X77 , 0X77 , 0X44 , 0X74 ,
0X77 , 0X77 , 0X44 , 0X77 ,
0X77 , 0X77 , 0X47 , 0X44 ,
0X77 , 0X77 , 0X47 , 0X47 ,
0X77 , 0X77 , 0X47 , 0X74 ,
0X77 , 0X77 , 0X47 , 0X77 ,
0X77 , 0X77 , 0X74 , 0X44 ,
0X77 , 0X77 , 0X74 , 0X47 ,
0X77 , 0X77 , 0X74 , 0X74 ,
0X77 , 0X77 , 0X74 , 0X77 ,
0X77 , 0X77 , 0X77 , 0X44 ,
0X77 , 0X77 , 0X77 , 0X47 ,
0X77 , 0X77 , 0X77 , 0X74 ,
0X77 , 0X77 , 0X77 , 0X77 ,
};
SPI NSS mode should configure as disable to prevent delay between 8 bit data
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
Initialize MOSI pin to be LOW
The MOSI pin was initialized to be high so that first LED might get the wrong signal. Transmit a byte 0x00 to MOSI first to initialize MOSI pin to be LOW.