nPM1300 fuel gauge SoC

Hi

Setup: nPM1300, nRF54L15-based board, SDK 2.9.2

I am trying to figure out how to use the nPM1300.


So far, I have not succeeded. Quite frequently, after power cycling my device, the SoC read from the fuel gauge, after rebooting, is quite different (e.g. 20% difference) from the SoC before power down.


My code calls nrf_fuel_gauge_process() every 100 ms.

ret = read_sensors(charger, &voltage, &current, &temp, &chg_status);
:
soc = nrf_fuel_gauge_process(voltage, current, temp, delta, charging_active, NULL);


Some of the things I have tried…


ONE


Simple usage.

Provide no further information to the fuel gauge.

On power up, initialise the fuel gauge straight away.

ret = read_sensors(charger, &p.v0, &p.i0, &p.t0, &chg_status);
:
sensor_channel_get(charger, SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT, &v);
:
nrf_fuel_gauge_init(&p, NULL);


TWO


Same as ONE, except wait 30 seconds before initialising


THREE


When the SoC reaches 100%, re-initialise the fuel gauge:


int ret = read_sensors(charger, &p.v0, &p.i0, &p.t0, &chg_status);
:
nrf_fuel_gauge_init(&p, NULL);

FOUR


After a few seconds after powering up, read the V,I,T


ret = read_sensors(charger, &p.v0, &p.i0, &p.t0, &chg_status);


then initialise the fuel gauge using these values


FIVE

Save V,I,T values to Flash on a regular basis, and on power-up, restore these  saved values and use them to initialise the fuel gauge.

SIX


Maintain an array of saved V,I,T values (e.g. size 120), and save the average of this circular array to Flash every so often (e.g. every 2 minutes for test purposes).
On power-up, restore these saved averages, and use them to initialise the fuel gauge.

FURTHER INFORMATION

In all cases, there is often quite a large drop or rise in the SoC after power cycling the device.

Here is a sample of the output while charging / discharging (from two separate logs).
One thing of note: the voltage and current fluctuate quite a bit during discharge.

Charging:

[00:13:31.976,257] : delta: 0.100000, V: 4.233, I: -0.019, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2122, Chging 1, Conn 1, ChgSt 17
[00:13:32.076,293] : delta: 0.100000, V: 4.233, I: -0.017, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2095, Chging 1, Conn 1, ChgSt 17
[00:13:32.176,239] : delta: 0.100000, V: 4.233, I: -0.018, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2107, Chging 1, Conn 1, ChgSt 17
[00:13:32.276,275] : delta: 0.100000, V: 4.233, I: -0.017, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2092, Chging 1, Conn 1, ChgSt 17
[00:13:32.376,312] : delta: 0.100000, V: 4.233, I: -0.017, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2091, Chging 1, Conn 1, ChgSt 17
[00:13:32.476,379] : delta: 0.101000, V: 4.233, I: -0.019, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2115, Chging 1, Conn 1, ChgSt 17
[00:13:32.576,293] : delta: 0.099000, V: 4.233, I: -0.018, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2101, Chging 1, Conn 1, ChgSt 17
[00:13:32.676,300] : delta: 0.100000, V: 4.233, I: -0.017, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2087, Chging 1, Conn 1, ChgSt 17
[00:13:32.776,367] : delta: 0.101000, V: 4.233, I: -0.017, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2085, Chging 1, Conn 1, ChgSt 17
[00:13:32.876,312] : delta: 0.099000, V: 4.233, I: -0.019, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2110, Chging 1, Conn 1, ChgSt 17
[00:13:32.976,348] : delta: 0.101000, V: 4.233, I: -0.018, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2096, Chging 1, Conn 1, ChgSt 17
[00:13:33.076,385] : delta: 0.100000, V: 4.233, I: -0.017, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2081, Chging 1, Conn 1, ChgSt 17
[00:13:33.176,330] : delta: 0.099000, V: 4.228, I: -0.018, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2093, Chging 1, Conn 1, ChgSt 17
[00:13:33.276,336] : delta: 0.100000, V: 4.233, I: -0.017, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2079, Chging 1, Conn 1, ChgSt 17
[00:13:33.376,403] : delta: 0.101000, V: 4.233, I: -0.019, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2103, Chging 1, Conn 1, ChgSt 17
[00:13:33.476,348] : delta: 0.100000, V: 4.228, I: -0.018, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2090, Chging 1, Conn 1, ChgSt 17
[00:13:33.576,354] : delta: 0.100000, V: 4.233, I: -0.018, T: 25.82, SoC: 98.59, TTE: nan, TTF: 2088, Chging 1, Conn 1, ChgSt 17
[00:13:33.676,452] : delta: 0.100000, V: 4.233, I: -0.019, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2100, Chging 1, Conn 1, ChgSt 17
[00:13:33.776,367] : delta: 0.100000, V: 4.228, I: -0.018, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2086, Chging 1, Conn 1, ChgSt 17
[00:13:33.876,342] : delta: 0.100000, V: 4.233, I: -0.016, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2057, Chging 1, Conn 1, ChgSt 17
[00:13:33.976,470] : delta: 0.100000, V: 4.233, I: -0.019, T: 25.93, SoC: 98.59, TTE: nan, TTF: 2096, Chging 1, Conn 1, ChgSt 17
[00:13:34.076,354] : delta: 0.100000, V: 4.228, I: -0.018, T: 25.93, SoC: 98.60, TTE: nan, TTF: 2082, Chging 1, Conn 1, ChgSt 17
[00:13:34.176,361] : delta: 0.100000, V: 4.233, I: -0.018, T: 25.93, SoC: 98.60, TTE: nan, TTF: 2081, Chging 1, Conn 1, ChgSt 17
[00:13:34.276,428] : delta: 0.100000, V: 4.233, I: -0.018, T: 25.93, SoC: 98.60, TTE: nan, TTF: 2079, Chging 1, Conn 1, ChgSt 17
[00:13:34.376,373] : delta: 0.100000, V: 4.233, I: -0.017, T: 25.93, SoC: 98.60, TTE: nan, TTF: 2065, Chging 1, Conn 1, ChgSt 17
[00:13:34.476,379] : delta: 0.100000, V: 4.233, I: -0.018, T: 25.93, SoC: 98.60, TTE: nan, TTF: 2077, Chging 1, Conn 1, ChgSt 17

Discharging:

[00:14:18.076,660] : delta: 0.100000, V: 3.994, I: 0.011, T: 26.03, SoC: 96.79, TTE: 15040, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.176,727] : delta: 0.100000, V: 3.984, I: 0.023, T: 26.03, SoC: 96.79, TTE: 15039, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.276,794] : delta: 0.100000, V: 3.994, I: 0.023, T: 26.03, SoC: 96.79, TTE: 15039, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.376,678] : delta: 0.100000, V: 3.989, I: 0.067, T: 26.03, SoC: 96.79, TTE: 15039, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.476,745] : delta: 0.100000, V: 3.989, I: 0.019, T: 26.03, SoC: 96.78, TTE: 15039, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.576,812] : delta: 0.100000, V: 3.999, I: 0.021, T: 26.03, SoC: 96.78, TTE: 15038, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.676,696] : delta: 0.100000, V: 3.989, I: 0.023, T: 26.03, SoC: 96.78, TTE: 15038, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.776,763] : delta: 0.100000, V: 3.994, I: 0.019, T: 26.03, SoC: 96.78, TTE: 15038, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.876,922] : delta: 0.100000, V: 3.999, I: 0.039, T: 26.03, SoC: 96.78, TTE: 15038, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:18.976,715] : delta: 0.100000, V: 4.003, I: 0.043, T: 26.03, SoC: 96.78, TTE: 15037, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.076,782] : delta: 0.100000, V: 3.994, I: 0.011, T: 26.03, SoC: 96.78, TTE: 15037, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.176,940] : delta: 0.100000, V: 3.999, I: 0.067, T: 26.03, SoC: 96.78, TTE: 15037, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.276,733] : delta: 0.100000, V: 3.994, I: 0.023, T: 26.03, SoC: 96.78, TTE: 15037, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.376,800] : delta: 0.100000, V: 3.984, I: 0.022, T: 26.03, SoC: 96.78, TTE: 15036, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.476,959] : delta: 0.100000, V: 3.994, I: 0.035, T: 26.03, SoC: 96.78, TTE: 15036, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.576,812] : delta: 0.100000, V: 4.003, I: 0.022, T: 26.03, SoC: 96.78, TTE: 15036, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.676,788] : delta: 0.100000, V: 3.994, I: 0.019, T: 26.03, SoC: 96.78, TTE: 15035, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.776,947] : delta: 0.100000, V: 4.003, I: 0.021, T: 26.03, SoC: 96.78, TTE: 15035, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.876,861] : delta: 0.100000, V: 3.994, I: 0.021, T: 26.03, SoC: 96.78, TTE: 15035, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:19.976,837] : delta: 0.100000, V: 3.994, I: 0.009, T: 26.03, SoC: 96.77, TTE: 15035, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.076,995] : delta: 0.100000, V: 4.003, I: 0.039, T: 26.03, SoC: 96.77, TTE: 15035, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.176,879] : delta: 0.100000, V: 4.003, I: 0.020, T: 26.03, SoC: 96.77, TTE: 15034, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.276,855] : delta: 0.100000, V: 3.994, I: 0.019, T: 26.03, SoC: 96.77, TTE: 15034, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.377,014] : delta: 0.100000, V: 3.994, I: 0.021, T: 26.03, SoC: 96.77, TTE: 15034, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.477,020] : delta: 0.100000, V: 3.989, I: 0.019, T: 26.03, SoC: 96.77, TTE: 15034, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.576,843] : delta: 0.100000, V: 3.999, I: 0.019, T: 26.03, SoC: 96.77, TTE: 15033, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.677,032] : delta: 0.100000, V: 3.994, I: 0.039, T: 26.03, SoC: 96.77, TTE: 15033, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.776,885] : delta: 0.100000, V: 3.994, I: 0.011, T: 26.03, SoC: 96.77, TTE: 15033, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.876,922] : delta: 0.100000, V: 3.999, I: 0.021, T: 26.03, SoC: 96.77, TTE: 15033, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:20.977,050] : delta: 0.100000, V: 4.003, I: 0.019, T: 26.03, SoC: 96.77, TTE: 15032, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:21.076,934] : delta: 0.100000, V: 3.994, I: 0.039, T: 26.03, SoC: 96.77, TTE: 15032, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:21.176,940] : delta: 0.100000, V: 3.989, I: 0.019, T: 26.03, SoC: 96.77, TTE: 15032, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:21.277,069] : delta: 0.100000, V: 3.999, I: 0.019, T: 26.03, SoC: 96.77, TTE: 15032, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:21.377,075] : delta: 0.100000, V: 3.994, I: 0.019, T: 26.03, SoC: 96.77, TTE: 15032, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:21.476,959] : delta: 0.100000, V: 3.979, I: 0.043, T: 25.62, SoC: 96.77, TTE: 15031, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:21.577,087] : delta: 0.100000, V: 4.003, I: 0.019, T: 26.03, SoC: 96.77, TTE: 15031, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:21.677,062] : delta: 0.100000, V: 3.999, I: 0.022, T: 26.03, SoC: 96.77, TTE: 15031, TTF: nan, Chging 0, Conn 0, ChgSt 0
[00:14:21.776,947] : delta: 0.100000, V: 3.999, I: 0.009, T: 26.03, SoC: 96.76, TTE: 15031, TTF: nan, Chging 0, Conn 0, ChgSt 0

Question: Do you have any suggestions on how I could get more reliable SoC values from the chip?
Question: Is there a maximum interval between nrf_fuel_gauge_process() calls (i.e. a maximum delta) beyond which the update is not reliable (e.g. 500 ms?)
Question: Does the fuel gauge algorithm average its input over time?

Thanks

  • Hi  

    I'm one of the fuel gauge developers, and I have been trying to recreate the issue based on the state memory you have sent.

    Which polarity are you using for the current measurement? Upstream zephyr changed the convention used in the sensor API, such that negative currents is used for battery discharging.The fuel gauge API still treats negative currents as battery charging.

    I have been able to recreate the behavior (soc = 0.00) by using the old convention:

    (initialization and printouts omitted for code clarity)

    /* Fuel gauge initialization done using provided state memory */
    
    while (true) {
    	voltage = 3.847f;
    	current = 0.029f;
    	temp = 24.79f;
    	delta = 5.0f;
    
    	soc = nrf_fuel_gauge_process(voltage, current, temp, delta, NULL);
    
    	k_msleep(100); // Iterate fast to trigger soc=0.00 issue
    }

    If I use the new convention I have not been able to trigger the issue:

    (note the negative current in `nrf_fuel_gauge_process()`

    /* Fuel gauge initialization done using provided state memory */
    
    while (true) {
    	voltage = 3.847f;
    	current = 0.029f;
    	temp = 24.79f;
    	delta = 5.0f;
    
    	soc = nrf_fuel_gauge_process(voltage, -current, temp, delta, NULL);
    
    	k_msleep(100); // Iterate fast to trigger soc=0.00 issue
    }

    Could this be the problem on your side?

    Best regards,

    Audun

  • Hi Audun,

    Thanks for your reply.

    I think I am using the current properly. Here are the two main functions I use - the init. function, which is called at the start of main (after restoring data from Flash, and initialising the I2C), and the update function, which is called every 5 seconds. Within the update function, there is a backup, of the fuel state data, to Flash every 10 minutes (for now). The code is very messy because I keep adding things / moving things / trying things etc.

    int fuel_gauge_init(const struct device *charger, char *status_str, size_t status_str_len)
    {
    	struct sensor_value value;
    	struct nrf_fuel_gauge_init_parameters parameters = {
    		.model = &battery_model,
    		.opt_params = NULL,
    		.state = NULL,
    	};
    	float max_charge_current;
    	float term_charge_current;
    	int32_t chg_status;
        bool b_used_buf = false;
    	int ret;
    
        status_str[0] = '\0';
    
    	ret = read_sensors(charger, &parameters.v0, &parameters.i0, &parameters.t0, &chg_status);
    	if (ret < 0) {
    		return ret;
    	}
    
    	/* Zephyr sensor API convention for Gauge current is negative=discharging,
    	 * while nrf_fuel_gauge lib expects the opposite negative=charging
    	 */
    	parameters.i0 = -parameters.i0;
    
        /* Optional: low-voltage gate */
        if (parameters.v0 < BATTERY_V_OK_THRESHOLD) {
            LOG_INF("Battery voltage too low on startup: V=%.3fV. Shutting down", parameters.v0);
            battery_init_v_low = true;
        }
        
        /* Store charge nominal and termination current, needed for ttf calculation */
    	sensor_channel_get(charger, SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT, &value);
    	max_charge_current = (float)value.val1 + ((float)value.val2 / 1000000);
    	term_charge_current = max_charge_current / 10.f;
    
        if ( have valid feul state buffer data restored from Flash...) {
            parameters.state = fuel_gauge_state.fuel_gauge_state_buf;
            b_used_buf = true;
        } else {
            parameters.state = NULL;
        }
    
    	ret = nrf_fuel_gauge_init(&parameters, NULL);
        if (ret < 0 && parameters.state != NULL) {
            LOG_ERR("Error: Could not initialise fuel gauge");
            return ret;
        }
        // Output fuel state buffeR data to RTT:
        log_fuel_gauge_state_buffer(fuel_gauge_state.fuel_gauge_state_buf, sizeof(fuel_gauge_state.fuel_gauge_state_buf));
        LOG_INF("FG init %s (used buf = %d)", (parameters.state != NULL) ? "RESUME" : "FRESH", b_used_buf);
    
    	ret = nrf_fuel_gauge_ext_state_update(NRF_FUEL_GAUGE_EXT_STATE_INFO_CHARGE_CURRENT_LIMIT,
    					      &(union nrf_fuel_gauge_ext_state_info_data){
    						      .charge_current_limit = max_charge_current});
    	if (ret < 0) {
    		LOG_ERR("Error: Could not set fuel gauge state");
    		return ret;
    	}
    
    	ret = nrf_fuel_gauge_ext_state_update(NRF_FUEL_GAUGE_EXT_STATE_INFO_TERM_CURRENT,
    					      &(union nrf_fuel_gauge_ext_state_info_data){
    						      .charge_term_current = term_charge_current});
    	if (ret < 0) {
    		LOG_ERR("Error: Could not set fuel gauge state");
    		return ret;
    	}
    
    	ret = charge_status_inform(chg_status);
    	if (ret < 0) {
    		LOG_ERR("Error: Could not set fuel gauge state");
    		return ret;
    	}
    
    	// Reference time, used when calculating elasped time, before next call to nrf_fuel_gauge_process()
    	ref_time = k_uptime_get();
    
        return 0;
    }

    int fuel_gauge_update(const struct device *charger, char *status_str, size_t status_str_len)
    {
    	static int32_t chg_status_prev;
    
    	float voltage;
    	float current;
    	float temp;
    	float soc;
    	float tte;
    	float ttf;
    	float delta;
    	int32_t chg_status;
    	int ret;
        static uint16_t backup_cntr = 0;
        static bool b_first_time = true;
    
        status_str[0] = '\0';
    
        ret = read_sensors(charger, &voltage, &current, &temp, &chg_status);
    
    	if (ret < 0) {
    		printk("Error: Could not read from charger device\n");
    		return ret;
    	}
    	/* Zephyr sensor API convention for Gauge current is negative=discharging,
    	 * while nrf_fuel_gauge lib expects the opposite negative=charging
    	 */
    	current = -current;
    
        /* Charging active: signed current (negative = charging) */
        bool charging_active = (current < -0.005f);   // -5 mA threshold
    
        // Update vbus connected
        ret = sensor_attr_get(charger, SENSOR_CHAN_CURRENT, SENSOR_ATTR_UPPER_THRESH, &sens_val);
        if (ret < 0) {
            return false;
        }    
    	vbus_connected = (sens_val.val1 != 0) || (sens_val.val2 != 0);
    
        b_usb_connected = vbus_connected;   // TODO: tidy this up!
    
        ret = nrf_fuel_gauge_ext_state_update(
    		vbus_connected ? NRF_FUEL_GAUGE_EXT_STATE_INFO_VBUS_CONNECTED
    			       : NRF_FUEL_GAUGE_EXT_STATE_INFO_VBUS_DISCONNECTED,
    		NULL);
    	if (ret < 0) {
    		printk("Error: Could not inform of state\n");
    		return ret;
    	}
    
    	if (chg_status != chg_status_prev) {
    		chg_status_prev = chg_status;
    
    		ret = charge_status_inform(chg_status);
    		if (ret < 0) {
    			printk("Error: Could not inform of charge status\n");
    			return ret;
    		}
    	}
    
    	delta = (float)k_uptime_delta(&ref_time) / 1000.f;
    
    	/* Zephyr sensor API convention for Gauge current is negative=discharging,
    	 * while nrf_fuel_gauge lib expects the opposite negative=charging
    	 */
    	soc = nrf_fuel_gauge_process(voltage, current, temp, delta, NULL);
    	tte = nrf_fuel_gauge_tte_get();
    	ttf = nrf_fuel_gauge_ttf_get();
    
        // Update globals
        battery_charging = charging_active;
        battery_soc = (uint8_t)soc;
        batt_voltage = voltage;
    
    
        // Sanity check
        if (b_first_time) {
            b_first_time = false;
            if ((soc == 0.0f) || (soc == 99.0f)) {
                LOG_INF("fuel_gauge_update - re-initialising fuel gauge");
                ret = fuel_gauge_re_init(charger, status_str, status_str_len);
                if (ret < 0) {
                    LOG_ERR("fuel_gauge_update - re-init failed");
                    return ret;
                }
            }
        }
    
        (void)snprintf(status_str, status_str_len, "dt:%.3f V:%.3f I:%.3f T:%.2f SoC:%.2f TTE:%.0f TTF:%.0f chg:%d st:%d  vbus=%d",
            (double)delta, (double)voltage, (double)current, (double)temp,
            (double)soc, (double)tte, (double)ttf,
            (int)charging_active, (int)chg_status, vbus_connected);
    
    
        backup_cntr = (backup_cntr + 1) % BATTERY_SAVE_CNTR;
        if (backup_cntr == 0) {
    	    // Save fuel state data in Flash, every 10 minutes...
            ret = nrf_fuel_gauge_state_get(fuel_gauge_state.fuel_gauge_state_buf, sizeof(fuel_gauge_state.fuel_gauge_state_buf));
    
            if (ret == 0) {
                fuel_gauge_state.fg_valid = true;
                log_fuel_gauge_state_buffer(fuel_gauge_state.fuel_gauge_state_buf, sizeof(fuel_gauge_state.fuel_gauge_state_buf));
                file_save_fg();
                LOG_INF("FG state saved (size = %d)", sizeof(fuel_gauge_state.fuel_gauge_state_buf));
            } else {
                fuel_gauge_state.fg_valid = false;
                LOG_WRN("FG state_get bad (ret=%d, where 0=success). Not saving.", ret);
            }
        }
    
    	return 0;
    }

  • Hi Garrett

    It still seems like you're using the old convention for the nrf_fuel_gauge_process() function. Please try using a negative current variable and see if the issue still occurs.

    nrf_fuel_gauge_process(voltage, -current, temp, delta, NULL);

    Best regards,

    Simon

  • Hi Simon,

    Am I not using the newer convention?

    My code is detailed above, but here is the important part:

    ret = read_sensors(charger, &voltage, &current, &temp, &chg_status);
    	:
    current = -current;
    	:
    soc = nrf_fuel_gauge_process(voltage, current, temp, delta, NULL);
    

    The value "current" is inverted before making the call to nrf_fuel_gauge_process().

    And, as I say, this seems works all the time, except when I re-Flash the system again.

  • Hi,

    I don't see any problems with the code, although I have not tried running it yet.

    The `charging_active` boolean looks to be wrong due to the sign change of the current, but this does not look to have any practical implications in this function at least.

    From your previous reply it could be that the issue is due to an incompatibility with the stored state being used in different versions of the fuel gauge library. Could this be the case, or do you see the problem persist even when re-flashing using the same SDK version?

    If you still see the issue I will try running your code and debug further, and I'd like to check the potential version incompatibility of the state structs. You mentioned using SDK 2.9.2, but we did not add the state get/set functionality for fuel gauge until SDK 3.0.0. Did you use a more recent fuel gauge library in your SDK 2.9.2 project, or did I misunderstand the versioning?

    Best regards,

    Audun

Related