From 6c4b995e2100cfd4010a2988e0fc003403c4c3e0 Mon Sep 17 00:00:00 2001 From: kormax <3392860+kormax@users.noreply.github.com> Date: Tue, 31 Mar 2026 20:42:15 +0300 Subject: [PATCH] Introduce 'GetTickCountLabel' for detecting RTCC reconfigurations --- armsrc/appmain.c | 14 +++++++++----- common_arm/ticks.c | 14 ++++++++++++++ common_arm/ticks.h | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 343001eca..a7ec648c5 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -97,7 +97,6 @@ common_area_t g_common_area __attribute__((section(".commonarea"))); static int button_status = BUTTON_NO_CLICK; static bool allow_send_wtx = false; static uint32_t g_hf_field_activity_timeout_ms = 0; -static uint32_t g_last_activity_ms = 0; uint16_t g_tearoff_delay_us = 0; bool g_tearoff_enabled = false; uint8_t g_tearoff_skip = 0; @@ -1014,7 +1013,6 @@ static void PacketReceived(PacketCommandNG *packet) { uint32_t timeout_ms = 0; memcpy(&timeout_ms, packet->data.asBytes, sizeof(timeout_ms)); g_hf_field_activity_timeout_ms = timeout_ms; - g_last_activity_ms = GetTickCount(); reply_ng(CMD_SET_HF_FIELD_TIMEOUT, PM3_SUCCESS, NULL, 0); break; } @@ -3333,7 +3331,8 @@ void __attribute__((noreturn)) AppMain(void) { FpgaDownloadAndGo(FPGA_BITSTREAM_HF); StartTickCount(); - g_last_activity_ms = GetTickCount(); + uint32_t last_activity_tick = GetTickCount(); + uint32_t last_activity_label = GetTickCountLabel(); #ifdef WITH_LCD LCDInit(); @@ -3397,14 +3396,19 @@ void __attribute__((noreturn)) AppMain(void) { int ret = receive_ng(&rx); if (ret == PM3_SUCCESS) { PacketReceived(&rx); - g_last_activity_ms = GetTickCount(); + last_activity_label = GetTickCountLabel(); + last_activity_tick = GetTickCount(); } else if (ret != PM3_ENODATA) { Dbprintf("Error in frame reception: %d %s", ret, (ret == PM3_EIO) ? "PM3_EIO" : ""); // TODO if error, shall we resync ? } if (g_hf_field_activity_timeout_ms > 0 && g_hf_field_timeout_active) { - if (GetTickCountDelta(g_last_activity_ms) >= g_hf_field_activity_timeout_ms) { + uint32_t tickcount_label = GetTickCountLabel(); + if (tickcount_label != last_activity_label) { + last_activity_label = tickcount_label; + last_activity_tick = GetTickCount(); + } else if (GetTickCountDelta(last_activity_tick) >= g_hf_field_activity_timeout_ms) { if (g_dbglevel >= DBG_INFO) { Dbprintf("HF field auto-off: inactivity timeout (%u ms)", g_hf_field_activity_timeout_ms); } diff --git a/common_arm/ticks.c b/common_arm/ticks.c index 73182a11c..e609dfd83 100644 --- a/common_arm/ticks.c +++ b/common_arm/ticks.c @@ -97,7 +97,13 @@ void SpinDelay(int ms) { // SpinDelay(1000); // ti = GetTickCount() - ti; // Dbprintf("timer(1s): %d t=%d", ti, GetTickCount()); +// Increments whenever StartTickCount() reconfigures/resets RTTC. +// Callers can use this to detect that previously saved tick deltas are no longer valid. +static uint32_t g_tickcount_label = 0; + void StartTickCount(void) { + g_tickcount_label++; + // This timer is based on the slow clock. The slow clock frequency is between 22kHz and 40kHz. // We can determine the actual slow clock frequency by looking at the Main Clock Frequency Register. while ((AT91C_BASE_PMC->PMC_MCFR & AT91C_CKGR_MAINRDY) == 0); // Wait for MAINF value to become available... @@ -122,6 +128,14 @@ uint32_t RAMFUNC GetTickCountDelta(uint32_t start_ticks) { return (UINT32_MAX - start_ticks) + stop_ticks; } +/* +* Get current RTTC counter label. +* If counter config changes between calls, the value is incremented. +*/ +uint32_t GetTickCountLabel(void) { + return g_tickcount_label; +} + // ------------------------------------------------------------------------- // Timer for iso14443 commands. Uses ssp_clk from FPGA // ------------------------------------------------------------------------- diff --git a/common_arm/ticks.h b/common_arm/ticks.h index 67d8dceed..4ec730131 100644 --- a/common_arm/ticks.h +++ b/common_arm/ticks.h @@ -47,6 +47,7 @@ void SpinDelayUsPrecision(int us); // precision 0.6us , running for 43ms before void StartTickCount(void); uint32_t RAMFUNC GetTickCount(void); uint32_t RAMFUNC GetTickCountDelta(uint32_t start_ticks); +uint32_t GetTickCountLabel(void); void ResetUSClock(void); void SpinDelayCountUs(uint32_t us);