forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds DT macro initializer and scaling function for voltage divider. Signed-off-by: Jason Yuan <[email protected]>
- Loading branch information
1 parent
f7c1f91
commit 67bdd17
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
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,59 @@ | ||
/* | ||
* Copyright (c) 2023 The ChromiumOS Authors | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_VOLTAGE_DIVIDER_H_ | ||
#define ZEPHYR_INCLUDE_DRIVERS_ADC_VOLTAGE_DIVIDER_H_ | ||
|
||
#include <zephyr/drivers/adc.h> | ||
|
||
struct voltage_divider_dt_spec { | ||
const struct adc_dt_spec port; | ||
uint32_t full_ohms; | ||
uint32_t output_ohms; | ||
}; | ||
|
||
/** | ||
* @brief Get voltage divider information from devicetree. | ||
* | ||
* This returns a static initializer for a @p voltage_divider_dt_spec structure | ||
* given a devicetree node. | ||
* | ||
* @param node_id Devicetree node identifier. | ||
* | ||
* @return Static initializer for an voltage_divider_dt_spec structure. | ||
*/ | ||
#define VOLTAGE_DIVIDER_DT_SPEC_GET(node_id) \ | ||
{ \ | ||
.port = ADC_DT_SPEC_GET(node_id), \ | ||
.full_ohms = DT_PROP_OR(node_id, full_ohms, 0), \ | ||
.output_ohms = DT_PROP(node_id, output_ohms), \ | ||
} | ||
|
||
/** | ||
* @brief Calculates the actual voltage from the measured voltage | ||
* | ||
* @param[in] spec voltage divider specification from Devicetree. | ||
* @param[in,out] v_to_v Pointer to the measured voltage on input, and the | ||
* corresponding scaled voltage value on output. | ||
* | ||
* @retval 0 on success | ||
* @retval -ENOTSUP if "full_ohms" is not specified | ||
*/ | ||
static inline int voltage_divider_scale_dt(const struct voltage_divider_dt_spec *spec, | ||
int32_t *v_to_v) | ||
{ | ||
/* cannot be scaled if "full_ohms" is not specified */ | ||
if (spec->full_ohms == 0) { | ||
return -ENOTSUP; | ||
} | ||
|
||
/* voltage scaled by voltage divider values using DT binding */ | ||
*v_to_v = *v_to_v * spec->full_ohms / spec->output_ohms; | ||
|
||
return 0; | ||
} | ||
|
||
#endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_VOLTAGE_DIVIDER_H_ */ |