Skip to content

Commit

Permalink
add example using the usb_c vbus driver
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Kettner <mark@kettner.io>
  • Loading branch information
mkettn committed Sep 16, 2023
1 parent 4f5efc7 commit 364f0f5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
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.

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
28 changes: 28 additions & 0 deletions samples/drivers/vbatt/boards/esp32_devkitc_wrover.overlay
Original file line number Diff line number Diff line change
@@ -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 = <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
17 changes: 17 additions & 0 deletions samples/drivers/vbatt/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#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(;;) {

Check warning on line 12 in samples/drivers/vbatt/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LINE_SPACING

samples/drivers/vbatt/src/main.c:12 Missing a blank line after declarations

Check warning on line 12 in samples/drivers/vbatt/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

SUSPECT_CODE_INDENT

samples/drivers/vbatt/src/main.c:12 suspect code indent for conditional statements (8, 10)

Check failure on line 12 in samples/drivers/vbatt/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

SPACING

samples/drivers/vbatt/src/main.c:12 space required before the open parenthesis '('
usbc_vbus_measure(bat_dev, &mea);

Check failure on line 13 in samples/drivers/vbatt/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CODE_INDENT

samples/drivers/vbatt/src/main.c:13 code indent should use tabs where possible

Check warning on line 13 in samples/drivers/vbatt/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LEADING_SPACE

samples/drivers/vbatt/src/main.c:13 please, no spaces at the start of a line
printf("bat=%i.%iV", mea/100,mea%100);

Check failure on line 14 in samples/drivers/vbatt/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

SPACING

samples/drivers/vbatt/src/main.c:14 space required after that ',' (ctx:VxV)
k_sleep(K_SECONDS(5));
}

Check failure on line 16 in samples/drivers/vbatt/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CODE_INDENT

samples/drivers/vbatt/src/main.c:16 code indent should use tabs where possible

Check warning on line 16 in samples/drivers/vbatt/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LEADING_SPACE

samples/drivers/vbatt/src/main.c:16 please, no spaces at the start of a line
}

0 comments on commit 364f0f5

Please sign in to comment.