fix(rgbled): double-buffer WS2812 strip to eliminate races (#7304)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Raphael Coeffic
2026-04-29 20:09:07 +10:00
committed by GitHub
co-authored by Claude Opus 4.7
parent 462b0351ef
commit ac4c4d7b6b
16 changed files with 244 additions and 169 deletions
@@ -221,32 +221,38 @@ void ws2812_init(const stm32_pulse_timer_t* timer, uint8_t* strip_colors,
_init_timer(timer);
}
void ws2812_set_color(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
void ws2812_set_color_in_buf(uint8_t* buf, uint8_t led,
uint8_t r, uint8_t g, uint8_t b)
{
if (led >= _led_strip_len) return;
uint8_t* pixel = &_led_colors[led * WS2812_BYTES_PER_LED];
uint8_t* pixel = &buf[led * WS2812_BYTES_PER_LED];
pixel[_r_offset] = r;
pixel[_g_offset] = g;
pixel[_b_offset] = b;
}
uint32_t ws2812_get_color(uint8_t led)
uint32_t ws2812_get_color_in_buf(const uint8_t* buf, uint8_t led)
{
if (led >= _led_strip_len) return 0;
uint8_t* pixel = &_led_colors[led * WS2812_BYTES_PER_LED];
return (pixel[1] << 16) + (pixel[0] << 8) + pixel[2];
const uint8_t* pixel = &buf[led * WS2812_BYTES_PER_LED];
return (pixel[1] << 16) + (pixel[0] << 8) + pixel[2];
}
bool ws2812_get_state(uint8_t led)
bool ws2812_get_state_in_buf(const uint8_t* buf, uint8_t led)
{
if (led >= _led_strip_len) return false;
uint8_t* pixel = &_led_colors[led * WS2812_BYTES_PER_LED];
const uint8_t* pixel = &buf[led * WS2812_BYTES_PER_LED];
return pixel[0] || pixel[1] || pixel[2];
}
bool ws2812_is_busy(const stm32_pulse_timer_t* tim)
{
return LL_DMA_IsEnabledStream(tim->DMAx, tim->DMA_Stream);
}
void ws2812_update(const stm32_pulse_timer_t* tim)
{
WS2812_DBG_HIGH;
@@ -38,8 +38,15 @@
void ws2812_init(const stm32_pulse_timer_t* timer, uint8_t* strip_colors,
uint8_t strip_len, uint8_t type);
void ws2812_update(const stm32_pulse_timer_t* timer);
bool ws2812_get_state(uint8_t led);
void ws2812_dma_isr(const stm32_pulse_timer_t* timer);
void ws2812_set_color(uint8_t led, uint8_t r, uint8_t g, uint8_t b);
uint32_t ws2812_get_color(uint8_t led);
// Returns true if a DMA transfer is currently in progress.
bool ws2812_is_busy(const stm32_pulse_timer_t* timer);
// Buffer-parameterised setter/getter. The caller owns the buffer; ws2812
// applies the strip's RGB byte ordering through the offsets configured at
// init time.
void ws2812_set_color_in_buf(uint8_t* buf, uint8_t led,
uint8_t r, uint8_t g, uint8_t b);
uint32_t ws2812_get_color_in_buf(const uint8_t* buf, uint8_t led);
bool ws2812_get_state_in_buf(const uint8_t* buf, uint8_t led);
-40
View File
@@ -23,7 +23,6 @@
#include "stm32_hal_ll.h"
#include "stm32_gpio.h"
#include "stm32_spi.h"
#include "stm32_ws2812.h"
#include "vs1053b.h"
#include "hal/adc_driver.h"
@@ -115,45 +114,6 @@ void audioInit()
}
#endif
#if defined(SIXPOS_SWITCH_INDEX)
uint8_t lastADCState = 0;
uint8_t sixPosState = 0;
bool dirty = true;
uint16_t getSixPosAnalogValue(uint16_t adcValue)
{
uint8_t currentADCState = 0;
if (adcValue > 3800)
currentADCState = 6;
else if (adcValue > 3100)
currentADCState = 5;
else if (adcValue > 2300)
currentADCState = 4;
else if (adcValue > 1500)
currentADCState = 3;
else if (adcValue > 1000)
currentADCState = 2;
else if (adcValue > 400)
currentADCState = 1;
if (lastADCState != currentADCState) {
lastADCState = currentADCState;
} else if (lastADCState != 0 && lastADCState - 1 != sixPosState) {
sixPosState = lastADCState - 1;
dirty = true;
}
if (dirty) {
for (uint8_t i = 0; i < 6; i++) {
if (i == sixPosState) {
ws2812_set_color(i, SIXPOS_LED_RED, SIXPOS_LED_GREEN, SIXPOS_LED_BLUE);
} else {
ws2812_set_color(i, 0, 0, 0);
}
}
rgbLedColorApply();
}
return (4096/5)*(sixPosState);
}
#endif
#if defined(HAS_IMU)
#include "drivers/lsm6ds.h"
#include "stm32_i2c_driver.h"
+4 -4
View File
@@ -185,10 +185,6 @@ uint32_t pwrPressedDuration();
void usbChargerInit();
bool usbChargerLed();
#if defined(RADIO_V16)
uint16_t getSixPosAnalogValue(uint16_t adcValue);
#endif
// Led driver
void ledInit();
void ledOff();
@@ -246,6 +242,10 @@ bool isBacklightEnabled();
}
#endif
#if defined(RADIO_V16)
uint16_t getSixPosAnalogValue(uint16_t adcValue);
#endif
// Audio driver
void audioInit();
+5 -7
View File
@@ -27,7 +27,6 @@
#include "edgetx.h"
#include "mainwindow.h"
#include "static.h"
#include "stm32_ws2812.h"
#include "LvglWrapper.h"
#define __BATTERY_DRIVER_C__
@@ -603,8 +602,8 @@ void setLedGroupColor(uint8_t index, uint8_t color, uint8_t brightness) {
scaled_b = brightness;
}
ws2812_set_color(rgbMapping[index], scaled_r, scaled_g, scaled_b);
ws2812_set_color(rgbMapping[index] + 1, scaled_r, scaled_g, scaled_b);
rgbSetLedColor(rgbMapping[index], scaled_r, scaled_g, scaled_b);
rgbSetLedColor(rgbMapping[index] + 1, scaled_r, scaled_g, scaled_b);
}
uint8_t ledBreathBright(float angle, uint8_t maxBright) {
@@ -655,11 +654,10 @@ void ledLoop(void) {
ledBreathUpdate(led_info.led_state, led_info.led_color, led_info.led_group);
}
extern const stm32_pulse_timer_t _led_timer;
static uint8_t _led_charge_colors[WS2812_BYTES_PER_LED * LED_STRIP_LENGTH];
void rgbChargeInit(void) {
ws2812_init(&_led_timer, _led_charge_colors, LED_STRIP_LENGTH, WS2812_GRB);
rgbLedClearAll();
// Bring up the WS2812 strip without starting the periodic refresh timer
// (the FreeRTOS scheduler isn't running yet during the charging UI).
rgbLedHwInit();
}
constexpr uint16_t vbatLedTable[] = {650, 720, 760, 800, 823 };
+15 -11
View File
@@ -22,7 +22,6 @@
#include "hal/gpio.h"
#include "hal/rgbleds.h"
#include "stm32_gpio.h"
#include "stm32_ws2812.h"
#include "boards/generic_stm32/rgb_leds.h"
#include "board.h"
@@ -36,32 +35,37 @@ uint8_t ledMapping[] = {4, 6, 0, 2};
void fsLedRGB(uint8_t index, uint32_t color)
{
ws2812_set_color(ledMapping[index], GET_RED(color),
rgbSetLedColor(ledMapping[index], GET_RED(color),
GET_GREEN(color),GET_BLUE(color));
ws2812_set_color(ledMapping[index]+1, GET_RED(color),
rgbSetLedColor(ledMapping[index]+1, GET_RED(color),
GET_GREEN(color),GET_BLUE(color));
rgbLedColorApply();
}
void ledOff()
{
ws2812_set_color(8, 0, 0, 0);
ws2812_set_color(9, 0, 0, 0);
rgbSetLedColor(8, 0, 0, 0);
rgbSetLedColor(9, 0, 0, 0);
rgbLedColorApply();
}
void ledRed()
{
ws2812_set_color(8, 20, 0, 0);
ws2812_set_color(9, 20, 0, 0);
rgbSetLedColor(8, 20, 0, 0);
rgbSetLedColor(9, 20, 0, 0);
rgbLedColorApply();
}
void ledGreen()
{
ws2812_set_color(8, 0, 20, 0);
ws2812_set_color(9, 0, 20, 0);
rgbSetLedColor(8, 0, 20, 0);
rgbSetLedColor(9, 0, 20, 0);
rgbLedColorApply();
}
void ledBlue()
{
ws2812_set_color(8, 0, 0, 20);
ws2812_set_color(9, 0, 0, 20);
rgbSetLedColor(8, 0, 0, 20);
rgbSetLedColor(9, 0, 0, 20);
rgbLedColorApply();
}
+1 -8
View File
@@ -21,7 +21,6 @@
#include "stm32_adc.h"
#include "stm32_gpio.h"
#include "stm32_ws2812.h"
#include "stm32_spi.h"
#include "hal/adc_driver.h"
@@ -139,15 +138,9 @@ void delay_self(int count)
}
#if defined(LED_STRIP_GPIO)
// Common LED driver
extern const stm32_pulse_timer_t _led_timer;
void ledStripOff()
{
for (uint8_t i = 0; i < LED_STRIP_LENGTH; i++) {
ws2812_set_color(i, 0, 0, 0);
}
ws2812_update(&_led_timer);
rgbLedClearAll();
}
#endif
+1
View File
@@ -35,6 +35,7 @@ void fsLedRGB(uint8_t index, uint32_t color)
index = (index * 2) + CFS_LED_STRIP_START;
rgbSetLedColor(index, GET_RED(color), GET_GREEN(color),GET_BLUE(color));
rgbSetLedColor(index+1, GET_RED(color), GET_GREEN(color),GET_BLUE(color));
rgbLedColorApply();
}
uint32_t fsGetLedRGB(uint8_t index)
-39
View File
@@ -21,7 +21,6 @@
#include "stm32_hal_ll.h"
#include "stm32_gpio.h"
#include "stm32_ws2812.h"
#include "hal/adc_driver.h"
#include "hal/trainer_driver.h"
@@ -72,44 +71,6 @@ HardwareOptions hardwareOptions;
#include "storage/storage.h"
#endif
#if defined(SIXPOS_SWITCH_INDEX)
uint8_t lastADCState = 0;
uint8_t sixPosState = 0;
bool dirty = true;
uint16_t getSixPosAnalogValue(uint16_t adcValue)
{
uint8_t currentADCState = 0;
if (adcValue > 3800)
currentADCState = 6;
else if (adcValue > 3100)
currentADCState = 5;
else if (adcValue > 2300)
currentADCState = 4;
else if (adcValue > 1500)
currentADCState = 3;
else if (adcValue > 1000)
currentADCState = 2;
else if (adcValue > 400)
currentADCState = 1;
if (lastADCState != currentADCState) {
lastADCState = currentADCState;
} else if (lastADCState != 0 && lastADCState - 1 != sixPosState) {
sixPosState = lastADCState - 1;
dirty = true;
}
if (dirty) {
for (uint8_t i = 0; i < 6; i++) {
if (i == sixPosState)
ws2812_set_color(i, SIXPOS_LED_RED, SIXPOS_LED_GREEN, SIXPOS_LED_BLUE);
else
ws2812_set_color(i, 0, 0, 0);
}
rgbLedColorApply();
}
return (4096/5)*(sixPosState);
}
#endif
#if defined(SALED_PWR_GPIO)
void SALEDpwrInit()
{