-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example using the usb_c vbus driver
Signed-off-by: Mark Kettner <mark@kettner.io>
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Run compliance checks on patch series (PR)LINE_SPACING
Check warning on line 12 in samples/drivers/vbatt/src/main.c GitHub Actions / Run compliance checks on patch series (PR)SUSPECT_CODE_INDENT
|
||
usbc_vbus_measure(bat_dev, &mea); | ||
Check failure on line 13 in samples/drivers/vbatt/src/main.c GitHub Actions / Run compliance checks on patch series (PR)CODE_INDENT
|
||
printf("bat=%i.%iV", mea/100,mea%100); | ||
k_sleep(K_SECONDS(5)); | ||
} | ||
Check failure on line 16 in samples/drivers/vbatt/src/main.c GitHub Actions / Run compliance checks on patch series (PR)CODE_INDENT
|
||
} |