in common/sensor/dev/ast_adc.c [52:108]
static bool adc_read_mv(uint8_t sensor_num, uint32_t index, uint32_t channel, int *adc_val)
{
if (!adc_val)
return false;
if (index >= ADC_NUM) {
printk("<error> ADC[%d] is invalid device!\n", index);
return false;
}
if (!is_ready[index]) {
printk("<error> ADC[%d] is not ready to read!\n", index);
return false;
}
int err, retval;
static struct adc_sequence sequence;
sequence.channels = BIT(channel);
sequence.buffer = sample_buffer;
sequence.buffer_size = sizeof(sample_buffer);
sequence.resolution = ADC_RESOLUTION;
sequence.calibrate = ADC_CALIBRATE;
static struct adc_channel_cfg channel_cfg;
channel_cfg.gain = ADC_GAIN;
channel_cfg.reference = ADC_REFERENCE;
channel_cfg.acquisition_time = ADC_ACQUISITION_TIME;
channel_cfg.channel_id = channel;
channel_cfg.differential = 0;
retval = adc_channel_setup(dev_adc[index], &channel_cfg);
if (retval) {
printk("<error> ADC[%d] with sensor[0x%x] channel set fail\n", index, sensor_num);
return false;
}
err = adc_read(dev_adc[index], &sequence);
if (err != 0) {
printk("<error> ADC[%d] with sensor[0x%x] reading fail with error %d\n", index,
sensor_num, err);
return false;
}
int32_t raw_value = sample_buffer[0];
int32_t ref_mv = adc_get_ref(dev_adc[index]);
if (ref_mv <= 0) {
printk("<error> ADC[%d] with sensor[0x%x] ref-mv get fail\n", index, sensor_num);
return false;
}
*adc_val = raw_value;
adc_raw_to_millivolts(ref_mv, channel_cfg.gain, sequence.resolution, adc_val);
return true;
}