Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
current_sense_amplifier.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_CURRENT_SENSE_AMPLIFIER_H_
8#define ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_AMPLIFIER_H_
9
10#include <zephyr/drivers/adc.h>
11#include <zephyr/drivers/gpio.h>
12
22
33#define CURRENT_SENSE_AMPLIFIER_DT_SPEC_GET(node_id) \
34 { \
35 .port = ADC_DT_SPEC_GET(node_id), \
36 .power_gpio = GPIO_DT_SPEC_GET_OR(node_id, power_gpios, {0}), \
37 .sense_milli_ohms = DT_PROP(node_id, sense_resistor_milli_ohms), \
38 .sense_gain_mult = DT_PROP(node_id, sense_gain_mult), \
39 .sense_gain_div = DT_PROP(node_id, sense_gain_div), \
40 .noise_threshold = DT_PROP(node_id, zephyr_noise_threshold), \
41 .enable_calibration = DT_PROP_OR(node_id, enable_calibration, false), \
42 }
43
51static inline void
53 int32_t *v_to_i)
54{
55 /* store in a temporary 64 bit variable to prevent overflow during calculation */
56 int64_t tmp = *v_to_i;
57
58 /* (INT32_MAX * 1000 * UINT16_MAX) < INT64_MAX
59 * Therefore all multiplications can be done before divisions, preserving resolution.
60 */
61 tmp = tmp * 1000 * spec->sense_gain_div / spec->sense_milli_ohms / spec->sense_gain_mult;
62
63 *v_to_i = (int32_t)tmp;
64}
65
66#endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_AMPLIFIER_H_ */
static void current_sense_amplifier_scale_dt(const struct current_sense_amplifier_dt_spec *spec, int32_t *v_to_i)
Calculates the actual amperage from the measured voltage.
Definition current_sense_amplifier.h:52
ADC public API header file.
Public APIs for GPIO drivers.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT64_TYPE__ int64_t
Definition stdint.h:75
Container for ADC channel information specified in devicetree.
Definition adc.h:265
Definition current_sense_amplifier.h:13
uint16_t noise_threshold
Definition current_sense_amplifier.h:19
bool enable_calibration
Definition current_sense_amplifier.h:20
uint16_t sense_gain_div
Definition current_sense_amplifier.h:18
uint16_t sense_gain_mult
Definition current_sense_amplifier.h:17
uint32_t sense_milli_ohms
Definition current_sense_amplifier.h:16
struct adc_dt_spec port
Definition current_sense_amplifier.h:14
struct gpio_dt_spec power_gpio
Definition current_sense_amplifier.h:15
Container for GPIO pin information specified in devicetree.
Definition gpio.h:289