Skip to content

Commit 122a147

Browse files
committed
CAPCOM frame detection
1 parent 00bdc8e commit 122a147

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

src/dmd_interface_capcom.pio

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,24 @@ dot_read_loop:
4343
in x, 5 ; shift in 5 bits, isr is 31 now
4444
set x, 3 ; load 3
4545
in x, 2 ; shift in 2 bits, isr is 127 now
46-
mov y, isr ; copy 127 to OSR
46+
mov y, isr ; copy 127 to Y
4747
mov isr, null ; clear ISR and reset shift counter
48+
set x, 30
4849

4950
wait 0 gpio RDATA
5051
wait 1 gpio RDATA
5152

53+
skip_31_rows:
54+
wait 1 gpio RCLK
55+
wait 0 gpio RCLK
56+
jmp x-- skip_31_rows
57+
5258
.wrap_target
59+
irq FRAME_START_IRQ
5360
mov x, y ; 128 rows need to be skipped as we are working with 4 planes per frame
5461

5562
rclk_loop:
5663
wait 1 gpio RCLK
5764
wait 0 gpio RCLK
5865
jmp x-- rclk_loop
59-
nop [3]
60-
irq FRAME_START_IRQ
61-
.wrap
66+
.wrap

src/dmdreader.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ uint32_t frame_crc;
138138
int32_t crc_previous_frame = 0;
139139
uint8_t skip_frames = 0;
140140
bool locked_in = false;
141+
bool plane0_shifted = false;
141142

142143
// SPI PIO
143144
PIO spi_pio;
@@ -511,6 +512,9 @@ void dmd_dma_handler() {
511512
return;
512513
}
513514

515+
// Required as long as CAPCOM is not locked-in:
516+
plane0_shifted = false;
517+
514518
// Fix byte order within the buffer
515519
uint32_t *planebuf = (uint32_t *)currentPlaneBuffer;
516520
buf32_t *v;
@@ -549,21 +553,29 @@ void dmd_dma_handler() {
549553
// The other three combinations that reuslt in a valus of 1 indicate that
550554
// the signal is out of sync. It is sufficient to check one pixel of the
551555
// group.
552-
if (DMD_CAPCOM == dmd_type && !locked_in && 1 == (pixval & 0xF)) {
553-
if (planebuf[offset[0] + px] & 0xF) {
554-
// We are in sync.
555-
locked_in = true;
556-
} else {
557-
// Stop the state machine that detects frames.
558-
pio_sm_set_enabled(dmd_pio, frame_sm, false);
559-
// Start state machine again. The PIO program will skip at least one
560-
// plane as it is waiting for RDATA at the beginning.
561-
pio_sm_set_enabled(dmd_pio, frame_sm, true);
562-
// We're out of sync. Skip the next frame which will contain garbage
563-
// as it gets filles already in the background in this moment and would
564-
// trigger the same correction.
565-
skip_frames = 1;
566-
// Do not switch buffers and return here. The DMD should show something.
556+
if (DMD_CAPCOM == dmd_type && !locked_in && !plane0_shifted) {
557+
if ((pixval & 0xF) == 1) {
558+
if ((planebuf[px] & 0xF) == 1) {
559+
// We are in sync.
560+
locked_in = true;
561+
} else {
562+
// Stop the state machine that detects frames.
563+
pio_sm_set_enabled(dmd_pio, frame_sm, false);
564+
dma_channel_abort(dmd_dma_channel);
565+
// Enable DMA interrupt 0 to be triggered when the transfer is done.
566+
dma_channel_set_irq0_enabled(dmd_dma_channel, true);
567+
dma_channel_configure(
568+
dmd_dma_channel, &dmd_dma_channel_cfg,
569+
(currentPlaneBuffer == planebuf1) ? planebuf2 : planebuf1,
570+
&dmd_pio->rxf[dmd_sm], // Source pointer
571+
source_dwordsperframe, // Number of transfers
572+
true // Start now
573+
);
574+
// Start state machine again. The PIO program will skip at least one
575+
// plane as it is waiting for RDATA at the beginning.
576+
pio_sm_set_enabled(dmd_pio, frame_sm, true);
577+
plane0_shifted = true;
578+
}
567579
}
568580
}
569581

@@ -822,7 +834,7 @@ void dmdreader_init(PIO pio) {
822834
source_width = 128;
823835
source_height = 32;
824836
source_bitsperpixel = 4;
825-
target_bitsperpixel = 4;
837+
target_bitsperpixel = 2;
826838
source_planesperframe = 4;
827839
source_lineoversampling = LINEOVERSAMPLING_NONE;
828840
source_mergeplanes = MERGEPLANES_ADD;

0 commit comments

Comments
 (0)