Skip to content

Commit ff67fd2

Browse files
committed
sample/fuel_gauge: Update sample to latest fuel gauge API
Signed-off-by: Audun Korneliussen <[email protected]>
1 parent 2a56701 commit ff67fd2

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

samples/fuel_gauge/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# SPDX-License-Identifier: BSD-3-Clause
55
#
66

7+
CONFIG_MAIN_STACK_SIZE=2048
8+
79
CONFIG_SERIAL=y
810
CONFIG_CONSOLE=y
911
CONFIG_UART_CONSOLE=y
@@ -21,4 +23,5 @@ CONFIG_DEBUG_COREDUMP=y
2123
CONFIG_DEBUG_COREDUMP_BACKEND_LOGGING=y
2224

2325
CONFIG_NRF_FUEL_GAUGE=y
26+
CONFIG_NRF_FUEL_GAUGE_VARIANT_SECONDARY_CELL=y
2427
CONFIG_REQUIRES_FLOAT_PRINTF=y

samples/fuel_gauge/src/fuel_gauge.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,64 @@ static int read_sensors(npmx_instance_t *const p_pm, float *voltage, float *curr
6666
return 0;
6767
}
6868

69+
static int process_vbus_state(npmx_instance_t *const p_pm)
70+
{
71+
npmx_vbusin_t *vbus_instance = npmx_vbusin_get(p_pm, 0);
72+
enum nrf_fuel_gauge_ext_state_info_type state_info;
73+
uint8_t vbusin_status;
74+
75+
if (npmx_vbusin_vbus_status_get(vbus_instance, &vbusin_status) != NPMX_SUCCESS) {
76+
LOG_ERR("Reading VBUS status failed.");
77+
return -EIO;
78+
}
79+
80+
if (vbusin_status & NPMX_VBUSIN_STATUS_CONNECTED_MASK) {
81+
state_info = NRF_FUEL_GAUGE_EXT_STATE_INFO_VBUS_CONNECTED;
82+
} else {
83+
state_info = NRF_FUEL_GAUGE_EXT_STATE_INFO_VBUS_DISCONNECTED;
84+
}
85+
86+
return nrf_fuel_gauge_ext_state_update(state_info, NULL);
87+
}
88+
89+
static int process_charger_state(npmx_instance_t *const p_pm)
90+
{
91+
npmx_charger_t *charger_instance = npmx_charger_get(p_pm, 0);
92+
union nrf_fuel_gauge_ext_state_info_data state_info;
93+
uint8_t charger_status;
94+
95+
if (npmx_charger_status_get(charger_instance, &charger_status) != NPMX_SUCCESS) {
96+
LOG_ERR("Reading charger status failed.");
97+
return -EIO;
98+
}
99+
100+
if (charger_status & NPMX_CHARGER_STATUS_COMPLETED_MASK) {
101+
LOG_INF("Charge complete");
102+
state_info.charge_state = NRF_FUEL_GAUGE_CHARGE_STATE_COMPLETE;
103+
} else if (charger_status & NPMX_CHARGER_STATUS_TRICKLE_CHARGE_MASK) {
104+
LOG_INF("Trickle charging");
105+
state_info.charge_state = NRF_FUEL_GAUGE_CHARGE_STATE_TRICKLE;
106+
} else if (charger_status & NPMX_CHARGER_STATUS_CONSTANT_CURRENT_MASK) {
107+
LOG_INF("Constant current charging");
108+
state_info.charge_state = NRF_FUEL_GAUGE_CHARGE_STATE_CC;
109+
} else if (charger_status & NPMX_CHARGER_STATUS_CONSTANT_VOLTAGE_MASK) {
110+
LOG_INF("Constant voltage charging");
111+
state_info.charge_state = NRF_FUEL_GAUGE_CHARGE_STATE_CV;
112+
} else {
113+
LOG_INF("Charger idle");
114+
state_info.charge_state = NRF_FUEL_GAUGE_CHARGE_STATE_IDLE;
115+
}
116+
117+
return nrf_fuel_gauge_ext_state_update(NRF_FUEL_GAUGE_EXT_STATE_INFO_CHARGE_STATE_CHANGE,
118+
&state_info);
119+
}
120+
69121
int fuel_gauge_init(npmx_instance_t *const p_pm)
70122
{
71123
struct nrf_fuel_gauge_init_parameters parameters = {
72124
.model = &battery_model,
73125
.opt_params = NULL,
126+
.state = NULL,
74127
};
75128
bool cc_charging;
76129
int ret;
@@ -103,6 +156,18 @@ int fuel_gauge_update(npmx_instance_t *const p_pm)
103156
bool cc_charging;
104157
int ret;
105158

159+
ret = process_vbus_state(p_pm);
160+
if (ret < 0) {
161+
LOG_ERR("Error: Could not process VBUS state.");
162+
return ret;
163+
}
164+
165+
ret = process_charger_state(p_pm);
166+
if (ret < 0) {
167+
LOG_ERR("Error: Could not process charger state.");
168+
return ret;
169+
}
170+
106171
ret = read_sensors(p_pm, &voltage, &current, &temp, &cc_charging);
107172
if (ret < 0) {
108173
LOG_ERR("Error: Could not read data from charger device.");
@@ -113,7 +178,7 @@ int fuel_gauge_update(npmx_instance_t *const p_pm)
113178

114179
soc = nrf_fuel_gauge_process(voltage, current, temp, delta, NULL);
115180
tte = nrf_fuel_gauge_tte_get();
116-
ttf = nrf_fuel_gauge_ttf_get(cc_charging, -term_charge_current);
181+
ttf = nrf_fuel_gauge_ttf_get();
117182

118183
LOG_INF("V: %.3f, I: %.3f, T: %.2f, SoC: %.2f, TTE: %.0f, TTF: %.0f", (double)voltage, (double)current,
119184
(double)temp, (double)soc, (double)tte, (double)ttf);

0 commit comments

Comments
 (0)