From 43b257ddb4099376bd1e362295458f607053d95b Mon Sep 17 00:00:00 2001 From: Christian Zietz Date: Thu, 1 Feb 2024 18:33:23 +0100 Subject: [PATCH] Make demodulation threshold for Legic configurable This adds a new parameter to the "hw sethfthresh" command. --- armsrc/appmain.c | 1 + armsrc/legicrf.c | 9 +++++++-- armsrc/legicrf.h | 1 + client/src/cmdhw.c | 16 +++++++++------- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index aa5461d33..bca106931 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1563,6 +1563,7 @@ static void PacketReceived(PacketCommandNG *packet) { case CMD_HF_ISO14443A_SET_THRESHOLDS: { FpgaDownloadAndGo(FPGA_BITSTREAM_HF); FpgaSendCommand(FPGA_CMD_SET_EDGE_DETECT_THRESHOLD, (packet->data.asBytes[0] & 0x3f) | ((packet->data.asBytes[1] & 0x3f) << 6)); + LegicRfSetThreshold((uint32_t)packet->data.asBytes[2]); break; } case CMD_HF_ISO14443A_SNIFF: { diff --git a/armsrc/legicrf.c b/armsrc/legicrf.c index 42d511875..56ba2e7e8 100644 --- a/armsrc/legicrf.c +++ b/armsrc/legicrf.c @@ -64,7 +64,7 @@ static uint32_t last_frame_end; /* ts of last bit of previews rx or tx frame */ #define LEGIC_CARD_MEMSIZE 1024 /* The largest Legic Prime card is 1k */ #define WRITE_LOWERLIMIT 4 /* UID and MCC are not writable */ -#define INPUT_THRESHOLD 8 /* heuristically determined, lower values */ +static uint32_t input_threshold = 8; /* heuristically determined, lower values */ /* lead to detecting false ack during write */ //----------------------------------------------------------------------------- @@ -129,7 +129,7 @@ static bool rx_bit(void) { int32_t power = (MAX(ABS(sum_ci), ABS(sum_cq)) + (MIN(ABS(sum_ci), ABS(sum_cq)) >> 1)); // compare average (power / 8) to threshold - return ((power >> 3) > INPUT_THRESHOLD); + return ((power >> 3) > input_threshold); } //----------------------------------------------------------------------------- @@ -566,3 +566,8 @@ OUT: switch_off(); StopTicks(); } + +void LegicRfSetThreshold(uint32_t threshold) +{ + input_threshold = threshold; +} diff --git a/armsrc/legicrf.h b/armsrc/legicrf.h index 02f20b536..5932c450e 100644 --- a/armsrc/legicrf.h +++ b/armsrc/legicrf.h @@ -26,6 +26,7 @@ void LegicRfInfo(void); int LegicRfReaderEx(uint16_t offset, uint16_t len, uint8_t iv); void LegicRfReader(uint16_t offset, uint16_t len, uint8_t iv); void LegicRfWriter(uint16_t offset, uint16_t len, uint8_t iv, const uint8_t *data); +void LegicRfSetThreshold(uint32_t threshold); legic_card_select_t *getLegicCardInfo(void); #endif /* __LEGICRF_H */ diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index a9d90b6ca..36b497c06 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -747,20 +747,22 @@ static int CmdSetHFThreshold(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hw sethfthresh", - "Set thresholds in HF/14a mode.", - "hw sethfthresh -i 20 -t 7" + "Set thresholds in HF/14a and Legic mode.", + "hw sethfthresh -t 7 -i 20 -l 8" ); void *argtable[] = { arg_param_begin, - arg_int0("i", "high", "", "high threshold, used in sniff mode (def 20)"), - arg_int0("t", "thresh", "", "threshold, used in reader mode (def 7)"), + arg_int0("t", "thresh", "", "threshold, used in 14a reader mode (def 7)"), + arg_int0("i", "high", "", "high threshold, used in 14a sniff mode (def 20)"), + arg_int0("l", "legic", "", "threshold used in Legic mode (def 8)"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); - uint8_t params[2]; - params[1] = arg_get_int_def(ctx, 1, 20); - params[0] = arg_get_int_def(ctx, 2, 7); + uint8_t params[3]; + params[0] = arg_get_int_def(ctx, 1, 7); + params[1] = arg_get_int_def(ctx, 2, 20); + params[2] = arg_get_int_def(ctx, 3, 8); CLIParserFree(ctx); if ((params[0]<1) || (params[0]>63) || (params[1]<1) || (params[1]>63)) {