9#if FL_PLATFORM_HAS_LARGE_MEMORY
12#define DEBUG_PRINT(x) do {} while(0)
13#define DEBUG_PRINTLN(x) do {} while(0)
47#if defined(FL_IS_ESP32)
48#include "freertos/FreeRTOS.h"
49#include "freertos/semphr.h"
50#include "freertos/task.h"
52#if defined(FL_IS_ESP32)
53#include "platforms/esp/32/drivers/spi/spi_hw_base.h"
59#include "platforms/arm/teensy/teensy4_common/drivers/flexio/flexio_spi_mode.h"
63#include "platforms/arm/teensy/teensy4_common/drivers/objectfled/objectfled_spi_mode.h"
74#if defined(FL_IS_ESP32)
77 ProbeSemaphore() =
default;
78 ~ProbeSemaphore() { reset(
nullptr); }
79 ProbeSemaphore(
const ProbeSemaphore&) =
delete;
80 ProbeSemaphore& operator=(
const ProbeSemaphore&) =
delete;
82 SemaphoreHandle_t
get()
const {
return mHandle; }
83 explicit operator bool()
const {
return mHandle !=
nullptr; }
84 void reset(SemaphoreHandle_t handle) {
86 vSemaphoreDelete(mHandle);
92 SemaphoreHandle_t mHandle =
nullptr;
95struct StaticInitProbe {
100 ProbeSemaphore entered;
101 ProbeSemaphore completed;
106StaticInitProbe staticInitProbe;
110 const bool owner = staticInitProbe.constructions.fetch_add(1) == 0;
111 xSemaphoreGive(staticInitProbe.entered.get());
114 vTaskDelay(pdMS_TO_TICKS(200));
117 const TickType_t wait_start = xTaskGetTickCount();
118 const TickType_t wait_ticks = pdMS_TO_TICKS(2000);
124 const bool blocked = contender && eTaskGetState(contender) == eBlocked;
130 }
while (
static_cast<TickType_t
>(xTaskGetTickCount() - wait_start) < wait_ticks);
134SlowStatic& contendedStatic() {
135 static SlowStatic instance;
139void staticInitWorker(
void*) {
140 staticInitProbe.contenders.fetch_add(1);
141 (void)contendedStatic();
142 xSemaphoreGive(staticInitProbe.completed.get());
145 vTaskSuspend(
nullptr);
151 result.set(
"success",
false);
152 if (staticInitProbe.used) {
153 result.set(
"error",
"ResetBoardBeforeStaticInitTest");
156 staticInitProbe.used =
true;
157 staticInitProbe.entered.reset(xSemaphoreCreateBinary());
158 staticInitProbe.completed.reset(xSemaphoreCreateCounting(2, 0));
159 if (!staticInitProbe.entered || !staticInitProbe.completed) {
160 staticInitProbe.entered.reset(
nullptr);
161 staticInitProbe.completed.reset(
nullptr);
162 result.set(
"error",
"SemaphoreAllocationFailed");
165 TaskHandle_t owner_task =
nullptr;
166 TaskHandle_t contender_task =
nullptr;
167 const bool owner_created = xTaskCreate(staticInitWorker,
"static-owner", 4096,
168 nullptr, 1, &owner_task) == pdPASS;
169 const bool entered = owner_created &&
170 xSemaphoreTake(staticInitProbe.entered.get(), pdMS_TO_TICKS(2000)) == pdTRUE;
171 const bool contender_created = entered &&
172 xTaskCreate(staticInitWorker,
"static-contender", 4096,
173 nullptr, 1, &contender_task) == pdPASS;
176 const int tasks = (owner_created ? 1 : 0) + (contender_created ? 1 : 0);
177 for (
int i = 0; i < tasks; ++i) {
178 if (xSemaphoreTake(staticInitProbe.completed.get(), pdMS_TO_TICKS(3000)) == pdTRUE) {
182 bool workers_suspended =
false;
183 if (completed == tasks) {
186 for (
int attempt = 0; attempt < 100; ++attempt) {
187 const bool owner_suspended = !owner_task || eTaskGetState(owner_task) == eSuspended;
188 const bool contender_suspended = !contender_task || eTaskGetState(contender_task) == eSuspended;
189 if (owner_suspended && contender_suspended) {
190 workers_suspended =
true;
199 result.set(
"constructorEntered", entered);
200 result.set(
"constructions", constructions);
201 result.set(
"contenders", contenders);
202 result.set(
"completed", completed);
203 result.set(
"overlapped", overlapped);
204 result.set(
"workersSuspended", workers_suspended);
205 result.set(
"success", entered && completed == 2 && contenders == 2 &&
206 overlapped && constructions == 1 && workers_suspended);
209 if (workers_suspended) {
212 vTaskDelete(owner_task);
214 if (contender_task) {
215 vTaskDelete(contender_task);
217 staticInitProbe.entered.reset(
nullptr);
218 staticInitProbe.completed.reset(
nullptr);
224#if defined(FL_IS_TEENSY_4X)
225struct FlexIoObjectFledState {
229FlexIoObjectFledState& flexIoObjectFledState() {
236#if defined(FL_AUTORESEARCH_MEMORY_BENCH)
241 remote.bind(
"staticInitTest", [](
const fl::json&) -> fl::json {
242#if defined(FL_IS_ESP32)
243 return staticInitTest();
246 result.set(
"success",
false);
247 result.set(
"error",
"Esp32Required");
251#if defined(FL_AUTORESEARCH_MEMORY_BENCH)
256 remote.bind(
"dualSpiHostLoopback", [
this](
const fl::json&
args) -> fl::json {
258#if !defined(FL_IS_ESP32) || !FASTLED_ESP32_HAS_SPI3
262 response.set(
"error",
"TwoSpiHostsUnavailable");
265 if (!
args.is_object() || !
args.contains(
"activeHost") ||
266 !
args[
"activeHost"].is_int()) {
268 response.set(
"error",
"ExpectedActiveHost");
271 const int active_host =
static_cast<int>(
args[
"activeHost"].as_int().value());
272 if (active_host != 2 && active_host != 3) {
274 response.set(
"error",
"InvalidActiveHost");
277 const bool dummy_first =
args.contains(
"dummyFirst") &&
278 args[
"dummyFirst"].is_bool() &&
args[
"dummyFirst"].as_bool().value();
279 constexpr int kLedCount = 100;
280 static bool already_run =
false;
283 response.set(
"error",
"ResetBoardBeforeNextOrder");
287 static CRGB active_leds[kLedCount];
288 static CRGB dummy_leds[kLedCount];
289 for (
int i = 0; i < kLedCount; ++i) {
290 active_leds[i] =
CRGB(
static_cast<uint8_t>(i * 19 + 1),
291 static_cast<uint8_t>(i * 13 + 2),
292 static_cast<uint8_t>(i * 7 + 3));
295 const auto register_active = [&]() {
297 active_leds, kLedCount, active_host == 2
300 const auto register_dummy = [&]() {
302 dummy_leds, kLedCount, active_host == 2
316 fl::RunResult diagnostics;
321 "SPI_UNIFIED", &diagnostics);
323 response.set(
"activeHost", active_host);
324 response.set(
"dummyFirst", dummy_first);
325 response.set(
"genericDecodedBytes",
static_cast<int>(captured));
330 fl::EdgeTime edge_samples[256];
331 const size_t edge_count =
mState->rx_channel->getRawEdgeTimes(
332 fl::span<fl::EdgeTime>(edge_samples, 256), 0);
333 response.set(
"rawEdges",
static_cast<int>(edge_count));
334 constexpr size_t kVerifiedLeds = 8;
335 uint8_t first_bytes[kVerifiedLeds * 4] = {};
336 size_t bit_count = 0;
337 for (
size_t i = 0; i < edge_count && bit_count <
sizeof(first_bytes) * 8; ++i) {
338 const size_t bits = (edge_samples[i].
ns + 208) / 416;
339 for (
size_t j = 0; j < bits && bit_count <
sizeof(first_bytes) * 8; ++j, ++bit_count) {
340 if (edge_samples[i].high) {
341 first_bytes[bit_count / 8] |=
342 static_cast<uint8_t>(1u << (7 - bit_count % 8));
346 int wire_mismatches = 0;
347 if (bit_count <
sizeof(first_bytes) * 8) {
348 wire_mismatches =
static_cast<int>(
sizeof(first_bytes) * 8 - bit_count);
350 for (
size_t i = 0; i < kVerifiedLeds; ++i) {
351 const size_t offset = i * 4;
352 wire_mismatches += first_bytes[
offset] != 0xFF;
353 wire_mismatches += first_bytes[
offset + 1] != active_leds[i].r;
354 wire_mismatches += first_bytes[
offset + 2] != active_leds[i].g;
355 wire_mismatches += first_bytes[
offset + 3] != active_leds[i].b;
358 response.set(
"verifiedLeds",
static_cast<int>(kVerifiedLeds));
359 response.set(
"wireBits",
static_cast<int>(bit_count));
360 response.set(
"wireMismatches", wire_mismatches);
361 response.set(
"first0", first_bytes[0]);
362 response.set(
"first1", first_bytes[1]);
363 response.set(
"first2", first_bytes[2]);
364 response.set(
"first3", first_bytes[3]);
365 response.set(
"first4", first_bytes[4]);
366 response.set(
"first5", first_bytes[5]);
367 response.set(
"first6", first_bytes[6]);
368 response.set(
"first7", first_bytes[7]);
369 if (edge_count >= 4) {
370 response.set(
"edge0Ns",
static_cast<int>(edge_samples[0].ns));
371 response.set(
"edge1Ns",
static_cast<int>(edge_samples[1].ns));
372 response.set(
"edge2Ns",
static_cast<int>(edge_samples[2].ns));
373 response.set(
"edge3Ns",
static_cast<int>(edge_samples[3].ns));
374 response.set(
"edge0High", edge_samples[0].high);
375 response.set(
"edge1High", edge_samples[1].high);
379 const bool native_driver_found =
static_cast<bool>(driver);
380 const bool native_driver_enabled = driver &&
382 response.set(
"nativeDriverFound", native_driver_found);
383 response.set(
"nativeDriverEnabled", native_driver_enabled);
384 response.set(
"success", primed && native_driver_enabled && wire_mismatches == 0);
392 remote.bind(
"softSpiClockLoopback", [
this](
const fl::json&
args) -> fl::json {
395 constexpr int kLedCount = 16;
396 static bool already_run =
false;
399 response.set(
"error",
"ResetBoardBeforeNextRun");
403#if !defined(FL_IS_ESP32)
413 struct SoftSpiProbe :
public CPixelLEDController<RGB> {
414 fl::SPIOutput<2, 0, 250000> spi;
416 void showPixels(PixelController<RGB>& pixels)
FL_NO_EXCEPT override {
418 for (
int i = 0; i < pixels.size() * 4; ++i) {
421 spi.finalizeTransmission();
425 static SoftSpiProbe probe;
428 fl::RunResult diagnostics;
431 "SOFT_SPI_CLOCK", &diagnostics);
432 fl::SPIBusManager& manager = fl::getSPIBusManager();
433 response.set(
"buses",
static_cast<int>(manager.getNumBuses()));
434 if (
const fl::SPIBusInfo* bus = manager.getBusInfo(0)) {
435 response.set(
"busType",
static_cast<int>(bus->bus_type));
436 response.set(
"busDevices",
static_cast<int>(bus->num_devices));
437 response.set(
"busInitialized", bus->is_initialized);
441 fl::EdgeTime edges[256];
442 const size_t edge_count =
mState->rx_channel->getRawEdgeTimes(
443 fl::span<fl::EdgeTime>(edges, 256), 0);
444 u32 high_sum = 0, low_sum = 0, high_n = 0, low_n = 0;
445 u32 high_min = 0xFFFFFFFFu, low_min = 0xFFFFFFFFu;
446 for (
size_t i = 1; i < edge_count; ++i) {
447 const u32 ns = edges[i].
ns;
452 high_sum += ns; ++high_n; high_min =
fl::min(high_min, ns);
454 low_sum += ns; ++low_n; low_min =
fl::min(low_min, ns);
457 const u32 high_avg = high_n ? high_sum / high_n : 0;
458 const u32 low_avg = low_n ? low_sum / low_n : 0;
459 const u32 period = high_avg + low_avg;
464 response.set(
"rawEdges",
static_cast<int>(edge_count));
465 response.set(
"highAvgNs",
static_cast<int>(high_avg));
466 response.set(
"lowAvgNs",
static_cast<int>(low_avg));
467 response.set(
"highMinNs",
static_cast<int>(high_n ? high_min : 0));
468 response.set(
"lowMinNs",
static_cast<int>(low_n ? low_min : 0));
469 response.set(
"periodNs",
static_cast<int>(period));
470 response.set(
"measuredKhz",
static_cast<int>(period ? 1000000u / period : 0));
473 response.set(
"success", primed && period >= 3600u);
496 remote.bind(
"flexioRxBenchmark", [](
const fl::json&
args) -> fl::json {
498#if !defined(FL_IS_TEENSY_4X)
501 response.set(
"error",
"PlatformNotSupported");
503 "flexioRxBenchmark is Teensy 4.x-only (FLEXIO1 capture).");
507 int frequency_hz = 1000;
508 int duration_ms = 100;
511 if (
args.is_array() &&
args.size() >= 1 &&
args[0].is_object()) {
512 fl::json cfg =
args[0];
513 if (cfg.
contains(
"frequency_hz") && cfg[
"frequency_hz"].is_int()) {
514 frequency_hz =
static_cast<int>(cfg[
"frequency_hz"].
as_int().value());
516 if (cfg.
contains(
"duration_ms") && cfg[
"duration_ms"].is_int()) {
517 duration_ms =
static_cast<int>(cfg[
"duration_ms"].
as_int().value());
519 if (cfg.
contains(
"tx_pin") && cfg[
"tx_pin"].is_int()) {
520 tx_pin =
static_cast<int>(cfg[
"tx_pin"].
as_int().value());
522 if (cfg.
contains(
"rx_pin") && cfg[
"rx_pin"].is_int()) {
523 rx_pin =
static_cast<int>(cfg[
"rx_pin"].
as_int().value());
526 if (frequency_hz < 1 || frequency_hz > 5000000 ||
527 duration_ms < 1 || duration_ms > 5000) {
529 response.set(
"error",
"InvalidArgs");
531 "frequency_hz in [1, 5_000_000]; duration_ms in [1, 5000].");
536 analogWriteFrequency(tx_pin, (
float)frequency_hz);
548 fl::u64 target = expected_edges + expected_edges / 2;
549 if (target < 1024ULL) target = 1024ULL;
550 if (target > 16384ULL) target = 16384ULL;
551 rx_cfg.edge_capacity = (
size_t)target;
552 rx_cfg.start_low =
false;
557 response.set(
"error",
"RxCreateFailed");
559 "Failed to create FlexIO RX channel on pin (no FLEXIO1 mapping).");
562 if (!rx_channel->begin(rx_cfg)) {
565 response.set(
"error",
"RxBeginFailed");
567 "RxChannel::begin() returned false (see device WARN log).");
572 rx_channel->wait((u32)duration_ms);
578 fl::vector<fl::EdgeTime> edges;
579 edges.
assign(rx_cfg.edge_capacity, fl::EdgeTime());
580 size_t edges_captured =
581 rx_channel->getRawEdgeTimes(fl::span<fl::EdgeTime>(edges), 0);
582 edges.
resize(edges_captured);
588 fl::u32 min_ns = 0xFFFFFFFFu;
591 for (
size_t i = 0; i + 1 < edges_captured; i += 2) {
592 const fl::u32 period_ns =
593 (fl::u32)edges[i].ns + (fl::u32)edges[i + 1].ns;
594 if (period_ns == 0)
continue;
597 if (period_ns < min_ns) min_ns = period_ns;
598 if (period_ns > max_ns) max_ns = period_ns;
602 fl::u32 sigma_ns = 0;
604 mean_ns = (fl::u32)(sum_ns / (
fl::u64)periods);
607 periods > 0 ? (sum_sq_ns / (
fl::u64)periods) - (mean64 * mean64)
615 if (min_ns == 0xFFFFFFFFu) min_ns = 0;
618 response.set(
"frequency_hz",
static_cast<int64_t
>(frequency_hz));
619 response.set(
"duration_ms",
static_cast<int64_t
>(duration_ms));
620 response.set(
"tx_pin",
static_cast<int64_t
>(tx_pin));
621 response.set(
"rx_pin",
static_cast<int64_t
>(rx_pin));
622 response.set(
"edges_captured",
static_cast<int64_t
>(edges_captured));
623 response.set(
"periods",
static_cast<int64_t
>(periods));
624 response.set(
"period_mean_ns",
static_cast<int64_t
>(mean_ns));
625 response.set(
"period_sigma_ns",
static_cast<int64_t
>(sigma_ns));
626 response.set(
"period_min_ns",
static_cast<int64_t
>(min_ns));
627 response.set(
"period_max_ns",
static_cast<int64_t
>(max_ns));
659 remote.bind(
"flexioObjectFledTest", [](
const fl::json&
args) -> fl::json {
661#if !defined(FL_IS_TEENSY_4X)
664 response.set(
"error",
"PlatformNotSupported");
666 "flexioObjectFledTest is Teensy 4.x-only (FLEXIO1 RX + "
667 "Teensy-core ObjectFLED driver).");
675 if (
args.is_array() &&
args.size() >= 1 &&
args[0].is_object()) {
676 fl::json cfg =
args[0];
677 if (cfg.
contains(
"test_case") && cfg[
"test_case"].is_int()) {
678 test_case =
static_cast<int>(cfg[
"test_case"].
as_int().value());
680 if (cfg.
contains(
"tx_pin") && cfg[
"tx_pin"].is_int()) {
681 tx_pin =
static_cast<int>(cfg[
"tx_pin"].
as_int().value());
683 if (cfg.
contains(
"rx_pin") && cfg[
"rx_pin"].is_int()) {
684 rx_pin =
static_cast<int>(cfg[
"rx_pin"].
as_int().value());
686 if (cfg.
contains(
"capture_ms") && cfg[
"capture_ms"].is_int()) {
687 capture_ms =
static_cast<int>(cfg[
"capture_ms"].
as_int().value());
690 if (test_case < 0 || test_case > 4 ||
691 capture_ms < 1 || capture_ms > 5000) {
693 response.set(
"error",
"InvalidArgs");
695 "test_case in [0,4]; capture_ms in [1, 5000].");
701 CRGB* leds_buf = flexIoObjectFledState().leds;
720 leds_buf[0] =
CRGB(0xFF, 0xFF, 0xFF);
724 for (
int i = 0; i < 100; ++i) {
726 : (i % 3 == 1) ?
CRGB::Green
733 fl::vector<u8> expected;
734 expected.
reserve((
size_t)num_leds * 3);
735 for (
int i = 0; i < num_leds; ++i) {
743 const size_t expected_edges = (
size_t)num_leds * 24u * 2u;
744 size_t edge_capacity = expected_edges + expected_edges / 4u + 64u;
745 if (edge_capacity > 16384u) edge_capacity = 16384u;
748 rx_cfg.edge_capacity = edge_capacity;
749 rx_cfg.start_low =
true;
751 if (!rx_channel || !rx_channel->begin(rx_cfg)) {
753 response.set(
"error",
"RxBeginFailed");
755 "Failed to bring up FlexIO RX on the requested pin "
756 "(check kFlexIo1Pins[] mapping).");
761 fl::ChannelOptions opts;
766 fl::NamedTimingConfig timing_cfg(resolved_timing,
"WS2812B-V5",
768 fl::ChannelConfig tx_cfg(
771 fl::span<CRGB>(leds_buf, (
size_t)num_leds),
777 response.set(
"error",
"TxAddFailed");
779 "FastLED.add() rejected the ObjectFLED ChannelConfig.");
792 const u32 ws2812_tx_us =
793 (u32)num_leds * 24u * 13u / 10u + 100u;
794 const u32 min_capture_ms = (ws2812_tx_us + 999u) / 1000u + 10u;
795 const u32 effective_capture_ms = ((u32)capture_ms > min_capture_ms)
798 rx_channel->wait(effective_capture_ms);
801 const fl::ChipsetTiming ws2812_timing =
803 fl::ChipsetTiming4Phase rx_timing =
805 fl::vector<u8> decoded;
808 rx_channel->decode(rx_timing, fl::span<u8>(decoded));
811 u32 decoded_bytes = 0u;
813 decoded_bytes = decode_result.value();
815 const size_t cmp_n = (decoded_bytes < expected.
size())
819 size_t mismatched = 0;
820 for (
size_t i = 0; i < cmp_n; ++i) {
821 if (decoded[i] == expected[i]) ++matched;
else ++mismatched;
823 if (decoded_bytes < expected.
size()) {
824 mismatched += expected.size() - decoded_bytes;
828 fl::vector<fl::EdgeTime> edges;
829 edges.
assign(edge_capacity, fl::EdgeTime());
830 const size_t edges_captured =
831 rx_channel->getRawEdgeTimes(fl::span<fl::EdgeTime>(edges), 0);
838 response.set(
"success", mismatched == 0 && decoded_bytes == expected.
size());
839 response.set(
"test_case",
static_cast<int64_t
>(test_case));
840 response.set(
"tx_pin",
static_cast<int64_t
>(tx_pin));
841 response.set(
"rx_pin",
static_cast<int64_t
>(rx_pin));
842 response.set(
"num_leds",
static_cast<int64_t
>(num_leds));
843 response.set(
"expected_bytes",
static_cast<int64_t
>(expected.
size()));
844 response.set(
"decoded_bytes",
static_cast<int64_t
>(decoded_bytes));
845 response.set(
"matched",
static_cast<int64_t
>(matched));
846 response.set(
"mismatched",
static_cast<int64_t
>(mismatched));
847 response.set(
"edges_captured",
static_cast<int64_t
>(edges_captured));
876 remote.bind(
"flexioRxLoopbackPing", [](
const fl::json&
args) -> fl::json {
878#if !defined(FL_IS_TEENSY_4X)
881 response.set(
"error",
"PlatformNotSupported");
883 "flexioRxLoopbackPing is Teensy 4.x-only (FLEXIO1 RX).");
890 if (
args.is_array() &&
args.size() >= 1 &&
args[0].is_object()) {
891 const fl::json &cfg =
args[0];
892 if (cfg.
contains(
"tx_pin") && cfg[
"tx_pin"].is_int()) {
893 tx_pin =
static_cast<int>(cfg[
"tx_pin"].
as_int().value());
895 if (cfg.
contains(
"rx_pin") && cfg[
"rx_pin"].is_int()) {
896 rx_pin =
static_cast<int>(cfg[
"rx_pin"].
as_int().value());
898 if (cfg.
contains(
"toggle_us") && cfg[
"toggle_us"].is_int()) {
899 toggle_us =
static_cast<int>(cfg[
"toggle_us"].
as_int().value());
901 if (cfg.
contains(
"edges") && cfg[
"edges"].is_int()) {
902 edges =
static_cast<int>(cfg[
"edges"].
as_int().value());
905 if (edges < 2 || edges > 64) {
907 response.set(
"error",
"InvalidEdges");
908 response.set(
"message",
"edges must be in [2, 64]");
914 rx_cfg.edge_capacity = 1024;
915 rx_cfg.start_low =
true;
917 if (!rx_channel || !rx_channel->begin(rx_cfg)) {
919 response.set(
"error",
"RxBeginFailed");
921 "FlexIO RX begin() failed (kFlexIo1Pins[] mapping?).");
933 for (
int i = 0; i < edges; ++i) {
944 constexpr uintptr_t kFLEXIO1_BASE_DIAG = 0x401AC000u;
946 volatile uint32_t *shftstat = (
volatile uint32_t *)(kFLEXIO1_BASE_DIAG + 0x010);
947 volatile uint32_t *timstat = (
volatile uint32_t *)(kFLEXIO1_BASE_DIAG + 0x018);
948 volatile uint32_t *shftbuf = (
volatile uint32_t *)(kFLEXIO1_BASE_DIAG + 0x200);
949 volatile uint32_t *shftbufbis = (
volatile uint32_t *)(kFLEXIO1_BASE_DIAG + 0x280);
950 volatile uint32_t *shftbufbys = (
volatile uint32_t *)(kFLEXIO1_BASE_DIAG + 0x300);
952 flexio_diag.
set(
"pin",
static_cast<int64_t
>(*pin));
953 flexio_diag.
set(
"shiftstat",
static_cast<int64_t
>(*shftstat));
954 flexio_diag.
set(
"timstat",
static_cast<int64_t
>(*timstat));
955 flexio_diag.
set(
"shiftbuf0",
static_cast<int64_t
>(*shftbuf));
956 flexio_diag.
set(
"shiftbufbis0",
static_cast<int64_t
>(*shftbufbis));
957 flexio_diag.
set(
"shiftbufbys0",
static_cast<int64_t
>(*shftbufbys));
958 response.set(
"flexio1", flexio_diag);
965 rx_channel->wait(50);
968 fl::vector<fl::EdgeTime> captured_edges;
969 captured_edges.
assign(64, fl::EdgeTime());
971 rx_channel->getRawEdgeTimes(fl::span<fl::EdgeTime>(captured_edges), 0);
977 response.set(
"tx_pin",
static_cast<int64_t
>(tx_pin));
978 response.set(
"rx_pin",
static_cast<int64_t
>(rx_pin));
979 response.set(
"toggle_us",
static_cast<int64_t
>(toggle_us));
980 response.set(
"edges",
static_cast<int64_t
>(edges));
981 response.set(
"edges_captured",
static_cast<int64_t
>(edge_count));
987 const size_t to_dump = (edge_count < 8u) ? edge_count : 8u;
988 for (
size_t i = 0; i < to_dump; ++i) {
990 e.
set(
"high", captured_edges[i].high);
991 e.
set(
"ns",
static_cast<int64_t
>(captured_edges[i].ns));
994 response.set(
"first_edges", edges_arr);
999 remote.bind(
"legacyAsyncProbe", [](
const fl::json&
args) -> fl::json {
1002 remote.bind(
"legacyAsyncErrorProbe", [](
const fl::json&
args) -> fl::json {
1014 remote.bind(
"timingDriftTest", [](
const fl::json&
args) -> fl::json {
1019 int iterations = 10;
1022 if (
args.is_object()) {
1024 }
else if (
args.is_array() &&
args.size() >= 1 &&
args[0].is_object()) {
1028 if (config.
contains(
"pin") && config[
"pin"].is_int()) {
1029 pin =
static_cast<int>(config[
"pin"].
as_int().value());
1031 if (config.
contains(
"numLeds") && config[
"numLeds"].is_int()) {
1032 num_leds =
static_cast<int>(config[
"numLeds"].
as_int().value());
1034 if (config.
contains(
"iterations") && config[
"iterations"].is_int()) {
1035 iterations =
static_cast<int>(config[
"iterations"].
as_int().value());
1041 response.set(
"success", r.valid_pin);
1043 response.set(
"error",
"InvalidPin");
1045 "pin is outside LegacyClocklessProxy range");
1050 response.set(
"num_leds", r.num_leds);
1051 response.set(
"iterations", r.iterations);
1052 response.set(
"cpu_mhz",
static_cast<int64_t
>(r.cpu_mhz));
1053 response.set(
"show_count",
static_cast<int64_t
>(r.show_count));
1054 response.set(
"show_min_us",
static_cast<int64_t
>(r.show_min_us));
1055 response.set(
"show_max_us",
static_cast<int64_t
>(r.show_max_us));
1056 response.set(
"show_total_us",
static_cast<int64_t
>(r.show_total_us));
1059 for (
int i = 0; i < r.iterations; ++i) {
1060 iter_ms_arr.
push_back(
static_cast<int64_t
>(r.iter_ms[i]));
1062 response.set(
"iter_ms", iter_ms_arr);
1091 remote.bind(
"flexioSpiSelfTest", [](
const fl::json&
args) -> fl::json {
1093#if !defined(FL_IS_TEENSY_4X)
1096 response.set(
"error",
"PlatformNotSupported");
1098 "flexioSpiSelfTest is Teensy 4.x-only (FlexIO2 SPI master mode).");
1110 int clock_hz = 1000000;
1112 if (
args.is_object()) {
1113 const fl::json& cfg =
args;
1114 if (cfg.
contains(
"mosi_pin") && cfg[
"mosi_pin"].is_int()) {
1115 mosi_pin =
static_cast<int>(cfg[
"mosi_pin"].
as_int().value());
1117 if (cfg.
contains(
"sclk_pin") && cfg[
"sclk_pin"].is_int()) {
1118 sclk_pin =
static_cast<int>(cfg[
"sclk_pin"].
as_int().value());
1120 if (cfg.
contains(
"clock_hz") && cfg[
"clock_hz"].is_int()) {
1121 clock_hz =
static_cast<int>(cfg[
"clock_hz"].
as_int().value());
1123 if (cfg.
contains(
"num_bytes") && cfg[
"num_bytes"].is_int()) {
1124 num_bytes =
static_cast<int>(cfg[
"num_bytes"].
as_int().value());
1128 response.set(
"mosi_pin", mosi_pin);
1129 response.set(
"sclk_pin", sclk_pin);
1130 response.set(
"clock_hz",
static_cast<int64_t
>(clock_hz));
1131 response.set(
"num_bytes", num_bytes);
1138 if (num_bytes <= 0 || num_bytes > 64) {
1140 response.set(
"error",
"InvalidArgs");
1141 response.set(
"message",
"num_bytes must be in [1, 64].");
1144 if (mosi_pin < 0 || mosi_pin > 255 ||
1145 sclk_pin < 0 || sclk_pin > 255) {
1147 response.set(
"error",
"InvalidArgs");
1148 response.set(
"message",
"mosi_pin / sclk_pin must be in [0, 255].");
1151 if (clock_hz <= 0) {
1153 response.set(
"error",
"InvalidArgs");
1154 response.set(
"message",
"clock_hz must be > 0.");
1159 fl::FlexIOSPIPinInfo pin_info{};
1160 bool lookup_ok = fl::flexio_spi_lookup_pins(
1161 static_cast<fl::u8>(mosi_pin),
1162 static_cast<fl::u8>(sclk_pin),
1164 response.set(
"lookup_ok", lookup_ok);
1167 response.set(
"error",
"PinNotRoutable");
1169 "(MOSI, SCLK) pair is not FlexIO2-routable; both pins "
1170 "must be in {6, 7, 8, 9, 10, 11, 12, 13, 32}.");
1174 static_cast<int64_t
>(pin_info.mosi_flexio_pin));
1176 static_cast<int64_t
>(pin_info.sclk_flexio_pin));
1179 bool init_ok = fl::flexio_spi_init(pin_info,
1180 static_cast<fl::u32
>(clock_hz));
1184 response.set(
"error",
"InitFailed");
1185 response.set(
"message",
"flexio_spi_init returned false.");
1193 for (
int i = 0; i < num_bytes; ++i) {
1195 case 0: buffer[i] = 0xA5;
break;
1196 case 1: buffer[i] = 0x5A;
break;
1197 case 2: buffer[i] = 0xFF;
break;
1198 case 3: buffer[i] = 0x00;
break;
1202 const fl::u32 show_start_ms =
millis();
1203 bool show_ok = fl::flexio_spi_show(buffer,
1204 static_cast<fl::u32
>(num_bytes));
1207 fl::flexio_spi_deinit();
1209 response.set(
"error",
"ShowFailed");
1210 response.set(
"message",
"flexio_spi_show returned false.");
1215 fl::flexio_spi_wait();
1216 const fl::u32 wait_ms =
millis() - show_start_ms;
1217 response.set(
"wait_ms",
static_cast<int64_t
>(wait_ms));
1220 fl::flexio_spi_deinit();
1245 remote.bind(
"objectfledSpiSelfTest", [](
const fl::json&
args) -> fl::json {
1247#if !defined(FL_IS_TEENSY_4X)
1250 response.set(
"error",
"PlatformNotSupported");
1252 "objectfledSpiSelfTest is Teensy 4.x-only (FlexPWM2 + eDMA + GPIO6).");
1260 int clock_hz = 1000000;
1262 if (
args.is_object()) {
1263 const fl::json& cfg =
args;
1264 if (cfg.
contains(
"mosi_pin") && cfg[
"mosi_pin"].is_int()) {
1265 mosi_pin =
static_cast<int>(cfg[
"mosi_pin"].
as_int().value());
1267 if (cfg.
contains(
"sclk_pin") && cfg[
"sclk_pin"].is_int()) {
1268 sclk_pin =
static_cast<int>(cfg[
"sclk_pin"].
as_int().value());
1270 if (cfg.
contains(
"clock_hz") && cfg[
"clock_hz"].is_int()) {
1271 clock_hz =
static_cast<int>(cfg[
"clock_hz"].
as_int().value());
1273 if (cfg.
contains(
"num_bytes") && cfg[
"num_bytes"].is_int()) {
1274 num_bytes =
static_cast<int>(cfg[
"num_bytes"].
as_int().value());
1278 response.set(
"mosi_pin", mosi_pin);
1279 response.set(
"sclk_pin", sclk_pin);
1280 response.set(
"clock_hz",
static_cast<int64_t
>(clock_hz));
1281 response.set(
"num_bytes", num_bytes);
1284 if (num_bytes <= 0 || num_bytes > 64) {
1286 response.set(
"error",
"InvalidArgs");
1287 response.set(
"message",
"num_bytes must be in [1, 64].");
1290 if (mosi_pin < 0 || mosi_pin > 255 ||
1291 sclk_pin < 0 || sclk_pin > 255) {
1293 response.set(
"error",
"InvalidArgs");
1294 response.set(
"message",
"mosi_pin / sclk_pin must be in [0, 255].");
1297 if (clock_hz <= 0) {
1299 response.set(
"error",
"InvalidArgs");
1300 response.set(
"message",
"clock_hz must be > 0.");
1305 fl::ObjectFLEDSPIPinInfo pin_info{};
1306 bool lookup_ok = fl::objectfled_spi_lookup_pins(
1307 static_cast<fl::u8>(mosi_pin),
1308 static_cast<fl::u8>(sclk_pin),
1310 response.set(
"lookup_ok", lookup_ok);
1313 response.set(
"error",
"PinNotRoutable");
1315 "(MOSI, SCLK) pair is not on GPIO6 or otherwise invalid; "
1316 "both pins must be GPIO6-resident (T4.x: 0, 1, 14-27, "
1317 "T4.1 adds 38-41).");
1320 response.set(
"mosi_bit",
static_cast<int64_t
>(pin_info.mosi_bit));
1321 response.set(
"sclk_bit",
static_cast<int64_t
>(pin_info.sclk_bit));
1324 bool init_ok = fl::objectfled_spi_init(pin_info,
1325 static_cast<fl::u32
>(clock_hz));
1329 response.set(
"error",
"InitFailed");
1330 response.set(
"message",
"objectfled_spi_init returned false.");
1336 for (
int i = 0; i < num_bytes; ++i) {
1338 case 0: buffer[i] = 0xA5;
break;
1339 case 1: buffer[i] = 0x5A;
break;
1340 case 2: buffer[i] = 0xFF;
break;
1341 case 3: buffer[i] = 0x00;
break;
1345 const fl::u32 show_start_ms =
millis();
1346 bool show_ok = fl::objectfled_spi_show(
1347 fl::span<const fl::u8>(buffer,
1348 static_cast<fl::size
>(num_bytes)));
1351 fl::objectfled_spi_deinit();
1353 response.set(
"error",
"ShowFailed");
1354 response.set(
"message",
"objectfled_spi_show returned false.");
1359 fl::objectfled_spi_wait();
1360 const fl::u32 wait_ms =
millis() - show_start_ms;
1361 response.set(
"wait_ms",
static_cast<int64_t
>(wait_ms));
1364 fl::ObjectFLEDSPIDiagnostics diag{};
1365 fl::objectfled_spi_read_diagnostics(&diag);
1366 response.set(
"dma_channel",
static_cast<int64_t
>(diag.dma_channel));
1367 response.set(
"dma_erq",
static_cast<int64_t
>(diag.dma_erq));
1368 response.set(
"dma_int",
static_cast<int64_t
>(diag.dma_int));
1369 response.set(
"dma_err",
static_cast<int64_t
>(diag.dma_err));
1370 response.set(
"dma_es",
static_cast<int64_t
>(diag.dma_es));
1371 response.set(
"dma_citer",
static_cast<int64_t
>(diag.dma_citer));
1372 response.set(
"dma_biter",
static_cast<int64_t
>(diag.dma_biter));
1373 response.set(
"tmr3_cntr0",
static_cast<int64_t
>(diag.tmr3_cntr0));
1374 response.set(
"tmr3_csctrl0",
static_cast<int64_t
>(diag.tmr3_csctrl0));
1375 response.set(
"tmr3_sctrl0",
static_cast<int64_t
>(diag.tmr3_sctrl0));
1376 response.set(
"tmr3_ctrl0",
static_cast<int64_t
>(diag.tmr3_ctrl0));
1377 response.set(
"tmr3_enbl",
static_cast<int64_t
>(diag.tmr3_enbl));
1378 response.set(
"xbar1_ctrl1",
static_cast<int64_t
>(diag.xbar1_ctrl1));
1381 fl::objectfled_spi_deinit();
Full PARLIO encode bench for ESP32-P4 (post-byte-LUT, #2526 landed).
PARLIO ISR-streaming functional validation (#2548 deep-dive follow-up).
size_t capture(fl::shared_ptr< fl::RxChannel > rx_channel, fl::span< uint8_t > rx_buffer, size_t expected_data_bytes, int tx_pin, const fl::ChipsetTimingConfig &timing, const char *driver_name, fl::RunResult *diagnostics=nullptr)
Issue #2994 repro: compounded per-sequence timing drift vs 3.10.3.
Wave8 expansion micro-benchmark for #2526.
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
@ CHANNELS
Remove all channels from controller list.
@ APA102
APA102 LED chipset.
fl::unique_ptr< fl::Remote > remote
fl::net::ble — BLE GATT transport layer for JSON-RPC
SPI encoder configuration for clocked LED chipsets.
fl::shared_ptr< AutoResearchState > mState
void bindDriverMethods(fl::Remote &remote)
void clear(bool writeData=false) FL_NO_EXCEPT
Clear the leds, wiping the local array of data.
static void add(fl::ChannelPtr channel) FL_NO_EXCEPT
Add a Channel-based LED controller (from ChannelPtr)
void show(fl::u8 scale) FL_NO_EXCEPT
Update all our controllers with the current led colors, using the passed in brightness.
void setBrightness(fl::u8 scale) FL_NO_EXCEPT
Set the global brightness scaling.
void wait() FL_NO_EXCEPT
Wait for all channel bus transmissions to complete.
::CLEDController & addLeds(::CLEDController *pLed, CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0) FL_NO_EXCEPT
Add a CLEDController instance to the world.
static ChannelManager & registry() FL_NO_EXCEPT
Get the singleton WITHOUT registering platform default drivers.
fl::shared_ptr< IChannelDriver > findDriverByName(const fl::string &name) const FL_NO_EXCEPT
Silent counterpart to getDriverByName().
bool isDriverEnabled(const char *name) const FL_NO_EXCEPT
Check if a driver is enabled by name.
JSON-RPC server with scheduling support.
static RxChannelPtr create(const RxChannelConfig &config) FL_NO_EXCEPT
static T & instance() FL_NO_EXCEPT
bool empty() const FL_NO_EXCEPT
const char * c_str() const FL_NO_EXCEPT
fl::optional< i64 > as_int() const FL_NO_EXCEPT
void set(const fl::string &key, const json &value) FL_NO_EXCEPT
bool is_null() const FL_NO_EXCEPT
void push_back(const json &value) FL_NO_EXCEPT
bool contains(size_t idx) const FL_NO_EXCEPT
static json object() FL_NO_EXCEPT
static json array() FL_NO_EXCEPT
static string from_literal(const char *literal) FL_NO_EXCEPT
fl::size size() const FL_NO_EXCEPT
void reserve(fl::size n) FL_NO_EXCEPT
void resize(fl::size n) FL_NO_EXCEPT
void push_back(const T &value) FL_NO_EXCEPT
void assign(InputIt first, InputIt last) FL_NO_EXCEPT
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
Task executor — runs registered task runners and manages the run loop.
Platform-abstracted heap memory query functions.
FastLED's Elegant JSON Library: fl::json
DriftResult run(int pin, int num_leds, int iterations)
fl::json legacyAsyncProbe(const fl::json &args)
Call after the regular driver test has registered the selected backend.
fl::json legacyAsyncErrorProbe(const fl::json &args)
constexpr ChipsetTiming to_runtime_timing() FL_NO_EXCEPT
Convert enum-based timing type to runtime ChipsetTiming struct.
constexpr ChipsetTimingConfig makeTimingConfig() FL_NO_EXCEPT
Convert compile-time CHIPSET type to runtime timing config.
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.
ChipsetTiming4Phase make4PhaseTiming(const ChipsetTiming &timing_3phase, u32 tolerance_ns) FL_NO_EXCEPT
Create 4-phase RX timing from 3-phase chipset timing with tolerance.
void init(Context &ctx, int w, int h) FL_NO_EXCEPT
@ FLEXIO
Teensy 4.x-only FlexIO capture backend (FLEXIO1 — FLEXIO2 is owned by the WS2812 TX driver)....
expected< T, E > result
Alias for expected (Rust-style naming)
void analogWrite(int pin, u16 val) FL_NO_EXCEPT
Write analog value to pin (PWM)
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) FL_NO_EXCEPT
constexpr ClocklessEncoder encoder_for() FL_NO_EXCEPT
Extract the encoder selector from a compile-time TIMING type.
void digitalWrite(int pin, PinValue val) FL_NO_EXCEPT
Write digital value to pin.
void pinMode(int pin, PinMode mode) FL_NO_EXCEPT
Set pin mode (input, output, pull-up, pull-down)
void delayMicroseconds(u32 us) FL_NO_EXCEPT
Delay for a given number of microseconds.
Promise-based fluent API for FastLED - standalone async primitives.
Umbrella header for SIMD subsystem.
@ Green
<div style='background:#008000;width:4em;height:4em;'></div>
@ Red
<div style='background:#FF0000;width:4em;height:4em;'></div>
@ Blue
<div style='background:#0000FF;width:4em;height:4em;'></div>
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
u32 ns
Duration in nanoseconds (31 bits, max ~2.1s)
int captureWaitResult
RxWaitResult value, or -1 before wait.
int rawEdgesAfterWait
Raw edges visible after RX wait/decode.
fl::string rxBeginError
Machine-readable reason when RX begin() fails.