Skip to content

Commit 0c9039e

Browse files
committed
drivers: adc: adc_emul: write function to set const raw value
implement storage and retrieval of raw value in adc_emul Signed-off-by: Nathan Olff <[email protected]>
1 parent c710f88 commit 0c9039e

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

drivers/adc/adc_emul.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef uint16_t adc_emul_res_t;
3131
enum adc_emul_input_source {
3232
ADC_EMUL_CONST_VALUE,
3333
ADC_EMUL_CUSTOM_FUNC,
34+
ADC_EMUL_CONST_RAW_VALUE,
3435
};
3536

3637
/**
@@ -43,7 +44,7 @@ struct adc_emul_chan_cfg {
4344
adc_emul_value_func func;
4445
/** Pointer to data that are passed to @a func on call */
4546
void *func_data;
46-
/** Constant mV input value */
47+
/** Constant mV input value or raw input value */
4748
uint32_t const_value;
4849
/** Gain used on output value */
4950
enum adc_gain gain;
@@ -127,8 +128,31 @@ int adc_emul_const_value_set(const struct device *dev, unsigned int chan,
127128
return 0;
128129
}
129130

130-
int adc_emul_value_func_set(const struct device *dev, unsigned int chan,
131-
adc_emul_value_func func, void *func_data)
131+
int adc_emul_const_raw_value_set(const struct device *dev, unsigned int chan, uint32_t raw_value)
132+
{
133+
const struct adc_emul_config *config = dev->config;
134+
struct adc_emul_data *data = dev->data;
135+
struct adc_emul_chan_cfg *chan_cfg;
136+
137+
if (chan >= config->num_channels) {
138+
LOG_ERR("unsupported channel %d", chan);
139+
return -EINVAL;
140+
}
141+
142+
chan_cfg = &data->chan_cfg[chan];
143+
144+
k_mutex_lock(&data->cfg_mtx, K_FOREVER);
145+
146+
chan_cfg->input = ADC_EMUL_CONST_RAW_VALUE;
147+
chan_cfg->const_value = raw_value;
148+
149+
k_mutex_unlock(&data->cfg_mtx);
150+
151+
return 0;
152+
}
153+
154+
int adc_emul_value_func_set(const struct device *dev, unsigned int chan, adc_emul_value_func func,
155+
void *func_data)
132156
{
133157
const struct adc_emul_config *config = dev->config;
134158
struct adc_emul_data *data = dev->data;
@@ -428,6 +452,10 @@ static int adc_emul_get_chan_value(struct adc_emul_data *data,
428452
}
429453
break;
430454

455+
case ADC_EMUL_CONST_RAW_VALUE:
456+
temp = chan_cfg->const_value;
457+
goto check_bound_and_out;
458+
431459
default:
432460
LOG_ERR("unknown input source %d", chan_cfg->input);
433461
err = -EINVAL;
@@ -446,6 +474,7 @@ static int adc_emul_get_chan_value(struct adc_emul_data *data,
446474
/* Calculate output value */
447475
temp = (uint64_t)input_mV * data->res_mask / ref_v;
448476

477+
check_bound_and_out:
449478
/* If output value is greater than resolution, it has to be trimmed */
450479
if (temp > data->res_mask) {
451480
temp = data->res_mask;

include/zephyr/drivers/adc/adc_emul.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ typedef int (*adc_emul_value_func)(const struct device *dev, unsigned int chan,
7272
int adc_emul_const_value_set(const struct device *dev, unsigned int chan,
7373
uint32_t value);
7474

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+
int adc_emul_const_raw_value_set(const struct device *dev, unsigned int chan, uint32_t raw_value);
86+
7587
/**
7688
* @brief Set function used to obtain voltage for input of emulated
7789
* ADC @p chan

0 commit comments

Comments
 (0)