Line data Source code
1 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 io_emulators
26 : * @ingroup adc_interface
27 : * @{
28 : *
29 : * Behaviour of emulated ADC is application-defined. As-such, each
30 : * application may
31 : *
32 : * - define a Device Tree overlay file to indicate the number of ADC
33 : * controllers as well as the number of channels for each controller
34 : * - set default reference voltages in Device Tree or using
35 : * @ref adc_emul_ref_voltage_set
36 : * - asynchronously call @ref adc_emul_const_value_set in order to set
37 : * constant mV value on emulated ADC input
38 : * - asynchronously call @ref adc_emul_value_func_set in order to assign
39 : * function which will be used to obtain voltage on emulated ADC input
40 : *
41 : * An example of an appropriate Device Tree overlay file is in
42 : * tests/drivers/adc/adc_api/boards/native_sim.overlay
43 : *
44 : * An example of using emulated ADC backend API is in the file
45 : * tests/drivers/adc/adc_emul/src/main.c
46 : */
47 :
48 : /**
49 : * @brief Type definition of the function which is used to obtain ADC
50 : * mV input values
51 : *
52 : * @param dev Pointer to the device structure for the driver instance
53 : * @param chan ADC channel to sample
54 : * @param data User data which was passed on @ref adc_emul_value_func_set
55 : * @param result The result value which will be set as input for ADC @p chan
56 : *
57 : * @return 0 on success
58 : * @return other as error code which ends ADC context
59 : */
60 1 : typedef int (*adc_emul_value_func)(const struct device *dev, unsigned int chan,
61 : void *data, uint32_t *result);
62 :
63 : /**
64 : * @brief Set constant mV value input for emulated ADC @p chan
65 : *
66 : * @param dev The emulated ADC device
67 : * @param chan The channel of ADC which input is assigned
68 : * @param value New voltage in mV to assign to @p chan input
69 : *
70 : * @return 0 on success
71 : * @return -EINVAL if an invalid argument is provided
72 : */
73 1 : int adc_emul_const_value_set(const struct device *dev, unsigned int chan,
74 : uint32_t value);
75 :
76 : /**
77 : * @brief Set constant raw value input for emulated ADC @p chan
78 : *
79 : * @param dev The emulated ADC device
80 : * @param chan The channel of ADC which input is assigned
81 : * @param raw_value New raw value to assign to @p chan input
82 : *
83 : * @return 0 on success
84 : * @return -EINVAL if an invalid argument is provided
85 : */
86 1 : int adc_emul_const_raw_value_set(const struct device *dev, unsigned int chan, uint32_t raw_value);
87 :
88 : /**
89 : * @brief Set function used to obtain voltage for input of emulated
90 : * ADC @p chan
91 : *
92 : * @param dev The emulated ADC device
93 : * @param chan The channel of ADC to which @p func is assigned
94 : * @param func New function to assign to @p chan
95 : * @param data Pointer to data passed to @p func on call
96 : *
97 : * @return 0 on success
98 : * @return -EINVAL if an invalid argument is provided
99 : */
100 1 : int adc_emul_value_func_set(const struct device *dev, unsigned int chan,
101 : adc_emul_value_func func, void *data);
102 :
103 : /**
104 : * @brief Set function used to obtain voltage for raw input value of emulated
105 : * ADC @p chan
106 : *
107 : * @param dev The emulated ADC device
108 : * @param chan The channel of ADC to which @p func is assigned
109 : * @param func New function to assign to @p chan
110 : * @param data Pointer to data passed to @p func on call
111 : *
112 : * @return 0 on success
113 : * @return -EINVAL if an invalid argument is provided
114 : */
115 1 : int adc_emul_raw_value_func_set(const struct device *dev, unsigned int chan,
116 : adc_emul_value_func func, void *data);
117 :
118 : /**
119 : * @brief Set reference voltage
120 : *
121 : * @param dev The emulated ADC device
122 : * @param ref Reference config which is changed
123 : * @param value New reference voltage in mV
124 : *
125 : * @return 0 on success
126 : * @return -EINVAL if an invalid argument is provided
127 : */
128 1 : int adc_emul_ref_voltage_set(const struct device *dev, enum adc_reference ref,
129 : uint16_t value);
130 : /**
131 : * @}
132 : */
133 :
134 : #ifdef __cplusplus
135 : }
136 : #endif
137 :
138 : #endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_ADC_EMUL_H_ */
|