@@ -592,6 +592,18 @@ void dmd_dma_reset() {
592592void dmd_dma_handler () {
593593 dmd_set_and_enable_new_dma_target ();
594594
595+ if (dmd_type == DMD_DE_X16_V2) {
596+ // Due to the complexity of x16 v2, we use this way to re-sync
597+ // if the signals are noisy, or whatever else could happen.
598+ pio_sm_set_enabled (dmd_pio, dmd_sm, false );
599+ // clear the interrupt for FRAME_START_IRQ 5 in the pio
600+ dmd_pio->irq = (1u << 5 );
601+ pio_sm_exec_wait_blocking (dmd_pio, dmd_sm, pio_encode_mov (pio_y, pio_null));
602+ dmd_dma_reset ();
603+ pio_sm_exec (dmd_pio, dmd_sm, pio_encode_jmp (dmd_offset));
604+ pio_sm_set_enabled (dmd_pio, dmd_sm, true );
605+ }
606+
595607 // Required as long as CAPCOM is not locked-in:
596608 plane0_shifted = false ;
597609 detected_0_1_0_1 = false ;
@@ -650,7 +662,7 @@ void dmd_dma_handler() {
650662 // It seems to be sufficient to check every 8th pixel for these patterns to
651663 // detect sync. So we could avoid bitschifiting of the uint32_t value to
652664 // check every single pixel.
653- if (dmd_type > = DMD_CAPCOM && !locked_in && !plane0_shifted) {
665+ if (dmd_type = = DMD_CAPCOM && !locked_in && !plane0_shifted) {
654666 digitalWrite (LED_BUILTIN, HIGH);
655667 uint8_t value = pixval & 0x0F ;
656668 if (value == 2 && (planebuf[px] & 0x0F ) != 1 &&
@@ -724,7 +736,7 @@ void dmd_dma_handler() {
724736 }
725737 }
726738
727- if (DMD_CAPCOM >= dmd_type && !locked_in && !plane0_shifted &&
739+ if (dmd_type == DMD_CAPCOM && !locked_in && !plane0_shifted &&
728740 detected_0_1_0_1 && detected_1_0_0_0) {
729741 locked_in = true ;
730742 }
@@ -865,20 +877,26 @@ void dmdreader_programs_init(const pio_program_t *dmd_reader_program,
865877 DmdConfigGetter framedetect_get_default_config,
866878 uint *input_pins, uint8_t num_input_pins,
867879 uint8_t jump_pin, uint8_t in_base_pin) {
880+ uint32_t sys_hz = clock_get_hz (clk_sys); // e.g. 125/200/266 MHz
881+ float target_hz = 125000000 .0f ; // PIO code designed for 125 MHz
882+ float dmd_clkdiv = (float )sys_hz / target_hz; // scales automatically
883+
868884 dmdreader_error_blink (pio_claim_free_sm_and_add_program_for_gpio_range (
869885 dmd_reader_program, &dmd_pio, &dmd_sm, &dmd_offset,
870886 (DE < SDATA_X16) ? DE : SDATA_X16, 8 , true ));
871887 pio_sm_config dmd_config = reader_get_default_config (dmd_offset);
872- dmd_reader_program_init (dmd_pio, dmd_sm, dmd_offset, dmd_config, in_base_pin);
888+ dmd_reader_program_init (dmd_clkdiv, dmd_pio, dmd_sm, dmd_offset, dmd_config,
889+ in_base_pin);
873890
874891 // The framedetect program just runs and detects the beginning of a new
875892 // frame
876893 dmdreader_error_blink (pio_claim_free_sm_and_add_program_for_gpio_range (
877894 dmd_framedetect_program, &frame_pio, &frame_sm, &frame_offset,
878895 (DE < SDATA_X16) ? DE : SDATA_X16, 8 , true ));
879896 pio_sm_config frame_config = framedetect_get_default_config (frame_offset);
880- dmd_framedetect_program_init (frame_pio, frame_sm, frame_offset, frame_config,
881- input_pins, num_input_pins, jump_pin);
897+ dmd_framedetect_program_init (dmd_clkdiv, frame_pio, frame_sm, frame_offset,
898+ frame_config, input_pins, num_input_pins,
899+ jump_pin);
882900 pio_sm_set_enabled (frame_pio, frame_sm, true );
883901}
884902
@@ -1032,13 +1050,13 @@ bool dmdreader_init(bool return_on_no_detection) {
10321050 pio_sm_exec_wait_blocking (dmd_pio, dmd_sm,
10331051 pio_encode_mov (pio_y, pio_null));
10341052
1035- // load 8192 directly to TX fifo
1036- pio_sm_put (dmd_pio, dmd_sm, 8192 );
1053+ // load 4096 directly to TX fifo (32uS)
1054+ pio_sm_put (dmd_pio, dmd_sm, 4096 );
10371055 // pull 32 bits from the TX fifo into osr
10381056 pio_sm_exec (dmd_pio, dmd_sm, pio_encode_pull (false , false ));
10391057
1040- // load 3000 directly to TX fifo
1041- pio_sm_put (frame_pio, frame_sm, 3000 );
1058+ // load 2500 directly to TX fifo (20uS)
1059+ pio_sm_put (frame_pio, frame_sm, 2500 );
10421060 // pull 32 bits from the TX fifo into osr
10431061 pio_sm_exec (frame_pio, frame_sm, pio_encode_pull (false , false ));
10441062
@@ -1311,7 +1329,6 @@ bool dmdreader_init(bool return_on_no_detection) {
13111329 irq_set_exclusive_handler (DMA_IRQ_0, dmd_dma_handler);
13121330 irq_set_enabled (DMA_IRQ_0, true );
13131331#endif
1314-
13151332 // Finally start DMD reader PIO program and DMA
13161333 dmd_set_and_enable_new_dma_target ();
13171334 pio_sm_set_enabled (dmd_pio, dmd_sm, true );
0 commit comments