Skip to content

Commit

Permalink
Merge branch 'feature/adc_oneshot_api_calibrated_result' into 'master'
Browse files Browse the repository at this point in the history
esp_adc: added an all-in-one API to get calibrated voltage

Closes IDF-6651

See merge request espressif/esp-idf!22055
  • Loading branch information
Icarus113 committed Jan 19, 2023
2 parents 9d63833 + c71c099 commit 8f82c5c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions components/esp_adc/adc_oneshot.c
Expand Up @@ -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])

Expand Down
22 changes: 21 additions & 1 deletion 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
*/
Expand All @@ -10,6 +10,8 @@
#include <stdbool.h>
#include "esp_err.h"
#include "hal/adc_types.h"
#include "adc_cali.h"
#include "adc_cali_scheme.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -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

0 comments on commit 8f82c5c

Please sign in to comment.