27 #include <libopencm3/stm32/rcc.h>
28 #include <libopencm3/stm32/gpio.h>
29 #include <libopencm3/stm32/spi.h>
30 #include <libopencm3/stm32/timer.h>
31 #include <libopencm3/stm32/dma.h>
32 #include <libopencm3/cm3/nvic.h>
33 #include <libopencmsis/core_cm3.h>
45 #define LED_WS2812B_SPI_TEMPLATE 0x924924
61 const uint8_t colors[] = {green, red, blue};
62 const uint8_t pattern_bit[] = {0x02, 0x10, 0x80, 0x04, 0x20, 0x01, 0x08, 0x40};
63 const uint8_t pattern_byte[] = {2,2,2,1,1,0,0,0};
64 for (uint8_t color=0; color<
LENGTH(colors); color++) {
66 for (uint8_t bit=0; bit<8; bit++) {
67 if (colors[color]&(1<<bit)) {
99 rcc_periph_clock_enable(RCC_AFIO);
102 timer_set_mode(
LED_WS2812B_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
113 rcc_periph_clock_enable(RCC_AFIO);
147 for (uint16_t i=LED_WS2812B_LEDS*3*3; i<
LENGTH(led_ws2812b_data); i++) {
148 led_ws2812b_data[i] = 0;
void led_ws2812b_setup(void)
setup WS2812B LED driver
#define LED_WS2812B_SPI
SPI peripheral.
#define LED_WS2812B_DMA_CH
DMA channel (only DMA1 channel 3 supports SPI1_TX interrupt)
#define LED_WS2812B_DMA_RCC
DMA peripheral clock.
#define LED_WS2812B_SPI_PORT
SPI port.
bool led_ws2812b_transmit(void)
transmit color values to WS2812B LEDs
#define LED_WS2812B_TIMER_OC
timer output compare used to set PWM frequency
#define LED_WS2812B_TIMER_RCC
timer peripheral clock
#define LED_WS2812B_SPI_CLK
SPI clock pin (PA5), connect to PWM output.
#define LED_WS2812B_SPI_RCC
SPI peripheral clock.
global definitions and methods
static volatile bool transmit_flag
flag set in software when transmission started, clear by interrupt when transmission completed ...
#define LED_WS2812B_CLK_RCC
timer port peripheral clock
#define LED_WS2812B_SPI_PORT_RCC
SPI I/O peripheral clock.
uint8_t led_ws2812b_data[LED_WS2812B_LEDS *3 *3+40 *3/8+1]
data encoded to be shifted out by SPI for the WS2812B, plus the 50us reset (~40 data bits) ...
void led_ws2812b_set_rgb(uint16_t led, uint8_t red, uint8_t green, uint8_t blue)
set color of a single LED
#define LED_WS2812B_CLK_PORT
timer port
#define LED_WS2812B_SPI_DOUT
SPI data pin (PA6), connect to WS2812B DIN.
#define LED_WS2812B_CLK_PIN
timer pin to output PWM (PB0), connect to SPI clock input
#define LED_WS2812B_LEDS
number of LEDs on the WS2812B strip
#define LENGTH(x)
get the length of an array
#define LED_WS2812B_SPI_TEMPLATE
bit template to encode one byte to be shifted out by SPI to the WS2812B LEDs
#define LED_WS2812B_TIMER
timer peripheral
#define LED_WS2812B_SPI_DR
SPI data register for the DMA.
#define LED_WS2812B_DMA
DMA peripheral to put data for WS2812B LED in SPI queue (only DMA1 supports SPI1_TX interrupt) ...
void LED_WS2812B_DMA_ISR(void)
DMA interrupt service routine to stop transmission after it finished.
#define LED_WS2812B_DMA_IRQ
DMA channel interrupt signal.
library to drive a WS2812B LED chain (API)