From f1cb4952086861f2e27c89fddf0d274269cff9d6 Mon Sep 17 00:00:00 2001 From: Tom Harkness Date: Sun, 27 Sep 2026 17:52:31 -0400 Subject: [PATCH] Fix CEP main-loop hang: start the ticks timer before I2C use, re-enable WITH_CEP cep_attach_poll() (and cep_init()) call I2C_BufferReadRaw()/I2C_init(true), which spin on the shared ticks timer via WaitUS()/WaitMS(). That timer is routinely stopped with StopTicks() by LF/HF protocol code once it's done with precision timing (em4x50.c, legicrf.c, hitag_common.c, lfsampling.c, etc). cep_attach_poll() runs unconditionally every 50ms in the main loop with no idea whether the timer is currently running - if its poll lands in a window where something just called StopTicks(), WaitTicks()'s `while (GetTicks() < target)` spins forever on a frozen counter, hanging the entire main loop irrecoverably. `hf search` cycles through many such probes, giving this poll many chances to land in that window, matching the reported "hf search hangs the PM5, needs a battery pull" regression - and since the poll runs regardless of Flipper attachment, merely compiling in WITH_CEP was enough to trigger it. bwm_lowbatt_poll() (armsrc/bwm_charger.c) is the same shape of periodic main-loop I2C poller and already guards against exactly this by calling StartTicks(); I2C_init(true); itself immediately before every I2C access. Mirror that convention in cep_attach_poll() and cep_init(). Re-enables WITH_CEP as the PM5 default (reverts f88880fc6's temporary disable) now that the underlying bug is fixed. SKIP_CEP=1 remains available for anyone who wants to opt out at build time regardless. Co-Authored-By: Claude Sonnet 5 --- armsrc/pm5_cep.c | 19 +++++++++++++++++++ common_arm/Makefile.hal | 4 +--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/armsrc/pm5_cep.c b/armsrc/pm5_cep.c index 8ac4e271a..8b3641cea 100644 --- a/armsrc/pm5_cep.c +++ b/armsrc/pm5_cep.c @@ -158,6 +158,11 @@ static void cep_spi_init(void) { void cep_init(void) { cep_usart_init(); cep_spi_init(); + // I2C_init(true) needs the ticks timer running (see cep_attach_poll()'s + // comment) - nothing in AppMain()'s boot sequence has started it yet at + // this point, so start it ourselves, matching bwm_charger_kick()'s + // identical boot-time I2C bring-up in armsrc/bwm_charger.c. + StartTicks(); I2C_init(true); } @@ -233,6 +238,20 @@ void cep_attach_poll(void) { } last_tick = GetTickCount(); + // The shared ticks timer (GetTicks()/WaitUS()/WaitMS(), which + // I2C_BufferReadRaw() spins on) is routinely stopped by LF/HF protocol + // code once it's done with precision timing (see e.g. em4x50.c, + // legicrf.c, hitag_common.c's StopTicks() calls). Landing here with the + // timer stopped turns WaitTicks()'s `while (GetTicks() < target)` into an + // infinite loop - a full, unrecoverable main-loop hang (this was the + // "hf search hangs the device" regression: it cycles through several + // such probes, giving this 50ms poll many chances to land in that + // window). Mirror bwm_lowbatt_poll()'s pattern (armsrc/bwm_charger.c) + // and (re)start the timer ourselves every time, exactly like every other + // periodic main-loop I2C poller in this codebase already does. + StartTicks(); + I2C_init(true); + uint8_t cc_ctrl_data; bool ret = I2C_BufferReadRaw(&cc_ctrl_data, 1, CEP_CC_STATUS_REG, CEP_CC_CONTROLLER_ADDR << 1); if (!ret) { diff --git a/common_arm/Makefile.hal b/common_arm/Makefile.hal index 99173f813..3dd901684 100644 --- a/common_arm/Makefile.hal +++ b/common_arm/Makefile.hal @@ -258,9 +258,7 @@ ifeq ($(SKIP_CEP),1) endif else ifeq ($(PLATFORM),PM5) -# Temporary measure as CEP seems to break things... -# PLATFORM_DEFS += -DWITH_CEP - $(warning Note: CEP is temporarely deactivated, until bugfix) + PLATFORM_DEFS += -DWITH_CEP endif endif