Make demodulation threshold for Legic configurable

This adds a new parameter to the "hw sethfthresh" command.
This commit is contained in:
Christian Zietz 2024-02-01 18:33:23 +01:00
commit 43b257ddb4
4 changed files with 18 additions and 9 deletions

View file

@ -1563,6 +1563,7 @@ static void PacketReceived(PacketCommandNG *packet) {
case CMD_HF_ISO14443A_SET_THRESHOLDS: { case CMD_HF_ISO14443A_SET_THRESHOLDS: {
FpgaDownloadAndGo(FPGA_BITSTREAM_HF); FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
FpgaSendCommand(FPGA_CMD_SET_EDGE_DETECT_THRESHOLD, (packet->data.asBytes[0] & 0x3f) | ((packet->data.asBytes[1] & 0x3f) << 6)); 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; break;
} }
case CMD_HF_ISO14443A_SNIFF: { case CMD_HF_ISO14443A_SNIFF: {

View file

@ -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 LEGIC_CARD_MEMSIZE 1024 /* The largest Legic Prime card is 1k */
#define WRITE_LOWERLIMIT 4 /* UID and MCC are not writable */ #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 */ /* 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)); int32_t power = (MAX(ABS(sum_ci), ABS(sum_cq)) + (MIN(ABS(sum_ci), ABS(sum_cq)) >> 1));
// compare average (power / 8) to threshold // compare average (power / 8) to threshold
return ((power >> 3) > INPUT_THRESHOLD); return ((power >> 3) > input_threshold);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -566,3 +566,8 @@ OUT:
switch_off(); switch_off();
StopTicks(); StopTicks();
} }
void LegicRfSetThreshold(uint32_t threshold)
{
input_threshold = threshold;
}

View file

@ -26,6 +26,7 @@ void LegicRfInfo(void);
int LegicRfReaderEx(uint16_t offset, uint16_t len, uint8_t iv); int LegicRfReaderEx(uint16_t offset, uint16_t len, uint8_t iv);
void LegicRfReader(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 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); legic_card_select_t *getLegicCardInfo(void);
#endif /* __LEGICRF_H */ #endif /* __LEGICRF_H */

View file

@ -747,20 +747,22 @@ static int CmdSetHFThreshold(const char *Cmd) {
CLIParserContext *ctx; CLIParserContext *ctx;
CLIParserInit(&ctx, "hw sethfthresh", CLIParserInit(&ctx, "hw sethfthresh",
"Set thresholds in HF/14a mode.", "Set thresholds in HF/14a and Legic mode.",
"hw sethfthresh -i 20 -t 7" "hw sethfthresh -t 7 -i 20 -l 8"
); );
void *argtable[] = { void *argtable[] = {
arg_param_begin, arg_param_begin,
arg_int0("i", "high", "<dec>", "high threshold, used in sniff mode (def 20)"), arg_int0("t", "thresh", "<dec>", "threshold, used in 14a reader mode (def 7)"),
arg_int0("t", "thresh", "<dec>", "threshold, used in reader mode (def 7)"), arg_int0("i", "high", "<dec>", "high threshold, used in 14a sniff mode (def 20)"),
arg_int0("l", "legic", "<dec>", "threshold used in Legic mode (def 8)"),
arg_param_end arg_param_end
}; };
CLIExecWithReturn(ctx, Cmd, argtable, true); CLIExecWithReturn(ctx, Cmd, argtable, true);
uint8_t params[2]; uint8_t params[3];
params[1] = arg_get_int_def(ctx, 1, 20); params[0] = arg_get_int_def(ctx, 1, 7);
params[0] = arg_get_int_def(ctx, 2, 7); params[1] = arg_get_int_def(ctx, 2, 20);
params[2] = arg_get_int_def(ctx, 3, 8);
CLIParserFree(ctx); CLIParserFree(ctx);
if ((params[0]<1) || (params[0]>63) || (params[1]<1) || (params[1]>63)) { if ((params[0]<1) || (params[0]>63) || (params[1]<1) || (params[1]>63)) {