Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
voltage_divider.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 The ChromiumOS Authors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_VOLTAGE_DIVIDER_H_
8#define ZEPHYR_INCLUDE_DRIVERS_ADC_VOLTAGE_DIVIDER_H_
9
10#include <zephyr/drivers/adc.h>
11
17
28#define VOLTAGE_DIVIDER_DT_SPEC_GET(node_id) \
29 { \
30 .port = ADC_DT_SPEC_GET(node_id), \
31 .full_ohms = DT_PROP_OR(node_id, full_ohms, 0), \
32 .output_ohms = DT_PROP(node_id, output_ohms), \
33 }
34
45static inline int voltage_divider_scale_dt(const struct voltage_divider_dt_spec *spec,
46 int32_t *v_to_v)
47{
48 /* cannot be scaled if "full_ohms" is not specified */
49 if (spec->full_ohms == 0) {
50 return -ENOTSUP;
51 }
52
53 /* voltage scaled by voltage divider values using DT binding */
54 *v_to_v = (int64_t)*v_to_v * spec->full_ohms / spec->output_ohms;
55
56 return 0;
57}
58
59#endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_VOLTAGE_DIVIDER_H_ */
ADC public API header file.
#define ENOTSUP
Unsupported value.
Definition errno.h:114
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__INT64_TYPE__ int64_t
Definition stdint.h:75
Container for ADC channel information specified in devicetree.
Definition adc.h:264
Definition voltage_divider.h:12
uint32_t full_ohms
Definition voltage_divider.h:14
const struct adc_dt_spec port
Definition voltage_divider.h:13
uint32_t output_ohms
Definition voltage_divider.h:15
static int voltage_divider_scale_dt(const struct voltage_divider_dt_spec *spec, int32_t *v_to_v)
Calculates the actual voltage from the measured voltage.
Definition voltage_divider.h:45