Add CMD_PM5_BWM_GET_BATTERY: compact structured battery telemetry
hw status's battery/BWM section (bwm_print_battery_status()) only ever formats its numbers into free-text Dbprintf lines, unusable by any embedded/non-terminal client. Add a new command that returns the same SoC/voltage/current/capacity/temp/health/fault numbers as one small packed struct via reply_ng(). Refactor the shared reads into bwm_read_battery_info() so hw status and the new command read the same values and can't drift apart - hw status keeps all its existing charger-detail lines (MainCtl, input limit, VIN_DPM, charge voltage/enable) unchanged, sourced from its own reads as before; only the fuel-gauge numbers and the two small charger-status reads the new struct needs are now shared. Motivated by wanting a Flipper Zero FAP battery-status test screen (separate app repo), but this command is generic and usable by any client, not CEP-specific. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ba4016d7e2
commit
5661f21d60
5 files changed
+105
-29
No files matched your search
@@ -9,6 +9,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac
|
||||
- Added `hw bwm wifipower` - turn the BWM WiFi fully off, or set its modem power-save type (none/min/max), persisted on the module (@Msprg)
|
||||
- Added `hw bwm powersave` - show/set the BWM (ESP32) power-save switch: DFS, light sleep and slow advertising after 30 s, or the stock always-on behaviour, persisted on the module (@Msprg)
|
||||
- Added `hw powersave` - PM5 low-power idle, on by default: between commands the core drops to 48 MHz with the PLL off, the FPGA clock stopped and the CPU halted (WFI); USB now runs crystal-less off HICK. Ported from the Fantasi firmware (@Msprg)
|
||||
- Added `CMD_PM5_BWM_GET_BATTERY` on PM5 - compact structured battery/charger telemetry (SoC, voltage, current, remaining/full capacity, temp, health, charger fault/status) for clients that want the raw numbers instead of `hw status`'s free-text lines; `bwm_print_battery_status()` now sources its numbers from the same shared read so the two can't drift (@TomHarkness)
|
||||
- Fixed `lf t55xx dump -p` - reuses the password already confirmed by `lf t55xx detect` (@iceman1001)
|
||||
- Fixed `lf t55xx p1detect`, `lf search` - T5577 clones with a non-Atmel manufacturer byte now recognised (@iceman1001)
|
||||
- Added Flipper Zero link on PM5's Type-C extended port (handshake + NG frames over SPI) - on by default, `PLATFORM_EXTRAS=SKIP_CEP` to disable, independent so it can run alongside BWM; RF-dependent functionality still needs the FPGA side, tracked in #3667 (@TomHarkness)
|
||||
|
||||
@@ -4614,6 +4614,12 @@ static void PacketReceived(PacketCommandNG *packet) {
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case CMD_PM5_BWM_GET_BATTERY: {
|
||||
bwm_battery_info_t info;
|
||||
bwm_read_battery_info(&info);
|
||||
reply_ng(CMD_PM5_BWM_GET_BATTERY, PM3_SUCCESS, (uint8_t *)&info, sizeof(info));
|
||||
break;
|
||||
}
|
||||
#endif // WITH_BWM_STATUS
|
||||
case CMD_PM5_POWERSAVE: {
|
||||
// Payload: 1 byte, non-zero = enable (the default), zero = disable.
|
||||
|
||||
+71
-29
@@ -195,35 +195,31 @@ void bwm_print_battery_status(void) {
|
||||
}
|
||||
}
|
||||
|
||||
// --- fuel gauge (BQ27427) ---
|
||||
uint16_t soc = 0, mv = 0, rem = 0, temp = 0, raw_i = 0;
|
||||
if (bwm_gauge_read16(BWM_GAUGE_SOC, &soc) && bwm_gauge_read16(BWM_GAUGE_VOLTAGE, &mv)) {
|
||||
bwm_gauge_read16(BWM_GAUGE_REMCAP, &rem);
|
||||
bwm_gauge_read16(BWM_GAUGE_TEMP, &temp);
|
||||
bwm_gauge_read16(BWM_GAUGE_CURRENT, &raw_i);
|
||||
|
||||
int16_t cur = (int16_t)raw_i; // +charge / -discharge (verify polarity on hw)
|
||||
int tempC10 = (int)temp - 2732; // 0.1 K -> 0.1 C
|
||||
int tabs = (tempC10 < 0) ? -tempC10 : tempC10;
|
||||
|
||||
Dbprintf(" Battery SoC......... %u %%", soc);
|
||||
Dbprintf(" Battery voltage..... %u mV", mv);
|
||||
Dbprintf(" Battery current..... %d mA %s", cur,
|
||||
(cur > 5) ? _GREEN_("(charging)") :
|
||||
(cur < -5) ? _YELLOW_("(discharging)") : "(idle)");
|
||||
Dbprintf(" Remaining capacity.. %u mAh", rem);
|
||||
// --- fuel gauge (BQ27427) --- shared with bwm_read_battery_info() below,
|
||||
// so the numbers hw status prints and CMD_PM5_BWM_GET_BATTERY returns
|
||||
// can't drift apart.
|
||||
bwm_battery_info_t info;
|
||||
bwm_read_battery_info(&info);
|
||||
if (info.gauge_ok) {
|
||||
Dbprintf(" Battery SoC......... %u %%", info.soc_pct);
|
||||
Dbprintf(" Battery voltage..... %u mV", info.voltage_mv);
|
||||
Dbprintf(" Battery current..... %d mA %s", info.current_ma,
|
||||
(info.current_ma > 5) ? _GREEN_("(charging)") :
|
||||
(info.current_ma < -5) ? _YELLOW_("(discharging)") : "(idle)");
|
||||
Dbprintf(" Remaining capacity.. %u mAh", info.remaining_mah);
|
||||
// Measured drain / projected runtime: when running on battery the gauge
|
||||
// Current is negative (discharging); |cur| is the real system draw, so
|
||||
// remaining runtime ~= RemainingCapacity / draw. Only meaningful while
|
||||
// discharging - on charge or idle there is no drain figure to report.
|
||||
// (Projection is only as good as RemCap - provision Design Capacity first.)
|
||||
if (cur < -5) {
|
||||
uint16_t draw = (uint16_t)(-cur); // mA drawn from the battery
|
||||
uint32_t mins = ((uint32_t)rem * 60) / draw; // RemCap/draw -> hours, *60 -> mins
|
||||
if (info.current_ma < -5) {
|
||||
uint16_t draw = (uint16_t)(-info.current_ma); // mA drawn from the battery
|
||||
uint32_t mins = ((uint32_t)info.remaining_mah * 60) / draw; // RemCap/draw -> hours, *60 -> mins
|
||||
Dbprintf(" Battery drain....... %u mA (approx %u h %u min left)",
|
||||
draw, (unsigned)(mins / 60), (unsigned)(mins % 60));
|
||||
}
|
||||
Dbprintf(" Temp (gauge)........ %d.%d C", tempC10 / 10, tabs % 10);
|
||||
int tabs = (info.temp_c10 < 0) ? -info.temp_c10 : info.temp_c10;
|
||||
Dbprintf(" Temp (gauge)........ %d.%d C", info.temp_c10 / 10, tabs % 10);
|
||||
|
||||
// Battery health: FullChargeCapacity (0x0E) vs the gauge's programmed Design
|
||||
// Capacity. FCC is the gauge's learned present full capacity; health = FCC/design.
|
||||
@@ -231,20 +227,66 @@ void bwm_print_battery_status(void) {
|
||||
// (a full charge/discharge). Before that it is an unconverged estimate. If Design
|
||||
// Capacity was never provisioned (hw bwm setcap), the ratio is against the gauge
|
||||
// default, not the fitted cell - so it can read wildly wrong.
|
||||
uint16_t fcc = 0, design = 0;
|
||||
if (bwm_gauge_read16(BWM_GAUGE_FCC, &fcc) && fcc > 0) {
|
||||
if (bq_read_design_cap(&design) == false || design == 0) {
|
||||
design = BWM_DEFAULT_DESIGN_CAP_MAH; // fall back to the fitted-cell rating
|
||||
}
|
||||
unsigned health = (unsigned)(((uint32_t)fcc * 100) / design);
|
||||
Dbprintf(" Full charge cap..... %u mAh (design %u)", fcc, design);
|
||||
Dbprintf(" Battery health...... %u", health);
|
||||
if (info.full_charge_mah > 0) {
|
||||
Dbprintf(" Full charge cap..... %u mAh (design %u)", info.full_charge_mah, info.design_cap_mah);
|
||||
Dbprintf(" Battery health...... %u", info.health_pct);
|
||||
}
|
||||
} else {
|
||||
Dbprintf(" Fuel gauge.......... " _YELLOW_("not responding") " (BQ27427 absent or I2C down)");
|
||||
}
|
||||
}
|
||||
|
||||
// See bwm_charger.h. Shares the fuel-gauge reads with bwm_print_battery_status()
|
||||
// above; only adds two small charger-side reads (fault, charge status) that
|
||||
// print doesn't already source from elsewhere.
|
||||
bool bwm_read_battery_info(bwm_battery_info_t *out) {
|
||||
bwm_battery_info_t info = {0};
|
||||
info.bwm_present = g_bwm_present;
|
||||
|
||||
if (g_bwm_present) {
|
||||
StartTicks();
|
||||
I2C_init(true);
|
||||
WaitMS(2); // let the bus settle
|
||||
|
||||
// Charger fault (REG09): read-on-clear, latched history on the 1st read,
|
||||
// live state on the 2nd - see bwm_print_battery_status()'s fuller comment.
|
||||
uint8_t f1 = 0, fault = 0, sysstat = 0;
|
||||
I2C_BufferReadRaw(&f1, 1, BWM_CHG_REG_FAULT, BWM_CHG_ADDR);
|
||||
if (I2C_BufferReadRaw(&fault, 1, BWM_CHG_REG_FAULT, BWM_CHG_ADDR) > 0) {
|
||||
info.charger_fault = fault & 0x3F;
|
||||
}
|
||||
if (I2C_BufferReadRaw(&sysstat, 1, BWM_CHG_REG_SYSSTAT, BWM_CHG_ADDR) > 0) {
|
||||
info.charge_status = (sysstat >> 3) & 0x03;
|
||||
}
|
||||
|
||||
uint16_t soc = 0, mv = 0, rem = 0, temp = 0, raw_i = 0;
|
||||
info.gauge_ok = bwm_gauge_read16(BWM_GAUGE_SOC, &soc) && bwm_gauge_read16(BWM_GAUGE_VOLTAGE, &mv);
|
||||
if (info.gauge_ok) {
|
||||
bwm_gauge_read16(BWM_GAUGE_REMCAP, &rem);
|
||||
bwm_gauge_read16(BWM_GAUGE_TEMP, &temp);
|
||||
bwm_gauge_read16(BWM_GAUGE_CURRENT, &raw_i);
|
||||
info.soc_pct = soc;
|
||||
info.voltage_mv = mv;
|
||||
info.current_ma = (int16_t)raw_i;
|
||||
info.remaining_mah = rem;
|
||||
info.temp_c10 = (int16_t)((int)temp - 2732); // 0.1 K -> 0.1 C
|
||||
|
||||
uint16_t fcc = 0, design = 0;
|
||||
if (bwm_gauge_read16(BWM_GAUGE_FCC, &fcc) && fcc > 0) {
|
||||
if (bq_read_design_cap(&design) == false || design == 0) {
|
||||
design = BWM_DEFAULT_DESIGN_CAP_MAH;
|
||||
}
|
||||
info.full_charge_mah = fcc;
|
||||
info.design_cap_mah = design;
|
||||
info.health_pct = (uint8_t)(((uint32_t)fcc * 100) / design);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*out = info;
|
||||
return g_bwm_present;
|
||||
}
|
||||
|
||||
// --- BQ27427 provisioning: set Design Capacity for the fitted cell -------------
|
||||
// One-time. The gauge ships with a ~1000+ mAh default profile, so RemainingCapacity
|
||||
// reads wrong for the fitted pack until Design Capacity is programmed. Invoked by the
|
||||
|
||||
@@ -51,6 +51,29 @@ void bwm_detect_and_init(void);
|
||||
// detected by bwm_detect_and_init().
|
||||
void bwm_print_battery_status(void);
|
||||
|
||||
// Compact battery telemetry for CMD_PM5_BWM_GET_BATTERY - a machine-readable
|
||||
// subset of what bwm_print_battery_status() prints (same underlying reads,
|
||||
// so the two can't drift on the numbers they share). All fields zeroed if
|
||||
// bwm_present is false or gauge_ok is false.
|
||||
typedef struct {
|
||||
bool bwm_present;
|
||||
bool gauge_ok;
|
||||
uint16_t soc_pct;
|
||||
uint16_t voltage_mv;
|
||||
int16_t current_ma; // +charge / -discharge
|
||||
uint16_t remaining_mah;
|
||||
uint16_t full_charge_mah;
|
||||
uint16_t design_cap_mah;
|
||||
int16_t temp_c10; // tenths of a degree C
|
||||
uint8_t charger_fault; // 0 = none, else AW32001E REG09 bits masked 0x3F
|
||||
uint8_t charge_status; // 0=not charging,1=pre-charge,2=charging,3=done
|
||||
uint8_t health_pct;
|
||||
} PACKED bwm_battery_info_t;
|
||||
|
||||
// Returns false (all fields zeroed) if no BWM was detected. A false gauge_ok
|
||||
// with bwm_present true means the charger ACKed but the fuel gauge didn't.
|
||||
bool bwm_read_battery_info(bwm_battery_info_t *out);
|
||||
|
||||
// One-time BQ27427 Design Capacity provisioning (CMD_PM5_BWM_SET_CAP /
|
||||
// `hw bwm setcap`). Idempotent - returns true without a CFGUPDATE cycle if the
|
||||
// value is already correct. Assumes the gauge is UNSEALED (factory default).
|
||||
|
||||
@@ -987,6 +987,10 @@ typedef struct {
|
||||
uint8_t bonded_count;
|
||||
uint8_t bonded[BWM_BLE_BONDED_MAX][7]; // addr[6] + type, first bonded_count valid
|
||||
} PACKED bwm_ble_status_t;
|
||||
// PM5, read compact BWM battery telemetry (bwm_battery_info_t). Used by `hw status`'s
|
||||
// underlying data source and by clients (e.g. the Flipper Zero FAP) that want the raw
|
||||
// numbers instead of hw status's free-text Dbprintf lines.
|
||||
#define CMD_PM5_BWM_GET_BATTERY 0x0184
|
||||
#define BWM_OTA_ACTION_BEGIN 0x00
|
||||
#define BWM_OTA_ACTION_WRITE 0x01
|
||||
#define BWM_OTA_ACTION_END 0x02
|
||||
|
||||
Reference in new issue
Block a user