Skip to content

Commit 03c4e8a

Browse files
committed
samples: fuelgauge: unify fuel gauge sample
Replace IC specific fuel gauge example with a generic fuel gauge example Signed-off-by: Philipp Steiner <[email protected]>
1 parent 9400bf8 commit 03c4e8a

24 files changed

+573
-150
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/max17048/CMakeLists.txt renamed to samples/fuel_gauge/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
cmake_minimum_required(VERSION 3.20.0)
44
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5-
project(max17048)
5+
project(fuel_gauge)
66

77
FILE(GLOB app_sources src/*.c)
88
target_sources(app PRIVATE ${app_sources})

samples/fuel_gauge/README.rst

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
.. zephyr:code-sample:: fuel_gauge
2+
:name: Fuel Gauge
3+
4+
fuel gauge driver API usage.
5+
6+
Overview
7+
********
8+
9+
This sample shows how to use the Zephyr :ref:`fuel_gauge_api` API driver for.
10+
11+
First, the sample tries to read all public fuel gauge properties to verify which are supported by
12+
the used fuel gauge driver.
13+
Second, the sample then reads the battery percentage and voltage proper periodically using the
14+
``FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE`` and ``FUEL_GAUGE_VOLTAGE`` properties.
15+
16+
.. note::
17+
The sample does not set/write any properties to avoid misconfiguration the IC.
18+
19+
Building and Running
20+
********************
21+
22+
The sample can be configured to support a fuel gauge.
23+
24+
Features
25+
********
26+
By using this fuel gauge you can get the following information:
27+
* Read all public fuel gauge properties except ``FUEL_GAUGE_BATTERY_CUTOFF``
28+
* Battery charge status as percentage (periodically)
29+
* Battery voltage (periodically)
30+
31+
Sample output
32+
*************
33+
34+
```
35+
*** Booting Zephyr OS build f95fd665ad26 ***
36+
[00:00:00.116,000] <inf> app: Found device "lc709203f@0b"
37+
[00:00:00.116,000] <inf> app: Test-Read generic fuel gauge properties to verify which are supported
38+
[00:00:00.116,000] <inf> app: Info: not all properties are supported by all fuel gauges!
39+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_AVG_CURRENT" is not supported
40+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_CURRENT" is not supported
41+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_CHARGE_CUTOFF" is not supported
42+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_CYCLE_COUNT" is not supported
43+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_CONNECT_STATE" is not supported
44+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_FLAGS" is not supported
45+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_FULL_CHARGE_CAPACITY" is not supported
46+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_PRESENT_STATE" is not supported
47+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_REMAINING_CAPACITY" is not supported
48+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_RUNTIME_TO_EMPTY" is not supported
49+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_RUNTIME_TO_FULL" is not supported
50+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_SBS_MFR_ACCESS" is not supported
51+
[00:00:00.116,000] <inf> app: Property "FUEL_GAUGE_ABSOLUTE_STATE_OF_CHARGE" is not supported
52+
[00:00:00.122,000] <inf> app: Property "FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE" is supported
53+
[00:00:00.122,000] <inf> app: Relative state of charge: 100
54+
[00:00:00.122,000] <err> lc709203f: Thermistor not enabled
55+
[00:00:00.122,000] <inf> app: Property "FUEL_GAUGE_TEMPERATURE" is not supported
56+
[00:00:00.123,000] <inf> app: Property "FUEL_GAUGE_VOLTAGE" is supported
57+
[00:00:00.123,000] <inf> app: Voltage: 4190000
58+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_SBS_MODE" is supported
59+
[00:00:00.124,000] <inf> app: SBS mode: 1
60+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_CHARGE_CURRENT" is not supported
61+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_CHARGE_VOLTAGE" is not supported
62+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_STATUS" is not supported
63+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_DESIGN_CAPACITY" is supported
64+
[00:00:00.124,000] <inf> app: Design capacity: 500
65+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_DESIGN_VOLTAGE" is not supported
66+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_SBS_ATRATE" is not supported
67+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_SBS_ATRATE_TIME_TO_FULL" is not supported
68+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_SBS_ATRATE_TIME_TO_EMPTY" is not supported
69+
[00:00:00.124,000] <inf> app: Property "FUEL_GAUGE_SBS_ATRATE_OK" is not supported
70+
[00:00:00.125,000] <inf> app: Property "FUEL_GAUGE_SBS_REMAINING_CAPACITY_ALARM" is not supported
71+
[00:00:00.125,000] <inf> app: Property "FUEL_GAUGE_SBS_REMAINING_TIME_ALARM" is not supported
72+
[00:00:00.125,000] <err> app: Error: cannot get property "FUEL_GAUGE_MANUFACTURER_NAME": -88
73+
[00:00:00.125,000] <err> app: Error: cannot get property "FUEL_GAUGE_DEVICE_NAME": -88
74+
[00:00:00.125,000] <err> app: Error: cannot get property "FUEL_GAUGE_DEVICE_CHEMISTRY": -88
75+
[00:00:00.125,000] <inf> app: Property "FUEL_GAUGE_CURRENT_DIRECTION" is supported
76+
[00:00:00.125,000] <inf> app: Current direction: 0
77+
[00:00:00.126,000] <inf> app: Property "FUEL_GAUGE_STATE_OF_CHARGE_ALARM" is supported
78+
[00:00:00.126,000] <inf> app: State of charge alarm: 8
79+
[00:00:00.127,000] <inf> app: Property "FUEL_GAUGE_LOW_VOLTAGE_ALARM" is supported
80+
[00:00:00.127,000] <inf> app: Low voltage alarm: 0
81+
[00:00:00.127,000] <inf> app: Polling fuel gauge data 'FUEL_GAUGE_RELATIVE_STATE_OF_CHARGE' & 'FUEL_GAUGE_VOLTAGE'
82+
[00:00:00.128,000] <inf> app: Fuel gauge data: Charge: 100%, Voltage: 4190mV
83+
[00:00:05.130,000] <inf> app: Fuel gauge data: Charge: 100%, Voltage: 4190mV
84+
[00:00:10.131,000] <inf> app: Fuel gauge data: Charge: 100%, Voltage: 4190mV
85+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2025 Philipp Steiner <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
fuel-gauge0 = &lc709203f;
10+
};
11+
};
12+
13+
&i2c0 {
14+
lc709203f: lc709203f@0b {
15+
compatible = "onnn,lc709203f";
16+
status = "okay";
17+
reg = <0x0b>;
18+
power-domains = <&i2c_reg>;
19+
apa = "500mAh";
20+
battery-profile = <0x01>;
21+
};
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2025 Philipp Steiner <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
fuel-gauge0 = &max17048;
10+
};
11+
};
12+
13+
&i2c0 {
14+
max17048: max17048@36 {
15+
compatible = "maxim,max17048";
16+
status = "okay";
17+
reg = <0x36 >;
18+
power-domains = <&i2c_reg>;
19+
};
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2025 Philipp Steiner <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
fuel-gauge0 = &lc709203f;
10+
};
11+
};
12+
13+
&i2c0 {
14+
lc709203f: lc709203f@0b {
15+
compatible = "onnn,lc709203f";
16+
status = "okay";
17+
reg = <0x0b>;
18+
power-domains = <&i2c_reg>;
19+
apa = "500mAh";
20+
battery-profile = <0x01>;
21+
};
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2025 Philipp Steiner <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&i2c0 {
8+
max17048: max17048@36 {
9+
status = "okay";
10+
};
11+
};

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/boards/adafruit_feather_esp32s2_tft.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

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

samples/fuel_gauge/sample.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
sample:
2+
name: Fuel Gauge Sensor Sample
3+
tests:
4+
sample.sensor.fuel_gauge:
5+
build_only: true
6+
platform_allow:
7+
- nrf52840dk/nrf52840
8+
- adafruit_feather_esp32s2@B
9+
- adafruit_feather_esp32s2@C
10+
- adafruit_feather_esp32s2_tft
11+
- adafruit_feather_esp32s2_tft_reverse/esp32s2
12+
- adafruit_feather_esp32s3/esp32s3/procpu
13+
integration_platforms:
14+
- nrf52840dk/nrf52840
15+
- adafruit_feather_esp32s2@B
16+
- adafruit_feather_esp32s2@C
17+
- adafruit_feather_esp32s2_tft
18+
- adafruit_feather_esp32s2_tft_reverse/esp32s2
19+
- adafruit_feather_esp32s3/esp32s3/procpu
20+
tags: fuel_gauge

0 commit comments

Comments
 (0)