From 85209f6c80f876cbc450d95b17350a41f85d61fa Mon Sep 17 00:00:00 2001 From: Cyril Fougeray Date: Mon, 3 Jun 2024 15:15:32 +0200 Subject: [PATCH] apa102: fix end frame end frame used to supply clock pulses so that data goes to next led Signed-off-by: Cyril Fougeray --- drivers/led_strip/apa102.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/led_strip/apa102.c b/drivers/led_strip/apa102.c index f5f4cb1d0378..f9a31f25cf77 100644 --- a/drivers/led_strip/apa102.c +++ b/drivers/led_strip/apa102.c @@ -21,7 +21,18 @@ static int apa102_update(const struct device *dev, void *buf, size_t size) { const struct apa102_config *config = dev->config; static const uint8_t zeros[] = { 0, 0, 0, 0 }; - static const uint8_t ones[] = { 0xFF, 0xFF, 0xFF, 0xFF }; + + /* + * the only function of the “End frame” is to supply more clock pulses + * to the string until the data has permeated to the last LED. The + * number of clock pulses required is exactly half the total number + * of LEDs in the string + */ + BUILD_ASSERT(sizeof(struct led_rgb) == 4); + uint8_t ones[((size / sizeof(struct led_rgb)) / sizeof(uint8_t) / 2) + 1]; + + memset(ones, 0xFF, sizeof(ones)); + const struct spi_buf tx_bufs[] = { { /* Start frame: at least 32 zeros */ @@ -50,13 +61,14 @@ static int apa102_update(const struct device *dev, void *buf, size_t size) return spi_write_dt(&config->bus, &tx); } -static int apa102_update_rgb(const struct device *dev, struct led_rgb *pixels, +static int apa102_update_rgb(const struct device *dev, + struct led_rgb *pixels, size_t count) { uint8_t *p = (uint8_t *)pixels; size_t i; /* SOF (3 bits) followed by the 0 to 31 global dimming level */ - uint8_t prefix = 0xE0; + const uint8_t prefix = 0xE0; /* Rewrite to the on-wire format */ for (i = 0; i < count; i++) {