Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_AMPLIFIER_H_
14#define ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_AMPLIFIER_H_
15
16#include <zephyr/drivers/adc.h>
17#include <zephyr/drivers/gpio.h>
18
25
53
64#define CURRENT_SENSE_AMPLIFIER_DT_SPEC_GET(node_id) \
65 { \
66 .port = ADC_DT_SPEC_GET(node_id), \
67 .power_gpio = GPIO_DT_SPEC_GET_OR(node_id, power_gpios, {0}), \
68 .sense_milli_ohms = DT_PROP(node_id, sense_resistor_milli_ohms), \
69 .sense_gain_mult = DT_PROP(node_id, sense_gain_mult), \
70 .sense_gain_div = DT_PROP(node_id, sense_gain_div), \
71 .noise_threshold = DT_PROP(node_id, zephyr_noise_threshold), \
72 .zero_current_voltage_mv = DT_PROP(node_id, zero_current_voltage_mv), \
73 .gain_extended_range = DT_STRING_TOKEN_OR(node_id, gain_extended_range, 0xFF), \
74 .enable_calibration = DT_PROP_OR(node_id, enable_calibration, false), \
75 }
76
84static inline void
86 int32_t *v_to_i)
87{
88 /* store in a temporary 64 bit variable to prevent overflow during calculation */
89 int64_t tmp = *v_to_i;
90
91 /* (INT32_MAX * 1000 * UINT16_MAX) < INT64_MAX
92 * Therefore all multiplications can be done before divisions, preserving resolution.
93 */
94 tmp = tmp - spec->zero_current_voltage_mv;
95 tmp = tmp * 1000 * spec->sense_gain_div / spec->sense_milli_ohms / spec->sense_gain_mult;
96
97 *v_to_i = (int32_t)tmp;
98}
99
108static inline int32_t
110 int32_t microvolts)
111{
112 int64_t temp = microvolts;
113 /* Perform all multiplications first to limit rounding errors.
114 * Micro-volts/milli-ohms would result in milli-amps, scale by factor of 1,000.
115 * Proof that the following cannot overflow:
116 * (millivolts * micro_scale) * max_gain <= INT64_MAX
117 * (INT32_MAX * 1000) * UINT16_MAX <= INT64_MAX
118 * ~2**57 <= 2**63
119 */
120 temp = temp - 1000 * (int64_t)(spec->zero_current_voltage_mv);
121 int64_t scaled = temp * 1000 * spec->sense_gain_div;
122 /* Perform final divisions */
123 return scaled / spec->sense_gain_mult / spec->sense_milli_ohms;
124}
125
127
128#endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_AMPLIFIER_H_ */
Main header file for ADC (Analog-to-Digital Converter) driver API.
Main header file for GPIO driver API.
static int32_t current_sense_amplifier_scale_ua_dt(const struct current_sense_amplifier_dt_spec *spec, int32_t microvolts)
Calculates the actual amperage from a measured voltage.
Definition current_sense_amplifier.h:109
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 a measured voltage.
Definition current_sense_amplifier.h:85
adc_gain
ADC channel gain factors.
Definition adc.h:42
__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
__INT16_TYPE__ int16_t
Definition stdint.h:73
Container for ADC channel information specified in devicetree.
Definition adc.h:291
Current sense amplifier DT struct.
Definition current_sense_amplifier.h:33
uint16_t noise_threshold
Noise threshold.
Definition current_sense_amplifier.h:45
bool enable_calibration
Enable calibration.
Definition current_sense_amplifier.h:51
uint16_t sense_gain_div
Sense amplifier gain divider.
Definition current_sense_amplifier.h:43
int16_t zero_current_voltage_mv
Voltage at zero current in millivolts.
Definition current_sense_amplifier.h:47
uint16_t sense_gain_mult
Sense amplifier gain multiplier.
Definition current_sense_amplifier.h:41
enum adc_gain gain_extended_range
Gain range.
Definition current_sense_amplifier.h:49
uint32_t sense_milli_ohms
Sense resistor value in milliohms.
Definition current_sense_amplifier.h:39
struct adc_dt_spec port
ADC channel info.
Definition current_sense_amplifier.h:35
struct gpio_dt_spec power_gpio
GPIO to enable the amplifier.
Definition current_sense_amplifier.h:37
Container for GPIO pin information specified in devicetree.
Definition gpio.h:296