From 9573cb355c3a507bb1ad3a50f5f78f3eb36b97e6 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 2 Sep 2026 12:06:37 +0200 Subject: [PATCH] BWM-related fcts: make sure every usage of I2C starts with StartTicks() & I2C_init() --- armsrc/appmain.c | 3 +++ armsrc/bwm_charger.c | 12 +++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 3e44fb59e..2a47ca6cd 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -3886,6 +3886,7 @@ static void PacketReceived(PacketCommandNG *packet) { uint16_t mv = (packet->length >= 2) ? (uint16_t)(packet->data.asBytes[0] | (packet->data.asBytes[1] << 8)) : BWM_DEFAULT_VCHG_MV; + StartTicks(); I2C_init(true); uint16_t applied = bwm_charger_set_vchg(mv); reply_ng(CMD_PM5_BWM_SET_VCHG, applied ? PM3_SUCCESS : PM3_EFAILED, (uint8_t *)&applied, sizeof(applied)); @@ -3897,6 +3898,7 @@ static void PacketReceived(PacketCommandNG *packet) { uint16_t cap = (packet->length >= 2) ? (uint16_t)(packet->data.asBytes[0] | (packet->data.asBytes[1] << 8)) : BWM_DEFAULT_DESIGN_CAP_MAH; + StartTicks(); I2C_init(true); bool ok = bwm_gauge_provision_capacity(cap); reply_ng(CMD_PM5_BWM_SET_CAP, ok ? PM3_SUCCESS : PM3_EFAILED, (uint8_t *)&cap, sizeof(cap)); @@ -3907,6 +3909,7 @@ static void PacketReceived(PacketCommandNG *packet) { // Payload: 1 byte, non-zero = enable (default), zero = disable. // One-shot: reverts on the charger watchdog timeout (~160 s). bool enable = (packet->length >= 1) ? (packet->data.asBytes[0] != 0) : true; + StartTicks(); I2C_init(true); bool ok = bwm_charger_set_charge(enable); reply_ng(CMD_PM5_BWM_CHARGE_EN, ok ? PM3_SUCCESS : PM3_EFAILED, NULL, 0); diff --git a/armsrc/bwm_charger.c b/armsrc/bwm_charger.c index f98900b36..075026c8c 100644 --- a/armsrc/bwm_charger.c +++ b/armsrc/bwm_charger.c @@ -75,6 +75,7 @@ // cannot reroute VBUS to the charger input. It only ensures that when the charger // HAS input, its path is enabled. void bwm_charger_kick(void) { + StartTicks(); I2C_init(true); WaitMS(2); // let the bus settle @@ -124,6 +125,7 @@ void bwm_print_battery_status(void) { } DbpString(_CYAN_("Battery / BWM")); + StartTicks(); I2C_init(true); WaitMS(2); // let the bus settle @@ -327,11 +329,11 @@ uint16_t bwm_charger_set_vchg(uint16_t mv) { // Program Design Capacity (and matching Design Energy). Idempotent: returns true // without a config-update cycle if the value is already correct. bool bwm_gauge_provision_capacity(uint16_t cap_mah) { + uint16_t cur = 0; if (bq_read_design_cap(&cur) && cur == cap_mah) { return true; // already correct - do NOT run another CFGUPDATE cycle } - uint16_t energy_mwh = (uint16_t)(((uint32_t)cap_mah * 37) / 10); // ~3.7 V nominal // Enter CONFIG_UPDATE and wait for the gauge to acknowledge it. @@ -389,6 +391,7 @@ bool bwm_gauge_provision_capacity(uint16_t cap_mah) { // // Probe for the BWM charger over I2C and, if found, apply the charge configuration. void bwm_detect_and_init(void) { + StartTicks(); I2C_init(true); // Single bounded probe. Any non-ACK => no BWM fitted; skip everything. @@ -478,13 +481,8 @@ void bwm_lowbatt_check(void) { } last_tick = GetTickCount(); - // The I2C bit-bang uses WaitUS()/WaitTicks(), which busy-waits on the TMR5 - // free-running counter. A command handler run just before this poll (e.g. the - // LEGIC probe in `hf search`) can leave TMR5 stopped via StopTicks(); WaitTicks() - // would then loop forever and the device would appear to lock up (WDT_HIT() is a - // no-op on AT32). Restart the counter first, exactly as RgbLedSet() does before - // its own idle-loop I2C access. StartTicks(); + I2C_init(true); uint8_t sysstat = 0; if (I2C_BufferReadRaw(&sysstat, 1, BWM_CHG_REG_SYSSTAT, BWM_CHG_ADDR) <= 0) {