diff --git a/components/esp_adc/adc_oneshot.c b/components/esp_adc/adc_oneshot.c index 9ecb4912851e..d8d37d9aed30 100644 --- a/components/esp_adc/adc_oneshot.c +++ b/components/esp_adc/adc_oneshot.c @@ -226,6 +226,15 @@ esp_err_t adc_oneshot_del_unit(adc_oneshot_unit_handle_t handle) return ESP_OK; } +esp_err_t adc_oneshot_get_calibrated_result(adc_oneshot_unit_handle_t handle, adc_cali_handle_t cali_handle, adc_channel_t chan, int *cali_result) +{ + int raw = 0; + ESP_RETURN_ON_ERROR(adc_oneshot_read(handle, chan, &raw), TAG, "adc oneshot read fail"); + ESP_LOGD(TAG, "raw: 0d%d", raw); + ESP_RETURN_ON_ERROR(adc_cali_raw_to_voltage(cali_handle, raw, cali_result), TAG, "adc calibration fail"); + + return ESP_OK; +} #define ADC_GET_IO_NUM(unit, channel) (adc_channel_io_map[unit][channel]) diff --git a/components/esp_adc/include/esp_adc/adc_oneshot.h b/components/esp_adc/include/esp_adc/adc_oneshot.h index b3b21778a019..aad86bd8713d 100644 --- a/components/esp_adc/include/esp_adc/adc_oneshot.h +++ b/components/esp_adc/include/esp_adc/adc_oneshot.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -10,6 +10,8 @@ #include #include "esp_err.h" #include "hal/adc_types.h" +#include "adc_cali.h" +#include "adc_cali_scheme.h" #ifdef __cplusplus extern "C" { @@ -125,6 +127,24 @@ esp_err_t adc_oneshot_io_to_channel(int io_num, adc_unit_t *unit_id, adc_channel */ esp_err_t adc_oneshot_channel_to_io(adc_unit_t unit_id, adc_channel_t channel, int *io_num); +/** + * @brief Convenience function to get ADC calibrated result + * + * This is an all-in-one function which does: + * - oneshot read ADC raw result + * - calibrate the raw result and convert it into calibrated result (in mV) + * + * @param[in] handle ADC oneshot handle, you should call adc_oneshot_new_unit() to get this handle + * @param[in] cali_handle ADC calibration handle, you should call adc_cali_create_scheme_x() in adc_cali_scheme.h to create a handle + * @param[in] chan ADC channel + * @param[out] cali_result Calibrated ADC result (in mV) + * + * @return + * - ESP_OK + * Other return errors from adc_oneshot_read() and adc_cali_raw_to_voltage() + */ +esp_err_t adc_oneshot_get_calibrated_result(adc_oneshot_unit_handle_t handle, adc_cali_handle_t cali_handle, adc_channel_t chan, int *cali_result); + #ifdef __cplusplus } #endif