Skip to content

Commit 7569d0a

Browse files
committed
drivers: fuelgauge: Add Onsemi LC709203F driver
Replace IC specific fuelgauge example with a generic fuelgauge example Signed-off-by: Philipp Steiner <[email protected]>
1 parent 2a4c054 commit 7569d0a

21 files changed

+143
-141
lines changed

boards/adafruit/feather_esp32s2/adafruit_feather_esp32s2_tft_reverse.dts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
aliases {
2121
backlight = &led1;
22+
fuel-gauge0 = &max17048;
2223
};
2324

2425
leds {

boards/adafruit/feather_esp32s3/adafruit_feather_esp32s3_procpu.dts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
sw0 = &button0;
3939
led0 = &led0;
4040
led-strip = &led_strip;
41+
fuel-gauge0 = &max17048;
4142
};
4243

4344
buttons {

boards/adafruit/feather_esp32s3_tft/adafruit_feather_esp32s3_tft_procpu.dts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
aliases {
2323
i2c-0 = &i2c0;
2424
watchdog0 = &wdt0;
25+
fuel-gauge0 = &max17048;
2526
};
2627

2728
chosen {

samples/fuel_gauge/README.rst

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.. zephyr:code-sample:: fuel_gauge
2+
:name: Fuel Gauge
3+
4+
Read battery percentage and power status using MAX17048 fuel gauge.
5+
6+
Overview
7+
********
8+
9+
This sample shows how to use the Zephyr :ref:`fuel_gauge_api` API driver for.
10+
11+
The sample periodically reads battery percentage and voltage.
12+
13+
Building and Running
14+
********************
15+
16+
The sample can be configured to support a fuel gauge.
17+
18+
Features
19+
********
20+
By using this fuel gauge you can get the following information:
21+
* Battery charge status as percentage
22+
* Battery voltage
23+
24+
Sample output
25+
*************
26+
27+
```
28+
*** Booting Zephyr OS build 95f75fc5596b ***
29+
[00:00:00.116,000] <inf> app: Found device "lc709203f@0b", getting fuel gauge data
30+
[00:00:00.124,000] <inf> app: Fuel gauge data: Charge: 98%, Voltage: 4159mV
31+
[00:00:05.125,000] <inf> app: Fuel gauge data: Charge: 98%, Voltage: 4162mV
32+
```

samples/fuel_gauge/max17048/boards/adafruit_feather_esp32s2_C.conf renamed to samples/fuel_gauge/boards/adafruit_feather_esp32s2.conf

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ CONFIG_POWER_DOMAIN=y
22
CONFIG_GPIO=y
33
CONFIG_PM_DEVICE=y
44
CONFIG_PM_DEVICE_POWER_DOMAIN=y
5+
CONFIG_CRC=y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/ {
2+
aliases {
3+
fuel-gauge0 = &lc709203f;
4+
};
5+
};
6+
7+
&i2c0 {
8+
lc709203f: lc709203f@0b {
9+
compatible = "onnn,lc709203f";
10+
status = "okay";
11+
reg = <0x0b>;
12+
power-domains = <&i2c_reg>;
13+
apa = "500mAh";
14+
battery-profile = <0x01>;
15+
};
16+
};

samples/fuel_gauge/max17048/boards/adafruit_feather_esp32s2_tft.overlay renamed to samples/fuel_gauge/boards/adafruit_feather_esp32s2_max17048.overlay

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/ {
2+
aliases {
3+
fuel-gauge0 = &max17048;
4+
};
5+
};
6+
17
&i2c0 {
28
max17048: max17048@36 {
39
compatible = "maxim,max17048";

samples/fuel_gauge/max17048/boards/adafruit_feather_esp32s2_tft.conf renamed to samples/fuel_gauge/boards/adafruit_feather_esp32s2_tft.conf

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ CONFIG_POWER_DOMAIN=y
22
CONFIG_GPIO=y
33
CONFIG_PM_DEVICE=y
44
CONFIG_PM_DEVICE_POWER_DOMAIN=y
5+
CONFIG_CRC=y

samples/fuel_gauge/max17048/boards/adafruit_feather_esp32s2_tft_reverse.conf renamed to samples/fuel_gauge/boards/adafruit_feather_esp32s2_tft_reverse.conf

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ CONFIG_POWER_DOMAIN=y
22
CONFIG_GPIO=y
33
CONFIG_PM_DEVICE=y
44
CONFIG_PM_DEVICE_POWER_DOMAIN=y
5+
CONFIG_CRC=y

samples/fuel_gauge/max17048/boards/nrf52840dk_nrf52840.overlay renamed to samples/fuel_gauge/boards/nrf52840dk_nrf52840.overlay

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
/ {
8+
aliases {
9+
fuel-gauge0 = &max17048;
10+
};
11+
};
12+
713
&i2c0 {
814
status = "okay";
915
compatible = "nordic,nrf-twim";

samples/fuel_gauge/fuel_gauge.rst

-5
This file was deleted.

samples/fuel_gauge/max17048/README.rst

-51
This file was deleted.

samples/fuel_gauge/max17048/boards/adafruit_feather_esp32s2_C.overlay

-8
This file was deleted.

samples/fuel_gauge/max17048/prj.conf

-1
This file was deleted.

samples/fuel_gauge/max17048/sample.yaml

-9
This file was deleted.

samples/fuel_gauge/max17048/src/main.c

-67
This file was deleted.

samples/fuel_gauge/prj.conf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_FUEL_GAUGE=y
2+
CONFIG_I2C=y
3+
CONFIG_LOG=y

samples/fuel_gauge/sample.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sample:
2+
name: Fuel Gauge Sensor Sample
3+
tests:
4+
sample.sensor.fuel_gauge:
5+
build_only: true
6+
platform_allow:
7+
- nrf52dk/nrf52832
8+
- adafruit_feather_esp32s2@B
9+
- adafruit_feather_esp32s2@C
10+
- adafruit_feather_esp32s2_tft
11+
integration_platforms:
12+
- nrf52dk/nrf52832
13+
- adafruit_feather_esp32s2@B
14+
- adafruit_feather_esp32s2@C
15+
- adafruit_feather_esp32s2_tft
16+
tags: fuel_gauge

samples/fuel_gauge/src/main.c

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2023 Alvaro Garcia Gomez <[email protected]>
3+
* Copyright (c) 2025 Philipp Steiner <[email protected]>
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include "zephyr/sys/util.h"
9+
#include <zephyr/kernel.h>
10+
#include <zephyr/device.h>
11+
#include <zephyr/devicetree.h>
12+
#include <zephyr/drivers/fuel_gauge.h>
13+
14+
#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
15+
#include <zephyr/logging/log.h>
16+
LOG_MODULE_REGISTER(app);
17+
18+
int main(void)
19+
{
20+
const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(fuel_gauge0));
21+
int ret = 0;
22+
23+
if (dev == NULL) {
24+
LOG_ERR("no device found.");
25+
return 0;
26+
}
27+
28+
if (!device_is_ready(dev)) {
29+
LOG_ERR("Error: Device \"%s\" is not ready; check the driver initialization logs "
30+
"for errors.",
31+
dev->name);
32+
return 0;
33+
}
34+
35+
LOG_INF("Found device \"%s\", getting fuel gauge data", dev->name);
36+
37+
while (1) {
38+
39+
fuel_gauge_prop_t props[] = {
40+
FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE,
41+
FUEL_GAUGE_VOLTAGE,
42+
};
43+
44+
union fuel_gauge_prop_val vals[ARRAY_SIZE(props)];
45+
46+
ret = fuel_gauge_get_props(dev, props, vals, ARRAY_SIZE(props));
47+
48+
if (ret < 0) {
49+
LOG_ERR("Error: cannot get properties");
50+
} else {
51+
LOG_INF("Fuel gauge data: Charge: %d%%, Voltage: %dmV",
52+
vals[0].relative_state_of_charge, vals[1].voltage / 1000);
53+
}
54+
55+
k_sleep(K_MSEC(5000));
56+
}
57+
return 0;
58+
}

0 commit comments

Comments
 (0)