@@ -31,6 +31,7 @@ typedef uint16_t adc_emul_res_t;
3131enum 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 ;
0 commit comments