ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
led_strip.cpp
Go to the documentation of this file.
1#include "led_strip.h"
2
3#ifdef USE_BK72XX
4
6#include "esphome/core/log.h"
7
8extern "C" {
9#include "rtos_pub.h"
10// rtos_pub.h must be included before the rest of the includes
11
12#include "arm_arch.h"
13#include "general_dma_pub.h"
14#include "gpio_pub.h"
15#include "icu_pub.h"
16#include "spi.h"
17#undef SPI_DAT
18#undef SPI_BASE
19};
20
21static const uint32_t SPI_TX_DMA_CHANNEL = GDMA_CHANNEL_3;
22
23static const uint32_t SPI_PERI_CLK_26M = 26000000;
24
25#if defined(USE_LIBRETINY_VARIANT_BK7251)
26static const uint32_t SPI_PERI_CLK_DCO = 180000000;
27#else
28static const uint32_t SPI_PERI_CLK_DCO = 120000000;
29#endif
30
31// The beken378 SDK has two SPI register layouts:
32// N-like (BK7231N, BK7238, BK7252N): CTRL +0x00, CONFIG +0x04, STAT +0x08, DAT +0x0C
33// T-like (BK7231T, BK7251): CTRL +0x00, STAT +0x04, DAT +0x08
34// The N-like variants also have TX enable / transfer length / TX finish bits in CONFIG.
35// The T-like variants have no CONFIG address.
36#if defined(USE_LIBRETINY_VARIANT_BK7231N) || defined(USE_LIBRETINY_VARIANT_BK7238)
37#define BEKEN_SPI_IS_N_LIKE
38#endif
39
40static const uint32_t SPI_BASE = 0x00802700;
41#ifdef BEKEN_SPI_IS_N_LIKE
42static const uint32_t SPI_DAT = SPI_BASE + 3 * 4;
43static const uint32_t SPI_CONFIG = SPI_BASE + 1 * 4;
44static const uint32_t SPI_TX_EN = 1 << 0;
45static const uint32_t SPI_TX_FINISH_EN = 1 << 2;
46static const uint32_t SPI_RX_FINISH_EN = 1 << 3;
47#else
48static const uint32_t SPI_DAT = SPI_BASE + 2 * 4;
49#endif
50
51static const uint32_t CTRL_NSSMD_3 = 1 << 17;
52
54
55ESPHOME_LOG_TAG(TAG, "beken_spi_led_strip");
56
57struct SpiData {
58 SemaphoreHandle_t dma_tx_semaphore;
59 volatile bool tx_in_progress;
60 bool first_run;
61};
62
63static SpiData *spi_data = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
64
65// BDK 3.0.78 makes REG_WRITE a statement expression that yields the volatile assignment,
66// a deprecated volatile read under clang
67static inline void reg_write(uint32_t addr, uint32_t value) {
68 // NOLINTNEXTLINE(performance-no-int-to-ptr) - MMIO register at a fixed address
69 *reinterpret_cast<volatile uint32_t *>(addr) = value;
70}
71
72static void set_spi_ctrl_register(uint32_t bit, bool val) {
73 uint32_t value = REG_READ(SPI_CTRL);
74 if (val == 0) {
75 value &= ~bit;
76 } else if (val == 1) {
77 value |= bit;
78 }
79 reg_write(SPI_CTRL, value);
80}
81
82#ifdef BEKEN_SPI_IS_N_LIKE
83static void set_spi_config_register(uint32_t bit, bool val) {
84 uint32_t value = REG_READ(SPI_CONFIG);
85 if (val == 0) {
86 value &= ~bit;
87 } else if (val == 1) {
88 value |= bit;
89 }
90 reg_write(SPI_CONFIG, value);
91}
92#endif
93
94void spi_dma_tx_enable(bool enable) {
95 GDMA_CFG_ST en_cfg;
96#ifdef BEKEN_SPI_IS_N_LIKE
97 set_spi_config_register(SPI_TX_EN, enable);
98#endif
99 en_cfg.channel = SPI_TX_DMA_CHANNEL;
100 en_cfg.param = enable ? 1 : 0;
101 sddev_control(GDMA_DEV_NAME, CMD_GDMA_SET_DMA_ENABLE, &en_cfg);
102}
103
104static void spi_set_clock(uint32_t max_hz) {
105 int source_clk = 0;
106 int spi_clk = 0;
107 int div = 0;
108 uint32_t param = PWD_SPI_CLK_BIT;
109 if (max_hz > 4333000) {
110 if (max_hz > 30000000) {
111 spi_clk = 30000000;
112 } else {
113 spi_clk = max_hz;
114 }
115 sddev_control(ICU_DEV_NAME, CMD_CLK_PWR_DOWN, &param);
116 source_clk = SPI_PERI_CLK_DCO;
117 param = PCLK_POSI_SPI;
118 sddev_control(ICU_DEV_NAME, CMD_CONF_PCLK_DCO, &param);
119 param = PWD_SPI_CLK_BIT;
120 sddev_control(ICU_DEV_NAME, CMD_CLK_PWR_UP, &param);
121 } else {
122 spi_clk = max_hz;
123#if CFG_XTAL_FREQUENCE
124 source_clk = CFG_XTAL_FREQUENCE;
125#else
126 source_clk = SPI_PERI_CLK_26M;
127#endif
128 param = PCLK_POSI_SPI;
129 sddev_control(ICU_DEV_NAME, CMD_CONF_PCLK_26M, &param);
130 }
131#ifdef BEKEN_SPI_IS_N_LIKE
132 // N-like variants: spi_clk = source_clk / (2 * div)
133 div = ((source_clk >> 1) / spi_clk);
134#else
135 // T-like variants: spi_clk = source_clk / (2 * (div + 1))
136 div = ((source_clk >> 1) / spi_clk) - 1;
137#endif
138 if (div < 2) {
139 div = 2;
140 } else if (div >= 255) {
141 div = 255;
142 }
143 param = REG_READ(SPI_CTRL);
144 param &= ~(SPI_CKR_MASK << SPI_CKR_POSI);
145 param |= (div << SPI_CKR_POSI);
146 reg_write(SPI_CTRL, param);
147#ifdef BEKEN_SPI_IS_N_LIKE
148 const int divisor = div;
149#else
150 const int divisor = div + 1;
151#endif
152 ESP_LOGD(TAG, "target frequency: %" PRIu32 ", actual frequency: %d", max_hz, source_clk / 2 / divisor);
153}
154
155void spi_dma_tx_finish_callback(unsigned int param) {
156 spi_data->tx_in_progress = false;
157 xSemaphoreGive(spi_data->dma_tx_semaphore);
158 spi_dma_tx_enable(false);
159}
160
162 RAMAllocator<uint8_t> allocator;
163 if (!this->internal_accessor_.allocate(allocator, this->num_leds_)) {
164 this->mark_failed(LOG_STR(ESP_LOG_MSG_ALLOC_FAIL));
165 return;
166 }
167
168 const size_t dma_buffer_size = (this->get_buffer_size_() * 8) + (2 * 64);
169 this->dma_buf_ = allocator.allocate(dma_buffer_size);
170 if (this->dma_buf_ == nullptr) {
171 this->mark_failed(LOG_STR(ESP_LOG_MSG_ALLOC_FAIL));
172 return;
173 }
174
175 memset(this->dma_buf_, 0, dma_buffer_size);
176
177 uint32_t value = PCLK_POSI_SPI;
178 sddev_control(ICU_DEV_NAME, CMD_CONF_PCLK_26M, &value);
179
180 value = PWD_SPI_CLK_BIT;
181 sddev_control(ICU_DEV_NAME, CMD_CLK_PWR_UP, &value);
182
183 if (spi_data != nullptr) {
184 this->mark_failed(LOG_STR("SPI already initialized"));
185 return;
186 }
187
188 spi_data = (SpiData *) calloc(1, sizeof(SpiData)); // NOLINT(cppcoreguidelines-no-malloc)
189 if (spi_data == nullptr) {
190 this->mark_failed(LOG_STR(ESP_LOG_MSG_ALLOC_FAIL));
191 return;
192 }
193
194 spi_data->dma_tx_semaphore = xSemaphoreCreateBinary();
195 if (spi_data->dma_tx_semaphore == nullptr) {
196 this->mark_failed(LOG_STR("TX semaphore init failed"));
197 return;
198 }
199
200 spi_data->first_run = true;
201
202 set_spi_ctrl_register(MSTEN, false);
203 set_spi_ctrl_register(BIT_WDTH, false);
204 spi_set_clock(this->spi_frequency_);
205 set_spi_ctrl_register(CKPOL, false);
206 set_spi_ctrl_register(CKPHA, false);
207 set_spi_ctrl_register(MSTEN, true);
208 set_spi_ctrl_register(SPIEN, true);
209
210 set_spi_ctrl_register(TXINT_EN, false);
211 set_spi_ctrl_register(RXINT_EN, false);
212#ifdef BEKEN_SPI_IS_N_LIKE
213 set_spi_config_register(SPI_TX_FINISH_EN, true);
214 set_spi_config_register(SPI_RX_FINISH_EN, true);
215#endif
216 set_spi_ctrl_register(RXOVR_EN, false);
217 set_spi_ctrl_register(TXOVR_EN, false);
218
219 value = REG_READ(SPI_CTRL);
220 value &= ~CTRL_NSSMD_3;
221 value |= (1 << 17);
222 reg_write(SPI_CTRL, value);
223
224#ifdef BEKEN_SPI_IS_N_LIKE
225 value = GFUNC_MODE_SPI_DMA;
226#else
227 // For T-like variants, GFUNC_MODE_SPI_DMA identifies an SPI-DMA peripheral
228 // with a different base address. We want the regular SPI controller here.
229 value = GFUNC_MODE_SPI;
230#endif
231 sddev_control(GPIO_DEV_NAME, CMD_GPIO_ENABLE_SECOND, &value);
232#ifdef BEKEN_SPI_IS_N_LIKE
233 set_spi_ctrl_register(SPI_S_CS_UP_INT_EN, false);
234#endif
235
236 GDMA_CFG_ST en_cfg;
237 GDMACFG_TPYES_ST init_cfg;
238 memset(&init_cfg, 0, sizeof(GDMACFG_TPYES_ST));
239
240 init_cfg.dstdat_width = 8;
241 init_cfg.srcdat_width = 32;
242 init_cfg.dstptr_incr = 0;
243 init_cfg.srcptr_incr = 1;
244 init_cfg.src_start_addr = this->dma_buf_;
245 init_cfg.dst_start_addr = (void *) SPI_DAT; // NOLINT(performance-no-int-to-ptr) SPI_DMA_REG4_TXFIFO
246 init_cfg.channel = SPI_TX_DMA_CHANNEL;
247 init_cfg.prio = 0; // 10
248 init_cfg.u.type4.src_loop_start_addr = this->dma_buf_;
249 init_cfg.u.type4.src_loop_end_addr = this->dma_buf_ + dma_buffer_size;
250 init_cfg.half_fin_handler = nullptr;
251 init_cfg.fin_handler = spi_dma_tx_finish_callback;
252 init_cfg.src_module = GDMA_X_SRC_DTCM_RD_REQ;
253 init_cfg.dst_module = GDMA_X_DST_GSPI_TX_REQ; // GDMA_X_DST_HSSPI_TX_REQ
254 sddev_control(GDMA_DEV_NAME, CMD_GDMA_CFG_TYPE4, (void *) &init_cfg);
255 en_cfg.channel = SPI_TX_DMA_CHANNEL;
256 en_cfg.param = dma_buffer_size;
257 sddev_control(GDMA_DEV_NAME, CMD_GDMA_SET_TRANS_LENGTH, (void *) &en_cfg);
258 en_cfg.channel = SPI_TX_DMA_CHANNEL;
259 en_cfg.param = 0;
260 sddev_control(GDMA_DEV_NAME, CMD_GDMA_CFG_WORK_MODE, (void *) &en_cfg);
261 en_cfg.channel = SPI_TX_DMA_CHANNEL;
262 en_cfg.param = 0;
263 sddev_control(GDMA_DEV_NAME, CMD_GDMA_CFG_SRCADDR_LOOP, &en_cfg);
264
265 spi_dma_tx_enable(false);
266
267#ifdef BEKEN_SPI_IS_N_LIKE
268 // N-like variants: The SPI controller must be told how many bytes to clock out.
269 value = REG_READ(SPI_CONFIG);
270 value &= ~(0xFFF << 8);
271 value |= ((dma_buffer_size & 0xFFF) << 8);
272 reg_write(SPI_CONFIG, value);
273#else
274 // T-like variants: No transfer-length register. The SPI controller shifts out
275 // everything the DMA writes into SPI_DAT.
276#endif
277
278 this->accessor_ = &this->internal_accessor_;
279}
280
281void BekenSPILEDStripLightOutput::set_led_params(uint8_t bit0, uint8_t bit1, uint32_t spi_frequency) {
282 this->bit0_ = bit0;
283 this->bit1_ = bit1;
284 this->spi_frequency_ = spi_frequency;
285}
286
288 uint32_t now = micros();
289 if (this->throttle_show_(now, this->last_refresh_, this->max_refresh_rate_))
290 return;
291 this->last_refresh_ = now;
292 this->mark_shown_();
293
294 ESP_LOGVV(TAG, "Writing RGB values to bus");
295
296 if (spi_data == nullptr) {
297 ESP_LOGE(TAG, "SPI not initialized");
298 this->status_set_warning();
299 return;
300 }
301
302 if (!spi_data->first_run && !xSemaphoreTake(spi_data->dma_tx_semaphore, 10 / portTICK_PERIOD_MS)) {
303 // Recover so the next update can try again instead of being stuck forever.
304 ESP_LOGE(TAG, "Timed out waiting for semaphore");
305 spi_dma_tx_enable(false);
306 spi_data->tx_in_progress = false;
307 xSemaphoreGive(spi_data->dma_tx_semaphore);
308 this->status_set_warning();
309 this->schedule_show();
310 return;
311 }
312
313#ifndef BEKEN_SPI_IS_N_LIKE
314 if (!spi_data->first_run) {
315 // For T-like variants, the GDMA finish interrupt only means that all bytes were written into
316 // the SPI TX FIFO. We must still wait for it to drain before refilling it.
317 uint32_t wait_start = micros();
318 while (!(REG_READ(SPI_STAT) & TXFIFO_EMPTY)) {
319 if (micros() - wait_start > 2000) {
320 ESP_LOGW(TAG, "SPI TX FIFO did not drain in time");
321 break;
322 }
323 }
324 }
325#endif
326
327 if (spi_data->tx_in_progress) {
328 ESP_LOGE(TAG, "tx_in_progress is set");
329 xSemaphoreGive(spi_data->dma_tx_semaphore);
330 this->status_set_warning();
331 this->schedule_show();
332 return;
333 }
334
335 spi_data->tx_in_progress = true;
336
337 size_t buffer_size = this->get_buffer_size_();
338 size_t size = 0;
339 uint8_t *psrc = this->internal_accessor_.color_data;
340 uint8_t *pdest = this->dma_buf_ + 64;
341 // The 64 byte padding is a workaround for a SPI DMA bug where the
342 // output doesn't exactly start at the beginning of dma_buf_
343
344 while (size < buffer_size) {
345 uint8_t b = *psrc;
346 for (int i = 0; i < 8; i++) {
347 *pdest++ = b & (1 << (7 - i)) ? this->bit1_ : this->bit0_;
348 }
349 size++;
350 psrc++;
351 }
352
353 spi_data->first_run = false;
354 spi_dma_tx_enable(true);
355
356 this->status_clear_warning();
357}
358
360 ESP_LOGCONFIG(TAG,
361 "Beken SPI LED Strip:\n"
362 " Pin: %u",
363 this->pin_);
364 char channel_colors[5];
365 ESP_LOGCONFIG(TAG,
366 " Channel colors: %s\n"
367 " Max refresh rate: %" PRIu32 "\n"
368 " Number of LEDs: %u",
369 this->internal_accessor_.channel_colors.to_string(channel_colors), this->max_refresh_rate_,
370 this->num_leds_);
371}
372
374
375} // namespace esphome::beken_spi_led_strip
376
377#endif // USE_BK72XX
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:309
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
T * allocate(size_t n)
Definition helpers.h:2423
light::CorrectedBufferColorAccessor internal_accessor_
Definition led_strip.h:41
void set_led_params(uint8_t bit0, uint8_t bit1, uint32_t spi_frequency)
void write_state(light::LightState *state) override
bool throttle_show_(uint32_t now_us, uint32_t last_us, uint32_t interval_us)
bool allocate(RAMAllocator< uint8_t > &allocator, size_t num_leds, size_t extra_bytes=0)
Allocate the color and effect data for num_leds LEDs as one zeroed block, followed by extra_bytes mor...
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
bool state
Definition fan.h:2
mopeka_std_values val[3]
void spi_dma_tx_enable(bool enable)
Definition led_strip.cpp:94
void spi_dma_tx_finish_callback(unsigned int param)
ESPHOME_LOG_TAG(TAG, "beken_spi_led_strip")
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
uint32_t value
Definition hal.cpp:41
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
static void uint32_t
const char * to_string(char *buf) const
Write the order back out as text, e.g.