2026-11-11 00:13:57 +01:00
|
|
|
#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>
|
2026-11-11 14:17:38 +01:00
|
|
|
#include <esp_rom_sys.h>
|
2026-11-11 00:13:57 +01:00
|
|
|
|
|
|
|
|
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
|
2026-11-11 14:17:38 +01:00
|
|
|
// MicrOBU: gpio_config, not gpio_set_direction alone, so the pad is certainly a plain GPIO output.
|
|
|
|
|
gpio_config_t enable = {};
|
|
|
|
|
enable.pin_bit_mask = 1ULL << 26;
|
|
|
|
|
enable.mode = GPIO_MODE_OUTPUT;
|
|
|
|
|
gpio_config(&enable);
|
2026-11-11 00:13:57 +01:00
|
|
|
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
|
2026-11-11 14:17:38 +01:00
|
|
|
// MicrOBU: a busy wait. At CONFIG_FREERTOS_HZ=100 pdMS_TO_TICKS(5) is 0 ticks, so vTaskDelay returned at
|
|
|
|
|
// once, the switched divider had not settled, and every reading came out below the 2.5 V validity floor
|
|
|
|
|
// (battery_mv 0 on the XIAO, 2026-11-11).
|
|
|
|
|
esp_rom_delay_us(5000); // settle the switched 100k/100k divider
|
2026-11-11 00:13:57 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|