Line data Source code
1 0 : /*
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_SHUNT_H_
8 : #define ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_SHUNT_H_
9 :
10 : #include <zephyr/drivers/adc.h>
11 :
12 0 : struct current_sense_shunt_dt_spec {
13 0 : const struct adc_dt_spec port;
14 0 : uint32_t shunt_micro_ohms;
15 : };
16 :
17 : /**
18 : * @brief Get current sensor information from devicetree.
19 : *
20 : * This returns a static initializer for a @p current_sense_shunt_dt_spec structure
21 : * given a devicetree node.
22 : *
23 : * @param node_id Devicetree node identifier.
24 : *
25 : * @return Static initializer for an current_sense_shunt_dt_spec structure.
26 : */
27 1 : #define CURRENT_SENSE_SHUNT_DT_SPEC_GET(node_id) \
28 : { \
29 : .port = ADC_DT_SPEC_GET(node_id), \
30 : .shunt_micro_ohms = DT_PROP(node_id, shunt_resistor_micro_ohms), \
31 : }
32 :
33 : /**
34 : * @brief Calculates the actual amperage from the measured voltage
35 : *
36 : * @param[in] spec current sensor specification from Devicetree.
37 : * @param[in,out] v_to_i Pointer to the measured voltage in millivolts on input, and the
38 : * corresponding scaled current value in milliamps on output.
39 : */
40 1 : static inline void current_sense_shunt_scale_dt(const struct current_sense_shunt_dt_spec *spec,
41 : int32_t *v_to_i)
42 : {
43 : /* store in a temporary 64 bit variable to prevent overflow during calculation */
44 : int64_t tmp = *v_to_i;
45 :
46 : /* multiplies by 1,000,000 before dividing by shunt resistance in micro-ohms. */
47 : tmp = tmp * 1000000 / spec->shunt_micro_ohms;
48 :
49 : *v_to_i = (int32_t)tmp;
50 : }
51 :
52 : #endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_CURRENT_SENSE_SHUNT_H_ */
|