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 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_sim.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 1 : 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 1 : int adc_emul_const_value_set(const struct device *dev, unsigned int chan, 73 : uint32_t value); 74 : 75 : /** 76 : * @brief Set constant raw value input for emulated ADC @p chan 77 : * 78 : * @param dev The emulated ADC device 79 : * @param chan The channel of ADC which input is assigned 80 : * @param raw_value New raw value to assign to @p chan input 81 : * 82 : * @return 0 on success 83 : * @return -EINVAL if an invalid argument is provided 84 : */ 85 1 : int adc_emul_const_raw_value_set(const struct device *dev, unsigned int chan, uint32_t raw_value); 86 : 87 : /** 88 : * @brief Set function used to obtain voltage for input of emulated 89 : * ADC @p chan 90 : * 91 : * @param dev The emulated ADC device 92 : * @param chan The channel of ADC to which @p func is assigned 93 : * @param func New function to assign to @p chan 94 : * @param data Pointer to data passed to @p func on call 95 : * 96 : * @return 0 on success 97 : * @return -EINVAL if an invalid argument is provided 98 : */ 99 1 : int adc_emul_value_func_set(const struct device *dev, unsigned int chan, 100 : adc_emul_value_func func, void *data); 101 : 102 : /** 103 : * @brief Set function used to obtain voltage for raw input value of emulated 104 : * ADC @p chan 105 : * 106 : * @param dev The emulated ADC device 107 : * @param chan The channel of ADC to which @p func is assigned 108 : * @param func New function to assign to @p chan 109 : * @param data Pointer to data passed to @p func on call 110 : * 111 : * @return 0 on success 112 : * @return -EINVAL if an invalid argument is provided 113 : */ 114 1 : int adc_emul_raw_value_func_set(const struct device *dev, unsigned int chan, 115 : adc_emul_value_func func, void *data); 116 : 117 : /** 118 : * @brief Set reference voltage 119 : * 120 : * @param dev The emulated ADC device 121 : * @param ref Reference config which is changed 122 : * @param value New reference voltage in mV 123 : * 124 : * @return 0 on success 125 : * @return -EINVAL if an invalid argument is provided 126 : */ 127 1 : int adc_emul_ref_voltage_set(const struct device *dev, enum adc_reference ref, 128 : uint16_t value); 129 : /** 130 : * @} 131 : */ 132 : 133 : #ifdef __cplusplus 134 : } 135 : #endif 136 : 137 : #endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_ADC_EMUL_H_ */