FastLED 3.10.10
Loading...
Searching...
No Matches

◆ autoResearchLowMemorySetup()

void autoResearchLowMemorySetup ( )
inline

Definition at line 474 of file AutoResearchLowMemory.h.

474 {
475 fl::serial_begin(115200);
476
477 // Arm the watchdog so a hung sketch unbricks itself on crash.
479
480 // Emit a literal-only warning so the autoresearch harness can verify
481 // the FL_WARN log pipeline reaches the host.
482 FL_WARN_LIT("FL_WARN: low-memory bring-up OK");
483
484 // The C++ inline-ODR rule guarantees a single shared instance across
485 // every translation unit that calls this inline function, which is
486 // exactly what we want (one Remote bound to the singleton Serial
487 // transport). The included-once-per-sketch model of `.ino` builds
488 // also reduces to a single TU in practice.
490 remote.bind("echo", [](int v) -> int {
491 FL_WARN_LIT("FL_WARN: echo invoked");
492 return v;
493 });
494
495#if defined(FL_IS_ARM_LPC_845) && defined(FASTLED_AUTORESEARCH_FAULT_TEST)
496 remote.bind("trigger_oom", []() -> bool {
497 // Intentional #3302 fault input: ask the 16 KB SRAM target for
498 // far more RAM than exists. If the platform allocator returns null
499 // instead of trapping, dereference null to exercise HardFault emit.
500 volatile char* const p = new char[16UL * 1024UL * 1024UL]; // ok bare allocation
501 if (p == nullptr) {
502 triggerLowMemoryNullFault();
503 }
504 p[0] = 0x33;
505 triggerLowMemoryNullFault();
506 return false;
507 });
508 remote.bind("trigger_stackblow", []() -> bool {
509 (void)recurseLowMemoryStackBlow(0);
510 triggerLowMemoryNullFault();
511 return false;
512 });
513#endif // FL_IS_ARM_LPC_845 && FASTLED_AUTORESEARCH_FAULT_TEST
514
515#if defined(FL_IS_ESP8266) // ok platform specific - ESP8266 bring-up benches
516 autoresearch::esp8266::bindEsp8266Benches(remote);
517#endif
518
519#if defined(FASTLED_AUTORESEARCH_IEEE754_MODE)
520 remote.bind("ieee754CodecTest", [](const fl::json& args) -> fl::json {
521 (void)args;
522 const auto r = autoresearch::ieee754_check::run();
524 response.set("success", r.success);
525 response.set("tests_run", static_cast<int64_t>(r.tests_run));
526 response.set("tests_failed", static_cast<int64_t>(r.tests_failed));
527 response.set("first_failure", r.first_failure ? r.first_failure : "");
528 response.set("expected_bits", static_cast<int64_t>(r.expected_bits));
529 response.set("actual_bits", static_cast<int64_t>(r.actual_bits));
530 return response;
531 });
532#endif
533
534#if defined(FL_IS_ARM_LPC) && !defined(FASTLED_AUTORESEARCH_IEEE754_MODE) && \
535 !defined(FASTLED_AUTORESEARCH_FAULT_TEST)
536#if defined(FL_IS_ARM_LPC_845) // ok platform specific - only the LPC845 has a pin-probe skip table
537 // LPC845 only: it is the LPC with a pin-probe skip table (SWD, RESET,
538 // ISP, console USART). Elsewhere the scan would drive those pads.
539 remote.bind("findConnectedPins",
540 []() -> fl::string { return lowMemFindConnectedPins(); });
541#endif
542
543 // pinToggleRx (FastLED #3021 Phase 1) — bit-bang square wave on tx_pin
544 // and capture SCT edges on rx_pin. CSV stats out.
545#if !defined(FASTLED_AUTORESEARCH_LPC_DMA_LOOPBACK)
546 // Left out of the RX-DMA loopback builds (--ws2812-loopback/--pwm-dma-cl):
547 // with the DMA capture linked, the 64 KB LPC845 has no room for this
548 // unrelated ~1 KB diagnostic. --pin-toggle-rx builds keep it.
549 remote.bind("pinToggleRx",
550 [](int tx_pin, int rx_pin, int freq_hz, int duration_ms) -> fl::string {
551 if (tx_pin < 0 || rx_pin < 0 || freq_hz <= 0 || duration_ms <= 0) {
552 return fl::string("0,0,0,0,0,0,0");
553 }
554 const fl::u32 expected_edges =
555 2u * static_cast<fl::u32>(freq_hz) *
556 static_cast<fl::u32>(duration_ms) / 1000u;
557 // Keep both the RX vector and diagnostic copy buffer small on
558 // 16 KB SRAM LPC8xx parts. Larger static buffers regressed early
559 // init by leaving too little room for stack/heap (FastLED #3200),
560 // while stack buffers previously overflowed during this handler
561 // (FastLED #3125).
562 constexpr fl::u32 kLowMemEdgeBufSize = 64u;
563 fl::u32 cap = expected_edges + (expected_edges / 2u);
564 if (cap < 2u) cap = 2u;
565 if (cap > kLowMemEdgeBufSize) cap = kLowMemEdgeBufSize;
566
567 // FastLED's own pin backend (#5260), not the Arduino core's:
568 // this bench is the on-silicon check of fl::pinMode/digitalWrite.
569 // TX settles LOW before capture starts, so the first captured
570 // edge is the first square-wave edge, not a pull-up -> LOW step.
573
574 auto rx = fl::LpcSctRxChannel::create(rx_pin);
575 if (!rx) return fl::string("0,0,0,0,0,0,0");
576
577 fl::RxConfig cfg;
578 cfg.buffer_size = cap;
579 cfg.start_low = true;
580 if (!rx->begin(cfg)) return fl::string("0,0,0,0,0,0,0");
581
582 const fl::u32 half_us = 500000u / static_cast<fl::u32>(freq_hz);
583 const fl::u32 cycles = static_cast<fl::u32>(freq_hz) *
584 static_cast<fl::u32>(duration_ms) / 1000u;
585
586 // Edges are scheduled on absolute SysTick deadlines. Measured on
587 // LPC845-BRK: delayMicroseconds(half_us) after each write and poll
588 // added that overhead to every half period (+20% at 1 kHz).
589 //
590 // The capture is polled once right after every write: the SCT
591 // holds one capture per direction, so a half period without a
592 // poll loses edges. Near the deadline only the clock is read, so
593 // a poll (~3 us) does not delay the next edge.
594 const fl::u32 ticks_per_us = F_CPU / 1000000u;
595 const fl::u32 half_ticks = half_us * ticks_per_us;
596 const fl::i32 poll_guard = static_cast<fl::i32>(6u * ticks_per_us);
597 LowMemTickClock clock;
598 fl::u32 deadline = clock.now();
599 for (fl::u32 i = 0; i < 2u * cycles; ++i) {
600 fl::digitalWrite(tx_pin, (i & 1u) ? fl::PinValue::Low
602 rx->pollOnce();
603 deadline += half_ticks;
604 for (;;) {
605 const fl::i32 left = static_cast<fl::i32>(deadline - clock.now());
606 if (left <= 0) break;
607 if (left > poll_guard) rx->pollOnce();
608 }
609 if ((i & 0xFFu) == 0u) {
611 }
612 }
613 rx->wait(1);
614
615 // Static storage keeps this off the stack; the small fixed size
616 // keeps .bss below the LPC845-BRK early-init failure threshold.
617 // Size from the array, not a pointer to it: sizeof(pointer) /
618 // sizeof(EdgeTime) is 1, which capped every capture at one edge.
619 auto& edges_buf = autoresearch::low_memory::pinToggleState().edges;
620 const fl::size n_read = rx->getRawEdgeTimes(
621 fl::span<fl::EdgeTime>(edges_buf, sizeof(edges_buf) / sizeof(edges_buf[0])));
622
623 LowMemPinTogglePeriodStats stats = computeLowMemPeriodStats(edges_buf, n_read);
624
625 fl::sstream s;
626 const bool success = (stats.periods > 0);
627 s << (success ? 1 : 0) << ',' << static_cast<fl::u32>(n_read) << ','
628 << stats.periods << ',' << stats.mean_ns << ',' << stats.sigma_ns << ','
629 << stats.min_ns << ',' << stats.max_ns;
630 return s.str();
631 });
632#if defined(FL_IS_ARM_LPC_845) && defined(FASTLED_AUTORESEARCH_LPC_CORE_PULL_CHECK) // ok platform specific - LPC8xx core pull check, --pin-toggle-rx build only
633 // corePullCheck: [pin_a, pin_b, pin_c, pin_d] -> "<pin>:up=..,down=.. ..."
634 // (pin < 0 = unused slot); `bash autoresearch lpc845 --pin-toggle-rx`
635 // defines FASTLED_AUTORESEARCH_LPC_CORE_PULL_CHECK and runs it on
636 // scan-idle pins. Other builds leave it unbound; only --pin-toggle-rx
637 // calls it, and there a missing binding fails the run.
638 remote.bind("corePullCheck",
639 [](int pin_a, int pin_b, int pin_c, int pin_d) -> fl::string {
640 fl::sstream out;
641 const int pins[] = {pin_a, pin_b, pin_c, pin_d};
642 for (int pin : pins) {
643 if (pin >= 0) {
644 lowMemCorePullCheck(pin, out);
645 }
646 }
647 return out.str();
648 });
649#endif
650#endif // !FASTLED_AUTORESEARCH_LPC_DMA_LOOPBACK
651
652#if defined(FASTLED_AUTORESEARCH_LPC_WS2812)
653 // ws2812SctTest (FastLED #3021 Phase 2) -- WS2812 byte-match loopback.
654 remote.bind("ws2812SctTest",
655 [](int test_case, int tx_pin, int rx_pin, int capture_ms) -> fl::string {
656 // TX/RX come from the host's findConnectedPins scan. An
657 // open-drain pad cannot drive TX: it only sinks, and its rising
658 // edge is the ~1.7 us pull-up, longer than any WS2812 HIGH.
659 // Reserved pads (SWD, ISP, console) are refused: routing USART
660 // TXD or the SCT input onto them cuts the link to the host.
661 // A rejected request replies with num_leds=0.
662 if (test_case < 0 || test_case > 4 || tx_pin < 0 || tx_pin > 31 ||
663 rx_pin < 0 || rx_pin > 31 || tx_pin == rx_pin ||
664 lowMemOpenDrainPin(tx_pin) || fl::pinProbeSkipReason(tx_pin) ||
665 fl::pinProbeSkipReason(rx_pin) || capture_ms <= 0) {
666 return fl::string("0,0,0,0,0,0,0,0,0,0");
667 }
668 int num_leds = 1;
669 switch (test_case) {
670 case 1: num_leds = 3; break;
671 case 4: num_leds = 100; break;
672 default: num_leds = 1; break;
673 }
674 const int expected_bytes = num_leds * 3;
675 // Setup failures keep the case fields, with nothing decoded and
676 // tx_done=2 (TX never started; 0 means it started but timed out).
677 auto setupFailed = [&]() -> fl::string {
678 fl::sstream f;
679 f << "0," << test_case << ',' << num_leds << ',' << expected_bytes
680 << ",0,0,0,0,0,2";
681 return f.str();
682 };
683
684 // Wire bytes in WS2812 GRB order: what addLeds<WS2812, PIN, GRB>
685 // would hand the driver for these colors at full brightness.
686 fl::vector_psram<u8> grb(static_cast<fl::size>(expected_bytes));
687 for (int i = 0; i < num_leds; ++i) {
688 u8 r = 0, g = 0, b = 0;
689 switch (test_case) {
690 case 0: r = 0xFF; break;
691 case 1: r = (i == 0) ? 0xFF : 0; g = (i == 1) ? 0xFF : 0;
692 b = (i == 2) ? 0xFF : 0; break;
693 case 3: r = g = b = 0xFF; break;
694 case 4: r = (i % 3 == 0) ? 0xFF : 0; g = (i % 3 == 1) ? 0xFF : 0;
695 b = (i % 3 == 2) ? 0xFF : 0; break;
696 default: break; // case 2: all zeros
697 }
698 grb[i * 3 + 0] = g;
699 grb[i * 3 + 1] = r;
700 grb[i * 3 + 2] = b;
701 }
702 autoresearch::low_memory::Ws2812State& ws_state =
703 autoresearch::low_memory::ws2812State();
704
705 // The TX pin is a run-time value (from the jumper scan), which
706 // addLeds<WS2812, PIN>() cannot take, and the 64 KB part has no
707 // room for the runtime Channel API (+21 KB). So drive the frame
708 // the way addLeds<> does on a low-memory part (SlimBridgeController
709 // direct show): GRB bytes in a ChannelData for the pin, handed to
710 // the platform default clockless driver.
712 fl::IChannelDriver& tx = TxBus::instance();
713 auto frame = fl::ChannelData::create(
715 fl::move(grb));
716 if (!frame || !tx.canHandle(frame)) return setupFailed();
717 // The engine has no abort, so a frame left over from a timed-out
718 // case must drain first, or show() would skip this case's frame.
719 constexpr fl::u32 kTxDeadlineMs = 100u;
720 if (!tx.waitForReady(kTxDeadlineMs)) return setupFailed();
721 const u8* expected_buf = frame->getData().data();
722
723 auto rx = fl::LpcSctRxChannel::create(rx_pin);
724 if (!rx) return setupFailed();
725 fl::RxConfig cfg;
726 const fl::u32 expected_edges = (fl::u32)num_leds * 24u * 2u;
727 fl::u32 cap = expected_edges + (expected_edges / 2u);
728 // The low-memory sketch cannot afford the old 2048-edge reserve
729 // on a 16 KB SRAM LPC845. 192 edges covers the 1- and 3-LED
730 // cases; the 100-LED case (4800 edges) is checked on the prefix
731 // that fits, reported in the `cap` field.
732 constexpr fl::u32 kLowMemWs2812EdgeCapacity = 192u;
733 if (cap < 48u) cap = 48u;
734 if (cap > kLowMemWs2812EdgeCapacity) cap = kLowMemWs2812EdgeCapacity;
735 cfg.buffer_size = cap;
736 cfg.start_low = true;
737 if (!rx->begin(cfg)) return setupFailed();
738
739 // TX gets its own deadline: a 100-LED frame is ~3 ms on the
740 // wire, whatever the RX window. A frame that does not finish
741 // fails the case.
742 tx.enqueue(frame);
743 tx.show();
744 const bool tx_done = tx.waitForReady(kTxDeadlineMs);
745 rx->wait(capture_ms);
746 // Give the TX pad back to GPIO once the frame is out (re-checked
747 // after the RX wait), so a later findConnectedPins in this boot
748 // sees the jumper, not a USART-held pin.
750 fl::lpc::LpcUartDmaRuntime::releaseTxPin(); // ok platform specific - only the LPC845 UART-DMA TX holds the pad on a movable SWM function between frames
751 }
752
753 uint8_t* decoded_buf = ws_state.decoded;
754 for (int i = 0; i < expected_bytes; ++i) decoded_buf[i] = 0;
755
756 // The second argument is the window half-width, not a reset
757 // time (passing 0 demanded exact widths). The UART encoder sends
758 // whole 250 ns slots -- a 1 is H750/L500 against the nominal
759 // T1H 875/T1L 375 -- so the window must exceed that 125 ns.
760 constexpr fl::u32 kWs2812DecodeToleranceNs = 200u;
764 kWs2812DecodeToleranceNs);
765
766 auto dec = rx->decode(timing, fl::span<u8>(decoded_buf, expected_bytes));
767 const fl::u32 decoded_bytes = dec.ok() ? dec.value() : 0u;
768
769 // mismatched counts decoded bytes that differ; bytes that were
770 // never decoded are exp_bytes - dec_bytes.
771 fl::u32 matched = 0;
772 fl::u32 mismatched = 0;
773 for (fl::u32 i = 0; i < decoded_bytes && i < (fl::u32)expected_bytes; ++i) {
774 if (decoded_buf[i] == expected_buf[i]) ++matched;
775 else ++mismatched;
776 }
777
778 // Count every captured edge, reading through a small .bss window
779 // (one read would stop at the window size).
780 constexpr fl::size kLowMemWs2812ProbeSize = 64u;
781 fl::span<fl::EdgeTime> probe(ws_state.probe, kLowMemWs2812ProbeSize);
782 fl::size edges_captured = 0;
783 for (fl::size n; (n = rx->getRawEdgeTimes(probe, edges_captured)) > 0;) {
784 edges_captured += n;
785 }
786
787 // A capture of `cap` edges holds cap / 16 bytes (16 edges per
788 // byte), so the 100-LED frame is checked on that prefix; the
789 // smaller frames fit whole. Same rule as the host bench.
790 fl::u32 need = cap / 16u;
791 if (need > (fl::u32)expected_bytes) need = (fl::u32)expected_bytes;
792 const bool success =
793 tx_done && mismatched == 0 && need > 0 && matched >= need;
794
795 fl::sstream s;
796 s << (success ? 1 : 0) << ',' << test_case << ',' << num_leds << ','
797 << expected_bytes << ',' << decoded_bytes << ','
798 << matched << ',' << mismatched << ','
799 << static_cast<fl::u32>(edges_captured) << ',' << cap << ','
800 << (tx_done ? 1 : 0);
801 return s.str();
802 });
803#endif // FASTLED_AUTORESEARCH_LPC_WS2812
804
805 // FastLED #3468: channels-API SCT+DMA clockless AutoResearch
806 // harness. Binds pwmDmaClFrameOnce / pwmDmaClFrameBurst /
807 // pwmDmaClCaptureSelf / pwmDmaClDmaError / pwmDmaClCaptureStress (the
808 // last two from #5278). Header self-gates on
809 // FL_IS_ARM_LPC_845 && FASTLED_LPC_PWM_DMA.
810#if defined(FL_IS_ARM_LPC_845) && defined(FASTLED_LPC_PWM_DMA)
811 autoresearch::pwm_dma_cl::bind(remote);
812#endif
813
814 // FastLED #3456: SPI+DMA async AutoResearch harness. Binds
815 // dmaSpiTransferOnce / dmaSpiTransferOverlap / dmaSpiMeasureSck.
816 // Header self-gates on FL_IS_ARM_LPC_845 && FASTLED_LPC_SPI_DMA.
817#if defined(FL_IS_ARM_LPC_845) && defined(FASTLED_LPC_SPI_DMA)
818 autoresearch::dma_spi::bind(remote);
819#endif
820
821 // #3453/#3611: UART DMA AutoResearch harness. Binds either the raw
822 // UART DMA stream bench or the clockless loopback verifier, depending
823 // on the build flags injected by `bash autoresearch`.
824#if defined(FL_IS_ARM_LPC_845) && FASTLED_AUTORESEARCH_LPC_UART_DMA
825 autoresearch::uart_dma::bind(remote);
826#endif
827
828#endif // FL_IS_ARM_LPC && !FASTLED_AUTORESEARCH_IEEE754_MODE
829}
fl::unique_ptr< fl::Remote > remote
Definition RpcClient.ino:43
int pins[]
Definition Spi.ino:11
static ChannelDataPtr create(const ChipsetVariant &chipset, fl::vector_psram< u8 > &&encodedData=fl::vector_psram< u8 >(), ChannelPixelFormat pixelFormat=ChannelPixelFormat::RGB) FL_NO_EXCEPT
Create channel transmission data (modern variant-based API)
Definition data.cpp.hpp:12
virtual bool canHandle(const ChannelDataPtr &data) const FL_NO_EXCEPT=0
Check if this driver can handle the given channel data.
virtual void enqueue(ChannelDataPtr channelData) FL_NO_EXCEPT=0
Enqueue channel data for transmission.
virtual void show() FL_NO_EXCEPT=0
Trigger transmission of enqueued data.
virtual DriverState poll() FL_NO_EXCEPT=0
Query driver state and perform maintenance.
bool waitForReady(u32 timeoutMs=1000) FL_NO_EXCEPT
Wait for driver to become READY.
Minimal interface for LED channel transmission drivers.
Definition driver.h:53
JSON-RPC server with scheduling support.
Definition remote.h:42
static Watchdog & instance() FL_NO_EXCEPT
void begin(fl::u32 timeout_ms) FL_NO_EXCEPT
void feed() FL_NO_EXCEPT
static json object() FL_NO_EXCEPT
Definition json.h:864
string str() const FL_NO_EXCEPT
Definition strstream.h:43
unsigned char u8
Definition stdint.h:132
#define FL_WARN_LIT(LITERAL)
Definition log.h:455
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:30
constexpr ChipsetTiming to_runtime_timing() FL_NO_EXCEPT
Convert enum-based timing type to runtime ChipsetTiming struct.
Definition led_timing.h:616
const dec_t dec
Definition ios.cpp.hpp:13
ChipsetTiming4Phase make4PhaseTiming(const ChipsetTiming &timing_3phase, u32 tolerance_ns) FL_NO_EXCEPT
Create 4-phase RX timing from 3-phase chipset timing with tolerance.
Definition rx.cpp.hpp:78
void serial_begin(u32 baudRate) FL_NO_EXCEPT
@ Low
Logic low (0V / GND)
Definition pin.h:51
@ High
Logic high (3.3V / 5V, platform-dependent)
Definition pin.h:52
fl::variant< ClocklessChipset, SpiChipsetConfig > ChipsetVariant
Definition config.h:157
InputGamut g
Definition rgbw.h:125
const char * pinProbeSkipReason(int pin) FL_NO_EXCEPT
Why a probe must not touch pin at all (no pinMode, read or write), or nullptr when it may.
constexpr ClocklessChipset makeClockless(int pin) FL_NO_EXCEPT
Build a ClocklessChipset from a compile-time TIMING trait.
Definition config.h:95
void digitalWrite(int pin, PinValue val) FL_NO_EXCEPT
Write digital value to pin.
Definition pin.cpp.hpp:51
@ Output
Digital output (push-pull)
Definition pin.h:44
void pinMode(int pin, PinMode mode) FL_NO_EXCEPT
Set pin mode (input, output, pull-up, pull-down)
Definition pin.cpp.hpp:398
unsigned char uint8_t
Definition s16x16x4.h:209
4-phase RX timing thresholds for chipset detection
Definition rx.h:88
Primary template — intentionally undefined.
Definition bus_traits.h:31
corkscrew_args args
Definition old.h:149
Value state
Current driver state.
Definition driver.h:177
@ READY
Hardware idle; ready to accept new transmissions.
Definition driver.h:171
size_t buffer_size
Buffer size in symbols/edges (default: 512)
Definition rx.h:225
bool start_low
Pin idle state: true=LOW (WS2812B), false=HIGH (inverted)
Definition rx.h:232
Configuration for RX device initialization.
Definition rx.h:223

References args, fl::Watchdog::begin(), fl::RxConfig::buffer_size, fl::IChannelDriver::canHandle(), fl::ChannelData::create(), fl::digitalWrite(), fl::IChannelDriver::enqueue(), fl::Watchdog::feed(), FL_WARN_LIT, fl::High, fl::Watchdog::instance(), fl::Low, fl::make4PhaseTiming(), fl::makeClockless(), fl::adl_barrier::move(), fl::json::object(), fl::Output, fl::pinMode(), fl::pinProbeSkipReason(), pins, fl::IChannelDriver::poll(), fl::IChannelDriver::DriverState::READY, autoresearch::low_memory::RemoteState::remote, remote, autoresearch::low_memory::remoteState(), autoresearch::ieee754_check::run(), fl::serial_begin(), fl::IChannelDriver::show(), fl::RxConfig::start_low, fl::IChannelDriver::DriverState::state, fl::sstream::str(), fl::to_runtime_timing(), and fl::IChannelDriver::waitForReady().

Referenced by setup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: