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>
42 #define WS2812B_SPI SPI1
43 #define WS2812B_SPI_DR SPI1_DR
44 #define WS2812B_SPI_RCC RCC_SPI1
45 #define WS2812B_SPI_PORT GPIOA
46 #define WS2812B_SPI_CLK GPIO_SPI1_SCK
47 #define WS2812B_SPI_DOUT GPIO_SPI1_MISO
52 #define WS2812B_TIMER TIM3
53 #define WS2812B_TIMER_RCC RCC_TIM3
54 #define WS2812B_TIMER_OC TIM_OC3
55 #define WS2812B_CLK_RCC RCC_GPIOB
56 #define WS2812B_CLK_PORT GPIOB
57 #define WS2812B_CLK_PIN GPIO_TIM3_CH3
62 #define WS2812B_DMA DMA1
63 #define WS2812B_DMA_RCC RCC_DMA1
64 #define WS2812B_DMA_CH DMA_CHANNEL3
65 #define WS2812B_DMA_IRQ NVIC_DMA1_CHANNEL3_IRQ
66 #define WS2812B_DMA_ISR dma1_channel3_isr
76 #define WS2812B_SPI_TEMPLATE 0x924924
92 const uint8_t colors[] = {green, red, blue};
93 const uint8_t pattern_bit[] = {0x02, 0x10, 0x80, 0x04, 0x20, 0x01, 0x08, 0x40};
94 const uint8_t pattern_byte[] = {2,2,2,1,1,0,0,0};
95 for (uint8_t color=0; color<
LENGTH(colors); color++) {
97 for (uint8_t bit=0; bit<8; bit++) {
98 if (colors[color]&(1<<bit)) {
99 ws2812b_data[led*3*3+color*3+pattern_byte[bit]] |= pattern_bit[bit];
101 ws2812b_data[led*3*3+color*3+pattern_byte[bit]] &= ~pattern_bit[bit];
129 rcc_periph_clock_enable(RCC_AFIO);
132 timer_set_mode(
WS2812B_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
134 timer_set_period(
WS2812B_TIMER, rcc_ahb_frequency/800000/3-1);
145 spi_set_bidirectional_transmit_only_mode(
WS2812B_SPI);
175 for (uint16_t i=WS2812B_LEDS*3*3; i<
LENGTH(ws2812b_data); i++) {
void ws2812b_setup(void)
setup WS2812b LED driver
global definitions and methods
static volatile bool transmit_flag
#define WS2812B_SPI_TEMPLATE
bit template to encode one byte to be shifted out by SPI to the WS2812b LEDs
void ws2812b_transmit(void)
transmit color values to WS2812b LEDs
#define WS2812B_TIMER_RCC
uint8_t ws2812b_data[WS2812B_LEDS *3 *3+40 *3/8+1]
library to drive a WS2812b LED chain (API)
void ws2812b_set_rgb(uint16_t led, uint8_t red, uint8_t green, uint8_t blue)
set color of a single LED