From b0d5178c92673ea1ec5bd6a57a9c8265f4f7b28d Mon Sep 17 00:00:00 2001 From: Matthew Carroll Date: Thu, 10 Sep 2026 03:05:30 -0700 Subject: [PATCH] lf t55xx detect: finish undoing the psk antenna-settle trim test_scan and t55xx_fallback_try drop 160 samples off the head of the capture before demodulating psk, then restore it. restore_bufferS32 copies the samples back and returns the length it put back, but no caller assigned that return, so g_GraphTraceLen stayed at the trimmed length for the rest of detect. That left the sample-space anchoring unable to engage. t55xx_demod_offset only resolves when config.anchor_tracelen equals g_GraphTraceLen; detect recorded 11840 while every block read acquires 12000, so the guard never matched and a dump fell back to config.offset - a bit index into a demod buffer that no longer exists, which is the case its own comment warns about. Over six instrumented dumps of a PSK1 RF/128 tag, one resolved and five did not; the one that resolved read block 0 correctly on the first try. A candidate found inside the trim was also measured 160 samples left of where a block read measures, so those anchors were out of frame even when the length happened to match. 160 samples is round(160 / clk) bits - 5 at RF/32, 1 at RF/128 - and a rotation of that size is what read-back was showing. Pair the trim with its undo so a third call site cannot pick up half of it, hold a bias while the capture is trimmed so a recorded anchor still names an untrimmed sample, and give the 160 one home. Co-Authored-By: Claude Opus 5 --- CHANGELOG.md | 1 + client/src/cmdlft55xx.c | 50 ++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f8272e76..141acff11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Fixed `lf t55xx detect` - PSK1 now detects at every bit rate and subcarrier. (@iceman1001) - Fixed `lf t55xx detect` - FSK was skipped entirely when the field-clock pair measured as neither legal pair, losing FSK1 at RF/32 and RF/40 and every variant at RF/16 (@iceman1001) - Fixed `lf t55xx detect` - the block 0 rotation was picked by scan order; the measured broadcast period now settles it (@mfcarroll) +- Fixed `lf t55xx detect` - the psk antenna-settle trim left the graph 160 samples short, so the graph-sample anchor was discarded on most block reads (@mfcarroll) - Fixed `lf psk demod` - lead-in samples were accepted as a phase shift, complementing the rest of the word (@mfcarroll) - Fixed `lf fsk demod` - a leading run too short to be a bit was forced to one, fabricating a bit and shifting the word (@mfcarroll) - Fixed `lf nrz demod` - the samples before the first level change were counted as bits, rotating the word whenever that edge moved (@mfcarroll) diff --git a/client/src/cmdlft55xx.c b/client/src/cmdlft55xx.c index 49e7e2433..eb1006e90 100644 --- a/client/src/cmdlft55xx.c +++ b/client/src/cmdlft55xx.c @@ -82,6 +82,13 @@ static t55xx_memory_item_t cardmem[T55x7_BLOCK_COUNT] = {{0}}; // Regular read mode cycles several blocks and a buffer loaded from a file could be either static bool s_block_read_capture = false; +// samples dropped from the head of a capture before psk demodulation +#define T55XX_PSK_SETTLE_TRIM 160 + +// non-zero while the capture is trimmed, so an anchor recorded inside the trim +// still names an untrimmed sample +static int32_t s_sample_bias = 0; + // A block read repeats one 32 bit word for as long as the field is on. // `offset` is a bit index into a demod buffer that no longer exists once the next acquisition lands, // and the demodulators do not all start on the same bit: @@ -90,16 +97,36 @@ static bool s_block_read_capture = false; static void t55xx_anchor(t55xx_conf_block_t *c, uint8_t offset) { c->offset = offset; c->anchor_valid = (g_DemodClock > 0); - c->anchor_sample = g_DemodStartIdx + ((int32_t)offset * g_DemodClock); - c->anchor_tracelen = (int32_t)g_GraphTraceLen; + c->anchor_sample = g_DemodStartIdx + ((int32_t)offset * g_DemodClock) + s_sample_bias; + c->anchor_tracelen = (int32_t)g_GraphTraceLen + s_sample_bias; } // records a detect candidate against the demodulation that is loaded right now static void t55xx_record_hit(t55xx_conf_block_t *t) { t->block0 = PackBits(t->offset, 32, g_DemodBuffer); t->anchor_valid = (g_DemodClock > 0); - t->anchor_sample = g_DemodStartIdx + ((int32_t)t->offset * g_DemodClock); - t->anchor_tracelen = (int32_t)g_GraphTraceLen; + t->anchor_sample = g_DemodStartIdx + ((int32_t)t->offset * g_DemodClock) + s_sample_bias; + t->anchor_tracelen = (int32_t)g_GraphTraceLen + s_sample_bias; +} + +// skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise) +static buffer_savestate_t t55xx_psk_trim_head(void) { + buffer_savestate_t st = save_bufferS32(g_GraphBuffer, g_GraphTraceLen); + st.offset = g_GridOffset; + + char ltrim[16]; + snprintf(ltrim, sizeof(ltrim), "-i %d", T55XX_PSK_SETTLE_TRIM); + CmdLtrim(ltrim); + + s_sample_bias = T55XX_PSK_SETTLE_TRIM; + return st; +} + +static void t55xx_psk_untrim_head(buffer_savestate_t st) { + s_sample_bias = 0; + // restore_bufferS32 returns the length it put back; dropping it leaves the trim in place + g_GraphTraceLen = restore_bufferS32(st, g_GraphBuffer); + g_GridOffset = st.offset; } // the bit offset to read a block at in the demod buffer loaded right now @@ -1617,9 +1644,7 @@ static bool t55xx_fallback_try(pm3_mod_t mod, pm3_enc_t enc, int fc_hi, int fc_l if (mod == PM3_MOD_PSK) { - buffer_savestate_t saveState = save_bufferS32(g_GraphBuffer, g_GraphTraceLen); - saveState.offset = g_GridOffset; - CmdLtrim("-i 160"); + buffer_savestate_t saveState = t55xx_psk_trim_head(); for (int inv = 0; inv < 2; inv++) { if (PSKDemod(fitclk, inv, PM3_T55_FALLBACK_MAXERR, false) != PM3_SUCCESS) { @@ -1653,8 +1678,7 @@ static bool t55xx_fallback_try(pm3_mod_t mod, pm3_enc_t enc, int fc_hi, int fc_l } } - restore_bufferS32(saveState, g_GraphBuffer); - g_GridOffset = saveState.offset; + t55xx_psk_untrim_head(saveState); if (*hits == before && coherent_ok) { t55xx_psk_coherent(fitclk, clk, tests, hits, downlink_mode); @@ -2002,10 +2026,7 @@ bool t55xxTryDetectModulationEx(uint8_t downlink_mode, bool print_config, uint32 clk = GetPskClock("", false); if (clk > 0) { // allow undo - buffer_savestate_t saveState = save_bufferS32(g_GraphBuffer, g_GraphTraceLen); - saveState.offset = g_GridOffset; - // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise) - CmdLtrim("-i 160"); + buffer_savestate_t saveState = t55xx_psk_trim_head(); if ((PSKDemod(0, 0, 6, false) == PM3_SUCCESS) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) { tests[hits].modulation = DEMOD_PSK1; tests[hits].bitrate = bitRate; @@ -2041,8 +2062,7 @@ bool t55xxTryDetectModulationEx(uint8_t downlink_mode, bool print_config, uint32 } // inverse waves does not affect this demod //undo trim samples - restore_bufferS32(saveState, g_GraphBuffer); - g_GridOffset = saveState.offset; + t55xx_psk_untrim_head(saveState); // t55xx_search_config_psk(g_GraphBuffer, 1); // t55xx_search_config_psk(g_GraphBuffer, 2); }