Skip to content

Commit db02fdc

Browse files
Yunshao-Chiangjhedberg
authored andcommitted
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: Yunshao Chiang <[email protected]>
1 parent 7ac7a0e commit db02fdc

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

drivers/adc/adc_ite_it51xxx.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ static int adc_it51xxx_start_read(const struct device *dev, const struct adc_seq
265265
{
266266
const struct adc_it51xxx_cfg *config = dev->config;
267267
struct adc_it51xxx_data *data = dev->data;
268-
struct adc_context *ctx = &data->ctx;
269-
uint32_t channels = ctx->sequence.channels;
270268
uint32_t channel_mask = sequence->channels;
271-
uint8_t channel_count = 0;
272269

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

280+
data->buffer = sequence->buffer;
281+
282+
adc_context_start_read(&data->ctx, sequence);
283+
284+
return adc_context_wait_for_completion(&data->ctx);
285+
}
286+
287+
static void adc_context_start_sampling(struct adc_context *ctx)
288+
{
289+
struct adc_it51xxx_data *data = CONTAINER_OF(ctx, struct adc_it51xxx_data, ctx);
290+
uint32_t channels = ctx->sequence.channels;
291+
uint8_t channel_count = 0;
292+
293+
data->repeat_buffer = data->buffer;
294+
283295
/*
284296
* The ADC sampling of it51xxx needs to read each channel
285297
* in sequence.
@@ -294,22 +306,9 @@ static int adc_it51xxx_start_read(const struct device *dev, const struct adc_seq
294306
}
295307

296308
if (check_buffer_size(&ctx->sequence, channel_count)) {
297-
return -ENOMEM;
309+
return;
298310
}
299311

300-
data->buffer = sequence->buffer;
301-
302-
adc_context_start_read(&data->ctx, sequence);
303-
304-
return adc_context_wait_for_completion(&data->ctx);
305-
}
306-
307-
static void adc_context_start_sampling(struct adc_context *ctx)
308-
{
309-
struct adc_it51xxx_data *data = CONTAINER_OF(ctx, struct adc_it51xxx_data, ctx);
310-
311-
data->repeat_buffer = data->buffer;
312-
313312
adc_context_on_sampling_done(&data->ctx, DEVICE_DT_INST_GET(0));
314313
}
315314

0 commit comments

Comments
 (0)