From 7377dae83dd64f6dbbd3675ae00df18120679480 Mon Sep 17 00:00:00 2001 From: Niel Nielsen Date: Sun, 23 Aug 2026 14:17:50 +0200 Subject: [PATCH] Add files via upload Signed-off-by: Niel Nielsen --- armsrc/appmain.c | 61 +++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 25283c60f..7d184a3cf 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -171,7 +171,7 @@ static void print_pm5_battery_status(void) { } else { Dbprintf(" Charger MainCtl..... 0x%02x %s", mainctl, (mainctl & BWM_CHG_FET_DIS) ? _RED_("FET_DIS (VMIX off / shipping)") - : _GREEN_("power path enabled")); + : _GREEN_("power path enabled")); I2C_BufferReadRaw(&f1, 1, BWM_CHG_REG_FAULT, BWM_CHG_ADDR); // 1st = latched history I2C_BufferReadRaw(&fault, 1, BWM_CHG_REG_FAULT, BWM_CHG_ADDR); // 2nd = current state @@ -242,9 +242,12 @@ static bool bq_flags(uint16_t *f) { } static bool bq_select_state_block(void) { uint8_t v; - v = 0x00; if (!I2C_BufferWrite(&v, 1, 0x61, BWM_GAUGE_ADDR)) return false; // BlockDataControl - v = 0x52; if (!I2C_BufferWrite(&v, 1, 0x3E, BWM_GAUGE_ADDR)) return false; // DataClass = 82 (State) - v = 0x00; if (!I2C_BufferWrite(&v, 1, 0x3F, BWM_GAUGE_ADDR)) return false; // DataBlock = 0 + v = 0x00; + if (!I2C_BufferWrite(&v, 1, 0x61, BWM_GAUGE_ADDR)) return false; // BlockDataControl + v = 0x52; + if (!I2C_BufferWrite(&v, 1, 0x3E, BWM_GAUGE_ADDR)) return false; // DataClass = 82 (State) + v = 0x00; + if (!I2C_BufferWrite(&v, 1, 0x3F, BWM_GAUGE_ADDR)) return false; // DataBlock = 0 WaitMS(5); return true; } @@ -266,19 +269,33 @@ static bool bwm_gauge_provision_capacity(uint16_t cap_mah) { uint16_t energy_mwh = (uint16_t)(((uint32_t)cap_mah * 37) / 10); // ~3.7 V nominal - if (!bq_control(0x0013)) return false; // SET_CFGUPDATE - uint16_t flags = 0; int tries = 0; - do { WaitMS(50); if (!bq_flags(&flags)) return false; } - while (((flags & 0x0010) == 0) && (++tries < 40)); // wait CFGUPDATE (Flags bit4), ~2s - if ((flags & 0x0010) == 0) return false; + // Enter CONFIG_UPDATE and wait for the gauge to acknowledge it. + if (bq_control(0x0013) == false) { // SET_CFGUPDATE + return false; + } + + uint16_t flags = 0; + int tries = 0; + do { + WaitMS(50); + if (bq_flags(&flags) == false) { + return false; + } + } while (((flags & 0x0010) == 0) && (++tries < 40)); // wait for CFGUPDATE (Flags bit 4), ~2 s + + if ((flags & 0x0010) == 0) { + return false; + } if (!bq_select_state_block()) return false; uint8_t blk[32] = {0}; if (I2C_BufferReadRaw(blk, 32, 0x40, BWM_GAUGE_ADDR) <= 0) return false; - blk[6] = (uint8_t)(cap_mah >> 8); blk[7] = (uint8_t)(cap_mah & 0xFF); // DesignCapacity - blk[8] = (uint8_t)(energy_mwh >> 8); blk[9] = (uint8_t)(energy_mwh & 0xFF); // DesignEnergy + blk[6] = (uint8_t)(cap_mah >> 8); + blk[7] = (uint8_t)(cap_mah & 0xFF); // DesignCapacity + blk[8] = (uint8_t)(energy_mwh >> 8); + blk[9] = (uint8_t)(energy_mwh & 0xFF); // DesignEnergy I2C_BufferWrite(&blk[6], 2, 0x46, BWM_GAUGE_ADDR); I2C_BufferWrite(&blk[8], 2, 0x48, BWM_GAUGE_ADDR); @@ -1260,7 +1277,7 @@ static bool QCTestPM5(uint8_t *failed_item) { gpio_init_struct.gpio_pins = BEEPER_EN_GPIO_PIN; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init(BEEPER_EN_GPIO, &gpio_init_struct); - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, FALSE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, FALSE); // 蜂鸣器调制脚 gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_pins = BEEPER_MOD_GPIO_PIN; @@ -1298,9 +1315,9 @@ static bool QCTestPM5(uint8_t *failed_item) { LED_A_ON(); BEEPER_MOD_TMR->pr = 999; - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, TRUE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, TRUE); SpinDelay(20); - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, FALSE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, FALSE); SpinDelay(200); LED_A_OFF(); @@ -1314,9 +1331,9 @@ static bool QCTestPM5(uint8_t *failed_item) { LED_B_ON(); BEEPER_MOD_TMR->pr = 1100; tmr_channel_value_set(BEEPER_MOD_TMR, BEEPER_MOD_TMR_CH, 550); - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, TRUE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, TRUE); SpinDelay(20); - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, FALSE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, FALSE); SpinDelay(200); LED_B_OFF(); @@ -1330,9 +1347,9 @@ static bool QCTestPM5(uint8_t *failed_item) { LED_C_ON(); BEEPER_MOD_TMR->pr = 1200; tmr_channel_value_set(BEEPER_MOD_TMR, BEEPER_MOD_TMR_CH, 600); - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, TRUE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, TRUE); SpinDelay(20); - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, FALSE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, FALSE); SpinDelay(200); LED_C_OFF(); @@ -1346,9 +1363,9 @@ static bool QCTestPM5(uint8_t *failed_item) { LED_D_ON(); BEEPER_MOD_TMR->pr = 1300; tmr_channel_value_set(BEEPER_MOD_TMR, BEEPER_MOD_TMR_CH, 650); - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, TRUE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, TRUE); SpinDelay(20); - gpio_bits_write(BEEPER_EN_GPIO,BEEPER_EN_GPIO_PIN, FALSE); + gpio_bits_write(BEEPER_EN_GPIO, BEEPER_EN_GPIO_PIN, FALSE); SpinDelay(200); LED_D_OFF(); } @@ -3752,7 +3769,7 @@ static void PacketReceived(PacketCommandNG *packet) { // Response if (res == PM3_EFAILED) { uint32_t plat_status = FpgaConfigPlatformStatus(); // Return status code of platform when res is PM3_EFAILED - reply_ng(packet->cmd, res, (uint8_t*)&plat_status, sizeof(plat_status)); + reply_ng(packet->cmd, res, (uint8_t *)&plat_status, sizeof(plat_status)); } else { reply_ng(packet->cmd, res, NULL, 0); } @@ -3840,7 +3857,7 @@ static void PacketReceived(PacketCommandNG *packet) { #endif case CMD_MAIN_CHIP_UNIQUEID: { uint8_t size = 0; - uint8_t* uid = GetChipUniqueId(&size); + uint8_t *uid = GetChipUniqueId(&size); reply_ng(CMD_MAIN_CHIP_UNIQUEID, PM3_SUCCESS, uid, size); break; }