73 lines
2.5 KiB
C++
73 lines
2.5 KiB
C++
#include "battery_monitor.hpp"
|
|||
|
|
#include <driver/gpio.h>
|
||
|
|
#include <esp_adc/adc_cali_scheme.h>
|
||
|
|
#include <freertos/FreeRTOS.h>
|
||
|
|
#include <freertos/task.h>
|
||
|
|
#include <esp_log.h>
|
||
|
|
#include <esp_sleep.h>
|
||
|
|
|
||
|
|
namespace microbu {
|
||
|
|
BatteryMonitor::BatteryMonitor() {
|
||
|
|
#if (CONFIG_MICROBU_XIAO_C5_BATTERY || CONFIG_MICROBU_BOARD_MAINBOARD_V5) && CONFIG_IDF_TARGET_ESP32C5
|
||
|
|
adc_unit_t unit;
|
||
|
|
#if CONFIG_MICROBU_BOARD_MAINBOARD_V5
|
||
|
|
constexpr int battery_gpio = 1;
|
||
|
|
#else
|
||
|
|
constexpr int battery_gpio = 6;
|
||
|
|
#endif
|
||
|
|
if (adc_oneshot_io_to_channel(battery_gpio, &unit, &channel_) != ESP_OK) return;
|
||
|
|
adc_oneshot_unit_init_cfg_t config = {};
|
||
|
|
config.unit_id = unit;
|
||
|
|
if (adc_oneshot_new_unit(&config, &adc_) != ESP_OK) return;
|
||
|
|
adc_oneshot_chan_cfg_t input = {};
|
||
|
|
input.atten = ADC_ATTEN_DB_12;
|
||
|
|
input.bitwidth = ADC_BITWIDTH_DEFAULT;
|
||
|
|
if (adc_oneshot_config_channel(adc_, channel_, &input) != ESP_OK) return;
|
||
|
|
adc_cali_curve_fitting_config_t calibration = {};
|
||
|
|
calibration.unit_id = unit;
|
||
|
|
calibration.chan = channel_;
|
||
|
|
calibration.atten = input.atten;
|
||
|
|
calibration.bitwidth = input.bitwidth;
|
||
|
|
if (adc_cali_create_scheme_curve_fitting(&calibration, &calibration_) != ESP_OK) {
|
||
|
|
ESP_LOGW("battery", "Calibration unavailable; battery voltage stays unavailable");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
#if CONFIG_MICROBU_XIAO_C5_BATTERY
|
||
|
|
gpio_set_direction(GPIO_NUM_26, GPIO_MODE_OUTPUT);
|
||
|
|
gpio_set_level(GPIO_NUM_26, 0);
|
||
|
|
#endif
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
void BatteryMonitor::deep_sleep() {
|
||
|
|
ESP_LOGW("battery", "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 BatteryMonitor::woke_from_battery_sleep() {
|
||
|
|
return esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::uint16_t BatteryMonitor::sample_millivolts() {
|
||
|
|
if (!adc_ || !calibration_) return 0;
|
||
|
|
#if CONFIG_MICROBU_XIAO_C5_BATTERY
|
||
|
|
gpio_set_level(GPIO_NUM_26, 1);
|
||
|
|
#endif
|
||
|
|
vTaskDelay(pdMS_TO_TICKS(5)); // settle the switched 100k/100k divider
|
||
|
|
int sum = 0;
|
||
|
|
bool valid = true;
|
||
|
|
for (unsigned i = 0; i < 16; ++i) {
|
||
|
|
int raw = 0, mv = 0;
|
||
|
|
if (adc_oneshot_read(adc_, channel_, &raw) != ESP_OK ||
|
||
|
|
adc_cali_raw_to_voltage(calibration_, raw, &mv) != ESP_OK) { valid = false; break; }
|
||
|
|
sum += mv;
|
||
|
|
}
|
||
|
|
#if CONFIG_MICROBU_XIAO_C5_BATTERY
|
||
|
|
gpio_set_level(GPIO_NUM_26, 0);
|
||
|
|
#endif
|
||
|
|
const int voltage = (sum * 2 + 8) / 16;
|
||
|
|
return valid && voltage >= 2500 && voltage <= 4500 ? voltage : 0;
|
||
|
|
}
|
||
|
|
}
|