FastLED 3.9.15
Loading...
Searching...
No Matches
I2SClockLessLedDriveresp32s3.h
Go to the documentation of this file.
1
2#if !__has_include("esp_memory_utils.h")
3#error \
4 "esp_memory_utils.h is not available, are you using esp-idf 4.4 or earlier?"
5#else
6
7#pragma GCC diagnostic push
8#pragma GCC diagnostic ignored "-Wattributes"
9#pragma GCC diagnostic ignored "-Wvolatile"
10
11#include "esp_attr.h"
12#include "esp_check.h"
13#include "esp_heap_caps.h"
14#include "esp_intr_alloc.h"
15#include "esp_memory_utils.h"
16#include "esp_pm.h"
17#include <stdint.h>
18#include <stdio.h> // ok include
19#include <string.h>
20// #include "esp_lcd_panel_io_interface.h"
21// #include "esp_lcd_panel_io.h"
22#include "driver/gpio.h"
23#include "esp_memory_utils.h"
24#include "esp_private/gdma.h"
25#include "esp_rom_gpio.h"
26#include "hal/dma_types.h"
27#include "hal/gpio_hal.h"
28#include "soc/rtc.h" // for `rtc_clk_xtal_freq_get()`
29#include "soc/soc_caps.h"
30// #include "esp_private/periph_ctrl.h"
31
32// #include "esp_lcd_common.h"
33#include "hal/lcd_hal.h"
34#include "hal/lcd_ll.h"
35#include "soc/lcd_periph.h"
36
37#include "esp_heap_caps.h"
38#include "esp_lcd_panel_interface.h"
39#include "esp_lcd_panel_io.h"
40#include "esp_lcd_panel_io_interface.h"
41#include "esp_lcd_panel_ops.h"
42#include "esp_log.h"
43#include "esp_timer.h"
44#include "soc/gdma_reg.h"
45#include "platforms/esp/esp_version.h"
46
47#define IDF_5_3_OR_EARLIER (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 0))
48
49// According to bug reports, this driver does not work well with the new WS2812-v5b. This is
50// probably due to the extrrra long reset time requirements of this chipset. so we put in
51// a hack that will always add 300 uS to the reset time.
52#define FASTLED_EXPERIMENTAL_YVES_EXTRA_WAIT_MICROS 300
53
54#ifndef NUMSTRIPS
55#define NUMSTRIPS 16
56#endif
57
58#ifndef SNAKEPATTERN
59#define SNAKEPATTERN 1
60#endif
61
62#ifndef ALTERNATEPATTERN
63#define ALTERNATEPATTERN 1
64#endif
65
66#define I2S_DEVICE 0
67
68#define AA (0x00AA00AAL)
69#define CC (0x0000CCCCL)
70#define FF (0xF0F0F0F0L)
71#define FF2 (0x0F0F0F0FL)
72
73#ifndef MIN
74#define MIN(a, b) (((a) < (b)) ? (a) : (b))
75#endif
76
77#define __OFFSET 0 // (24*3*2*2*2+2)
78
79#define __OFFSET_END (24 * 3 * 2 * 2 * 2 + 2)
80#ifndef HARDWARESPRITES
81#define HARDWARESPRITES 0
82#endif
83
84#if HARDWARESPRITES == 1
85#include "hardwareSprite.h"
86#endif
87
88#ifdef COLOR_ORDER_GRBW
89#define _p_r 1
90#define _p_g 0
91#define _p_b 2
92#define _nb_components 4
93#else
94#ifdef COLOR_ORDER_RGB
95#define _p_r 0
96#define _p_g 1
97#define _p_b 2
98#define _nb_components 3
99#else
100#ifdef COLOR_ORDER_RBG
101#define _p_r 0
102#define _p_g 2
103#define _p_b 1
104#define _nb_components 3
105#else
106#ifdef COLOR_ORDER_GBR
107#define _p_r 2
108#define _p_g 0
109#define _p_b 1
110#define _nb_components 3
111#else
112#ifdef COLOR_ORDER_BGR
113#define _p_r 2
114#define _p_g 1
115#define _p_b 0
116#define _nb_components 3
117#else
118#ifdef COLOR_ORDER_BRG
119#define _p_r 1
120#define _p_g 2
121#define _p_b 0
122#define _nb_components 3
123#else
124#ifdef COLOR_ORDER_GRB
125#define _p_r 1
126#define _p_g 0
127#define _p_b 2
128#define _nb_components 3
129#else
130
131#define _p_r 1
132#define _p_g 0
133#define _p_b 2
134#define _nb_components 3
135#endif
136#endif
137#endif
138#endif
139#endif
140#endif
141#endif
142
143#ifdef USE_PIXELSLIB
144// #include "pixelslib.h"
145#else
146#include "___pixeltypes.h"
147#endif
148
149#define LCD_DRIVER_PSRAM_DATA_ALIGNMENT 64
150
151// Note: I2S REQUIRES that the FASTLED_OVERCLOCK factor is a a build-level-define and
152// not an include level define. This is easy if you are already using PlatformIO or CMake.
153// If you are using ArduinoIDE you'll have to download FastLED source code and hack the src
154// to make this work.
155#ifdef FASTLED_OVERCLOCK
156#define FASTLED_ESP32S3_I2S_CLOCK_HZ_SCALE (FASTLED_OVERCLOCK)
157#else
158#define FASTLED_ESP32S3_I2S_CLOCK_HZ_SCALE (1)
159#endif
160
161#ifndef FASTLED_ESP32S3_I2S_CLOCK_HZ
162#define FASTLED_ESP32S3_I2S_CLOCK_HZ uint32_t((24 * 100 * 1000)*FASTLED_ESP32S3_I2S_CLOCK_HZ_SCALE)
163#endif
164
165namespace fl {
166
167static bool IRAM_ATTR flush_ready(esp_lcd_panel_io_handle_t panel_io,
168 esp_lcd_panel_io_event_data_t *edata,
169 void *user_ctx);
170
171typedef union {
172 uint8_t bytes[16];
173 uint32_t shorts[8];
174 uint32_t raw[2];
175} Lines;
176
177enum colorarrangment {
178 ORDER_GRBW,
179 ORDER_RGB,
180 ORDER_RBG,
181 ORDER_GRB,
182 ORDER_GBR,
183 ORDER_BRG,
184 ORDER_BGR,
185};
186
187enum displayMode {
188 NO_WAIT,
189 WAIT,
190 LOOP,
191 LOOP_INTERUPT,
192};
193
194bool DRIVER_READY = true;
195
196typedef struct led_driver_t led_driver_t;
197
198struct led_driver_t {
199 size_t (*init)();
200 void (*update)(uint8_t *colors, size_t len);
201};
202volatile xSemaphoreHandle I2SClocklessLedDriverS3_sem = NULL;
203volatile bool isDisplaying = false;
204volatile bool iswaiting = false;
205
206static void IRAM_ATTR transpose16x1_noinline2(unsigned char *A, uint16_t *B) {
207
208 uint32_t x, y, x1, y1, t;
209
210 y = *(unsigned int *)(A);
211#if NUMSTRIPS > 4
212 x = *(unsigned int *)(A + 4);
213#else
214 x = 0;
215#endif
216
217#if NUMSTRIPS > 8
218 y1 = *(unsigned int *)(A + 8);
219#else
220 y1 = 0;
221#endif
222#if NUMSTRIPS > 12
223 x1 = *(unsigned int *)(A + 12);
224#else
225 x1 = 0;
226#endif
227
228 // pre-transform x
229#if NUMSTRIPS > 4
230 t = (x ^ (x >> 7)) & AA;
231 x = x ^ t ^ (t << 7);
232 t = (x ^ (x >> 14)) & CC;
233 x = x ^ t ^ (t << 14);
234#endif
235#if NUMSTRIPS > 12
236 t = (x1 ^ (x1 >> 7)) & AA;
237 x1 = x1 ^ t ^ (t << 7);
238 t = (x1 ^ (x1 >> 14)) & CC;
239 x1 = x1 ^ t ^ (t << 14);
240#endif
241 // pre-transform y
242 t = (y ^ (y >> 7)) & AA;
243 y = y ^ t ^ (t << 7);
244 t = (y ^ (y >> 14)) & CC;
245 y = y ^ t ^ (t << 14);
246#if NUMSTRIPS > 8
247 t = (y1 ^ (y1 >> 7)) & AA;
248 y1 = y1 ^ t ^ (t << 7);
249 t = (y1 ^ (y1 >> 14)) & CC;
250 y1 = y1 ^ t ^ (t << 14);
251#endif
252 // final transform
253 t = (x & FF) | ((y >> 4) & FF2);
254 y = ((x << 4) & FF) | (y & FF2);
255 x = t;
256
257 t = (x1 & FF) | ((y1 >> 4) & FF2);
258 y1 = ((x1 << 4) & FF) | (y1 & FF2);
259 x1 = t;
260
261 *((uint16_t *)(B)) =
262 (uint16_t)(((x & 0xff000000) >> 8 | ((x1 & 0xff000000))) >> 16);
263 *((uint16_t *)(B + 3)) =
264 (uint16_t)(((x & 0xff0000) >> 16 | ((x1 & 0xff0000) >> 8)));
265 *((uint16_t *)(B + 6)) =
266 (uint16_t)(((x & 0xff00) | ((x1 & 0xff00) << 8)) >> 8);
267 *((uint16_t *)(B + 9)) = (uint16_t)((x & 0xff) | ((x1 & 0xff) << 8));
268 *((uint16_t *)(B + 12)) =
269 (uint16_t)(((y & 0xff000000) >> 8 | ((y1 & 0xff000000))) >> 16);
270 *((uint16_t *)(B + 15)) =
271 (uint16_t)(((y & 0xff0000) | ((y1 & 0xff0000) << 8)) >> 16);
272 *((uint16_t *)(B + 18)) =
273 (uint16_t)(((y & 0xff00) | ((y1 & 0xff00) << 8)) >> 8);
274 *((uint16_t *)(B + 21)) = (uint16_t)((y & 0xff) | ((y1 & 0xff) << 8));
275}
276
277esp_lcd_panel_io_handle_t led_io_handle = NULL;
278
279class I2SClocklessLedDriveresp32S3 {
280
281 public:
282 int testcount;
283 uint16_t *buffers[2];
284 uint16_t *led_output = NULL;
285 uint16_t *led_output2 = NULL;
286 uint8_t *ledsbuff = NULL;
287 int num_leds_per_strip;
288 int _numstrips;
289 int currentframe;
290
291 uint8_t __green_map[256];
292 uint8_t __blue_map[256];
293 uint8_t __red_map[256];
294 uint8_t __white_map[256];
295 uint8_t _brightness;
296 float _gammar, _gammab, _gammag, _gammaw;
297
298 void setBrightness(int brightness) {
299 _brightness = brightness;
300 float tmp;
301 for (int i = 0; i < 256; i++) {
302 tmp = powf((float)i / 255, 1 / _gammag);
303 __green_map[i] = (uint8_t)(tmp * brightness);
304 tmp = powf((float)i / 255, 1 / _gammag);
305 __blue_map[i] = (uint8_t)(tmp * brightness);
306 tmp = powf((float)i / 255, 1 / _gammag);
307 __red_map[i] = (uint8_t)(tmp * brightness);
308 tmp = powf((float)i / 255, 1 / _gammag);
309 __white_map[i] = (uint8_t)(tmp * brightness);
310 }
311 }
312
313 void setGamma(float gammar, float gammab, float gammag, float gammaw) {
314 _gammag = gammag;
315 _gammar = gammar;
316 _gammaw = gammaw;
317 _gammab = gammab;
318 setBrightness(_brightness);
319 }
320
321 void setGamma(float gammar, float gammab, float gammag) {
322 _gammag = gammag;
323 _gammar = gammar;
324 _gammab = gammab;
325 setBrightness(_brightness);
326 }
327
328 void _initled(uint8_t *leds, const int *pins, int numstrip,
329 int NUM_LED_PER_STRIP) {
330
331 // esp_lcd_panel_io_handle_t init_lcd_driver(unsigned int
332 // FASTLED_ESP32S3_I2S_CLOCK_HZ, size_t _nb_components) {
333
334 esp_lcd_i80_bus_handle_t i80_bus = NULL;
335
336 esp_lcd_i80_bus_config_t bus_config;
337
338 bus_config.clk_src = LCD_CLK_SRC_PLL160M;
339 bus_config.dc_gpio_num = 0;
340 bus_config.wr_gpio_num = 0;
341 // bus_config.data_gpio_nums = (int*)malloc(16*sizeof(int));
342 for (int i = 0; i < numstrip; i++) {
343 bus_config.data_gpio_nums[i] = pins[i];
344 }
345 if (numstrip < 16) {
346 for (int i = numstrip; i < 16; i++) {
347 bus_config.data_gpio_nums[i] = 0;
348 }
349 }
350 bus_config.bus_width = 16;
351 bus_config.max_transfer_bytes =
352 _nb_components * NUM_LED_PER_STRIP * 8 * 3 * 2 + __OFFSET + __OFFSET_END;
353 #if IDF_5_3_OR_EARLIER
354 #pragma GCC diagnostic push
355 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
356 #endif
357 // In IDF 5.3, psram_trans_align became deprecated. We kick the can down
358 // the road a little bit and suppress the warning until idf 5.4 arrives.
359 bus_config.psram_trans_align = LCD_DRIVER_PSRAM_DATA_ALIGNMENT;
360 bus_config.sram_trans_align = 4;
361 #if IDF_5_3_OR_EARLIER
362 #pragma GCC diagnostic pop
363 #endif
364
365 ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
366
367 esp_lcd_panel_io_i80_config_t io_config;
368
369 io_config.cs_gpio_num = -1;
370 io_config.pclk_hz = FASTLED_ESP32S3_I2S_CLOCK_HZ;
371 io_config.trans_queue_depth = 1;
372 io_config.dc_levels = {
373 .dc_idle_level = 0,
374 .dc_cmd_level = 0,
375 .dc_dummy_level = 0,
376 .dc_data_level = 1,
377 };
378 //.on_color_trans_done = flush_ready,
379 // .user_ctx = nullptr,
380 io_config.lcd_cmd_bits = 0;
381 io_config.lcd_param_bits = 0;
382 io_config.user_ctx = this;
383
384 io_config.on_color_trans_done = flush_ready;
385 ESP_ERROR_CHECK(
386 esp_lcd_new_panel_io_i80(i80_bus, &io_config, &led_io_handle));
387 }
388
389 void initled(uint8_t *leds, const int *pins, int numstrip,
390 int NUM_LED_PER_STRIP) {
391 currentframe = 0;
392 _gammab = 1;
393 _gammar = 1;
394 _gammag = 1;
395 _gammaw = 1;
396 setBrightness(255);
397 if (I2SClocklessLedDriverS3_sem == NULL) {
398 I2SClocklessLedDriverS3_sem = xSemaphoreCreateBinary();
399 }
400 // esp_lcd_panel_io_handle_t init_lcd_driver(unsigned int
401 // FASTLED_ESP32S3_I2S_CLOCK_HZ, size_t _nb_components) {
402 led_output = (uint16_t *)heap_caps_aligned_alloc(
403 LCD_DRIVER_PSRAM_DATA_ALIGNMENT,
404 8 * _nb_components * NUM_LED_PER_STRIP * 3 * 2 + __OFFSET +
405 __OFFSET_END,
406 MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
407 memset(led_output, 0,
408 8 * _nb_components * NUM_LED_PER_STRIP * 3 * 2 + __OFFSET +
409 __OFFSET_END);
410
411 led_output2 = (uint16_t *)heap_caps_aligned_alloc(
412 LCD_DRIVER_PSRAM_DATA_ALIGNMENT,
413 8 * _nb_components * NUM_LED_PER_STRIP * 3 * 2 + __OFFSET +
414 __OFFSET_END,
415 MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
416 memset(led_output2, 0,
417 8 * _nb_components * NUM_LED_PER_STRIP * 3 * 2 + __OFFSET +
418 __OFFSET_END);
419 buffers[0] = led_output;
420 buffers[1] = led_output2;
421 // led_output[0] = 0xFFFF; //the +1 because it's like the first value
422 // doesnt get pushed do not ask me why for now
423 // led_output2[0] = 0xFFFF;
424 led_output2 += __OFFSET / 2;
425 led_output += __OFFSET / 2;
426
427 for (int i = 0; i < NUM_LED_PER_STRIP * _nb_components * 8; i++) {
428 led_output[3 * i + 1] =
429 0xFFFF; // the +1 because it's like the first value doesnt get
430 // pushed do not ask me why for now
431 led_output2[3 * i + 1] = 0xFFFF;
432 }
433 ledsbuff = leds;
434 _numstrips = numstrip;
435 num_leds_per_strip = NUM_LED_PER_STRIP;
436 _initled(leds, pins, numstrip, NUM_LED_PER_STRIP);
437 }
438
439 void transposeAll(uint16_t *ledoutput) {
440
441 uint16_t ledToDisplay = 0;
442 Lines secondPixel[_nb_components];
443 uint16_t *buff =
444 ledoutput + 2; //+1 pour le premier empty +1 pour le 1 systématique
445 uint16_t jump = num_leds_per_strip * _nb_components;
446 for (int j = 0; j < num_leds_per_strip; j++) {
447 uint8_t *poli = ledsbuff + ledToDisplay * _nb_components;
448 for (int i = 0; i < _numstrips; i++) {
449
450 secondPixel[_p_g].bytes[i] = __green_map[*(poli + 1)];
451 secondPixel[_p_r].bytes[i] = __red_map[*(poli + 0)];
452 secondPixel[_p_b].bytes[i] = __blue_map[*(poli + 2)];
453 if (_nb_components > 3)
454 secondPixel[3].bytes[i] = __white_map[*(poli + 3)];
455 // #endif
456 poli += jump;
457 }
458 ledToDisplay++;
459 transpose16x1_noinline2(secondPixel[0].bytes, buff);
460 buff += 24;
461 transpose16x1_noinline2(secondPixel[1].bytes, buff);
462 buff += 24;
463 transpose16x1_noinline2(secondPixel[2].bytes, buff);
464 buff += 24;
465 if (_nb_components > 3) {
466 transpose16x1_noinline2(secondPixel[3].bytes, buff);
467 buff += 24;
468 }
469 }
470 }
471
472 void show() {
473 transposeAll(buffers[currentframe]);
474 if (isDisplaying) {
475 // Serial.println("on display dejà");
476 iswaiting = true;
477 if (I2SClocklessLedDriverS3_sem == NULL)
478 I2SClocklessLedDriverS3_sem = xSemaphoreCreateBinary();
479 xSemaphoreTake(I2SClocklessLedDriverS3_sem, portMAX_DELAY);
480 }
481 isDisplaying = true;
482
483 if (FASTLED_EXPERIMENTAL_YVES_EXTRA_WAIT_MICROS) {
484 delayMicroseconds(FASTLED_EXPERIMENTAL_YVES_EXTRA_WAIT_MICROS);
485 }
486
487 led_io_handle->tx_color(led_io_handle, 0x2C, buffers[currentframe],
488 _nb_components * num_leds_per_strip * 8 * 3 *
489 2 +
490 __OFFSET + __OFFSET_END);
491
492 currentframe = (currentframe + 1) % 2;
493 }
494};
495
496static bool IRAM_ATTR flush_ready(esp_lcd_panel_io_handle_t panel_io,
497 esp_lcd_panel_io_event_data_t *edata,
498 void *user_ctx) {
499 // printf("we're here");
500 DRIVER_READY = true;
501 isDisplaying = false;
502 I2SClocklessLedDriveresp32S3 *cont =
503 (I2SClocklessLedDriveresp32S3 *)user_ctx;
504 cont->testcount++;
505 if (iswaiting) {
506 portBASE_TYPE HPTaskAwoken = 0;
507 iswaiting = false;
508 xSemaphoreGiveFromISR(I2SClocklessLedDriverS3_sem, &HPTaskAwoken);
509 if (HPTaskAwoken == pdTRUE)
510 portYIELD_FROM_ISR(HPTaskAwoken);
511 }
512 return false;
513}
514
515#pragma GCC diagnostic pop
516
517} // namespace fl
518
519#endif // __has_include("esp_memory_utils.h")
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
int y
Definition Audio.ino:72
int x
Definition Audio.ino:71
UISlider brightness("Brightness", 255, 0, 255, 1)
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16