Skip to content

Commit 374662a

Browse files
committed
drivers: adc: adc_emul: implement raw func set function in adc_emul
allow setting a function as generator of raw adc values in adc_emul Signed-off-by: Nathan Olff <[email protected]>
1 parent f7febfb commit 374662a

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

drivers/adc/adc_emul.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum adc_emul_input_source {
3232
ADC_EMUL_CONST_VALUE,
3333
ADC_EMUL_CUSTOM_FUNC,
3434
ADC_EMUL_CONST_RAW_VALUE,
35+
ADC_EMUL_CUSTOM_FUNC_RAW_VALUE,
3536
};
3637

3738
/**
@@ -40,7 +41,7 @@ enum adc_emul_input_source {
4041
* This structure contains configuration of one channel of emulated ADC.
4142
*/
4243
struct adc_emul_chan_cfg {
43-
/** Pointer to function used to obtain input mV */
44+
/** Pointer to function used to obtain input mV or raw input value */
4445
adc_emul_value_func func;
4546
/** Pointer to data that are passed to @a func on call */
4647
void *func_data;
@@ -176,6 +177,31 @@ int adc_emul_value_func_set(const struct device *dev, unsigned int chan, adc_emu
176177
return 0;
177178
}
178179

180+
int adc_emul_raw_value_func_set(const struct device *dev, unsigned int chan,
181+
adc_emul_value_func func, void *func_data)
182+
{
183+
const struct adc_emul_config *config = dev->config;
184+
struct adc_emul_data *data = dev->data;
185+
struct adc_emul_chan_cfg *chan_cfg;
186+
187+
if (chan >= config->num_channels) {
188+
LOG_ERR("unsupported channel %d", chan);
189+
return -EINVAL;
190+
}
191+
192+
chan_cfg = &data->chan_cfg[chan];
193+
194+
k_mutex_lock(&data->cfg_mtx, K_FOREVER);
195+
196+
chan_cfg->func = func;
197+
chan_cfg->func_data = func_data;
198+
chan_cfg->input = ADC_EMUL_CUSTOM_FUNC_RAW_VALUE;
199+
200+
k_mutex_unlock(&data->cfg_mtx);
201+
202+
return 0;
203+
}
204+
179205
int adc_emul_ref_voltage_set(const struct device *dev, enum adc_reference ref,
180206
uint16_t value)
181207
{
@@ -456,6 +482,15 @@ static int adc_emul_get_chan_value(struct adc_emul_data *data,
456482
temp = chan_cfg->const_value;
457483
goto check_bound_and_out;
458484

485+
case ADC_EMUL_CUSTOM_FUNC_RAW_VALUE:
486+
err = chan_cfg->func(data->dev, chan, chan_cfg->func_data, &input_mV);
487+
if (err) {
488+
LOG_ERR("failed to read channel %d (err %d)", chan, err);
489+
goto out;
490+
}
491+
temp = input_mV;
492+
goto check_bound_and_out;
493+
459494
default:
460495
LOG_ERR("unknown input source %d", chan_cfg->input);
461496
err = -EINVAL;

include/zephyr/drivers/adc/adc_emul.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ int adc_emul_const_raw_value_set(const struct device *dev, unsigned int chan, ui
9999
int adc_emul_value_func_set(const struct device *dev, unsigned int chan,
100100
adc_emul_value_func func, void *data);
101101

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+
int adc_emul_raw_value_func_set(const struct device *dev, unsigned int chan,
115+
adc_emul_value_func func, void *data);
116+
102117
/**
103118
* @brief Set reference voltage
104119
*

0 commit comments

Comments
 (0)