Skip to content

Commit

Permalink
tweak ADC driver for ESP-IDF 5.x.x ( ArduinoESP32 v3.x.x ) ( #89 #90 #…
Browse files Browse the repository at this point in the history
  • Loading branch information
lovyan03 committed Aug 8, 2024
1 parent 4d7f0e4 commit 0360b63
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
72 changes: 56 additions & 16 deletions src/utility/Power_Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <esp_sleep.h>
#include <sdkconfig.h>

#include <esp_adc_cal.h>
#include <soc/soc_caps.h>
#include <soc/adc_channel.h>

Expand Down Expand Up @@ -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 (<esp_adc/adc_oneshot.h>)

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)
Expand All @@ -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;
Expand Down Expand Up @@ -928,7 +968,7 @@ namespace m5
#endif

case pmic_t::pmic_adc:
mv = getBatteryAdcRaw(_batAdcCh, _batAdcUnit) * _adc_ratio;
mv = _getBatteryAdcRaw() * _adc_ratio;
break;

default:
Expand Down
6 changes: 5 additions & 1 deletion src/utility/Power_Class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
#include "INA3221_Class.hpp"
#include "RTC8563_Class.hpp"

#if __has_include (<driver/adc.h>)
#if __has_include (<esp_adc/adc_oneshot.h>) // ESP-IDF v5 or later
#include <esp_adc/adc_oneshot.h>
#elif __has_include (<driver/adc.h>)
#include <driver/adc.h>
#include <esp_adc_cal.h>
#endif

namespace m5
Expand Down Expand Up @@ -170,6 +173,7 @@ namespace m5
#endif

private:
std::int32_t _getBatteryAdcRaw(void);
void _powerOff(bool withTimer);
void _timerSleep(void);

Expand Down

0 comments on commit 0360b63

Please sign in to comment.