101 lines
3.8 KiB
C++
101 lines
3.8 KiB
C++
#include "battery.hpp"
|
|||
|
|
#include "sdkconfig.h"
|
||
|
|
|
||
|
|
#include <algorithm>
|
||
|
|
#include <driver/gpio.h>
|
||
|
|
#include <esp_adc/adc_cali.h>
|
||
|
|
#include <esp_adc/adc_cali_scheme.h>
|
||
|
|
#include <esp_adc/adc_oneshot.h>
|
||
|
|
#include <esp_log.h>
|
||
|
|
#include <esp_rom_sys.h>
|
||
|
|
#include <esp_sleep.h>
|
||
|
|
|
||
|
|
namespace microbu {
|
||
|
|
namespace {
|
||
|
|
const char* TAG = "battery";
|
||
|
|
constexpr int samples = 16;
|
||
|
|
constexpr std::uint32_t settle_us = 2000; // the load switch and the divider's RC settle in well under this
|
||
|
|
constexpr int divider = 2; // two equal 100 k resistors on the XIAO ESP32-C5
|
||
|
|
}
|
||
|
|
|
||
|
|
Battery::Battery() {
|
||
|
|
#if CONFIG_MICROBU_BATTERY_ADC_GPIO < 0
|
||
|
|
ESP_LOGI(TAG, "battery monitoring disabled");
|
||
|
|
return;
|
||
|
|
#endif
|
||
|
|
gpio_config_t enable = {};
|
||
|
|
enable.pin_bit_mask = 1ULL << CONFIG_MICROBU_BATTERY_EN_GPIO;
|
||
|
|
enable.mode = GPIO_MODE_OUTPUT;
|
||
|
|
if (gpio_config(&enable) != ESP_OK) { ESP_LOGE(TAG, "enable GPIO %d unusable", CONFIG_MICROBU_BATTERY_EN_GPIO); return; }
|
||
|
|
gpio_set_level(static_cast<gpio_num_t>(CONFIG_MICROBU_BATTERY_EN_GPIO), 0);
|
||
|
|
|
||
|
|
adc_unit_t unit;
|
||
|
|
adc_channel_t channel;
|
||
|
|
if (adc_oneshot_io_to_channel(CONFIG_MICROBU_BATTERY_ADC_GPIO, &unit, &channel) != ESP_OK) {
|
||
|
|
ESP_LOGE(TAG, "GPIO %d is not an ADC pin", CONFIG_MICROBU_BATTERY_ADC_GPIO);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
adc_oneshot_unit_init_cfg_t unit_config = {};
|
||
|
|
unit_config.unit_id = unit;
|
||
|
|
adc_oneshot_unit_handle_t adc = nullptr;
|
||
|
|
if (adc_oneshot_new_unit(&unit_config, &adc) != ESP_OK) { ESP_LOGE(TAG, "ADC unit init failed"); return; }
|
||
|
|
adc_oneshot_chan_cfg_t channel_config = {};
|
||
|
|
channel_config.atten = ADC_ATTEN_DB_12; // the divided cell is at most about 2.1 V
|
||
|
|
channel_config.bitwidth = ADC_BITWIDTH_DEFAULT;
|
||
|
|
if (adc_oneshot_config_channel(adc, channel, &channel_config) != ESP_OK) {
|
||
|
|
ESP_LOGE(TAG, "ADC channel config failed");
|
||
|
|
adc_oneshot_del_unit(adc);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||
|
|
adc_cali_curve_fitting_config_t cali_config = {};
|
||
|
|
cali_config.unit_id = unit;
|
||
|
|
cali_config.chan = channel;
|
||
|
|
cali_config.atten = ADC_ATTEN_DB_12;
|
||
|
|
cali_config.bitwidth = ADC_BITWIDTH_DEFAULT;
|
||
|
|
adc_cali_handle_t cali = nullptr;
|
||
|
|
if (adc_cali_create_scheme_curve_fitting(&cali_config, &cali) == ESP_OK) cali_ = cali;
|
||
|
|
#endif
|
||
|
|
if (!cali_) ESP_LOGW(TAG, "no ADC calibration: readings are approximate");
|
||
|
|
adc_ = adc;
|
||
|
|
channel_ = static_cast<int>(channel);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::uint16_t Battery::measure() {
|
||
|
|
if (!adc_) return 0;
|
||
|
|
auto* adc = static_cast<adc_oneshot_unit_handle_t>(adc_);
|
||
|
|
gpio_set_level(static_cast<gpio_num_t>(CONFIG_MICROBU_BATTERY_EN_GPIO), 1);
|
||
|
|
esp_rom_delay_us(settle_us);
|
||
|
|
int values[samples];
|
||
|
|
int count = 0;
|
||
|
|
for (int i = 0; i < samples; ++i) {
|
||
|
|
int raw = 0;
|
||
|
|
if (adc_oneshot_read(adc, static_cast<adc_channel_t>(channel_), &raw) != ESP_OK) continue;
|
||
|
|
int millivolts = 0;
|
||
|
|
if (cali_) {
|
||
|
|
if (adc_cali_raw_to_voltage(static_cast<adc_cali_handle_t>(cali_), raw, &millivolts) != ESP_OK) continue;
|
||
|
|
} else {
|
||
|
|
millivolts = raw * 3100 / 4095; // uncalibrated 12 dB full scale, roughly
|
||
|
|
}
|
||
|
|
values[count++] = millivolts;
|
||
|
|
}
|
||
|
|
gpio_set_level(static_cast<gpio_num_t>(CONFIG_MICROBU_BATTERY_EN_GPIO), 0);
|
||
|
|
if (count < samples / 2) return 0;
|
||
|
|
std::sort(values, values + count);
|
||
|
|
const int median = values[count / 2];
|
||
|
|
const long cell_mv = static_cast<long>(median) * divider * CONFIG_MICROBU_BATTERY_SCALE_PERMILLE / 1000;
|
||
|
|
return static_cast<std::uint16_t>(std::clamp(cell_mv, 0L, 65535L));
|
||
|
|
}
|
||
|
|
|
||
|
|
void Battery::deep_sleep() {
|
||
|
|
ESP_LOGW(TAG, "deep sleep for %d s", CONFIG_MICROBU_BATTERY_SLEEP_S);
|
||
|
|
esp_sleep_enable_timer_wakeup(static_cast<std::uint64_t>(CONFIG_MICROBU_BATTERY_SLEEP_S) * 1000000ULL);
|
||
|
|
esp_deep_sleep_start();
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Battery::woke_from_battery_sleep() {
|
||
|
|
return esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace microbu
|