Skip to content

Commit c2e47bd

Browse files
waihongtamfabiobaltieri
authored andcommitted
led_strip: ws2812_spi: Optimize for 8-bit symbols with build-time check
The ws2812_spi driver supports both a fast path for 8-bit symbols and a generic path for other sizes. This change introduces a devicetree-based, build-time check to determine if all enabled instances use 8-bit symbols. If so, the generic path logic is removed from the build. Signed-off-by: Wai-Hong Tam <[email protected]>
1 parent a5bc5fa commit c2e47bd

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

drivers/led_strip/ws2812_spi.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ static const struct ws2812_spi_cfg *dev_cfg(const struct device *dev)
6161
return dev->config;
6262
}
6363

64+
/*
65+
* Check if all enabled instances have a `bits-per-symbol` of 8.
66+
* This allows the fast path to be used exclusively, removing the generic
67+
* path logic from the build when it's not needed.
68+
*/
69+
#define WS2812_SPI_8BIT_SYMBOL_CHECK(idx) \
70+
(DT_INST_PROP(idx, bits_per_symbol) == SPI_FRAME_BITS) &&
71+
#define WS2812_SPI_ALL_SYMBOLS_ARE_8_BITS \
72+
(DT_INST_FOREACH_STATUS_OKAY(WS2812_SPI_8BIT_SYMBOL_CHECK) 1)
73+
6474
/*
6575
* Serialize an 8-bit color value into the SPI buffer, MSbit first. Each of
6676
* the 8 data bits is represented by a N-bit symbol (`one_frame` for a '1' or
@@ -76,8 +86,10 @@ static inline void ws2812_spi_ser(uint8_t color, uint8_t one, uint8_t zero,
7686
if (bits_per_symbol == SPI_FRAME_BITS) {
7787
/* Fast path for the common 8-bit symbol case */
7888
*(*buf)++ = pattern;
79-
} else {
80-
/* Generic path for N-bit symbols */
89+
}
90+
#if !WS2812_SPI_ALL_SYMBOLS_ARE_8_BITS
91+
else {
92+
/* Generic path for N-bit symbols, compiled only when needed. */
8193
for (int p = bits_per_symbol - 1; p >= 0; p--) {
8294
if (pattern & BIT(p)) {
8395
**buf |= *bit_mask;
@@ -92,6 +104,7 @@ static inline void ws2812_spi_ser(uint8_t color, uint8_t one, uint8_t zero,
92104
}
93105
}
94106
}
107+
#endif
95108
}
96109
}
97110

0 commit comments

Comments
 (0)