Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/fw/drivers/pmic/npm1300.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* SPDX-FileCopyrightText: 2026 Core Devices LLC */
/* SPDX-License-Identifier: Apache-2.0 */

/* Because nPM1300 also has the battery monitor, we implement both the
* pmic_* and the battery_* API here. */

Expand Down Expand Up @@ -370,8 +373,6 @@ bool pmic_init(void) {

ok &= prv_write_register(PmicRegisters_BCHARGER_BCHGVBATLOWCHARGE, 1);

ok &= prv_write_register(PmicRegisters_BCHARGER_BCHGENABLESET, 1);

prv_configure_interrupts();

if (!ok) {
Expand Down
30 changes: 15 additions & 15 deletions src/fw/services/common/battery/nrf_fuel_gauge/battery_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "board/board.h"
#include "drivers/battery.h"
#include "drivers/pmic.h"
#include "drivers/rtc.h"
#include "kernel/events.h"
#include "services/common/analytics/analytics.h"
Expand All @@ -14,7 +15,6 @@
#include "syscall/syscall_internal.h"
#include "system/logging.h"
#include "system/passert.h"
#include "util/math.h"
#include "util/ratio.h"

#ifndef RECOVERY_FW
Expand All @@ -35,9 +35,6 @@
// TODO: Adjust sample rate based on activity periods once we have good
// power consumption profiles
#define BATTERY_SAMPLE_RATE_S 1
// Use fake current data for the first N seconds to improve model accuracy
#define FAKE_CURRENT_DURATION_S 10
#define FAKE_CURRENT_UA 100

#define LOG_MIN_SEC 30

Expand All @@ -58,14 +55,14 @@ static TimerID s_periodic_timer_id = TIMER_INVALID_ID;

static BatteryChargeStatus s_last_chg_status;
static uint64_t prv_ref_time;
static uint32_t s_sample_count;
static int32_t s_last_voltage_mv;
static int32_t s_last_temp_mc;
static int32_t s_analytics_last_voltage_mv;
static uint8_t s_analytics_last_pct;
static uint32_t s_last_tte;
static uint32_t s_last_ttf;
static RtcTicks s_last_log;
static bool s_charger_enabled;

#if FUEL_GAUGE_STATEFUL
#define FUEL_GAUGE_SAVE_INTERVAL_S 300
Expand Down Expand Up @@ -138,6 +135,8 @@ static bool prv_load_state(void *state, size_t size) {
sizeof(FUEL_GAUGE_STATE_KEY), state, size);
settings_file_close(&file);

PBL_LOG_DBG("Fuel gauge state loaded");

return (ret == S_SUCCESS);
}

Expand All @@ -164,6 +163,8 @@ static void prv_save_state(void) {

if (ret != S_SUCCESS) {
PBL_LOG_ERR("Failed to save fuel gauge state");
} else {
PBL_LOG_DBG("Fuel gauge state saved");
}
}
#endif // MANUFACTURING_FW
Expand Down Expand Up @@ -264,14 +265,7 @@ static void prv_update_state(void *force_update) {
delta = (now - prv_ref_time) / RTC_TICKS_HZ;
prv_ref_time = now;

// Use fake current data for the first N seconds to improve model accuracy
int32_t current_ua = constants.i_ua;
if (s_sample_count < DIVIDE_CEIL(FAKE_CURRENT_DURATION_S, BATTERY_SAMPLE_RATE_S)) {
current_ua = FAKE_CURRENT_UA;
s_sample_count++;
}

pct = nrf_fuel_gauge_process((float)constants.v_mv / 1000.0f, (float)current_ua / 1000000.0f,
pct = nrf_fuel_gauge_process((float)constants.v_mv / 1000.0f, (float)constants.i_ua / 1000000.0f,
(float)constants.t_mc / 1000.0f, (float)delta, NULL);

pct_int = (uint8_t)ceilf(pct);
Expand Down Expand Up @@ -302,7 +296,7 @@ static void prv_update_state(void *force_update) {
}

#if FUEL_GAUGE_STATEFUL
if (++s_save_counter >= FUEL_GAUGE_SAVE_INTERVAL_S) {
if (update || (++s_save_counter >= FUEL_GAUGE_SAVE_INTERVAL_S)) {
s_save_counter = 0;
prv_save_state();
}
Expand All @@ -322,6 +316,12 @@ static void prv_update_state(void *force_update) {
prv_battery_state_put_change_event(s_last_battery_charge_state);
s_last_log = now;
}

// Enable battery charging after fuel gauge state has been updated for the first time
if (!s_charger_enabled) {
s_charger_enabled = true;
pmic_set_charger_state(true);
}
}

static void prv_update_callback(void *data) {
Expand Down Expand Up @@ -354,7 +354,7 @@ void battery_state_init(void) {
PBL_ASSERTN(ret == 0);

parameters.v0 = (float)constants.v_mv / 1000.0f;
parameters.i0 = (float)FAKE_CURRENT_UA / 1000000.0f;
parameters.i0 = (float)constants.i_ua / 1000000.0f;
parameters.t0 = (float)constants.t_mc / 1000.0f;

s_last_voltage_mv = constants.v_mv;
Expand Down
Loading