#include "ws2812.h"
#include "quantum.h"
#include <hal.h>
#ifdef RGBW
# error "RGBW not supported"
#endif
#ifndef WS2812_PWM_DRIVER
# define WS2812_PWM_DRIVER PWMD2
#endif
#ifndef WS2812_PWM_CHANNEL
# define WS2812_PWM_CHANNEL 2
#endif
#ifndef WS2812_PWM_PAL_MODE
# define WS2812_PWM_PAL_MODE 2
#endif
#ifndef WS2812_DMA_STREAM
# define WS2812_DMA_STREAM STM32_DMA1_STREAM2
#endif
#ifndef WS2812_DMA_CHANNEL
# define WS2812_DMA_CHANNEL 2
#endif
#if (STM32_DMA_SUPPORTS_DMAMUX == TRUE) && !defined(WS2812_DMAMUX_ID)
# error "please consult your MCU's datasheet and specify in your config.h: #define WS2812_DMAMUX_ID STM32_DMAMUX1_TIM?_UP"
#endif
#ifndef WS2812_PWM_COMPLEMENTARY_OUTPUT
# define WS2812_PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH
#else
# if !STM32_PWM_USE_ADVANCED
# error "WS2812_PWM_COMPLEMENTARY_OUTPUT requires STM32_PWM_USE_ADVANCED == TRUE"
# endif
# define WS2812_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH
#endif
#ifndef WS2812_EXTERNAL_PULLUP
# if defined(USE_GPIOV1)
# define WS2812_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
# else
# define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING
# endif
#else
# if defined(USE_GPIOV1)
# define WS2812_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN
# else
# define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING
# endif
#endif
#ifndef WS2812_PWM_TARGET_PERIOD
# define WS2812_PWM_TARGET_PERIOD 80000
#endif
#define WS2812_PWM_FREQUENCY (STM32_SYSCLK / 2)
#define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY / WS2812_PWM_TARGET_PERIOD)
#define WS2812_RESET_BIT_N (1000 * WS2812_TRST_US / 1250)
#define WS2812_COLOR_BIT_N (RGBLED_NUM * 24)
#define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N)
#define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY / (1000000000 / 350))
#define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY / (1000000000 / 800))
#define WS2812_BIT(led, byte, bit) (24 * (led) + 8 * (byte) + (7 - (bit)))
#if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB)
# define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
# define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
# define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
#elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB)
# define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 0, (bit))
# define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
# define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
#elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR)
# define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 2, (bit))
# define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
# define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 0, (bit))
#endif
static uint32_t ws2812_frame_buffer[WS2812_BIT_N + 1];
void ws2812_init(void) {
uint32_t i;
for (i = 0; i < WS2812_COLOR_BIT_N; i++) ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; for (i = 0; i < WS2812_RESET_BIT_N; i++) ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0;
palSetLineMode(RGB_DI_PIN, WS2812_OUTPUT_MODE);
static const PWMConfig ws2812_pwm_config = {
.frequency = WS2812_PWM_FREQUENCY,
.period = WS2812_PWM_PERIOD, .callback = NULL,
.channels =
{
[0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, [WS2812_PWM_CHANNEL - 1] = {.mode = WS2812_PWM_OUTPUT_MODE, .callback = NULL}, },
.cr2 = 0,
.dier = TIM_DIER_UDE, };
dmaStreamAlloc(WS2812_DMA_STREAM - STM32_DMA_STREAM(0), 10, NULL, NULL);
dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer);
dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N);
dmaStreamSetMode(WS2812_DMA_STREAM, STM32_DMA_CR_CHSEL(WS2812_DMA_CHANNEL) | STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_WORD | STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
#if (STM32_DMA_SUPPORTS_DMAMUX == TRUE)
dmaSetRequestSource(WS2812_DMA_STREAM, WS2812_DMAMUX_ID);
#endif
dmaStreamEnable(WS2812_DMA_STREAM);
pwmStart(&WS2812_PWM_DRIVER, &ws2812_pwm_config);
pwmEnableChannel(&WS2812_PWM_DRIVER, WS2812_PWM_CHANNEL - 1, 0); }
void ws2812_write_led(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b) {
for (uint8_t bit = 0; bit < 8; bit++) {
ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
}
}
void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) {
static bool s_init = false;
if (!s_init) {
ws2812_init();
s_init = true;
}
for (uint16_t i = 0; i < leds; i++) {
ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b);
}
}