1 /**
2  * @file
3  *
4  * @brief Backend API for emulated ADC
5  */
6 
7 /*
8  * Copyright 2021 Google LLC
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 #ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_ADC_EMUL_H_
13 #define ZEPHYR_INCLUDE_DRIVERS_ADC_ADC_EMUL_H_
14 
15 #include <zephyr/types.h>
16 #include <zephyr/drivers/adc.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /**
23  * @brief Emulated ADC backend API
24  * @defgroup adc_emul Emulated ADC
25  * @ingroup adc_interface
26  * @{
27  *
28  * Behaviour of emulated ADC is application-defined. As-such, each
29  * application may
30  *
31  * - define a Device Tree overlay file to indicate the number of ADC
32  *   controllers as well as the number of channels for each controller
33  * - set default reference voltages in Device Tree or using
34  *   @ref adc_emul_ref_voltage_set
35  * - asynchronously call @ref adc_emul_const_value_set in order to set
36  *   constant mV value on emulated ADC input
37  * - asynchronously call @ref adc_emul_value_func_set in order to assign
38  *   function which will be used to obtain voltage on emulated ADC input
39  *
40  * An example of an appropriate Device Tree overlay file is in
41  * tests/drivers/adc/adc_api/boards/native_posix.overlay
42  *
43  * An example of using emulated ADC backend API is in the file
44  * tests/drivers/adc/adc_emul/src/main.c
45  */
46 
47 /**
48  * @brief Type definition of the function which is used to obtain ADC
49  *        mV input values
50  *
51  * @param dev Pointer to the device structure for the driver instance
52  * @param chan ADC channel to sample
53  * @param data User data which was passed on @ref adc_emul_value_func_set
54  * @param result The result value which will be set as input for ADC @p chan
55  *
56  * @return 0 on success
57  * @return other as error code which ends ADC context
58  */
59 typedef int (*adc_emul_value_func)(const struct device *dev, unsigned int chan,
60 				   void *data, uint32_t *result);
61 
62 /**
63  * @brief Set constant mV value input for emulated ADC @p chan
64  *
65  * @param dev The emulated ADC device
66  * @param chan The channel of ADC which input is assigned
67  * @param value New voltage in mV to assign to @p chan input
68  *
69  * @return 0 on success
70  * @return -EINVAL if an invalid argument is provided
71  */
72 int adc_emul_const_value_set(const struct device *dev, unsigned int chan,
73 			     uint32_t value);
74 
75 /**
76  * @brief Set function used to obtain voltage for input of emulated
77  *        ADC @p chan
78  *
79  * @param dev The emulated ADC device
80  * @param chan The channel of ADC to which @p func is assigned
81  * @param func New function to assign to @p chan
82  * @param data Pointer to data passed to @p func on call
83  *
84  * @return 0 on success
85  * @return -EINVAL if an invalid argument is provided
86  */
87 int adc_emul_value_func_set(const struct device *dev, unsigned int chan,
88 			    adc_emul_value_func func, void *data);
89 
90 /**
91  * @brief Set reference voltage
92  *
93  * @param dev The emulated ADC device
94  * @param ref Reference config which is changed
95  * @param value New reference voltage in mV
96  *
97  * @return 0 on success
98  * @return -EINVAL if an invalid argument is provided
99  */
100 int adc_emul_ref_voltage_set(const struct device *dev, enum adc_reference ref,
101 			     uint16_t value);
102 /**
103  * @}
104  */
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_ADC_EMUL_H_ */
111