2#if !__has_include("esp_memory_utils.h")
4 "esp_memory_utils.h is not available, are you using esp-idf 4.4 or earlier?"
7#pragma GCC diagnostic push
8#pragma GCC diagnostic ignored "-Wattributes"
9#pragma GCC diagnostic ignored "-Wvolatile"
13#include "esp_heap_caps.h"
14#include "esp_intr_alloc.h"
15#include "esp_memory_utils.h"
18#include "platforms/assert_defs.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"
29#include "soc/soc_caps.h"
33#include "hal/lcd_hal.h"
34#include "hal/lcd_ll.h"
35#include "soc/lcd_periph.h"
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"
44#include "soc/gdma_reg.h"
45#include "platforms/esp/esp_version.h"
47#define IDF_5_3_OR_EARLIER (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 0))
52#define FASTLED_EXPERIMENTAL_YVES_EXTRA_WAIT_MICROS 300
62#ifndef ALTERNATEPATTERN
63#define ALTERNATEPATTERN 1
68#pragma push_macro("AA")
69#pragma push_macro("CC")
70#pragma push_macro("FF")
71#pragma push_macro("FF2")
99#define AA (0x00AA00AAL)
100#define CC (0x0000CCCCL)
101#define FF (0xF0F0F0F0L)
102#define FF2 (0x0F0F0F0FL)
109#define MIN(a, b) (((a) < (b)) ? (a) : (b))
114#define __OFFSET_END (24 * 3 * 2 * 2 * 2 + 2)
115#ifndef HARDWARESPRITES
116#define HARDWARESPRITES 0
119#if HARDWARESPRITES == 1
120#include "hardwareSprite.h"
123#ifdef COLOR_ORDER_GRBW
127#define _nb_components 4
129#ifdef COLOR_ORDER_RGB
133#define _nb_components 3
135#ifdef COLOR_ORDER_RBG
139#define _nb_components 3
141#ifdef COLOR_ORDER_GBR
145#define _nb_components 3
147#ifdef COLOR_ORDER_BGR
151#define _nb_components 3
153#ifdef COLOR_ORDER_BRG
157#define _nb_components 3
159#ifdef COLOR_ORDER_GRB
163#define _nb_components 3
169#define _nb_components 3
184#define LCD_DRIVER_PSRAM_DATA_ALIGNMENT 64
190#ifdef FASTLED_OVERCLOCK
191#define FASTLED_ESP32S3_I2S_CLOCK_HZ_SCALE (FASTLED_OVERCLOCK)
193#define FASTLED_ESP32S3_I2S_CLOCK_HZ_SCALE (1)
196#ifndef FASTLED_ESP32S3_I2S_CLOCK_HZ
197#define FASTLED_ESP32S3_I2S_CLOCK_HZ uint32_t((24 * 100 * 1000)*FASTLED_ESP32S3_I2S_CLOCK_HZ_SCALE)
202static bool IRAM_ATTR flush_ready(esp_lcd_panel_io_handle_t panel_io,
203 esp_lcd_panel_io_event_data_t *edata,
212enum colorarrangment {
229bool DRIVER_READY =
true;
231typedef struct led_driver_t led_driver_t;
235 void (*update)(uint8_t *colors,
size_t len);
237volatile xSemaphoreHandle I2SClocklessLedDriverS3_sem = NULL;
238volatile bool isDisplaying =
false;
239volatile bool iswaiting =
false;
241static void IRAM_ATTR transpose16x1_noinline2(
unsigned char *A, uint16_t *B) {
243 uint32_t
x,
y, x1, y1,
t;
245 y = *(
unsigned int *)(A);
247 x = *(
unsigned int *)(A + 4);
253 y1 = *(
unsigned int *)(A + 8);
258 x1 = *(
unsigned int *)(A + 12);
265 t = (
x ^ (
x >> 7)) & AA;
266 x =
x ^
t ^ (
t << 7);
267 t = (
x ^ (
x >> 14)) & CC;
268 x =
x ^
t ^ (
t << 14);
271 t = (x1 ^ (x1 >> 7)) & AA;
272 x1 = x1 ^
t ^ (
t << 7);
273 t = (x1 ^ (x1 >> 14)) & CC;
274 x1 = x1 ^
t ^ (
t << 14);
277 t = (
y ^ (
y >> 7)) & AA;
278 y =
y ^
t ^ (
t << 7);
279 t = (
y ^ (
y >> 14)) & CC;
280 y =
y ^
t ^ (
t << 14);
282 t = (y1 ^ (y1 >> 7)) & AA;
283 y1 = y1 ^
t ^ (
t << 7);
284 t = (y1 ^ (y1 >> 14)) & CC;
285 y1 = y1 ^
t ^ (
t << 14);
288 t = (
x & FF) | ((
y >> 4) & FF2);
289 y = ((
x << 4) & FF) | (
y & FF2);
292 t = (x1 & FF) | ((y1 >> 4) & FF2);
293 y1 = ((x1 << 4) & FF) | (y1 & FF2);
297 (uint16_t)(((
x & 0xff000000) >> 8 | ((x1 & 0xff000000))) >> 16);
298 *((uint16_t *)(B + 3)) =
299 (uint16_t)(((
x & 0xff0000) >> 16 | ((x1 & 0xff0000) >> 8)));
300 *((uint16_t *)(B + 6)) =
301 (uint16_t)(((
x & 0xff00) | ((x1 & 0xff00) << 8)) >> 8);
302 *((uint16_t *)(B + 9)) = (uint16_t)((
x & 0xff) | ((x1 & 0xff) << 8));
303 *((uint16_t *)(B + 12)) =
304 (uint16_t)(((
y & 0xff000000) >> 8 | ((y1 & 0xff000000))) >> 16);
305 *((uint16_t *)(B + 15)) =
306 (uint16_t)(((
y & 0xff0000) | ((y1 & 0xff0000) << 8)) >> 16);
307 *((uint16_t *)(B + 18)) =
308 (uint16_t)(((
y & 0xff00) | ((y1 & 0xff00) << 8)) >> 8);
309 *((uint16_t *)(B + 21)) = (uint16_t)((
y & 0xff) | ((y1 & 0xff) << 8));
312esp_lcd_panel_io_handle_t led_io_handle = NULL;
314class I2SClocklessLedDriveresp32S3 {
318 uint16_t *buffers[2];
319 uint16_t *led_output = NULL;
320 uint16_t *led_output2 = NULL;
321 uint8_t *ledsbuff = NULL;
322 int num_leds_per_strip;
326 uint8_t __green_map[256];
327 uint8_t __blue_map[256];
328 uint8_t __red_map[256];
329 uint8_t __white_map[256];
331 float _gammar, _gammab, _gammag, _gammaw;
336 for (
int i = 0; i < 256; i++) {
337 tmp = powf((
float)i / 255, 1 / _gammag);
339 tmp = powf((
float)i / 255, 1 / _gammab);
341 tmp = powf((
float)i / 255, 1 / _gammar);
343 tmp = powf((
float)i / 255, 1 / _gammaw);
348 void setGamma(
float gammar,
float gammab,
float gammag,
float gammaw) {
353 setBrightness(_brightness);
356 void setGamma(
float gammar,
float gammab,
float gammag) {
360 setBrightness(_brightness);
363 void _initled(uint8_t *
leds,
const int *pins,
int numstrip,
364 int NUM_LED_PER_STRIP) {
369 esp_lcd_i80_bus_handle_t i80_bus = NULL;
371 esp_lcd_i80_bus_config_t bus_config;
373 bus_config.clk_src = LCD_CLK_SRC_PLL160M;
374 bus_config.dc_gpio_num = 0;
375 bus_config.wr_gpio_num = 0;
377 for (
int i = 0; i < numstrip; i++) {
378 bus_config.data_gpio_nums[i] = pins[i];
381 for (
int i = numstrip; i < 16; i++) {
382 bus_config.data_gpio_nums[i] = 0;
385 bus_config.bus_width = 16;
386 bus_config.max_transfer_bytes =
387 _nb_components * NUM_LED_PER_STRIP * 8 * 3 * 2 + __OFFSET + __OFFSET_END;
388 #if IDF_5_3_OR_EARLIER
389 #pragma GCC diagnostic push
390 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
394 bus_config.psram_trans_align = LCD_DRIVER_PSRAM_DATA_ALIGNMENT;
395 bus_config.sram_trans_align = 4;
396 #if IDF_5_3_OR_EARLIER
397 #pragma GCC diagnostic pop
400 esp_err_t bus_err = esp_lcd_new_i80_bus(&bus_config, &i80_bus);
401 FASTLED_ASSERT(bus_err == ESP_OK,
"Failed to create ESP32 I2S LCD bus");
403 esp_lcd_panel_io_i80_config_t io_config;
405 io_config.cs_gpio_num = -1;
406 io_config.pclk_hz = FASTLED_ESP32S3_I2S_CLOCK_HZ;
407 io_config.trans_queue_depth = 1;
408 io_config.dc_levels = {
416 io_config.lcd_cmd_bits = 0;
417 io_config.lcd_param_bits = 0;
418 io_config.user_ctx =
this;
420 io_config.on_color_trans_done = flush_ready;
421 esp_err_t panel_err = esp_lcd_new_panel_io_i80(i80_bus, &io_config, &led_io_handle);
422 FASTLED_ASSERT(panel_err == ESP_OK,
"Failed to create ESP32 I2S LCD panel IO");
425 void initled(uint8_t *
leds,
const int *pins,
int numstrip,
426 int NUM_LED_PER_STRIP) {
433 if (I2SClocklessLedDriverS3_sem == NULL) {
434 I2SClocklessLedDriverS3_sem = xSemaphoreCreateBinary();
438 led_output = (uint16_t *)heap_caps_aligned_alloc(
439 LCD_DRIVER_PSRAM_DATA_ALIGNMENT,
440 8 * _nb_components * NUM_LED_PER_STRIP * 3 * 2 + __OFFSET +
442 MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
443 memset(led_output, 0,
444 8 * _nb_components * NUM_LED_PER_STRIP * 3 * 2 + __OFFSET +
447 led_output2 = (uint16_t *)heap_caps_aligned_alloc(
448 LCD_DRIVER_PSRAM_DATA_ALIGNMENT,
449 8 * _nb_components * NUM_LED_PER_STRIP * 3 * 2 + __OFFSET +
451 MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
452 memset(led_output2, 0,
453 8 * _nb_components * NUM_LED_PER_STRIP * 3 * 2 + __OFFSET +
455 buffers[0] = led_output;
456 buffers[1] = led_output2;
460 led_output2 += __OFFSET / 2;
461 led_output += __OFFSET / 2;
463 for (
int i = 0; i < NUM_LED_PER_STRIP * _nb_components * 8; i++) {
464 led_output[3 * i + 1] =
467 led_output2[3 * i + 1] = 0xFFFF;
470 _numstrips = numstrip;
471 num_leds_per_strip = NUM_LED_PER_STRIP;
472 _initled(
leds, pins, numstrip, NUM_LED_PER_STRIP);
475 void transposeAll(uint16_t *ledoutput) {
477 uint16_t ledToDisplay = 0;
478 Lines secondPixel[_nb_components];
481 uint16_t jump = num_leds_per_strip * _nb_components;
482 for (
int j = 0; j < num_leds_per_strip; j++) {
483 uint8_t *poli = ledsbuff + ledToDisplay * _nb_components;
484 for (
int i = 0; i < _numstrips; i++) {
486 secondPixel[_p_g].bytes[i] = __green_map[*(poli + 1)];
487 secondPixel[_p_r].bytes[i] = __red_map[*(poli + 0)];
488 secondPixel[_p_b].bytes[i] = __blue_map[*(poli + 2)];
489 if (_nb_components > 3)
490 secondPixel[3].bytes[i] = __white_map[*(poli + 3)];
495 transpose16x1_noinline2(secondPixel[0].bytes, buff);
497 transpose16x1_noinline2(secondPixel[1].bytes, buff);
499 transpose16x1_noinline2(secondPixel[2].bytes, buff);
501 if (_nb_components > 3) {
502 transpose16x1_noinline2(secondPixel[3].bytes, buff);
509 transposeAll(buffers[currentframe]);
513 if (I2SClocklessLedDriverS3_sem == NULL)
514 I2SClocklessLedDriverS3_sem = xSemaphoreCreateBinary();
515 xSemaphoreTake(I2SClocklessLedDriverS3_sem, portMAX_DELAY);
519 if (FASTLED_EXPERIMENTAL_YVES_EXTRA_WAIT_MICROS) {
520 delayMicroseconds(FASTLED_EXPERIMENTAL_YVES_EXTRA_WAIT_MICROS);
523 led_io_handle->tx_color(led_io_handle, 0x2C, buffers[currentframe],
524 _nb_components * num_leds_per_strip * 8 * 3 *
526 __OFFSET + __OFFSET_END);
528 currentframe = (currentframe + 1) % 2;
532static bool IRAM_ATTR flush_ready(esp_lcd_panel_io_handle_t panel_io,
533 esp_lcd_panel_io_event_data_t *edata,
536 isDisplaying =
false;
537 I2SClocklessLedDriveresp32S3 *cont =
538 (I2SClocklessLedDriveresp32S3 *)user_ctx;
541 portBASE_TYPE HPTaskAwoken = 0;
543 xSemaphoreGiveFromISR(I2SClocklessLedDriverS3_sem, &HPTaskAwoken);
544 if (HPTaskAwoken == pdTRUE)
545 portYIELD_FROM_ISR(HPTaskAwoken);
550#pragma GCC diagnostic pop
554#pragma pop_macro("AA")
555#pragma pop_macro("CC")
556#pragma pop_macro("FF")
557#pragma pop_macro("FF2")
UISlider brightness("Brightness", 128, 0, 255, 1)