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

Parents
  • Hi

    Based on the logs you have provided I'm not sure what values it is you refer to being different before/after a reboot? As far as I know there is no maximum delta where measurements aren't supported anymore.

    As for why the voltage and current changes that is normal for a running device since it moves between different modes pretty often.

    No, it does not average over time, why would you think that? 

    As for the state memory, that is indeed used for storing the fuel gauge state so that it can resume from the last known state. As the comment says, on older SDKs this was retrieved using nrf_fuel_gauge_state_get() if moving to NCS v3.1.1 is not an option for you.

    Best regards,

    Simon

  • I upgraded the project to use SDK 3.1.1, so the firmware could take advantage of the nrf_fuel_gauge_state_get() function.

    What the firmware does

    For test purposes, once every minute it calls nrf_fuel_gauge_state_get(), saving the contents to a 256 byte buffer.
    Whenever the device is going to shutdown (nPM1300, ship mode), it first saves the fuel gauge state information to Flash, and restores this on boot up.
    Generally, this setup has been working for a few days for me – I have observed the same SoC being restored on boot, as was saved on the previous shutdown.
    Note: When saving data to Flash before shutdown, there is a 3 second delay after saving, before shutdown (shipmode).


    However, what I have noticed is that sometimes, after flashing the device, the restored SoC goes to zero.

    This has happened while re-flashing the same hex file as was currently running on the device. See step 8 below.
    In this example, the hex file was different (I added a global variable, and incremented it in one function, just to change the memory layout of the code). But I have also observed this phenomenon when re-flashing the same hex file.
    Also, notice that the fuel gauge state buffer contents are the same for step6 and step 8.

    Code

    This is what is saved in Flash. The magic number helps the firmware determine if the contents are valid or not.

    #define FG_MAGIC 12344321u
    #define FG_STATE_BUF_SIZE 256
    typedef struct
    {
        int magic;
        bool fg_valid;
        uint8_t fuel_gauge_state_buf[FG_STATE_BUF_SIZE];
    } imme_fuelgauge_state__t;

    Saving to Flash:

    ret = nrf_fuel_gauge_state_get(fuel_gauge_state.fuel_gauge_state_buf, sizeof(fuel_gauge_state.fuel_gauge_state_buf));

    Restoring from Flash:

    	rc = nvs_read(&fs, FUEL_GAUGE_STATE_ID, &fuel_gauge_state_saved, sizeof(fuel_gauge_state_saved));
    	if (rc > 0) { 		
    		// item was found, show it
            if (fuel_gauge_state_exists()) {
                LOG_INF("FUEL_GAUGE_STATE_ID: Saved data valid (rc = %d, magic %d, valid %d) - restoring", rc, fuel_gauge_state_saved.magic, fuel_gauge_state_saved.fg_valid);
                fuel_gauge_state = fuel_gauge_state_saved;
                log_fuel_gauge_state_buffer(fuel_gauge_state.fuel_gauge_state_buf, sizeof(fuel_gauge_state.fuel_gauge_state_buf));

    Initialising the fuel gauge:

    	ret = read_sensors(charger, &parameters.v0, &parameters.i0, &parameters.t0, &chg_status);
    	if (ret < 0) {
    		return ret;
    	}
    	parameters.i0 = -parameters.i0;
    
        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;
        }
        
    	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 (fuel_gauge_state.fg_valid &&
            fuel_gauge_state_exists() &&
            (nrf_fuel_gauge_state_size <= sizeof(fuel_gauge_state.fuel_gauge_state_buf))) {
            parameters.state = fuel_gauge_state.fuel_gauge_state_buf;
            b_used_buf = true;
        } else {
            parameters.state = NULL;
        }
    
    	ret = nrf_fuel_gauge_init(&parameters, NULL);

    The firmware then outputs the contents of fuel_gauge_state.fuel_gauge_state_buf, and I can confirm that the contents are the same, as expected (and the b_used_buf indicates that the fuel gauge state buffer was used in the initialisation).

    Question: Can you explain this behaviour / can you see if I am doing anything wrong?

    Here is an example:

    Step 1

    Before shipmode, Fuel Gauge state is saved, when SoC was approx. 89.03.

    Step 2
    Contents after restore (missed some of the RTT output):

    [00:00:00.288,940] <inf> fuel_gauge: [080-095]: 00 00 A0 40 00 00 48 42 00 00 F0 41 33 33 73 3F
    [00:00:00.306,518] <inf> fuel_gauge: [096-111]: 66 66 A6 3F 00 00 80 3F 00 00 70 42 00 00 C0 3F
    [00:00:00.324,066] <inf> fuel_gauge: [112-127]: 66 66 66 3F 33 33 73 3F 7A 6E 21 BC 00 00 00 00
    [00:00:00.341,674] <inf> fuel_gauge: [128-143]: 2B 87 76 40 00 00 C0 7F 7A 6E 21 BC 00 00 C8 41
    [00:00:00.359,313] <inf> fuel_gauge: [144-159]: 85 E9 63 3F 00 00 00 00 EC 51 18 3F 00 00 C0 7F
    [00:00:00.376,861] <inf> fuel_gauge: [160-175]: 85 E9 63 3F 1B 82 82 46 09 60 A0 B8 00 00 C8 41
    [00:00:00.394,470] <inf> fuel_gauge: [176-191]: 00 00 C8 41 01 00 00 00 00 00 40 40 00 00 80 40
    [00:00:00.412,048] <inf> fuel_gauge: [192-207]: EE 54 36 3C CB 95 40 3C 00 00 00 00 85 E9 63 3F
    [00:00:00.429,656] <inf> fuel_gauge: [208-223]: E9 03 AE 40 E9 03 AE 40 D8 02 AE 40 D8 02 AE 40
    [00:00:00.447,296] <inf> fuel_gauge: [224-239]: 78 D2 05 00 34 12 CD AB 00 00 00 00 00 00 00 00
    [00:00:00.464,904] <inf> fuel_gauge: [240-255]: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    SoC restored at 89.03:

    [00:00:04.736,328] <inf> Charger update: dt:2.470 V:3.906 I:0.018 T:23.98 SoC:89.03 TTE:nan TTF:nan chg:0 st:0 vbus=0, full=0

    Step 3
    One minute later, save state when SoC is approx. 88.80:

    [00:00:59.776,428] <inf> fuel_gauge: [000-015]: 00 00 80 3F 00 00 00 40 D9 52 24 37 26 60 47 37
    [00:00:59.794,067] <inf> fuel_gauge: [016-031]: 00 00 00 00 93 7B DE B6 26 60 47 37 50 8A 74 37
    [00:00:59.811,584] <inf> fuel_gauge: [032-047]: 00 00 00 00 97 1F 06 B7 00 00 00 00 00 00 00 00
    [00:00:59.829,071] <inf> fuel_gauge: [048-063]: 77 CC 2B 32 00 00 00 00 88 7B DE B6 97 1F 06 B7
    [00:00:59.846,557] <inf> fuel_gauge: [064-079]: 00 00 00 00 8F 01 7B 37 A6 9B 44 3B 6F 12 83 3A
    [00:00:59.864,044] <inf> fuel_gauge: [080-095]: 00 00 A0 40 00 00 48 42 00 00 F0 41 33 33 73 3F
    [00:00:59.881,530] <inf> fuel_gauge: [096-111]: 66 66 A6 3F 00 00 80 3F 00 00 70 42 00 00 C0 3F
    [00:00:59.899,017] <inf> fuel_gauge: [112-127]: 66 66 66 3F 33 33 73 3F 7A 6E 21 3C 00 00 00 00
    [00:00:59.916,503] <inf> fuel_gauge: [128-143]: 39 B4 78 40 00 00 C0 7F 7A 6E 21 3C 00 00 C8 41
    [00:00:59.933,990] <inf> fuel_gauge: [144-159]: 73 2D 63 3F 00 00 00 00 48 E1 65 42 85 E9 63 3F
    [00:00:59.951,477] <inf> fuel_gauge: [160-175]: 00 00 C0 7F 00 00 C0 7F 48 DF 69 B8 00 00 C8 41
    [00:00:59.968,963] <inf> fuel_gauge: [176-191]: 00 00 C8 41 01 00 00 00 00 00 40 40 00 00 80 40
    [00:00:59.986,663] <inf> fuel_gauge: [192-207]: DD 29 14 3C F0 6D 1B 3C 00 00 00 00 73 2D 63 3F
    [00:01:00.004,150] <inf> fuel_gauge: [208-223]: E9 03 AE 40 E9 03 AE 40 CC 02 AE 40 CC 02 AE 40
    [00:01:00.021,636] <inf> fuel_gauge: [224-239]: 78 D2 05 00 34 12 CD AB 00 00 00 00 00 00 00 00
    [00:01:00.039,154] <inf> fuel_gauge: [240-255]: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    Step 4
    15 seconds later (SoC approx. 88.59), flash in SAME hex file, while device still running.

    Step 5
    On boot, after flash restore, contents:

    [00:00:00.209,808] <inf> fuel_gauge: [016-031]: 00 00 00 00 93 7B DE B6 26 60 47 37 50 8A 74 37
    [00:00:00.227,447] <inf> fuel_gauge: [032-047]: 00 00 00 00 97 1F 06 B7 00 00 00 00 00 00 00 00
    [00:00:00.244,995] <inf> fuel_gauge: [048-063]: 77 CC 2B 32 00 00 00 00 88 7B DE B6 97 1F 06 B7
    [00:00:00.262,573] <inf> fuel_gauge: [064-079]: 00 00 00 00 8F 01 7B 37 A6 9B 44 3B 6F 12 83 3A
    [00:00:00.280,212] <inf> fuel_gauge: [080-095]: 00 00 A0 40 00 00 48 42 00 00 F0 41 33 33 73 3F
    [00:00:00.297,821] <inf> fuel_gauge: [096-111]: 66 66 A6 3F 00 00 80 3F 00 00 70 42 00 00 C0 3F
    [00:00:00.315,399] <inf> fuel_gauge: [112-127]: 66 66 66 3F 33 33 73 3F 7A 6E 21 3C 00 00 00 00
    [00:00:00.332,977] <inf> fuel_gauge: [128-143]: 39 B4 78 40 00 00 C0 7F 7A 6E 21 3C 00 00 C8 41
    [00:00:00.350,555] <inf> fuel_gauge: [144-159]: 73 2D 63 3F 00 00 00 00 48 E1 65 42 85 E9 63 3F
    [00:00:00.368,194] <inf> fuel_gauge: [160-175]: 00 00 C0 7F 00 00 C0 7F 48 DF 69 B8 00 00 C8 41
    [00:00:00.385,803] <inf> fuel_gauge: [176-191]: 00 00 C8 41 01 00 00 00 00 00 40 40 00 00 80 40
    [00:00:00.403,350] <inf> fuel_gauge: [192-207]: DD 29 14 3C F0 6D 1B 3C 00 00 00 00 73 2D 63 3F
    [00:00:00.420,928] <inf> fuel_gauge: [208-223]: E9 03 AE 40 E9 03 AE 40 CC 02 AE 40 CC 02 AE 40
    [00:00:00.438,568] <inf> fuel_gauge: [224-239]: 78 D2 05 00 34 12 CD AB 00 00 00 00 00 00 00 00
    [00:00:00.456,176] <inf> fuel_gauge: [240-255]: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    SoC restored to 88.68

    [00:00:04.727,203] <inf> iCharger update: dt:2.470 V:3.867 I:0.010 T:24.59 SoC:88.68 TTE:nan TTF:nan chg:0 st:0 vbus=0, full=0
    Step 6

    One minute later, save state when SoC is approx. 88.33:

    [00:00:59.767,608] <inf> fuel_gauge: [000-015]: 00 00 80 3F 00 00 00 40 97 45 25 37 56 A0 48 37
    [00:00:59.785,095] <inf> fuel_gauge: [016-031]: 00 00 00 00 40 1E E7 B6 56 A0 48 37 AA 20 76 37
    [00:00:59.802,612] <inf> fuel_gauge: [032-047]: 00 00 00 00 A7 A3 0A B7 00 00 00 00 00 00 00 00
    [00:00:59.820,068] <inf> fuel_gauge: [048-063]: 77 CC 2B 32 00 00 00 00 34 1E E7 B6 A6 A3 0A B7
    [00:00:59.837,554] <inf> fuel_gauge: [064-079]: 00 00 00 00 BA AF 78 37 A6 9B 44 3B 6F 12 83 3A
    [00:00:59.855,041] <inf> fuel_gauge: [080-095]: 00 00 A0 40 00 00 48 42 00 00 F0 41 33 33 73 3F
    [00:00:59.872,772] <inf> fuel_gauge: [096-111]: 66 66 A6 3F 00 00 80 3F 00 00 70 42 00 00 C0 3F
    [00:00:59.890,258] <inf> fuel_gauge: [112-127]: 66 66 66 3F 33 33 73 3F 5D 16 93 3C 00 00 00 00
    [00:00:59.907,745] <inf> fuel_gauge: [128-143]: EE 7C 77 40 AC 60 64 46 5D 16 93 3C 00 00 C8 41
    [00:00:59.925,231] <inf> fuel_gauge: [144-159]: F9 0F 62 3F 00 00 00 00 48 E1 E5 42 85 E9 63 3F
    [00:00:59.942,718] <inf> fuel_gauge: [160-175]: 00 00 C0 7F AC 60 64 46 FE B6 81 B8 00 00 C8 41
    [00:00:59.960,205] <inf> fuel_gauge: [176-191]: 00 00 C8 41 01 00 00 00 00 00 40 40 00 00 80 40
    [00:00:59.977,905] <inf> fuel_gauge: [192-207]: 5F 16 1B 3C 76 A9 26 3C 00 00 00 00 F9 0F 62 3F
    [00:00:59.995,391] <inf> fuel_gauge: [208-223]: E9 03 AE 40 E9 03 AE 40 C0 02 AE 40 C0 02 AE 40
    [00:01:00.012,878] <inf> fuel_gauge: [224-239]: 78 D2 05 00 34 12 CD AB 00 00 00 00 00 00 00 00
    [00:01:00.030,395] <inf> fuel_gauge: [240-255]: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    Step 7

    30 seconds later (SoC approx. 88.11), flash in DIFFERENT hex file, while device still running.

    Step 8
    On boot, after flash restore, contents:

    [00:00:00.176,086] <inf> fuel_gauge: [000-015]: 00 00 80 3F 00 00 00 40 97 45 25 37 56 A0 48 37
    [00:00:00.193,603] <inf> fuel_gauge: [016-031]: 00 00 00 00 40 1E E7 B6 56 A0 48 37 AA 20 76 37
    [00:00:00.211,212] <inf> fuel_gauge: [032-047]: 00 00 00 00 A7 A3 0A B7 00 00 00 00 00 00 00 00
    [00:00:00.228,759] <inf> fuel_gauge: [048-063]: 77 CC 2B 32 00 00 00 00 34 1E E7 B6 A6 A3 0A B7
    [00:00:00.246,307] <inf> fuel_gauge: [064-079]: 00 00 00 00 BA AF 78 37 A6 9B 44 3B 6F 12 83 3A
    [00:00:00.263,885] <inf> fuel_gauge: [080-095]: 00 00 A0 40 00 00 48 42 00 00 F0 41 33 33 73 3F
    [00:00:00.281,433] <inf> fuel_gauge: [096-111]: 66 66 A6 3F 00 00 80 3F 00 00 70 42 00 00 C0 3F
    [00:00:00.299,011] <inf> fuel_gauge: [112-127]: 66 66 66 3F 33 33 73 3F 5D 16 93 3C 00 00 00 00
    [00:00:00.316,558] <inf> fuel_gauge: [128-143]: EE 7C 77 40 AC 60 64 46 5D 16 93 3C 00 00 C8 41
    [00:00:00.334,136] <inf> fuel_gauge: [144-159]: F9 0F 62 3F 00 00 00 00 48 E1 E5 42 85 E9 63 3F
    [00:00:00.351,684] <inf> fuel_gauge: [160-175]: 00 00 C0 7F AC 60 64 46 FE B6 81 B8 00 00 C8 41
    [00:00:00.369,293] <inf> fuel_gauge: [176-191]: 00 00 C8 41 01 00 00 00 00 00 40 40 00 00 80 40
    [00:00:00.386,840] <inf> fuel_gauge: [192-207]: 5F 16 1B 3C 76 A9 26 3C 00 00 00 00 F9 0F 62 3F
    [00:00:00.404,418] <inf> fuel_gauge: [208-223]: E9 03 AE 40 E9 03 AE 40 C0 02 AE 40 C0 02 AE 40
    [00:00:00.422,027] <inf> fuel_gauge: [224-239]: 78 D2 05 00 34 12 CD AB 00 00 00 00 00 00 00 00
    [00:00:00.439,605] <inf> fuel_gauge: [240-255]: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    SoC is now zero:

    [00:00:09.708,831] <inf> Charger update: dt:5.000 V:3.847 I:0.029 T:24.79 SoC:0.00 TTE:nan TTF:nan chg:0 st:0 vbus=0, full=0


    Step 9
    Device entered shipmode approx. 1 minute later (firmware logic, because SoC is zero), and FG state buffer contents were:

    [00:00:54.844,238] <inf> fuel_gauge: [000-015]: 00 00 80 3F 00 00 00 40 FF FF FF 7F FF FF FF 7F
    [00:00:54.861,694] <inf> fuel_gauge: [016-031]: FF FF FF 7F FF FF FF 7F FF FF FF 7F FF FF FF 7F
    [00:00:54.879,150] <inf> fuel_gauge: [032-047]: FF FF FF 7F FF FF FF 7F FF FF FF 7F FF FF FF 7F
    [00:00:54.896,728] <inf> fuel_gauge: [048-063]: FF FF FF 7F FF FF FF 7F FF FF FF 7F FF FF FF 7F
    [00:00:54.914,337] <inf> fuel_gauge: [064-079]: FF FF FF 7F FF FF FF 7F 9A 99 99 3E BD 37 86 35
    [00:00:54.931,823] <inf> fuel_gauge: [080-095]: 00 00 A0 40 00 00 48 42 00 00 F0 41 33 33 73 3F
    [00:00:54.949,279] <inf> fuel_gauge: [096-111]: 66 66 A6 3F 00 00 80 3F 00 00 70 42 00 00 C0 3F
    [00:00:54.966,735] <inf> fuel_gauge: [112-127]: 66 66 66 3F 33 33 73 3F 27 BC 84 BC 00 00 00 00
    [00:00:54.984,191] <inf> fuel_gauge: [128-143]: 68 91 75 40 00 00 C0 7F 27 BC 84 BC 00 00 C8 41
    [00:00:55.001,678] <inf> fuel_gauge: [144-159]: 00 00 00 00 00 00 00 00 68 91 ED 3D 00 00 C0 7F
    [00:00:55.019,165] <inf> fuel_gauge: [160-175]: 00 00 00 00 B1 30 59 46 EB 13 9C BB 00 00 C8 41
    [00:00:55.036,621] <inf> fuel_gauge: [176-191]: 00 00 C8 41 01 00 00 00 00 00 40 40 00 00 80 40
    [00:00:55.054,107] <inf> fuel_gauge: [192-207]: FF FF FF FF FF FF FF FF 00 00 80 BF 00 00 00 00
    [00:00:55.071,563] <inf> fuel_gauge: [208-223]: E9 03 AE 40 54 61 D1 3A B4 02 AE 40 B4 02 AE 40
    [00:00:55.089,019] <inf> fuel_gauge: [224-239]: 78 D2 05 00 34 12 CD AB 00 00 00 00 00 00 00 00
    [00:00:55.106,506] <inf> fuel_gauge: [240-255]: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

  • Hi.

    The same thing occurred just now as I tested DFU.

    I flashed my device with an older version of my project (built using SDK 2.9.2).

    With my latest project (SDK 3.1.1), I created two .bin files - the difference being an extra global debug counter which is incremented in one of the functions - just to be different, no other reason.

    I used Nordic's Device Manager to flash the remote device, with the first .bin file. All went well. SoC remained almost the same (90%). I then reset the device, checked everything, and used Device Manager to send the second .bin file. When the upload was complete, the SoC was now zero.

    I then repeated the above test with two new .bin files (SDK 13.1.1). This time, as well as one of the builds using a debug counter, I also changed the Magic Number used for validating the Fuel Gauge state. So the two .bin files had different magic numbers. I flashed the device (using VSCode) with the 2.9.2 project then used Device Manager to send the first .bin file. All good. SoC is about 90%. I then used Device Manager to send the second bin file. Again, all good. Because this time, the device did NOT use the restored state buffer (256 bytes), when initialising the fuel gauge.

    So it seems, for now, until this is solved, I have to change the magic number associated with teh saved fuel gauge state, every time I send a new .bin file out.

  • 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;
    }

Reply
  • 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;
    }

Children
  • 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

  • Hi Audun,

    I am using SDK 3.1.1. I use the same application when Flashing. Sometimes, I modify the application slightly (e.g. add one debug global variable). Always SDK 3.1.1.

    When I first posted this ticket I was using SDK 2.9.2, but I then upgraded to SDK 3.1.1 to use the nrf_fuel_gauge_state_get() function.

  • Thanks for the details,

    I do have one more theory of what could have gone wrong. The state blob includes a pointer to the battery model. If this is no longer valid after you have changed the binary, the behavior would change.

    Can you try to update the pointer within the binary blob before passing it on to nrf_fuel_gauge_init()? I assume you have the data in RAM already.

    #include <zephyr/sys/byteorder.h>
    
    sys_put_le32(&battery_model, &fuel_gauge_state.fuel_gauge_state_buf[224]);

    This API needs to be updated on our side to allow the battery model to be moved around. It's inconvenient to require this pointer to be static across firmware updates.

    Best regards,

    Audun

Related