4#if FL_PLATFORM_HAS_LARGE_MEMORY && defined(FL_AUTORESEARCH_MEMORY_BENCH)
16#if FL_AUTORESEARCH_MEMORY_BENCH == 3
17#include "driver/spi_slave.h"
18#include "driver/gpio.h"
19#include "esp_rom_gpio.h"
20#include "soc/gpio_sig_map.h"
21#include "esp_heap_caps.h"
22#include "esp_idf_version.h"
23#include "soc/soc_caps.h"
28int integerArg(
const fl::json&
args,
const char* name,
int fallback) {
29 return static_cast<int>(
args[name].as_int().value_or(fallback));
32#if FL_AUTORESEARCH_MEMORY_BENCH != 3
35 const int pin = integerArg(
args,
"pin", 4);
42 const fl::size drivers_before =
FastLED.getDriverCount();
43#if FL_AUTORESEARCH_MEMORY_BENCH == 1
46 auto channel =
FastLED.add(cfg);
49 const fl::size drivers_registered =
FastLED.getDriverCount();
50 bool completed = channel !=
nullptr;
51 fl::size heap_first = heap_registered;
52 fl::size heap_steady = heap_registered;
53 for (
int frame = 0; completed && frame < 20; ++frame) {
64 const bool selected_drivers = FL_AUTORESEARCH_MEMORY_BENCH == 1
65 ? drivers_registered == 1 : drivers_registered > 0;
66 result.set(
"success", completed && drivers_before == 0 &&
67 selected_drivers && heap_steady == heap_end);
68 result.set(
"heapBefore",
static_cast<int64_t
>(heap_before));
69 result.set(
"heapAfterRegister",
static_cast<int64_t
>(heap_registered));
70 result.set(
"registrationBytes",
static_cast<int64_t
>(heap_before) -
static_cast<int64_t
>(heap_registered));
71 result.set(
"heapAfterFirst",
static_cast<int64_t
>(heap_first));
72 result.set(
"heapSteady",
static_cast<int64_t
>(heap_steady));
73 result.set(
"heapEnd",
static_cast<int64_t
>(heap_end));
74 result.set(
"driversBefore",
static_cast<int64_t
>(drivers_before));
75 result.set(
"driverCount",
static_cast<int64_t
>(drivers_registered));
76 result.set(
"selectedBus",
static_cast<int>(cfg.options.mBus));
79 result.set(
"actualDriverName", actual_driver);
84#if FL_AUTORESEARCH_MEMORY_BENCH == 3
86FL_STATIC_ASSERT(SOC_SPI_PERIPH_NUM > 2,
"SPI memory bench requires independent SPI2 and SPI3 peripherals");
94struct SpiWireReceiver {
96 spi_slave_transaction_t transaction = {};
97 bool initialized =
false;
99 volatile bool armed =
false;
100 const char* stage =
"idle";
101 esp_err_t error = ESP_OK;
103 static void ready(spi_slave_transaction_t* transaction) {
104 static_cast<SpiWireReceiver*
>(transaction->user)->armed =
true;
107 bool begin(fl::size byte_count) {
108 stage =
"allocateReceiver";
109 bytes =
static_cast<fl::u8*
>(heap_caps_malloc(byte_count + 4, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL));
110 if (!bytes)
return false;
111 spi_bus_config_t bus = {};
113 bus.miso_io_num = -1;
115 bus.quadwp_io_num = -1;
116 bus.quadhd_io_num = -1;
117 bus.max_transfer_sz =
static_cast<int>(byte_count + 4);
118 spi_slave_interface_config_t slave = {};
119 slave.spics_io_num = 4;
120 slave.queue_size = 1;
122 slave.post_setup_cb = &ready;
123 stage =
"initializeReceiver";
124 error = spi_slave_initialize(SPI3_HOST, &bus, &slave, SPI_DMA_CH_AUTO);
125 if (error != ESP_OK)
return false;
130 gpio_set_direction(GPIO_NUM_1, GPIO_MODE_INPUT_OUTPUT);
131 gpio_set_direction(GPIO_NUM_3, GPIO_MODE_INPUT_OUTPUT);
132 gpio_set_direction(GPIO_NUM_4, GPIO_MODE_INPUT_OUTPUT);
138 esp_rom_gpio_connect_out_signal(1, FSPID_OUT_IDX,
false,
false);
139 esp_rom_gpio_connect_out_signal(3, FSPICLK_OUT_IDX,
false,
false);
140 esp_rom_gpio_connect_out_signal(4, SIG_GPIO_OUT_IDX,
false,
false);
141 esp_rom_gpio_connect_in_signal(1, SPI3_D_IN_IDX,
false);
142 esp_rom_gpio_connect_in_signal(3, SPI3_CLK_IN_IDX,
false);
143 esp_rom_gpio_connect_in_signal(4, SPI3_CS0_IN_IDX,
false);
144 gpio_set_level(GPIO_NUM_4, 1);
145 transaction.length = (byte_count + 4) * 8;
146 transaction.rx_buffer = bytes;
147 transaction.user =
this;
152 stage =
"queueReceiver";
154 transaction.trans_len = 0;
155 error = spi_slave_queue_trans(SPI3_HOST, &transaction, 0);
156 if (error != ESP_OK)
return false;
159 stage =
"waitReceiverArmed";
160 while (!armed &&
fl::micros() - started < 1000000u) {}
161 if (!armed) { error = ESP_ERR_TIMEOUT;
return false; }
163 gpio_set_level(GPIO_NUM_4, 0);
168 stage =
"drainReceiver";
169 gpio_set_level(GPIO_NUM_4, 1);
170 spi_slave_transaction_t* completed =
nullptr;
171 error = spi_slave_get_trans_result(SPI3_HOST, &completed, pdMS_TO_TICKS(1000));
172 if (error != ESP_OK)
return false;
175 return completed == &transaction;
182 if (initialized) spi_slave_free(SPI3_HOST);
183 heap_caps_free(bytes);
191 static bool already_run =
false;
193 result.set(
"success",
false);
194 result.set(
"error",
"ResetBoardBeforeNextBench");
197 const int pixels = integerArg(
args,
"pixels", 1000);
198 const int frames = integerArg(
args,
"frames", 20);
199 const fl::string chipset =
args[
"chipset"].as_string().value_or(
"apa102");
200 if (pixels < 1 || pixels > 2048 || frames < 3 || frames > 100 ||
201 (chipset !=
"apa102" && chipset !=
"sk9822")) {
202 result.set(
"success",
false);
203 result.set(
"error",
"InvalidBenchArguments");
211 for (
int i = 0; i < pixels; ++i) leds[i] = CRGB(255, static_cast<fl::u8>(i * 13 + 2),
static_cast<fl::u8>(i * 7 + 3));
213 if (chipset ==
"apa102") {
223#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
224 const fl::size prime_before = heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
225 heap_caps_monitor_local_minimum_free_size_start();
228 const bool prime_completed =
FastLED.wait(1000);
229#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
230 const fl::size prime_low = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
231 heap_caps_monitor_local_minimum_free_size_stop();
234 if (!prime_completed) {
235 result.set(
"success",
false);
236 result.set(
"error",
"PrimeFailed");
239 const fl::size frame_bytes = 4 + 4 * pixels + 4 * (pixels / 32 + 1);
241 if (!receiver.begin(frame_bytes)) {
242 result.set(
"success",
false);
243 result.set(
"error",
"Spi3ReceiverInitFailed");
244 result.set(
"stage", receiver.stage);
245 result.set(
"sdkError",
static_cast<int>(receiver.error));
253 bool completed =
true;
254 fl::size mismatches = 0;
255 fl::size heap_steady = 0;
256 fl::size heap_end = 0;
257 fl::size peak_internal_bytes = 0;
258 fl::size peak_dma_bytes = 0;
259 fl::size first_bad_offset = frame_bytes;
260 int first_expected = -1;
261 int first_actual = -1;
262 fl::size min_captured_bits = frame_bytes * 8;
263 fl::size max_captured_bits = 0;
264 for (
int frame = 0; frame < frames && completed; ++frame) {
266 for (
int i = 0; i < pixels; ++i) leds[i] = CRGB(255, static_cast<fl::u8>(i * 13 + frame),
static_cast<fl::u8>(i * 7 + frame * 3));
267 if (!receiver.arm()) { completed =
false;
break; }
268#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
269 const fl::size before_internal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
270 const fl::size before_dma = heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
271 if (heap_caps_monitor_local_minimum_free_size_start() != ESP_OK) { completed =
false;
break; }
275 completed =
FastLED.wait(1000);
276 const fl::u32 elapsed =
fl::micros() - started;
277#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
278 const fl::size low_internal = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
279 const fl::size low_dma = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
280 heap_caps_monitor_local_minimum_free_size_stop();
281 if (before_internal >= low_internal && before_internal - low_internal > peak_internal_bytes) peak_internal_bytes = before_internal - low_internal;
282 if (before_dma >= low_dma && before_dma - low_dma > peak_dma_bytes) peak_dma_bytes = before_dma - low_dma;
284 completed = receiver.finish() && completed;
285 if (!completed)
break;
286 const fl::size received_bits = receiver.transaction.trans_len;
287 captured_bits.
push_back(
static_cast<fl::u32
>(received_bits));
288 if (received_bits < min_captured_bits) min_captured_bits = received_bits;
289 if (received_bits > max_captured_bits) max_captured_bits = received_bits;
290 const fl::size received_bytes = received_bits / 8 < frame_bytes
291 ? received_bits / 8 : frame_bytes;
292 mismatches += received_bits != frame_bytes * 8;
295 if (received_bytes < frame_bytes && first_bad_offset == frame_bytes) {
296 first_bad_offset = received_bytes;
300 if (
offset < 4) expected = 0;
301 else if (
offset < 4 + 4 * pixels) {
302 const fl::size pixel = (
offset - 4) / 4;
303 const fl::size component = (
offset - 4) % 4;
304 if (component != 0) expected =
leds[pixel].raw[component - 1];
306 if (receiver.bytes[
offset] != expected) {
308 if (first_bad_offset == frame_bytes) {
309 first_bad_offset =
offset;
310 first_expected = expected;
311 first_actual = receiver.bytes[
offset];
317 if (frame == 2) heap_steady = heap_end;
320 result.set(
"success", completed && mismatches == 0 && heap_steady == heap_end);
321 result.set(
"chipset", chipset);
322 result.set(
"pixels", pixels);
323 result.set(
"frames",
static_cast<int64_t
>(timings.
size()));
324 result.set(
"frameBytes",
static_cast<int64_t
>(frame_bytes));
325 result.set(
"wireMismatches",
static_cast<int64_t
>(mismatches));
326 result.set(
"stage", receiver.stage);
327 result.set(
"sdkError",
static_cast<int>(receiver.error));
328 result.set(
"capturedBits",
static_cast<int64_t
>(receiver.transaction.trans_len));
329 result.set(
"minCapturedBits",
static_cast<int64_t
>(min_captured_bits));
330 result.set(
"maxCapturedBits",
static_cast<int64_t
>(max_captured_bits));
331 result.set(
"firstBadOffset", first_bad_offset == frame_bytes ? -1 :
static_cast<int64_t
>(first_bad_offset));
332 result.set(
"firstExpected", first_expected);
333 result.set(
"firstActual", first_actual);
334 result.set(
"heapBefore",
static_cast<int64_t
>(heap_before));
335 result.set(
"heapAfterRegister",
static_cast<int64_t
>(heap_registered));
336 result.set(
"heapAfterPrime",
static_cast<int64_t
>(heap_prime));
337#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
338 result.set(
"primeBeforeInternal",
static_cast<int64_t
>(prime_before));
339 result.set(
"primeLowInternal",
static_cast<int64_t
>(prime_low));
340 result.set(
"primePeakInternalBytes", prime_before >= prime_low ?
static_cast<int64_t
>(prime_before - prime_low) : 0);
342 result.set(
"heapSteady",
static_cast<int64_t
>(heap_steady));
343 result.set(
"heapEnd",
static_cast<int64_t
>(heap_end));
344 result.set(
"peakInternalBytes",
static_cast<int64_t
>(peak_internal_bytes));
345 result.set(
"peakDmaBytes",
static_cast<int64_t
>(peak_dma_bytes));
346 result.set(
"heapWatermarkMeasured", ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0));
348 for (
const fl::u32 us : timings) times.
push_back(
static_cast<int64_t
>(us));
349 result.set(
"frameUs", times);
351 for (
const fl::u32 bits : captured_bits) bit_counts.
push_back(
static_cast<int64_t
>(bits));
352 result.set(
"frameBits", bit_counts);
362#if FL_AUTORESEARCH_MEMORY_BENCH == 3
367 result.set(
"mode", FL_AUTORESEARCH_MEMORY_BENCH);
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
@ SK9822
SK9822 LED chipset.
@ APA102
APA102 LED chipset.
fl::unique_ptr< fl::Remote > remote
FastLED chrono implementation - duration types for time measurements.
JSON-RPC server with scheduling support.
static T & instance() FL_NO_EXCEPT
void push_back(const json &value) FL_NO_EXCEPT
static json object() FL_NO_EXCEPT
static json array() FL_NO_EXCEPT
fl::size size() const FL_NO_EXCEPT
void reserve(fl::size n) FL_NO_EXCEPT
void push_back(const T &value) FL_NO_EXCEPT
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
Platform-abstracted heap memory query functions.
FastLED's Elegant JSON Library: fl::json
constexpr T * begin(T(&array)[N]) FL_NO_EXCEPT
const char * busName(Bus b, fl::u8 which=0) FL_NO_EXCEPT
fl::u32 micros() FL_NO_EXCEPT
Universal microsecond timer - returns microseconds since system startup.
const char * busDriverName(Bus b, fl::u8 which=0, bool spi=false) FL_NO_EXCEPT
Concrete ChannelManager driver name for a portable bus.
expected< T, E > result
Alias for expected (Rust-style naming)
HeapInfo getFreeHeap() FL_NO_EXCEPT
Query available heap memory.
constexpr ClocklessChipset makeClockless(int pin) FL_NO_EXCEPT
Build a ClocklessChipset from a compile-time TIMING trait.
#define FL_STATIC_ASSERT(...)
Portable compile-time assertion wrapper.
@ Red
<div style='background:#FF0000;width:4em;height:4em;'></div>
Configuration for a single LED channel.
fl::size free_sram
Free SRAM in bytes (internal fast memory)