Lines Matching +full:scaling +full:- +full:x +full:- +full:resolution

2  * Copyright 2023-2024 NXP
6 * Copyright (c) 2017-2018, NXP
9 * SPDX-License-Identifier: Apache-2.0
65 const struct mcux_lpadc_config *config = dev->config; in mcux_lpadc_acquisition_time_setup()
88 if (clock_control_get_rate(config->clock_dev, config->clock_subsys, &adc_freq_hz)) { in mcux_lpadc_acquisition_time_setup()
90 return -EINVAL; in mcux_lpadc_acquisition_time_setup()
101 cmd->sampleTimeMode = kLPADC_SampleTimeADCK3; in mcux_lpadc_acquisition_time_setup()
103 cmd->sampleTimeMode = kLPADC_SampleTimeADCK5; in mcux_lpadc_acquisition_time_setup()
105 cmd->sampleTimeMode = kLPADC_SampleTimeADCK7; in mcux_lpadc_acquisition_time_setup()
107 cmd->sampleTimeMode = kLPADC_SampleTimeADCK11; in mcux_lpadc_acquisition_time_setup()
109 cmd->sampleTimeMode = kLPADC_SampleTimeADCK19; in mcux_lpadc_acquisition_time_setup()
111 cmd->sampleTimeMode = kLPADC_SampleTimeADCK35; in mcux_lpadc_acquisition_time_setup()
113 cmd->sampleTimeMode = kLPADC_SampleTimeADCK67; in mcux_lpadc_acquisition_time_setup()
115 cmd->sampleTimeMode = kLPADC_SampleTimeADCK131; in mcux_lpadc_acquisition_time_setup()
117 return -EINVAL; in mcux_lpadc_acquisition_time_setup()
126 const struct mcux_lpadc_config *config = dev->config; in mcux_lpadc_channel_setup()
127 const struct device *regulator = config->ref_supplies; in mcux_lpadc_channel_setup()
128 int32_t vref_uv = config->ref_supply_val * 1000; in mcux_lpadc_channel_setup()
129 struct mcux_lpadc_data *data = dev->data; in mcux_lpadc_channel_setup()
136 if (channel_cfg->channel_id >= CONFIG_LPADC_CHANNEL_COUNT) { in mcux_lpadc_channel_setup()
137 LOG_ERR("Channel %d is not valid", channel_cfg->channel_id); in mcux_lpadc_channel_setup()
138 return -EINVAL; in mcux_lpadc_channel_setup()
142 cmd = &data->cmd_config[channel_cfg->channel_id]; in mcux_lpadc_channel_setup()
145 channel_side = 0x20 & channel_cfg->input_positive; in mcux_lpadc_channel_setup()
147 channel_num = ADC_CMDL_ADCH(channel_cfg->input_positive); in mcux_lpadc_channel_setup()
155 if (mcux_lpadc_acquisition_time_setup(dev, channel_cfg->acquisition_time, cmd)) { in mcux_lpadc_channel_setup()
157 return -EINVAL; in mcux_lpadc_channel_setup()
162 if (channel_cfg->differential) { in mcux_lpadc_channel_setup()
164 if ((ADC_CMDL_ADCH(channel_cfg->input_positive)) != in mcux_lpadc_channel_setup()
165 (ADC_CMDL_ADCH(channel_cfg->input_negative))) { in mcux_lpadc_channel_setup()
166 return -ENOTSUP; in mcux_lpadc_channel_setup()
171 if (channel_cfg->input_positive & 0x20) { in mcux_lpadc_channel_setup()
173 cmd->sampleChannelMode = in mcux_lpadc_channel_setup()
177 cmd->sampleChannelMode = in mcux_lpadc_channel_setup()
181 cmd->sampleChannelMode = kLPADC_SampleChannelDiffBothSide; in mcux_lpadc_channel_setup()
184 cmd->sampleChannelMode = kLPADC_SampleChannelSingleEndSideB; in mcux_lpadc_channel_setup()
191 * The true scaling factor used by the LPADC is 30/64, instead of in mcux_lpadc_channel_setup()
192 * 1/2. Select 1/2 as this is the closest scaling factor available in mcux_lpadc_channel_setup()
195 if (channel_cfg->gain == ADC_GAIN_1_2) { in mcux_lpadc_channel_setup()
197 cmd->sampleScaleMode = kLPADC_SamplePartScale; in mcux_lpadc_channel_setup()
198 } else if (channel_cfg->gain == ADC_GAIN_1) { in mcux_lpadc_channel_setup()
199 cmd->sampleScaleMode = kLPADC_SampleFullScale; in mcux_lpadc_channel_setup()
202 return -EINVAL; in mcux_lpadc_channel_setup()
205 if (channel_cfg->gain != ADC_GAIN_1) { in mcux_lpadc_channel_setup()
207 return -EINVAL; in mcux_lpadc_channel_setup()
216 if (channel_cfg->reference == ADC_REF_EXTERNAL1) { in mcux_lpadc_channel_setup()
224 return -EINVAL; in mcux_lpadc_channel_setup()
226 } else if (channel_cfg->reference == ADC_REF_EXTERNAL0) { in mcux_lpadc_channel_setup()
230 return -EINVAL; in mcux_lpadc_channel_setup()
233 cmd->channelNumber = channel_num; in mcux_lpadc_channel_setup()
240 const struct mcux_lpadc_config *config = dev->config; in mcux_lpadc_start_read()
241 struct mcux_lpadc_data *data = dev->data; in mcux_lpadc_start_read()
248 switch (sequence->resolution) { in mcux_lpadc_start_read()
257 LOG_ERR("Unsupported resolution %d", sequence->resolution); in mcux_lpadc_start_read()
258 return -ENOTSUP; in mcux_lpadc_start_read()
262 only 12/13 bit resolution is supported. */ in mcux_lpadc_start_read()
263 if (sequence->resolution != 12 && sequence->resolution != 13) { in mcux_lpadc_start_read()
264 LOG_ERR("Unsupported resolution %d", sequence->resolution); in mcux_lpadc_start_read()
265 return -ENOTSUP; in mcux_lpadc_start_read()
269 switch (sequence->oversampling) { in mcux_lpadc_start_read()
296 sequence->oversampling); in mcux_lpadc_start_read()
297 return -ENOTSUP; in mcux_lpadc_start_read()
310 while (channel-- > 0) { in mcux_lpadc_start_read()
311 if (sequence->channels & BIT(channel)) { in mcux_lpadc_start_read()
314 data->cmd_config[channel].conversionResolutionMode = in mcux_lpadc_start_read()
317 data->cmd_config[channel].hardwareAverageMode = in mcux_lpadc_start_read()
321 data->cmd_config[channel].chainedNextCommandNumber = in mcux_lpadc_start_read()
327 data->cmd_config[channel].chainedNextCommandNumber = 0; in mcux_lpadc_start_read()
330 LPADC_SetConvCommandConfig(config->base, in mcux_lpadc_start_read()
331 channel + 1, &data->cmd_config[channel]); in mcux_lpadc_start_read()
335 data->buffer = sequence->buffer; in mcux_lpadc_start_read()
337 adc_context_start_read(&data->ctx, sequence); in mcux_lpadc_start_read()
338 int error = adc_context_wait_for_completion(&data->ctx); in mcux_lpadc_start_read()
347 struct mcux_lpadc_data *data = dev->data; in mcux_lpadc_read_async()
350 adc_context_lock(&data->ctx, async ? true : false, async); in mcux_lpadc_read_async()
352 adc_context_release(&data->ctx, error); in mcux_lpadc_read_async()
365 const struct mcux_lpadc_config *config = dev->config; in mcux_lpadc_start_channel()
366 struct mcux_lpadc_data *data = dev->data; in mcux_lpadc_start_channel()
370 first_channel = find_lsb_set(data->channels) - 1; in mcux_lpadc_start_channel()
373 data->cmd_config[first_channel].channelNumber); in mcux_lpadc_start_channel()
380 LPADC_SetConvTriggerConfig(config->base, 0, &trigger_config); in mcux_lpadc_start_channel()
383 LPADC_DoSoftwareTrigger(config->base, 1); in mcux_lpadc_start_channel()
391 data->channels = ctx->sequence.channels; in adc_context_start_sampling()
392 data->repeat_buffer = data->buffer; in adc_context_start_sampling()
394 mcux_lpadc_start_channel(data->dev); in adc_context_start_sampling()
404 data->buffer = data->repeat_buffer; in adc_context_update_buffer_pointer()
410 const struct mcux_lpadc_config *config = dev->config;
411 struct mcux_lpadc_data *data = dev->data;
412 ADC_Type *base = config->base;
426 channel = conv_result.commandIdSource - 1;
427 LOG_DBG("Finished channel %d. Raw result is 0x%04x",
430 * For 12 or 13 bit resolution the LSBs will be 0, so a bit shift
432 * millivolts expects to use a shift one less than the resolution.
438 conv_mode = data->cmd_config[channel].sampleChannelMode;
439 if (data->ctx.sequence.resolution < 15) {
451 result -= 0x1000;
454 *data->buffer++ = result;
457 *data->buffer++ = conv_result.convValue;
461 data->channels &= ~BIT(channel);
467 if (data->channels == 0) {
468 adc_context_on_sampling_done(&data->ctx, dev);
474 const struct mcux_lpadc_config *config = dev->config;
475 struct mcux_lpadc_data *data = dev->data;
476 ADC_Type *base = config->base;
480 err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
486 const struct device *regulator = config->ref_supplies;
498 adc_config.referenceVoltageSource = config->voltage_ref;
502 adc_config.conversionAverageMode = config->calibration_average;
506 adc_config.powerLevelMode = config->power_level;
522 config->offset_a,
523 config->offset_b);
544 config->irq_config_func(dev);
545 data->dev = dev;
547 adc_context_unlock_unconditionally(&data->ctx);