47#if defined(FL_IS_ARM_LPC_845) && defined(FASTLED_LPC_PWM_DMA)
63#include "platforms/arm/lpc/drivers/sct_dma/channel_engine_lpc_sct_dma.h"
67#include "platforms/arm/lpc/drivers/sct_dma/bus_traits.h"
68#include "platforms/arm/lpc/drivers/sct_dma/lpc_ctimer_pulse_capture.h"
69#include "platforms/arm/lpc/lpc_dma_descriptor_table.h"
70#include "platforms/arm/lpc/lpc_dma_isr.h"
81constexpr int kMaxCaptureLeds = 17;
83struct PwmDmaClocklessState {
84 fl::u32 highTicks[kMaxCaptureLeds * 24];
85 fl::u8 decoded[kMaxCaptureLeds * 3];
88inline PwmDmaClocklessState& pwmDmaClocklessState()
FL_NO_EXCEPT {
93inline fl::ChipsetTimingConfig ws2812b_timing()
FL_NO_EXCEPT {
94 return fl::ChipsetTimingConfig(350, 450, 450,
107inline fl::ChipsetTimingConfig captureTiming()
FL_NO_EXCEPT {
108 return fl::ChipsetTimingConfig(350, 450, 1450,
109 50,
"WS2812B-long-low");
115inline fl::ChannelDataPtr makeChannelData(
116 int pin,
int num_leds, fl::u32 rgb, fl::u32 step,
117 const fl::ChipsetTimingConfig&
t = ws2812b_timing())
FL_NO_EXCEPT {
118 fl::vector_psram<fl::u8> encoded(
static_cast<fl::size
>(num_leds * 3));
119 for (
int i = 0; i < num_leds; ++i) {
120 const fl::u32 c = (rgb +
static_cast<fl::u32
>(i) *
step) & 0xFFFFFFu;
121 encoded[i * 3 + 0] =
static_cast<fl::u8>((c >> 8) & 0xFF);
122 encoded[i * 3 + 1] =
static_cast<fl::u8>((c >> 16) & 0xFF);
123 encoded[i * 3 + 2] =
static_cast<fl::u8>(c & 0xFF);
128inline fl::ChannelDataPtr makeSolidChannelData(
130 return makeChannelData(pin, num_leds, rgb, 0u);
135inline bool lastFrameComplete(fl::ChannelEngineLpcSctDma& engine)
FL_NO_EXCEPT {
136 return engine.transmitter().lastFrameComplete();
140inline bool waitReady(fl::IChannelDriver& engine, fl::u32 timeout_us)
FL_NO_EXCEPT {
141 const fl::u32 t0 =
micros();
143 if (
micros() - t0 > timeout_us) {
151inline fl::u32 frameTimeoutUs(
int led_count)
FL_NO_EXCEPT {
152 return static_cast<fl::u32
>(led_count) * 30u + 20000u;
163inline fl::string frameOnceHandler(
int led_count, fl::u32 rgb,
int data_pin)
FL_NO_EXCEPT {
164 if (led_count <= 0 || led_count > 300 || !usablePin(data_pin)) {
165 return fl::string(
"0,0,0,0");
167 auto& engine = fl::BusTraits<fl::Bus::BIT_BANG>::instance();
168 auto ch = makeSolidChannelData(data_pin, led_count, rgb);
169 if (!engine.canHandle(ch)) {
170 return fl::string(
"0,0,0,0");
173 const fl::u32 t_start =
micros();
176 const bool ok = waitReady(engine, frameTimeoutUs(led_count)) &&
177 lastFrameComplete(engine);
178 const fl::u32 t_end =
micros();
181 s << (ok ? 1 : 0) <<
',' << t_start <<
',' << t_end <<
',' << (t_end - t_start);
186inline fl::string frameBurstHandler(
int led_count, fl::u32 rgb,
188 if (led_count <= 0 || led_count > 300 || n_frames <= 0 ||
189 n_frames > 1000 || !usablePin(data_pin)) {
190 return fl::string(
"0,0,0");
192 auto& engine = fl::BusTraits<fl::Bus::BIT_BANG>::instance();
193 auto ch = makeSolidChannelData(data_pin, led_count, rgb);
194 if (!engine.canHandle(ch)) {
195 return fl::string(
"0,0,0");
202 const fl::u32 reset_us = ws2812b_timing().reset_us;
205 if (!waitReady(engine, frameTimeoutUs(kMaxCaptureLeds))) {
206 return fl::string(
"0,0,0");
208 const fl::u32 t_start =
micros();
209 fl::u32 last_end = t_start;
210 for (
int i = 0; i < n_frames; ++i) {
211 while (
micros() - last_end < reset_us) {
215 if (!waitReady(engine, frameTimeoutUs(led_count))) {
217 s <<
"0,stalled_at_frame=" << i;
221 if (!lastFrameComplete(engine)) {
223 s <<
"0,short_frame=" << i;
227 if ((i & 0x3F) == 0) {
231 const fl::u32 t_end =
micros();
232 const fl::u32 total_us = t_end - t_start;
234 const fl::u32 fps_x100 = total_us > 0
235 ? (
static_cast<fl::u32
>(n_frames) * 100u * 1000000u / total_us)
239 s << 1 <<
',' << total_us <<
',' << fps_x100;
244 return ticks * 1000u /
static_cast<fl::u32
>(F_CPU / 1000000UL);
250 fl::u32 lo = 0xFFFFFFFFu;
253 lo = ticks < lo ? ticks : lo;
254 hi = ticks > hi ? ticks : hi;
259 s << (r.hi ? ticksToNs(r.lo) : 0u) <<
'-' << ticksToNs(r.hi);
273 volatile fl::lpc::DmaChannelDescriptor* table =
274 reinterpret_cast<volatile fl::lpc::DmaChannelDescriptor*
>(DMA0->SRAMBASE);
275 const fl::u32 link = table[FASTLED_LPC_PWM_DMA_BASECH].link;
279 volatile fl::lpc::DmaChannelDescriptor* next =
280 reinterpret_cast<volatile fl::lpc::DmaChannelDescriptor*
>(link);
281 next->dst_end =
reinterpret_cast<fl::u32
>(&GPIO->SET[0]);
296constexpr fl::u32 kStressCh = 7u;
298 kStressCh != FL_LPC_CTIMER_CAPTURE_DMA_CH,
299 "the #5278 stressor channel must differ from the transmitter's and the capture's");
301inline volatile fl::lpc::DmaChannelDescriptor& stressLinkDesc()
FL_NO_EXCEPT {
302 static volatile fl::lpc::DmaChannelDescriptor d;
307 static volatile fl::u32 w[2];
312 DMA0->COMMON[0].ENABLECLR = (1u << kStressCh);
313 for (fl::u32 i = 0; i < 1000u && (DMA0->COMMON[0].BUSY & (1u << kStressCh)); ++i) {
315 DMA0->COMMON[0].ABORT = (1u << kStressCh);
322inline void stressStart(fl::u32 priority,
bool paced,
324 fl::lpc::ensureDmaSramBase();
327 const fl::u32 xfer = (1u << 0) | (1u << 1) | (paced ? 0u : (1u << 2)) |
328 (2u << 8) | (1023u << 16);
329 volatile fl::u32* w = stressWords();
330 volatile fl::lpc::DmaChannelDescriptor& link = stressLinkDesc();
331 link.reserved = xfer;
332 link.src_end =
reinterpret_cast<fl::u32
>(&w[0]);
333 link.dst_end =
reinterpret_cast<fl::u32
>(&w[1]);
334 link.link =
reinterpret_cast<fl::u32
>(&link);
336 volatile fl::lpc::DmaChannelDescriptor* table =
337 reinterpret_cast<volatile fl::lpc::DmaChannelDescriptor*
>(DMA0->SRAMBASE);
338 table[kStressCh].src_end = link.src_end;
339 table[kStressCh].dst_end = link.dst_end;
340 table[kStressCh].link = link.link;
343 INPUTMUX->DMA_ITRIG_INMUX[kStressCh] = paced ? 0x3u : 0xFu;
344 DMA0->CHANNEL[kStressCh].CFG =
345 (paced ? ((1u << 1) | (1u << 4) | (1u << 6) | ((burst_power & 15u) << 8))
347 ((priority & 7u) << 16);
349 SCT0->DMAREQ1 = SCT0->DMAREQ0;
351 DMA0->COMMON[0].SETVALID = (1u << kStressCh);
352 DMA0->COMMON[0].ENABLESET = (1u << kStressCh);
353 DMA0->CHANNEL[kStressCh].XFERCFG = xfer;
359constexpr fl::u32 kMaxRiseGapNs = 3000;
367inline fl::string captureFrame(
int led_count, fl::u32 rgb, fl::u32 step,
368 int data_pin,
int rx_pin,
int hold_us,
370 const fl::ChipsetTimingConfig&
t = captureTiming(),
371 int stress_priority = -1,
372 int stress_paced = 0,
374 const fl::u32 t0h = fl::lpc_sct_ticks(
t.t1_ns);
375 const fl::u32 t1h = fl::lpc_sct_ticks(
t.t1_ns +
t.t2_ns);
376 const fl::u32 period = fl::lpc_sct_ticks(
t.t1_ns +
t.t2_ns +
t.t3_ns);
378 auto& st = pwmDmaClocklessState();
379 const fl::u32 n_bits =
static_cast<fl::u32
>(led_count) * 24u;
386 const fl::u32
sample = reload_stats ? t1h + fl::lpc_sct_ticks(100)
387 : (t1h + period) / 2u;
390 auto& engine = fl::BusTraits<fl::Bus::BIT_BANG>::instance();
391 if (!waitReady(engine, frameTimeoutUs(kMaxCaptureLeds))) {
392 return fl::string(
"0,busy");
394 fl::LpcCtimerPulseCapture cap;
395 if (!cap.begin(rx_pin, sample, fl::lpc_sct_ticks(max_gap_ns),
396 fl::span<fl::u32>(st.highTicks, n_bits))) {
397 return fl::string(
"0,capture_begin");
400 auto ch = makeChannelData(data_pin, led_count, rgb, step,
t);
401 if (!engine.canHandle(ch)) {
403 return fl::string(
"0,cannot_handle");
405 if (stress_priority >= 0 && stress_paced == 0) {
406 stressStart(
static_cast<fl::u32
>(stress_priority),
false, 0u);
410 if (stress_priority >= 0 && stress_paced > 0) {
411 stressStart(
static_cast<fl::u32
>(stress_priority),
true,
412 static_cast<fl::u32
>(stress_paced - 1));
417 const bool sent = waitReady(engine, frameTimeoutUs(led_count));
419 if (stress_priority >= 0) {
422 const fl::u32 got = cap.count();
424 if (!fl::ChannelEngineLpcSctDma::frameFits(ch->getSize())) {
428 if (sent && got == 0u && !lastFrameComplete(engine)) {
429 return fl::string(
"0,hub_off_refused");
432 s <<
"0,refusal_leaked,bits=" << got;
437 s <<
"0,tx_stalled,bits=" << got;
440 if (!lastFrameComplete(engine)) {
442 s <<
"0,tx_short,bits=" << got;
446 const fl::u32 threshold = (t0h + t1h) / 2u;
450 HighRange r0, r1, rr0, rr1;
451 fl::memset(st.decoded, 0,
sizeof(st.decoded));
452 for (fl::u32 i = 0; i < got; ++i) {
453 const fl::u32 h = st.highTicks[i];
454 const bool reload = i > 0u && (i % FASTLED_LPC_PWM_DMA_CHUNK_BITS) == 0u;
456 st.decoded[i >> 3] |=
static_cast<fl::u8>(0x80u >> (i & 7u));
458 if (reload) rr1.add(h);
461 if (reload) rr0.add(h);
464 const fl::u32 n_bytes = got / 8u;
470 fl::u32 bad = n_bytes == n_bits / 8u ? 0u : 1u;
471 fl::u32 first = 0xFFFFFFFFu, first_exp = 0;
472 for (fl::u32 i = 0; i < n_bytes && i < n_bits / 8u; ++i) {
473 const fl::u32 c = (rgb + (i / 3u) *
step) & 0xFFFFFFu;
474 const fl::u32 shift = (i % 3u) == 0u ? 8u : ((i % 3u) == 1u ? 16u : 0u);
476 if (st.decoded[i] != want) {
484 s << 1 <<
",bytes=" << n_bytes <<
",bad=" << bad;
485 if (first != 0xFFFFFFFFu) {
486 s <<
",first=" << first <<
':'
487 <<
static_cast<fl::u32
>(st.decoded[first]) <<
'/' << first_exp;
490 <<
",t0h=" << r0 <<
",t1h=" << r1 <<
",reload_t0h=" << rr0
491 <<
",reload_t1h=" << rr1;
494 s << 1 <<
',' << n_bytes;
495 for (fl::u32 i = 0; i < n_bytes; ++i) {
496 s << ',' << static_cast<fl::u32>(st.decoded[i]);
498 s <<
",bits=" << got;
499 s <<
",t0h=" << r0 <<
",t1h=" << r1;
500 s <<
",gap_guard=" << max_gap_ns;
504inline fl::string captureSelfHandler(
int led_count, fl::u32 rgb,
505 int data_pin,
int rx_pin,
507 if (led_count <= 0 || led_count > kMaxCaptureLeds || !usablePin(data_pin) ||
508 !usablePin(rx_pin) || rx_pin == data_pin || hold_us < 0 ||
510 return fl::string(
"0,bad_args");
512 return captureFrame(led_count, rgb, step, data_pin, rx_pin, hold_us,
521inline fl::string captureStressHandler(
int led_count, fl::u32 rgb,
522 int data_pin,
int rx_pin, fl::u32 step,
523 int real_timing,
int stress_priority,
525 if (led_count <= 0 || led_count > kMaxCaptureLeds || !usablePin(data_pin) ||
526 !usablePin(rx_pin) || rx_pin == data_pin || stress_priority < -1 ||
527 stress_priority > 7 || paced < 0 || paced > 4) {
528 return fl::string(
"0,bad_args");
530 return captureFrame(led_count, rgb, step, data_pin, rx_pin, 0,
532 real_timing ? ws2812b_timing() : captureTiming(),
533 stress_priority, paced,
true);
546inline fl::string dmaErrorHandler(
int led_count, fl::u32 rgb,
int data_pin,
548 if (led_count <= 0) {
549 return fl::string(
"0,bad_args");
551 const fl::u32 n_bits =
static_cast<fl::u32
>(led_count) * 24u;
552 if (n_bits <= FASTLED_LPC_PWM_DMA_CHUNK_BITS || led_count > kMaxCaptureLeds ||
553 !usablePin(data_pin) || !usablePin(rx_pin) || rx_pin == data_pin ||
554 hold_us < 0 || hold_us > 100000) {
555 return fl::string(
"0,bad_args");
557 const fl::ChipsetTimingConfig
t = captureTiming();
558 const fl::u32 t1h = fl::lpc_sct_ticks(
t.t1_ns +
t.t2_ns);
559 const fl::u32 period = fl::lpc_sct_ticks(
t.t1_ns +
t.t2_ns +
t.t3_ns);
560 auto& engine = fl::BusTraits<fl::Bus::BIT_BANG>::instance();
561 if (!waitReady(engine, frameTimeoutUs(kMaxCaptureLeds))) {
562 return fl::string(
"0,busy");
564 auto ch = makeChannelData(data_pin, led_count, rgb, 0u,
t);
565 if (!engine.canHandle(ch)) {
566 return fl::string(
"0,cannot_handle");
570 if (!fl::ChannelEngineLpcSctDma::frameFits(ch->getSize())) {
571 return fl::string(
"0,too_long_without_hub");
573 auto& st = pwmDmaClocklessState();
574 fl::LpcCtimerPulseCapture cap;
575 if (!cap.begin(rx_pin, (t1h + period) / 2u, fl::lpc_sct_ticks(kMaxRiseGapNs),
576 fl::span<fl::u32>(st.highTicks, kMaxCaptureLeds * 24))) {
577 return fl::string(
"0,capture_begin");
583 if (!corruptSecondChunk()) {
586 return fl::string(
"0,no_second_chunk");
591 const bool ready = waitReady(engine, frameTimeoutUs(led_count));
593 const fl::u32 got = cap.count();
598 s << 1 <<
",ready=" << (ready ? 1 : 0)
599 <<
",complete=" << (lastFrameComplete(engine) ? 1 : 0)
600 <<
",bits=" << got <<
",chunk_bits=" << FASTLED_LPC_PWM_DMA_CHUNK_BITS
601 <<
",line=" << (line_high ? 1 : 0) <<
",hub=" << FASTLED_LPC_DMA_ISR;
608 remote.bind(
"pwmDmaClFrameOnce",
609 [](
int led_count, fl::u32 rgb,
int data_pin) -> fl::string {
610 return frameOnceHandler(led_count, rgb, data_pin);
612 remote.bind(
"pwmDmaClFrameBurst",
613 [](
int led_count, fl::u32 rgb,
int n_frames,
int data_pin) -> fl::string {
614 return frameBurstHandler(led_count, rgb, n_frames, data_pin);
616 remote.bind(
"pwmDmaClCaptureSelf",
617 [](
int led_count, fl::u32 rgb,
int data_pin,
int rx_pin,
618 int hold_us, fl::u32 step_rgb) -> fl::string {
619 return captureSelfHandler(led_count, rgb, data_pin, rx_pin,
622 remote.bind(
"pwmDmaClCaptureStress",
623 [](
int led_count, fl::u32 rgb,
int data_pin,
int rx_pin,
624 fl::u32 step_rgb,
int real_timing,
int stress_priority,
625 int paced) -> fl::string {
626 return captureStressHandler(led_count, rgb, data_pin, rx_pin,
627 step_rgb, real_timing, stress_priority,
630 remote.bind(
"pwmDmaClDmaError",
631 [](
int led_count, fl::u32 rgb,
int data_pin,
int rx_pin,
632 int hold_us) -> fl::string {
633 return dmaErrorHandler(led_count, rgb, data_pin, rx_pin, hold_us);
fl::unique_ptr< fl::Remote > remote
Portable compile-time identifiers for LED channel buses.
Per-driver traits keyed on fl::Bus.
Runtime chipset timing configuration for clockless LED drivers.
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)
static T & instance() FL_NO_EXCEPT
static Watchdog & instance() FL_NO_EXCEPT
string str() const FL_NO_EXCEPT
Channel transmission data - lightweight DTO for driver transmission.
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
CRGB sample(const CRGB *grid, const XYMap &xyMap, float x, float y, SampleMode mode) FL_NO_EXCEPT
Sample a pixel from a 2D CRGB grid at floating-point coordinates.
ostream & operator<<(ostream &os, const hex_t &) FL_NO_EXCEPT
fl::u32 micros() FL_NO_EXCEPT
Universal microsecond timer - returns microseconds since system startup.
@ High
Logic high (3.3V / 5V, platform-dependent)
FASTLED_FORCE_INLINE void * memset(void *s, int c, size_t n) FL_NO_EXCEPT
constexpr enable_if< is_fixed_point< T >::value, T >::type step(T edge, T x) FL_NO_EXCEPT
float add(float &a, float &b) FL_NO_EXCEPT
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.
PinValue digitalRead(int pin) FL_NO_EXCEPT
Read digital value from pin.
@ Input
Digital input (high impedance)
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.
Platform-neutral "may a pin probe touch this pin?" API.
#define FL_STATIC_ASSERT(...)
Portable compile-time assertion wrapper.
@ READY
Hardware idle; ready to accept new transmissions.
Compile-time timing extraction from ChipsetTiming structs.