added WS2812 support

This commit is contained in:
2026-06-12 12:17:57 +03:00
parent ac1ce30121
commit 622644a6a1
12 changed files with 29124 additions and 28189 deletions
+152 -3
View File
@@ -6,6 +6,15 @@
#include "tim.h"
#include <string.h>
/* Второй светодиод в цепочке WS2812 */
static RGB_t ws2812_led1 = { .R = 0, .G = 0, .B = 0 };
#define WS2812_ERROR_BLINK_MS 100
#define WS2812_ERROR_BLINK_PAUSE 30
/* Яркость обоих WS2812 на плате, 0..255 */
#define WS2812_BRIGHTNESS 60
RGB_State_t LED_State;
RGB_Cycle_t LED_Cycle;
@@ -179,10 +188,147 @@ void interpolateColors(RGB_t* color1, RGB_t* color2, uint16_t a, uint16_t b, RGB
}
#pragma GCC push_options
#pragma GCC optimize("O2")
#define WS2812_T0H_NOP 25
#define WS2812_T0L_NOP 52
#define WS2812_T1H_NOP 52
#define WS2812_T1L_NOP 25
#define _WS2812_DELAY_NOPS(n) __asm volatile(".rept " #n "\nnop\n.endr" ::: "memory")
#define WS2812_DELAY_NOPS(n) _WS2812_DELAY_NOPS(n)
static void ws2812_send_pixel(uint8_t r, uint8_t g, uint8_t b)
{
uint32_t tmp = ~(((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b);
for (int i = 0; i < 24; i++) {
LED_DATA_GPIO_Port->BSRR = LED_DATA_Pin;
if (tmp & (1U << 23)) {
WS2812_DELAY_NOPS(WS2812_T0H_NOP);
LED_DATA_GPIO_Port->BRR = LED_DATA_Pin;
WS2812_DELAY_NOPS(WS2812_T0L_NOP);
} else {
WS2812_DELAY_NOPS(WS2812_T1H_NOP);
LED_DATA_GPIO_Port->BRR = LED_DATA_Pin;
WS2812_DELAY_NOPS(WS2812_T1L_NOP);
}
tmp <<= 1;
}
}
static void ws2812_update(RGB_t *led0, RGB_t *led1)
{
uint32_t primask = __get_PRIMASK();
__disable_irq();
ws2812_send_pixel(led0->R, led0->G, led0->B);
ws2812_send_pixel(led1->R, led1->G, led1->B);
__set_PRIMASK(primask);
}
#pragma GCC pop_options
static RGB_t RGB_ScaleBrightness(const RGB_t *color)
{
RGB_t out = {
.R = (uint8_t)((uint16_t)color->R * WS2812_BRIGHTNESS / 255),
.G = (uint8_t)((uint16_t)color->G * WS2812_BRIGHTNESS / 255),
.B = (uint8_t)((uint16_t)color->B * WS2812_BRIGHTNESS / 255),
};
return out;
}
/*
* Моргание красным на втором WS2812 при chargingError > 0.
* Логика как Slave-LB24 LED_Task: N вспышек = код ошибки, пауза 30×100 ms.
* Вызывается из LED_Task каждые 20 ms; шаг FSM — 100 ms.
*/
static void LED_Ws2812ErrorBlinkUpdate(void)
{
static uint8_t err_counter;
static uint8_t led_state;
static uint8_t led_pause;
static uint32_t blink_tick;
static uint32_t flash_until;
static CONN_Error_t last_error = CONN_NO_ERROR;
const CONN_Error_t err = CONN.chargingError;
const uint32_t now = HAL_GetTick();
if (err == CONN_NO_ERROR) {
err_counter = 0;
led_state = 0;
led_pause = 0;
flash_until = 0;
last_error = CONN_NO_ERROR;
ws2812_led1.R = 0;
ws2812_led1.G = 0;
ws2812_led1.B = 0;
return;
}
if (err != last_error) {
err_counter = 0;
led_state = 0;
led_pause = 0;
flash_until = 0;
last_error = err;
}
if (now < flash_until) {
ws2812_led1.R = 255;
ws2812_led1.G = 0;
ws2812_led1.B = 0;
return;
}
ws2812_led1.R = 0;
ws2812_led1.G = 0;
ws2812_led1.B = 0;
if ((now - blink_tick) < WS2812_ERROR_BLINK_MS) {
return;
}
blink_tick = now;
uint8_t led_flash = 0;
if (led_pause > 0) {
led_pause--;
} else if (err_counter < (uint8_t)err) {
if (led_state == 0) {
led_state = 1;
} else {
led_flash = 1;
led_state = 0;
err_counter++;
}
} else {
err_counter = 0;
led_pause = WS2812_ERROR_BLINK_PAUSE;
}
if (led_flash) {
flash_until = now + WS2812_ERROR_BLINK_MS;
ws2812_led1.R = 255;
ws2812_led1.G = 0;
ws2812_led1.B = 0;
}
}
void RGB_SetColor(RGB_t *color){
htim4.Instance->CCR2 = color->R * 100 / 255;
htim4.Instance->CCR3 = color->G * 100 / 255;
htim4.Instance->CCR4 = color->B * 100 / 255;
htim4.Instance->CCR2 = color->R * 100 / 255;
htim4.Instance->CCR3 = color->G * 100 / 255;
htim4.Instance->CCR4 = color->B * 100 / 255;
}
void WS2812_SetColor(RGB_t *color){
RGB_t led0 = RGB_ScaleBrightness(color);
RGB_t led1 = RGB_ScaleBrightness(&ws2812_led1);
ws2812_update(&led0, &led1);
}
void LED_SetColor(RGB_Cycle_t *color){
@@ -196,6 +342,7 @@ void LED_Init(){
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
RGB_SetColor(&color);
WS2812_SetColor(&color);
}
void LED_Task(){
@@ -239,6 +386,8 @@ void LED_Task(){
default:
LED_State.state = LED_RISING;
}
LED_Ws2812ErrorBlinkUpdate();
RGB_SetColor(&LED_State.color);
WS2812_SetColor(&LED_State.color);
}
}