Introduce 'GetTickCountLabel' for detecting RTCC reconfigurations

This commit is contained in:
kormax
2026-03-31 20:42:15 +03:00
parent 75a49716a9
commit 6c4b995e21
3 changed files with 24 additions and 5 deletions
+9 -5
View File
@@ -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);
}
+14
View File
@@ -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
// -------------------------------------------------------------------------
+1
View File
@@ -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);