diff --git a/CHANGELOG.md b/CHANGELOG.md index b0713d10f..3c4f75566 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac ## [unreleased][unreleased] - Fixed `lf pcf7931 reader` - every read ran all 12 captures, no reply without a tag, field left on, block lost after a 16T0 PMC (@GiacoBot) +- Fixed `lf pcf7931 config` - out of range delay and offsets are refused instead of silently truncated (@GiacoBot) +- Fixed `lf pcf7931 write` - device froze on the 16-bit timer wrap, client always timed out, write right after a read failed; default init delay is now 17450 us (@GiacoBot) - Fixed `lf nexwatch clone` - `-m` defaults to 1 as its help says, values the frame can't carry and conflicting options are refused, `--psk2` writes block 4 (@matrix) - Fixed `lf nexwatch demod` - the raw id is the id field the tag sends instead of 0x40c00080 for every tag (@matrix) - Fixed `lf nexwatch demod` - a magic number of 0xFF is found (@matrix) diff --git a/armsrc/pcf7931.c b/armsrc/pcf7931.c index 5778728a2..c8ea70af0 100644 --- a/armsrc/pcf7931.c +++ b/armsrc/pcf7931.c @@ -555,6 +555,7 @@ void WritePCF7931( uint8_t password[7] = {pass1, pass2, pass3, pass4, pass5, pass6, pass7}; RealWritePCF7931(password, init_delay, offsetPulseWidth, offsetPulsePosition, address, byte, data, ledcontrol); + reply_ng(CMD_LF_PCF7931_WRITE, PM3_SUCCESS, NULL, 0); } @@ -562,14 +563,28 @@ void WritePCF7931( * @param tab : array of the data frame */ +// wait until `target` ticks since frame start; 32-bit accumulation survives 16-bit counter wraps +static void WaitUntilPCF7931(uint32_t *elapsed, uint16_t *last, uint32_t target) { + while (*elapsed < target) { + uint16_t now = GetPrecisionCounterRaw(); + *elapsed += (uint16_t)(now - *last); + *last = now; + } +} + void SendCmdPCF7931(uint32_t *tab, bool ledcontrol) { - uint16_t u = 0, tempo = 0; + uint16_t u = 0; if (g_dbglevel >= DBG_INFO) { Dbprintf("Sending data frame..."); } FpgaDownloadAndGo(FPGA_BITSTREAM_LF); + + // init_delay is counted from tag power-up, make sure it was reset + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + SpinDelay(50); + FpgaSendCommand(FPGA_CMD_SET_DIVISOR, LF_DIVISOR_125); //125kHz FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU); @@ -582,39 +597,26 @@ void SendCmdPCF7931(uint32_t *tab, bool ledcontrol) { tab[u] = (tab[u] * 3) / 2; } - // compensation for the counter overflow - // only one overflow should be possible. - for (u = 0; tab[u] != 0; ++u) - if (tab[u] > 0xFFFF) { - tab[u] -= 0xFFFF; - break; - } - // steal this pin from the SSP and use it to control the modulation gpio_fpga_mod_only_setup(); //initialization of the timer (free-running 1.5 MHz precision counter on TC0) StartPrecisionCounter(); - tempo = GetPrecisionCounter(); + uint32_t elapsed = 0; + uint16_t last = GetPrecisionCounterRaw(); for (u = 0; tab[u] != 0; u += 3) { // modulate antenna Gpio_SSC_DOUT_High(); - while ((uint32_t)tempo < tab[u]) { - tempo = GetPrecisionCounter(); - } + WaitUntilPCF7931(&elapsed, &last, tab[u]); // stop modulating antenna Gpio_SSC_DOUT_Low(); - while ((uint32_t)tempo < tab[u + 1]) { - tempo = GetPrecisionCounter(); - } + WaitUntilPCF7931(&elapsed, &last, tab[u + 1]); // modulate antenna Gpio_SSC_DOUT_High(); - while ((uint32_t)tempo < tab[u + 2]) { - tempo = GetPrecisionCounter(); - } + WaitUntilPCF7931(&elapsed, &last, tab[u + 2]); } if (ledcontrol) LED_A_OFF(); diff --git a/client/src/cmdlfpcf7931.c b/client/src/cmdlfpcf7931.c index de8d69a74..3c3250b11 100644 --- a/client/src/cmdlfpcf7931.c +++ b/client/src/cmdlfpcf7931.c @@ -25,7 +25,7 @@ static int CmdHelp(const char *Cmd); -#define PCF7931_DEFAULT_INITDELAY 17500 +#define PCF7931_DEFAULT_INITDELAY 17450 #define PCF7931_DEFAULT_OFFSET_WIDTH 0 #define PCF7931_DEFAULT_OFFSET_POSITION 0 @@ -131,6 +131,16 @@ static int CmdLFPCF7931Config(const char *Cmd) { return PM3_EINVARG; } + if (delay != -1 && (delay == 0 || delay > 0xFFFF)) { + PrintAndLogEx(ERR, "Delay must be between 1-65535 us"); + return PM3_EINVARG; + } + + if ((ow != 0xFFFF && (ow < -128 || ow > 127)) || (op != 0xFFFF && (op < -128 || op > 127))) { + PrintAndLogEx(ERR, "Offsets must be between -128 and 127 us"); + return PM3_EINVARG; + } + if (pwd_len) { memcpy(configPcf.Pwd, pwd, sizeof(configPcf.Pwd)); }