27 #include <libopencmsis/core_cm3.h> 28 #include <libopencm3/stm32/rcc.h> 29 #include <libopencm3/stm32/gpio.h> 30 #include <libopencm3/stm32/spi.h> 31 #include <libopencm3/stm32/timer.h> 32 #include <libopencm3/stm32/dma.h> 33 #include <libopencm3/cm3/nvic.h> 42 #define LED_WS2812B_SPI 1 48 #define LED_WS2812B_TIMER 3 49 #define LED_WS2812B_CLK_CH 3 50 #define LED_WS2812B_TIMER_OC TIM_OC3 60 #define LED_WS2812B_SPI_TEMPLATE 0x924924 76 const uint8_t colors[] = {green, red, blue};
77 const uint8_t pattern_bit[] = {0x02, 0x10, 0x80, 0x04, 0x20, 0x01, 0x08, 0x40};
78 const uint8_t pattern_byte[] = {2,2,2,1,1,0,0,0};
79 for (uint8_t color=0; color<
LENGTH(colors); color++) {
81 for (uint8_t bit=0; bit<8; bit++) {
82 if (colors[color]&(1<<bit)) {
114 rcc_periph_clock_enable(RCC_AFIO);
129 rcc_periph_clock_enable(RCC_AFIO);
void led_ws2812b_setup(void)
setup WS2812B LED driver
#define LED_WS2812B_SPI
SPI peripheral.
#define RCC_TIM_CH(x, y)
get RCC for port based on TIMx_CHy identifier
#define DMA_IRQ_SPI_TX(x)
get DMA NVIC IRQ for SPI TX based on SPI identifier
#define DMA_CHANNEL_SPI_TX(x)
get DMA channel for SPI TX based on SPI identifier
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_CLK_CH
timer channel to output PWM (PB0), connect to SPI clock input
global definitions and methods (API)
static volatile bool transmit_flag
flag set in software when transmission started, clear by interrupt when transmission completed ...
#define RCC_SPI_MISO_PORT(x)
get RCC for GPIO port for SPI MISO signals
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
#define RCC_SPI(x)
get RCC for SPI based on SPI identifier
#define TIM_CH_PIN(x, y)
get pin based on TIMx_CHy identifier
#define RCC_DMA_SPI(x)
get RCC for DMA based on SPI identifier
#define DMA_ISR_SPI_TX(x)
get DMA ISR for SPI TX based on SPI identifier
#define SPI_SCK_PORT(x)
get SPI port for SCK signal based on SPI identifier
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 TIM_CH_PORT(x, y)
get port based on TIMx_CHy identifier
#define SPI_MISO_PORT(x)
get SPI port for MISO signal based on SPI identifier
#define SPI(x)
get SPI based on SPI identifier
#define LED_WS2812B_LEDS
number of LEDs on the WS2812B strip
#define RCC_SPI_SCK_PORT(x)
get RCC for GPIO port for SPI SCK signals
#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 TIM(x)
get TIM based on TIM identifier
#define SPI_MISO_PIN(x)
get SPI pin for MISO signal based on SPI identifier
#define DMA_SPI(x)
get DMA based on SPI identifier
#define SPI_SCK_PIN(x)
get SPI pin for SCK signal based on SPI identifier
library to drive a WS2812B LED chain (API)