Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: USB C vbus Kconfig #62740

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions samples/drivers/vbatt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
50 changes: 50 additions & 0 deletions samples/drivers/vbatt/README.rst
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by "indicates the current charging level". Your sample only prints the battery voltage. Without knowing the battery capacity and/or empty voltage, I'm not sure how one infers the charge 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is incomplete.



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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To build for another board, change "qemu_x86" above to that board's name.
To build for another board, change "esp32_devkitc_wrover" above to that board's name.


Sample Output
=============

.. code-block:: console

bat=1.85V
bat=1.80V
bat=1.85V
bat=1.80V
34 changes: 34 additions & 0 deletions samples/drivers/vbatt/boards/esp32_devkitc_wrover.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2023 Mark Kettner <mark@kettner.io>
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
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 = <ADC_ACQ_TIME_DEFAULT>;
zephyr,resolution = <12>;
};
};
5 changes: 5 additions & 0 deletions samples/drivers/vbatt/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CONFIG_USBC_VBUS_ADC=y
CONFIG_USBC_VBUS_DRIVER=y

CONFIG_GPIO=y
CONFIG_ADC=y
24 changes: 24 additions & 0 deletions samples/drivers/vbatt/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 Mark Kettner <mark@kettner.io>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/device.h>
#include <zephyr/drivers/usb_c/usbc_vbus.h>
#include <zephyr/kernel.h>

#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));
}
}
Loading