Monitor the XIAO's battery and make the XIAO the production board

The production OBU is now a Seeed XIAO ESP32-C5 (8 MB flash, one native
USB-C port) on a LiPo with no protection board. The firmware reads the
cell on GPIO6 (divider switched by GPIO26) every 5 s, sends it in STATUS
(battery_mv and battery_flags, octets 103-105; decoders accept 103 or
>= 106 octets), warns below 3.50 V and deep-sleeps below 3.30 V when no
phone is on USB, waking every 5 min to check the cell again. Thresholds
are in Kconfig; host test `make battery`.

The app shows voltage, a rough percentage and the low/critical state on
the ESP32-C5 card and in the top bar. With several bonded micrOBUs (a
replaced board stays bonded), the BLE link tries the one that last
connected first and moves on to the next after each failure.

tools/station_status_watch.py reads STATUS from the native port.
This commit is contained in:
Ashin Walpola
2026-10-28 16:48:18 +01:00
parent d0fcb45a55
commit 9f77f9f0e4
28 changed files with 955 additions and 31 deletions
+28
View File
@@ -0,0 +1,28 @@
#pragma once
// Cell voltage of the XIAO ESP32-C5's battery, read through the board's own divider: GPIO6 (ADC1)
// behind two 100 k resistors, switched on by GPIO26 (a load switch that keeps the divider from
// draining the cell between readings). The policy (warning, shutdown) is in battery_monitor.hpp.
#include <cstdint>
namespace microbu {
class Battery {
public:
/// @brief Configures the enable GPIO and the ADC channel. On failure measure() returns 0.
Battery();
/// @brief Takes one measurement: enables the divider, samples, disables it again. Blocks for a few ms.
/// @return Cell voltage in millivolts, or 0 if the ADC could not be set up or read.
std::uint16_t measure();
/// @brief Puts the board into deep sleep and wakes it after CONFIG_MICROBU_BATTERY_SLEEP_S seconds.
/// Does not return.
[[noreturn]] static void deep_sleep();
/// @return true if this boot is the end of such a sleep (the only timer wake-up the firmware arms).
static bool woke_from_battery_sleep();
private:
void* adc_ = nullptr; // adc_oneshot_unit_handle_t
void* cali_ = nullptr; // adc_cali_handle_t, null when the chip has no calibration
int channel_ = -1;
};
} // namespace microbu