Commit db02fdc8 authored by Yunshao Chiang's avatar Yunshao Chiang Committed by Johan Hedberg
Browse files

drivers: adc: it51xxx: fix the ADC channels sampling flow



The original code process causes the following two issues:

1. The first sample is always 0 because the `ctx` sequence in `data` is
   assigned the input sequence until the `adc_context_start_read`. As a
   result, the `while (channels) { ... }` loop is not executed, and
   `adc_enable_measurement` is not called.

2. Since the `ctx` sequence in `data` is assigned in
   `adc_context_start_read`, which occurs after the `while (channels) {
   ... }` loop, the ADC samples the previously set channel.

Signed-off-by: default avatarYunshao Chiang <Yunshao.Chiang@ite.com.tw>
parent 7ac7a0e0
Loading
Loading
Loading
Loading
+16 −17
Original line number Diff line number Diff line
@@ -265,10 +265,7 @@ static int adc_it51xxx_start_read(const struct device *dev, const struct adc_seq
{
	const struct adc_it51xxx_cfg *config = dev->config;
	struct adc_it51xxx_data *data = dev->data;
	struct adc_context *ctx = &data->ctx;
	uint32_t channels = ctx->sequence.channels;
	uint32_t channel_mask = sequence->channels;
	uint8_t channel_count = 0;

	if (!channel_mask || channel_mask & ~BIT_MASK(config->channel_count)) {
		LOG_ERR("Invalid selection of channels");
@@ -280,6 +277,21 @@ static int adc_it51xxx_start_read(const struct device *dev, const struct adc_seq
		return -EINVAL;
	}

	data->buffer = sequence->buffer;

	adc_context_start_read(&data->ctx, sequence);

	return adc_context_wait_for_completion(&data->ctx);
}

static void adc_context_start_sampling(struct adc_context *ctx)
{
	struct adc_it51xxx_data *data = CONTAINER_OF(ctx, struct adc_it51xxx_data, ctx);
	uint32_t channels = ctx->sequence.channels;
	uint8_t channel_count = 0;

	data->repeat_buffer = data->buffer;

	/*
	 * The ADC sampling of it51xxx needs to read each channel
	 * in sequence.
@@ -294,22 +306,9 @@ static int adc_it51xxx_start_read(const struct device *dev, const struct adc_seq
	}

	if (check_buffer_size(&ctx->sequence, channel_count)) {
		return -ENOMEM;
	}

	data->buffer = sequence->buffer;

	adc_context_start_read(&data->ctx, sequence);

	return adc_context_wait_for_completion(&data->ctx);
		return;
	}

static void adc_context_start_sampling(struct adc_context *ctx)
{
	struct adc_it51xxx_data *data = CONTAINER_OF(ctx, struct adc_it51xxx_data, ctx);

	data->repeat_buffer = data->buffer;

	adc_context_on_sampling_done(&data->ctx, DEVICE_DT_INST_GET(0));
}