Skip to content

Commit e464540

Browse files
nordic-aukorlubos
authored andcommittedDec 15, 2023
lib: nrf_fuel_gauge: Update nRF Fuel Gauge
Update nrfxlib nRF Fuel Gauge to v0.9.2, and adjust sample and test accordingly. Signed-off-by: Audun Korneliussen <audun.korneliussen@nordicsemi.no>
1 parent 396a3bb commit e464540

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed
 

‎doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,9 @@ Peripheral samples
377377
PMIC samples
378378
------------
379379

380-
|no_changes_yet_note|
380+
* :ref:`npm1300_fuel_gauge` sample:
381+
382+
* Updated to accommodate API changes in the :ref:`nrfxlib:nrf_fuel_gauge`.
381383

382384
Sensor samples
383385
--------------

‎samples/pmic/native/npm1300_fuel_gauge/prj.conf

+2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
CONFIG_SHELL=y
55
CONFIG_LOG=y
6+
CONFIG_LOG_CMDS=y
67
CONFIG_GPIO=y
78
CONFIG_REGULATOR=y
89
CONFIG_SENSOR=y
10+
CONFIG_I2C_SHELL=y
911
CONFIG_NRF_FUEL_GAUGE=y

‎samples/pmic/native/npm1300_fuel_gauge/src/fuel_gauge.c

+23-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
#include <zephyr/drivers/sensor.h>
1111
#include <zephyr/drivers/sensor/npm1300_charger.h>
1212
#include <zephyr/sys/printk.h>
13+
#include <zephyr/sys/util.h>
1314

1415
#include "nrf_fuel_gauge.h"
1516

17+
/* nPM1300 CHARGER.BCHGCHARGESTATUS.CONSTANTCURRENT register bitmask */
18+
#define NPM1300_CHG_STATUS_CC_MASK BIT_MASK(3)
19+
1620
static float max_charge_current;
1721
static float term_charge_current;
1822
static int64_t ref_time;
@@ -21,7 +25,8 @@ static const struct battery_model battery_model = {
2125
#include "battery_model.inc"
2226
};
2327

24-
static int read_sensors(const struct device *charger, float *voltage, float *current, float *temp)
28+
static int read_sensors(const struct device *charger,
29+
float *voltage, float *current, float *temp, int32_t *chg_status)
2530
{
2631
struct sensor_value value;
2732
int ret;
@@ -40,16 +45,25 @@ static int read_sensors(const struct device *charger, float *voltage, float *cur
4045
sensor_channel_get(charger, SENSOR_CHAN_GAUGE_AVG_CURRENT, &value);
4146
*current = (float)value.val1 + ((float)value.val2 / 1000000);
4247

48+
sensor_channel_get(charger, SENSOR_CHAN_NPM1300_CHARGER_STATUS, &value);
49+
*chg_status = value.val1;
50+
4351
return 0;
4452
}
4553

4654
int fuel_gauge_init(const struct device *charger)
4755
{
4856
struct sensor_value value;
49-
struct nrf_fuel_gauge_init_parameters parameters = { .model = &battery_model };
57+
struct nrf_fuel_gauge_init_parameters parameters = {
58+
.model = &battery_model,
59+
.opt_params = NULL,
60+
};
61+
int32_t chg_status;
5062
int ret;
5163

52-
ret = read_sensors(charger, &parameters.v0, &parameters.i0, &parameters.t0);
64+
printk("nRF Fuel Gauge version: %s\n", nrf_fuel_gauge_version);
65+
66+
ret = read_sensors(charger, &parameters.v0, &parameters.i0, &parameters.t0, &chg_status);
5367
if (ret < 0) {
5468
return ret;
5569
}
@@ -75,19 +89,23 @@ int fuel_gauge_update(const struct device *charger)
7589
float tte;
7690
float ttf;
7791
float delta;
92+
int32_t chg_status;
93+
bool cc_charging;
7894
int ret;
7995

80-
ret = read_sensors(charger, &voltage, &current, &temp);
96+
ret = read_sensors(charger, &voltage, &current, &temp, &chg_status);
8197
if (ret < 0) {
8298
printk("Error: Could not read from charger device\n");
8399
return ret;
84100
}
85101

102+
cc_charging = (chg_status & NPM1300_CHG_STATUS_CC_MASK) != 0;
103+
86104
delta = (float) k_uptime_delta(&ref_time) / 1000.f;
87105

88106
soc = nrf_fuel_gauge_process(voltage, current, temp, delta, NULL);
89107
tte = nrf_fuel_gauge_tte_get();
90-
ttf = nrf_fuel_gauge_ttf_get(-max_charge_current, -term_charge_current);
108+
ttf = nrf_fuel_gauge_ttf_get(cc_charging, -term_charge_current);
91109

92110
printk("V: %.3f, I: %.3f, T: %.2f, ", voltage, current, temp);
93111
printk("SoC: %.2f, TTE: %.0f, TTF: %.0f\n", soc, tte, ttf);

‎tests/lib/nrf_fuel_gauge/src/nrf_fuel_gauge_test.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void test_nrf_fuel_gauge_init(void)
3131
.i0 = 0.0f,
3232
.t0 = 25.0f,
3333
.model = &battery,
34+
.opt_params = NULL,
3435
};
3536
float v0;
3637
int err;
@@ -90,7 +91,7 @@ void test_nrf_fuel_gauge_sanity(void)
9091
}
9192

9293
/* Don't expect time-to-full to be valid when discharging */
93-
ttf = nrf_fuel_gauge_ttf_get(0.4f, 0.04f);
94+
ttf = nrf_fuel_gauge_ttf_get(false, 0.04f);
9495
TEST_ASSERT_TRUE(isnan(ttf));
9596
}
9697

@@ -134,8 +135,13 @@ void test_nrf_fuel_gauge_linking(void)
134135
(void) nrf_fuel_gauge_process(4.2f, 0.07f, 25.0f, 60.0f, NULL);
135136
(void) nrf_fuel_gauge_idle_set(4.2f, 25.0f, 0.0f);
136137
(void) nrf_fuel_gauge_tte_get();
137-
(void) nrf_fuel_gauge_ttf_get(0.0f, 0.0f);
138-
(void) nrf_fuel_gauge_param_adjust(0.0f, 0.0f, 0.0f, 0.0f);
138+
(void) nrf_fuel_gauge_ttf_get(false, 0.0f);
139+
(void) nrf_fuel_gauge_param_adjust(&(struct nrf_fuel_gauge_runtime_parameters) {
140+
.a = 0.0f,
141+
.b = 0.0f,
142+
.c = 0.0f,
143+
.d = 0.0f,
144+
});
139145
}
140146

141147
extern int unity_main(void);

‎west.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ manifest:
141141
- name: nrfxlib
142142
repo-path: sdk-nrfxlib
143143
path: nrfxlib
144-
revision: dcb888fd890fa409a15ce030ff10ffe2debd0d6c
144+
revision: f61d074355d8adff09e1e0414d6a82652c08a126
145145
- name: trusted-firmware-m
146146
repo-path: sdk-trusted-firmware-m
147147
path: modules/tee/tf-m/trusted-firmware-m

0 commit comments

Comments
 (0)
Please sign in to comment.