From 0360b638b9ed1ccf57c8274d378ff1d71ad92cce Mon Sep 17 00:00:00 2001 From: lovyan03 <42724151+lovyan03@users.noreply.github.com> Date: Thu, 8 Aug 2024 13:05:37 +0900 Subject: [PATCH] tweak ADC driver for ESP-IDF 5.x.x ( ArduinoESP32 v3.x.x ) ( #89 #90 #121 ) --- src/utility/Power_Class.cpp | 72 ++++++++++++++++++++++++++++--------- src/utility/Power_Class.hpp | 6 +++- 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/utility/Power_Class.cpp b/src/utility/Power_Class.cpp index d7f0037..14ac447 100644 --- a/src/utility/Power_Class.cpp +++ b/src/utility/Power_Class.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include @@ -835,39 +834,80 @@ namespace m5 _timerSleep(); } -#if !defined (M5UNIFIED_PC_BUILD) - - static std::int32_t getBatteryAdcRaw(uint8_t adc_ch, uint8_t adc_unit) + std::int32_t Power_Class::_getBatteryAdcRaw(void) { -#if !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32) || defined (CONFIG_IDF_TARGET_ESP32S3) +#if defined (M5UNIFIED_PC_BUILD) + return 0; +#elif !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32) || defined (CONFIG_IDF_TARGET_ESP32S3) + +#if __has_include () + + static adc_oneshot_unit_handle_t adc_handle; + if (adc_handle == nullptr) { + adc_oneshot_unit_init_cfg_t init_config; + init_config.unit_id = _batAdcUnit == 1 ? ADC_UNIT_1 : ADC_UNIT_2; + adc_oneshot_new_unit(&init_config, &adc_handle); + if (adc_handle == nullptr) { return 0; } + + adc_oneshot_chan_cfg_t config; + config.bitwidth = ADC_BITWIDTH_12; + config.atten = ADC_ATTEN_DB_12; + adc_oneshot_config_channel(adc_handle, (adc_channel_t)_batAdcCh, &config); + } + static adc_cali_handle_t adc_cali; + if (adc_cali == nullptr) { +#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED + adc_cali_curve_fitting_config_t cali_config; + cali_config.unit_id = _batAdcUnit == 1 ? ADC_UNIT_1 : ADC_UNIT_2; + cali_config.chan = (adc_channel_t)_batAdcCh; + cali_config.atten = ADC_ATTEN_DB_12; + cali_config.bitwidth = ADC_BITWIDTH_12; + adc_cali_create_scheme_curve_fitting(&cali_config, &adc_cali); +#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED + adc_cali_line_fitting_config_t cali_config; + cali_config.unit_id = _batAdcUnit == 1 ? ADC_UNIT_1 : ADC_UNIT_2; + cali_config.atten = ADC_ATTEN_DB_12; + cali_config.bitwidth = ADC_BITWIDTH_12; + adc_cali_create_scheme_line_fitting(&cali_config, &adc_cali); +#endif + } + int raw, volt; + adc_oneshot_read(adc_handle, (adc_channel_t)_batAdcCh, &raw); + if (adc_cali == nullptr) { + return raw; + } + adc_cali_raw_to_voltage(adc_cali, raw, &volt); + return volt; + +#else static constexpr int BASE_VOLATAGE = 3600; static esp_adc_cal_characteristics_t* adc_chars = nullptr; if (adc_chars == nullptr) { - if (adc_unit == 2) { - adc2_config_channel_atten((adc2_channel_t)adc_ch, ADC_ATTEN_DB_11); + if (_batAdcUnit == 2) { + adc2_config_channel_atten((adc2_channel_t)_batAdcCh, ADC_ATTEN_DB_11); } else { adc1_config_width(ADC_WIDTH_BIT_12); - adc1_config_channel_atten((adc1_channel_t)adc_ch, ADC_ATTEN_DB_11); + adc1_config_channel_atten((adc1_channel_t)_batAdcCh, ADC_ATTEN_DB_11); } adc_chars = (esp_adc_cal_characteristics_t*)calloc(1, sizeof(esp_adc_cal_characteristics_t)); - esp_adc_cal_characterize((adc_unit_t)adc_unit, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, BASE_VOLATAGE, adc_chars); + esp_adc_cal_characterize((adc_unit_t)_batAdcUnit, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, BASE_VOLATAGE, adc_chars); } int raw; - if (adc_unit == 2) { - adc2_get_raw((adc2_channel_t)adc_ch, adc_bits_width_t::ADC_WIDTH_BIT_12, &raw); + if (_batAdcUnit == 2) { + adc2_get_raw((adc2_channel_t)_batAdcCh, adc_bits_width_t::ADC_WIDTH_BIT_12, &raw); } else { - raw = adc1_get_raw((adc1_channel_t)adc_ch); + raw = adc1_get_raw((adc1_channel_t)_batAdcCh); } return esp_adc_cal_raw_to_voltage(raw, adc_chars); +#endif + #else return 0; #endif } -#endif - int16_t Power_Class::getBatteryVoltage(void) { #if !defined (M5UNIFIED_PC_BUILD) @@ -891,7 +931,7 @@ namespace m5 #endif case pmic_t::pmic_adc: - return getBatteryAdcRaw(_batAdcCh, _batAdcUnit) * _adc_ratio; + return _getBatteryAdcRaw() * _adc_ratio; default: return 0; @@ -928,7 +968,7 @@ namespace m5 #endif case pmic_t::pmic_adc: - mv = getBatteryAdcRaw(_batAdcCh, _batAdcUnit) * _adc_ratio; + mv = _getBatteryAdcRaw() * _adc_ratio; break; default: diff --git a/src/utility/Power_Class.hpp b/src/utility/Power_Class.hpp index 721f50c..3f68e9c 100644 --- a/src/utility/Power_Class.hpp +++ b/src/utility/Power_Class.hpp @@ -13,8 +13,11 @@ #include "INA3221_Class.hpp" #include "RTC8563_Class.hpp" -#if __has_include () +#if __has_include () // ESP-IDF v5 or later +#include +#elif __has_include () #include +#include #endif namespace m5 @@ -170,6 +173,7 @@ namespace m5 #endif private: + std::int32_t _getBatteryAdcRaw(void); void _powerOff(bool withTimer); void _timerSleep(void);