diff --git a/samples/drivers/vbatt/CMakeLists.txt b/samples/drivers/vbatt/CMakeLists.txt new file mode 100644 index 000000000000000..63173fe552efc6e --- /dev/null +++ b/samples/drivers/vbatt/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(vbatt) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/drivers/vbatt/README.rst b/samples/drivers/vbatt/README.rst new file mode 100644 index 000000000000000..2d9f6d6ce4e3be3 --- /dev/null +++ b/samples/drivers/vbatt/README.rst @@ -0,0 +1,50 @@ +.. _vbatt_sample: + +Battery Voltage Measurement Sample +################################## + +Overview +******** + +This sample demonstrates how one can use the configuration of the Zephyr ADC infrastructure to +measure the voltage of the device power supply using a voltage-divider. The sample assumes a +regulator that provides a fixed voltage to IO38 of the esp32 behind a voltage divider. In this +sample the voltage divider consists of 300k and 100k Ohms resistors. It will then print the measured +battery voltage to the console. If you take care to not power the device thru the usb port, you can +see how the battery voltage decreases over time, Thus indicating the current charging level. + +Application Details +=================== + +The application initializes battery measurement on startup, then loops +displaying the battery status every five seconds. + +Requirements +************ + +A charging regulator (e.g. MAX777) connected to a voltage divider to the + + +Building and Running +******************** + +This application can be built and executed on QEMU as follows: + +.. zephyr-app-commands:: + :zephyr-app: samples/drivers/vbatt + :host-os: unix + :board: esp32_devkitc_wrover + :goals: build flash + :compact: + +To build for another board, change "qemu_x86" above to that board's name. + +Sample Output +============= + +.. code-block:: console + + bat=1.85V + bat=1.80V + bat=1.85V + bat=1.80V diff --git a/samples/drivers/vbatt/boards/esp32_devkitc_wrover.overlay b/samples/drivers/vbatt/boards/esp32_devkitc_wrover.overlay new file mode 100644 index 000000000000000..813fbb53648fc4e --- /dev/null +++ b/samples/drivers/vbatt/boards/esp32_devkitc_wrover.overlay @@ -0,0 +1,28 @@ +/ { + aliases { + battery = &vbatt; + }; + + vbatt: vbatt { + compatible = "zephyr,usb-c-vbus-adc"; + status = "okay"; + io-channels = <&adc0 3>; + output-ohms = <300000>; + full-ohms = <(300000 + 100000)>; + }; + +}; + +&adc0 { + status = "okay"; + #address-cells = <1>; + #size-cells = <0>; + + channel@3 { + reg = <3>; + zephyr,gain = "ADC_GAIN_1_4"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <12>; + }; +}; diff --git a/samples/drivers/vbatt/prj.conf b/samples/drivers/vbatt/prj.conf new file mode 100644 index 000000000000000..e7052e3e18f95a6 --- /dev/null +++ b/samples/drivers/vbatt/prj.conf @@ -0,0 +1,5 @@ +CONFIG_USBC_VBUS_ADC=y +CONFIG_USBC_VBUS_DRIVER=y + +CONFIG_GPIO=y +CONFIG_ADC=y diff --git a/samples/drivers/vbatt/src/main.c b/samples/drivers/vbatt/src/main.c new file mode 100644 index 000000000000000..866b7c56c140b2a --- /dev/null +++ b/samples/drivers/vbatt/src/main.c @@ -0,0 +1,17 @@ +#include +#include + +#include +#define BAT_NODE DT_ALIAS(battery) +static const struct device *bat_dev = DEVICE_DT_GET(BAT_NODE); + +int main(void) +{ + int rc = 0; + int mea; + for(;;) { + usbc_vbus_measure(bat_dev, &mea); + printf("bat=%i.%iV", mea/100,mea%100); + k_sleep(K_SECONDS(5)); + } +}