Skip to content

Commit 652f25e

Browse files
gmarullnashif
authored andcommitted
samples: boards: nrf: battery: fix usage without voltage divider
The nRF battery sample claims that it works without the need of a vbatt node (voltage divider). However, this was currently not possible because the sample code did have no means to extract the necessary ADC details (instance and channel) from Devicetree unless vbatt was present. Similar to other ADC samples, the zephyr,user node is used to define io-channels property in this case. The sample README has been updated with an example of how measurement without a voltage divider can be achieved. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 76c4591 commit 652f25e

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

samples/boards/nrf/battery/README.rst

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,40 @@ infrastructure to measure the voltage of the device power supply. Two
1111
power supply configurations are supported:
1212

1313
* If the board devicetree has a ``/vbatt`` node with compatible
14-
``voltage-divider`` then the voltage is measured using that divider.
15-
* Otherwise the power source is assumed to be directly connected to the
16-
digital voltage signal, and the internal source for ``Vdd`` is
17-
selected.
18-
19-
An example of a devicetree node describing a voltage divider for battery
20-
monitoring is:
21-
22-
.. code-block:: none
23-
24-
/ {
25-
vbatt {
26-
compatible = "voltage-divider";
27-
io-channels = <&adc 4>;
28-
output-ohms = <180000>;
29-
full-ohms = <(1500000 + 180000)>;
30-
power-gpios = <&sx1509b 4 0>;
31-
};
32-
};
14+
``voltage-divider`` then the voltage is measured using that divider. An
15+
example of a devicetree node describing a voltage divider for battery
16+
monitoring is:
17+
18+
.. code-block:: devicetree
19+
20+
/ {
21+
vbatt {
22+
compatible = "voltage-divider";
23+
io-channels = <&adc 4>;
24+
output-ohms = <180000>;
25+
full-ohms = <(1500000 + 180000)>;
26+
power-gpios = <&sx1509b 4 0>;
27+
};
28+
};
29+
30+
* If the board does not have a voltage divider and so no ``/vbatt`` node
31+
present, the ADC configuration (device and channel) needs to be provided via
32+
the ``zephyr,user`` node. In this case the power source is assumed to be
33+
directly connected to the digital voltage signal, and the internal source for
34+
``Vdd`` is selected. An example of a Devicetree configuration for this case is
35+
shown below:
36+
37+
.. code-block :: devicetree
38+
39+
/ {
40+
zephyr,user {
41+
io-channels = <&adc 4>;
42+
};
43+
};
3344
3445
Note that in many cases where there is no voltage divider the digital
3546
voltage will be fed from a regulator that provides a fixed voltage
36-
regardless of source voltage, rather than by the source directly. In
47+
regardless of source voltage, rather than by the source directly. In
3748
configuration involving a regulator the measured voltage will be
3849
constant.
3950

samples/boards/nrf/battery/src/battery.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
LOG_MODULE_REGISTER(BATTERY, CONFIG_ADC_LOG_LEVEL);
2222

2323
#define VBATT DT_PATH(vbatt)
24+
#define ZEPHYR_USER DT_PATH(zephyr_user)
2425

2526
#ifdef CONFIG_BOARD_THINGY52_NRF52832
2627
/* This board uses a divider that reduces max voltage to
@@ -71,7 +72,7 @@ static const struct divider_config divider_config = {
7172
.full_ohm = DT_PROP(VBATT, full_ohms),
7273
#else /* /vbatt exists */
7374
.io_channel = {
74-
DT_LABEL(DT_NODELABEL(adc)),
75+
DT_IO_CHANNELS_INPUT(ZEPHYR_USER),
7576
},
7677
#endif /* /vbatt exists */
7778
};
@@ -84,7 +85,11 @@ struct divider_data {
8485
int16_t raw;
8586
};
8687
static struct divider_data divider_data = {
88+
#if DT_NODE_HAS_STATUS(VBATT, okay)
8789
.adc = DEVICE_DT_GET(DT_IO_CHANNELS_CTLR(VBATT)),
90+
#else
91+
.adc = DEVICE_DT_GET(DT_IO_CHANNELS_CTLR(ZEPHYR_USER)),
92+
#endif
8893
};
8994

9095
static int divider_setup(void)

0 commit comments

Comments
 (0)