From 3cd1924e620ffe6bde3f37060c34f8c229a56d19 Mon Sep 17 00:00:00 2001 From: Jan Vos Date: Wed, 18 Nov 2026 23:23:28 +0100 Subject: [PATCH 1/2] Keep each clock counter on its own PIO count_clock() claims three state machines one by one, and each claim could land on a different PIO, but all three wrote the same dmd_pio. read_clock_count() then read and released all three counters on that last PIO: the detection read a state machine that never counted and failed, and the counters on the other PIO stayed claimed and running. This happens when another user already holds state machines, e.g. the HUB75 driver with two chains plus WS2812 strips on a PPUCDMD: the first detection always failed and the DMD came up a retry (about 1s) later. Store each counter's PIO in spi_pio/dmd_pio/frame_pio, next to the state machines and offsets the counters already reuse. Co-Authored-By: Claude Opus 5.5 --- src/dmdreader.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/dmdreader.cpp b/src/dmdreader.cpp index 30b5355..3d29919 100644 --- a/src/dmdreader.cpp +++ b/src/dmdreader.cpp @@ -292,30 +292,34 @@ void spi_dma_handler() { void count_clock() { const uint pins[3] = {DOTCLK, RCLK, RDATA}; - // just make use of the already defined sms/offsets + // just make use of the already defined pios/sms/offsets + // Each counter keeps its own PIO: when another user already holds state + // machines, the three claims do not all land on the same PIO. + PIO *pios[3] = {&spi_pio, &dmd_pio, &frame_pio}; uint *sms[3] = {&spi_sm, &dmd_sm, &frame_sm}; uint *offsets[3] = {&spi_offset, &dmd_offset, &frame_offset}; for (int i = 0; i < 3; i++) { pio_claim_free_sm_and_add_program_for_gpio_range( - &dmd_count_signal_program, &dmd_pio, sms[i], offsets[i], pins[i], 1, + &dmd_count_signal_program, pios[i], sms[i], offsets[i], pins[i], 1, true); - dmd_counter_program_init(dmd_pio, *sms[i], *offsets[i], pins[i]); - pio_sm_set_enabled(dmd_pio, *sms[i], true); + dmd_counter_program_init(*pios[i], *sms[i], *offsets[i], pins[i]); + pio_sm_set_enabled(*pios[i], *sms[i], true); } } uint64_t read_clock_count() { uint32_t counts[3]; - // just make use of the already defined sms/offsets + // just make use of the already defined pios/sms/offsets + PIO *pios[3] = {&spi_pio, &dmd_pio, &frame_pio}; uint *sms[3] = {&spi_sm, &dmd_sm, &frame_sm}; uint *offsets[3] = {&spi_offset, &dmd_offset, &frame_offset}; for (int i = 0; i < 3; i++) { - pio_sm_exec(dmd_pio, *sms[i], pio_encode_in(pio_x, 32)); - counts[i] = (~pio_sm_get(dmd_pio, *sms[i])) * COUNT_MULTIPLIER; - pio_sm_set_enabled(dmd_pio, *sms[i], false); - pio_remove_program_and_unclaim_sm(&dmd_count_signal_program, dmd_pio, + pio_sm_exec(*pios[i], *sms[i], pio_encode_in(pio_x, 32)); + counts[i] = (~pio_sm_get(*pios[i], *sms[i])) * COUNT_MULTIPLIER; + pio_sm_set_enabled(*pios[i], *sms[i], false); + pio_remove_program_and_unclaim_sm(&dmd_count_signal_program, *pios[i], *sms[i], *offsets[i]); } From eea93287d60373141f02f9d17557549ee8c426ab Mon Sep 17 00:00:00 2001 From: Jan Vos Date: Wed, 18 Nov 2026 23:43:54 +0100 Subject: [PATCH 2/2] Claim the reader and frame detection on the same PIO The frame detection starts the reader through a PIO IRQ, and PIO IRQs only reach state machines of the same PIO. Both were claimed one by one, so they could land on different PIOs: the reader then waited for an IRQ that never came and no frame was read. Before the previous commit the counters a failed detection left behind happened to fill the PIO that would have split them. Now that they are released, a PPUCDMD with two HUB75 chains and speaker lights put the reader and the frame detection on different PIOs. Co-Authored-By: Claude Opus 5.5 --- src/dmdreader.cpp | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/dmdreader.cpp b/src/dmdreader.cpp index 3d29919..9908eb0 100644 --- a/src/dmdreader.cpp +++ b/src/dmdreader.cpp @@ -939,6 +939,44 @@ void dmdreader_error_blink(bool no_error) { using DmdConfigGetter = pio_sm_config (*)(uint); +/** + * @brief Claims a state machine for the reader and one for the frame detection + * on the same PIO and loads both programs there. The frame detection starts + * the reader through a PIO IRQ, and PIO IRQs only reach state machines of the + * same PIO. + * + * @return false if no PIO has two free state machines and room for both + */ +bool claim_reader_programs(const pio_program_t *reader_program, + const pio_program_t *framedetect_program) { + // Like pio_claim_free_sm_and_add_program_for_gpio_range(), start at the + // last PIO. + for (int i = NUM_PIOS - 1; i >= 0; i--) { + PIO pio = pio_get_instance(i); + // The DMD pins are GPIO 0 - 15. + if (pio_get_gpio_base(pio) != 0) continue; + + int reader_sm = pio_claim_unused_sm(pio, false); + if (reader_sm < 0) continue; + int framedetect_sm = pio_claim_unused_sm(pio, false); + if (framedetect_sm >= 0 && pio_can_add_program(pio, reader_program)) { + uint reader_offset = pio_add_program(pio, reader_program); + if (pio_can_add_program(pio, framedetect_program)) { + dmd_pio = frame_pio = pio; + dmd_sm = reader_sm; + dmd_offset = reader_offset; + frame_sm = framedetect_sm; + frame_offset = pio_add_program(pio, framedetect_program); + return true; + } + pio_remove_program(pio, reader_program, reader_offset); + } + if (framedetect_sm >= 0) pio_sm_unclaim(pio, framedetect_sm); + pio_sm_unclaim(pio, reader_sm); + } + return false; +} + void dmdreader_programs_init(const pio_program_t *dmd_reader_program, DmdConfigGetter reader_get_default_config, const pio_program_t *dmd_framedetect_program, @@ -949,18 +987,14 @@ void dmdreader_programs_init(const pio_program_t *dmd_reader_program, float target_hz = 125000000.0f; // PIO code designed for 125 MHz float dmd_clkdiv = (float)sys_hz / target_hz; // scales automatically - dmdreader_error_blink(pio_claim_free_sm_and_add_program_for_gpio_range( - dmd_reader_program, &dmd_pio, &dmd_sm, &dmd_offset, - (DE < SDATA_X16) ? DE : SDATA_X16, 8, true)); + dmdreader_error_blink( + claim_reader_programs(dmd_reader_program, dmd_framedetect_program)); pio_sm_config dmd_config = reader_get_default_config(dmd_offset); dmd_reader_program_init(dmd_clkdiv, dmd_pio, dmd_sm, dmd_offset, dmd_config, dmd_type, in_base_pin); // The framedetect program just runs and detects the beginning of a new // frame - dmdreader_error_blink(pio_claim_free_sm_and_add_program_for_gpio_range( - dmd_framedetect_program, &frame_pio, &frame_sm, &frame_offset, - (DE < SDATA_X16) ? DE : SDATA_X16, 8, true)); pio_sm_config frame_config = framedetect_get_default_config(frame_offset); dmd_framedetect_program_init(dmd_clkdiv, frame_pio, frame_sm, frame_offset, frame_config, input_pins, num_input_pins,