Skip to content

Commit 3416f9a

Browse files
wmrsouzanashif
authored andcommitted
drivers: adc: esp32: fix invalid width mask and calibration
Fixed WIDTH_MASK invalid values and use HW calibration. Fixes #72983 Fixes #73798 Fixes #73400 Signed-off-by: Marcio Ribeiro <[email protected]>
1 parent eaa2bf0 commit 3416f9a

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

drivers/adc/adc_esp32.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ LOG_MODULE_REGISTER(adc_esp32, CONFIG_ADC_LOG_LEVEL);
4646
#define ADC_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
4747
#endif
4848

49-
/* Convert resolution in bits to esp32 enum values */
50-
#define WIDTH_MASK(r) ((((r) - 9) < ADC_WIDTH_MAX) ? ((r) - 9) : (ADC_WIDTH_MAX - 1))
51-
5249
/* Validate if resolution in bits is within allowed values */
5350
#define VALID_RESOLUTION(r) ((r) >= ADC_RESOLUTION_MIN && (r) <= ADC_RESOLUTION_MAX)
5451
#define INVALID_RESOLUTION(r) (!VALID_RESOLUTION(r))
@@ -128,6 +125,22 @@ static void atten_to_gain(adc_atten_t atten, uint32_t *val_mv)
128125
}
129126
#endif /* !defined(CONFIG_ADC_ESP32_DMA) */
130127

128+
static void adc_hw_calibration(adc_unit_t unit)
129+
{
130+
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
131+
adc_hal_calibration_init(unit);
132+
for (int j = 0; j < SOC_ADC_ATTEN_NUM; j++) {
133+
adc_calc_hw_calibration_code(unit, j);
134+
#if SOC_ADC_CALIB_CHAN_COMPENS_SUPPORTED
135+
/* Load the channel compensation from efuse */
136+
for (int k = 0; k < SOC_ADC_CHANNEL_NUM(unit); k++) {
137+
adc_load_hw_calibration_chan_compens(unit, k, j);
138+
}
139+
#endif /* SOC_ADC_CALIB_CHAN_COMPENS_SUPPORTED */
140+
}
141+
#endif /* SOC_ADC_CALIBRATION_V1_SUPPORTED */
142+
}
143+
131144
static bool adc_calibration_init(const struct device *dev)
132145
{
133146
struct adc_esp32_data *data = dev->data;
@@ -423,7 +436,7 @@ static int adc_esp32_read(const struct device *dev, const struct adc_sequence *s
423436
adc1_config_width(ADC_WIDTH_BIT_DEFAULT);
424437
}
425438
#else
426-
adc_set_data_width(conf->unit, WIDTH_MASK(data->resolution[channel_id]));
439+
adc_set_data_width(conf->unit, data->resolution[channel_id]);
427440
#endif /* CONFIG_SOC_SERIES_ESP32C3 */
428441

429442
#if !defined(CONFIG_ADC_ESP32_DMA)
@@ -440,7 +453,7 @@ static int adc_esp32_read(const struct device *dev, const struct adc_sequence *s
440453

441454
/* Calibration scheme is available */
442455
if (data->calibrate) {
443-
data->chars[channel_id].bit_width = WIDTH_MASK(data->resolution[channel_id]);
456+
data->chars[channel_id].bit_width = data->resolution[channel_id];
444457
/* Get corrected voltage output */
445458
cal = cal_mv = esp_adc_cal_raw_to_voltage(reading, &data->chars[channel_id]);
446459

@@ -593,7 +606,7 @@ static int adc_esp32_channel_setup(const struct device *dev, const struct adc_ch
593606
if (data->calibrate) {
594607
esp_adc_cal_value_t cal = esp_adc_cal_characterize(conf->unit,
595608
data->attenuation[cfg->channel_id],
596-
WIDTH_MASK(data->resolution[cfg->channel_id]),
609+
data->resolution[cfg->channel_id],
597610
data->meas_ref_internal,
598611
&data->chars[cfg->channel_id]);
599612
if (cal >= ESP_ADC_CAL_VAL_NOT_SUPPORTED) {
@@ -637,10 +650,11 @@ static int adc_esp32_channel_setup(const struct device *dev, const struct adc_ch
637650
static int adc_esp32_init(const struct device *dev)
638651
{
639652
struct adc_esp32_data *data = (struct adc_esp32_data *) dev->data;
653+
const struct adc_esp32_conf *conf = (struct adc_esp32_conf *) dev->config;
640654

641-
#if defined(CONFIG_ADC_ESP32_DMA)
642-
struct adc_esp32_conf *conf = (struct adc_esp32_conf *) dev->config;
655+
adc_hw_calibration(conf->unit);
643656

657+
#if defined(CONFIG_ADC_ESP32_DMA)
644658
if (!device_is_ready(conf->gpio_port)) {
645659
LOG_ERR("gpio0 port not ready");
646660
return -ENODEV;

0 commit comments

Comments
 (0)