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
+76 -9
View File
@@ -1,5 +1,12 @@
# obu-firmware — setup & flashing notes
**Board (since 2026-10-05): Seeed XIAO ESP32-C5.** One USB-C port, which is the chip's native USB
Serial/JTAG (VID/PID 0x303A/0x1001); no UART bridge, no second port. 8 MB flash (`esptool flash-id`),
so `sdkconfig.defaults` sets `FLASHSIZE_8MB` (the earlier CH343 bench board had 16 MB). The same
port flashes the board *and* carries the phone's station link, so the PC and the phone take turns on
it. Pin map and what is wired: "Battery and pins" below. The sections that still say "UART-bridge
port" or "COM3" describe the earlier two-port bench board.
## Two toolchains - use a dedicated terminal for each
Since 2026-09-23 this project builds against ESP-IDF **6.0.2** exactly
@@ -36,18 +43,25 @@ idf.py --version # v6.0.2
## Build & flash
Flash over the board's **UART-bridge port** (CH343; COM3 for the production OBU
on the bench), not the native port the phone uses.
The XIAO flashes over its one USB-C port (COM10 on the bench; Device Manager → Ports shows it as
"USB Serial Device"). esptool resets it into the bootloader by itself; no BOOT button is needed
unless the running firmware has crashed the USB peripheral, then hold BOOT while plugging in.
```powershell
cd C:\Users\Ashin\AndroidStudioProjects\MicrOBU\obu-firmware
idf.py set-target esp32c5 # only needed once per clean build folder
idf.py build
idf.py -p COM3 -b 921600 flash monitor
idf.py -p COM10 -b 921600 flash
```
Swap `COM3` for whatever port the ESP32-C5's bridge enumerates as (Device Manager →
Ports). `monitor` opens the serial console after flashing — `Ctrl+]` to exit.
There is **no console on this port**: ESP_LOG stays on UART0 (GPIO11/GPIO12 = pads D6/D7), which a
bare XIAO does not route to USB, and `idf.py monitor` would only see the link frames. To read the
log, put a 3.3 V USB-UART adapter on D6 (TX) / D7 (RX), or set `CONFIG_MICROBU_LOG_OVER_LINK=y`
(LOG frames of type 0x7F on the link; the Android app has not been checked against them). To read
what the board says over the link, use `tools/station_status_watch.py COM10`.
The earlier two-port board flashed through its CH343 bridge port (COM3) with
`idf.py -p COM3 -b 921600 flash monitor`; its console was on that port.
Use `flash` (bootloader + partition table + app), not `app-flash`, whenever the
partition table may differ from what the board has.
@@ -58,8 +72,8 @@ The partition table changed with the port (NVS 24 KB → 80 KB, app 0x10000 →
behind. Erase the whole new NVS range once, then do a full flash:
```powershell
python -m esptool --chip esp32c5 -p COM3 -b 921600 erase-region 0x9000 0x15000
idf.py -p COM3 -b 921600 flash
python -m esptool --chip esp32c5 -p COM10 -b 921600 erase-region 0x9000 0x15000
idf.py -p COM10 -b 921600 flash
```
Without the erase, the board cannot store the BLE bond and the phone has to pair
@@ -103,9 +117,14 @@ can come back two ways. The app detects it and falls back to its protocol.
idf.py build
```
## Connecting the phone (ESP32-C5-WIFI6-KIT)
## Connecting the phone (XIAO ESP32-C5)
The board has two USB-C ports — use the right one:
The XIAO has a single USB-C port, the native USB Serial/JTAG one: the phone plugs into it via
USB-OTG, the station link runs over it (below), and unplugging the phone and plugging the PC in
is how it is flashed. Over BLE the phone does not need the cable at all (the board runs on its cell).
The rest of this section describes the earlier **ESP32-C5-WIFI6-KIT** bench board with two USB-C
ports — use the right one:
- **Native USB-C port** (labeled for JTAG/native USB, up to 12 Mbps) — this
is where the phone plugs in via USB-OTG. The station link
@@ -168,6 +187,54 @@ the first time: passkey **123456** (fixed in `simple_ble.cpp`). The bond is kept
on both sides; the board keeps one bond, so pairing a second phone or a PC
replaces the first. Log lines on the console start with `cits_ble:`.
## Battery and pins (XIAO ESP32-C5)
The prototype runs on the 3.7 V LiPo of an Intenso XS5000 power bank (5000 mAh), on the XIAO's BAT
pads: cell `+` through a slide switch to **BAT+**, cell `−` to **BAT−**. The board's SGM40567 charger
charges it from USB (the phone, or the PC). Check the polarity with a multimeter before connecting:
reversed, the cell destroys the board. **The cell has no protection board** (the power bank's own
circuit board did that job), so nothing but the firmware stops an over-discharge, and nothing at all
stops a short.
| Pin | GPIO | Used for |
|---|---|---|
| (back pad) | GPIO6 | `BAT_VOLT_PIN`: the cell through two 100 kΩ resistors (ADC1) |
| (back pad) | GPIO26 | `BAT_VOLT_PIN_EN`: load switch that powers that divider, high only while measuring |
| D6 / D7 | GPIO11 / GPIO12 | UART0 (console) |
| native USB | GPIO13 / GPIO14 | USB D− / D+ (the phone link) |
| — | GPIO27 | the yellow user LED, active low; `CONFIG_MICROBU_TX_LED_GPIO=27` turns the activity LED on |
(Pin names from Seeed's XIAO ESP32-C5 wiki; GPIO13/14 and the LED are the chip's defaults, not
re-checked against the schematic.) The cell's two extra wires (temperature / ID) are not used.
What the firmware does with it (`battery.cpp`, `battery_monitor.hpp`):
- Every 5 s it enables the divider, takes the median of 16 samples, disables it, and smooths the
result. The cell voltage (twice the pin voltage) goes into the STATUS message the board sends
every second over USB and BLE: `battery_mv` u16 at octet 103 (0: no cell or no sensor) and
`battery_flags` u8 at 105 (bit 0 low, bit 1 critical). STATUS grew from 103 to 106 octets; the
app and the firmware both accept 103 (older board) or 106 and more. The colleague's Python
`Status.decode` requires exactly 103, so their `phone_emulator` rejects this board's STATUS.
- **Low** below 3.50 V (cleared at 3.60 V): the app shows an amber battery icon in the top bar and a
warning on the ESP32-C5 card. **Critical** below 3.30 V: red.
- **Shutdown:** when the smoothed voltage has stayed below 3.30 V for 30 s (6 samples), more than
60 s after boot, and **no phone has sent a frame over USB in the last 10 s**, the board sends a
last STATUS with the critical flag and goes into deep sleep. A phone on USB powers the board, so
the cell is not the reason it would stop and no shutdown happens. In deep sleep the board wakes
every 300 s, measures, and sleeps again until the cell reads 3.60 V (charged over USB); a reset
or power-cycle starts it normally and the rule applies again.
- The battery percentage in the app is a LiPo open-circuit-voltage table (`StationStatus.lipoPercent`):
an indicator, not a fuel gauge. It sags under TX and rises while charging.
All thresholds are `CONFIG_MICROBU_BATTERY_*` in `main/Kconfig.projbuild`. If the app's voltage
differs from a multimeter on the cell, set `CONFIG_MICROBU_BATTERY_SCALE_PERMILLE` to
1000 × multimeter ÷ reading. Check it with `py -3.11 tools/station_status_watch.py COM10`: it
requests STATUS once a second and prints the battery fields.
Not measured yet: the charge current of the XIAO's charger (the wiki does not give it; a 5000 mAh
cell on a trickle charge takes days), whether the ADC reading is disturbed while the radio is
transmitting, and the board's own idle current from the cell. See TODO.md.
## Notes
- No `git submodule update` needed here — obu-firmware has no pinned