Files
MicrOBU/obu-firmware/FLASHING.md
T

151 lines
8.8 KiB
Markdown
Raw Normal View History

# obu-firmware: build and flash
**Board: Seeed XIAO ESP32-C5.** One USB-C port, the chip's native USB Serial/JTAG (VID/PID
0x303A/0x1001); no UART bridge. 8 MB flash. The same port flashes the board, carries the phone's
station link and carries the log (as LOG frames, type 0x7F), so the PC and the phone take turns on
it. Over BLE the phone does not need the cable.
## Toolchain
ESP-IDF **6.0.2** exactly (`C:\Espressif\frameworks\esp-idf-v6.0.2`): `components/microbu_v2x`
pins the C5 radio's private Wi-Fi driver ABI to it. Not the receiver firmware's 6.1, not
`obu-cam-transmistter`'s 5.5.4. Exporting two IDFs in one PowerShell window fails; open a fresh
window, or clear `$env:IDF_PYTHON_ENV_PATH` and `$env:IDF_PATH` first.
```powershell
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
$env:IDF_TOOLS_PATH = "C:\Espressif"
C:\Espressif\frameworks\esp-idf-v6.0.2\export.ps1
cd C:\Users\Ashin\AndroidStudioProjects\MicrOBU\obu-firmware
idf.py set-target esp32c5 # once per clean build folder
idf.py build # first build ~20 min; downloads espressif/esp-boost into managed_components/
```
## Flash
Use a full `flash` (bootloader + partition table + app), never `app-flash`: the partition table is
not Niklas Dathe's (80 KB NVS, app at 0x20000).
```powershell
idf.py -p COM6 flash
```
**If the running firmware has taken the USB port down** (or the board holds an unrelated image),
auto-reset fails with "No serial data received". Unplug, hold BOOT, plug in, release. If the flasher
stub then fails to start (seen 2026-11-10 with esptool 5.4 on this board), flash without it, from
`build/`:
```powershell
python -m esptool --chip esp32c5 -p COM6 --no-stub --before=no-reset --after=hard-reset write-flash "@flash_args"
```
A board left in download mode comes back after unplugging and replugging without BOOT.
**Before flashing a board you did not flash yourself, read its app descriptor**
(`esptool read-flash 0x10000 0x100`; `esp_app_desc_t` starts at 0x20): project name `microbu_firmware`
is this firmware, `obu_firmware` an older MicrOBU one, `arduino-lib-builder` an Arduino sketch.
Back the flash up first to `firmware-backups/` (gitignored).
### First flash onto a board with other firmware
Erase the NVS range once, so no old Wi-Fi or BLE data sits in the new 80 KB NVS:
```powershell
python -m esptool --chip esp32c5 -p COM6 erase-region 0x9000 0x15000
```
Then: the boot log shows `BLE advertising started as 'micrOBU-XXXX'` and `station task ready`; the
radio stays off until the app (or `tools/station_status_watch.py --configure`) sends
STATION_CONFIGURE; the app provisions the demo credentials on the first Connect. A phone that was
paired before must forget `micrOBU-XXXX` in Android's Bluetooth settings and pair again (passkey
123456, `CONFIG_MICROBU_BLE_PASSKEY`).
### Pairing, bonds and several boards
- The name is `micrOBU-` plus the last two octets of the board's BLE address (`micrOBU-66AA` is the production
XIAO on COM10, `10:BD:A3:CF:66:AA`). The board keeps **one** bond in NVS and refuses to replace it.
- **With no bond the app connects to the first micrOBU it hears.** On a bench with several (2026-11-11: 4AFA, 3FAA,
A25E and 66AA all at -49 to -55 dBm) that is not necessarily yours, and it can bond with the wrong board. Pair the
right one first in Android's Bluetooth settings (Pair new device, passkey 123456); the app then prefers a
bonded micrOBU. Forget a wrong bond there too.
- If the phone has lost its bond but the board still holds one, the phone connects and drops again after about 3 s.
Erase the board's NVS (`python -m esptool --chip esp32c5 -p COMx erase-region 0x9000 0x15000`) and pair again.
- The bond survives a reflash that keeps the NVS and a reset of the board (checked 2026-11-11).
## Look at the board from the PC
```powershell
py -3.11 tools/station_status_watch.py COM6 30 --configure --rx
```
prints the log, the STATUS snapshot once a second (uptime, signing and radio counters, heap,
battery) and the first V2X_RX messages heard on air. DTR/RTS stay low in the script because toggling
them resets the board. **Opening the port resets the board** (USB Serial/JTAG), so a phone on BLE is dropped.
```powershell
py -3.11 tools/station_tx_test.py COMx --seconds 60 --mac 02:aa:bb:cc:dd:01 # signed CAMs from a named MAC
py -3.11 tools/station_tx_test.py COMx --seconds 60 --unsigned # unsigned, a random MAC
```
plays the phone over USB (configure, position, a CAM request per second) and reads the station's own raw capture, so it
prints the 802.11 source address and whether each frame is signed, plus how many frames the radio submitted and
completed. Pair it with a second board listening on the air to count what arrives.
## Production OBU checklist (board with the cell connected)
1. **Before touching the board:** read its app descriptor and back its flash up (see above). Check the
cell polarity with a multimeter before it goes on the BAT pads; the slide switch can stay off while
flashing, USB powers the board.
2. Erase the NVS range, then a full `idf.py -p COMx flash` (or the `--no-stub` recipe if the stub
fails). Unplug and replug the cable **without** BOOT afterwards.
3. `py -3.11 tools/station_status_watch.py COMx 20 --configure --rx`: expect `BLE advertising started as
'micrOBU-XXXX'`, a STATUS line every second, `configured=1` after the configure, and `battery=` close
to the multimeter reading on the cell. The firmware has no calibration trim any more: if the two differ
by more than about 50 mV, note both values (a `CONFIG_MICROBU_BATTERY_SCALE_PERMILLE` option of the
earlier port can be added back).
4. Switch the cell on, unplug USB, check the board keeps running (LED pulses on RX, the phone connects
over BLE). Then work down `TODO.md` ("obu-firmware on Niklas Dathe's microbu-firmware").
5. **Know the shutdown rule before leaving it unattended:** with the cell below 3.30 V, no phone on USB
and more than 60 s after boot, the board deep-sleeps and only wakes every 5 minutes to check; it runs
again from 3.60 V (charge it over USB). A board that "went dead" on a flat cell is this, not a crash.
6. Install the app built from this commit on the phone: the app and firmware of 2026-11-10 only talk
to each other (CONFIRM/STATUS_REQUEST, V2X_RX 0x90). The phone must forget an old `micrOBU-XXXX`
bond and pair again (passkey 123456), because the NVS was erased.
## Battery and pins (XIAO ESP32-C5)
The prototype runs on a 3.7 V LiPo on the BAT pads (cell `+` through a slide switch to **BAT+**,
`−` to **BAT−**); the board's charger charges it from USB. Check the polarity with a multimeter
first: reversed, it destroys the board. **The cell has no protection board**, so only the firmware
stops an over-discharge, and nothing stops a short.
| Pin | GPIO | Used for |
|---|---|---|
| on-board | GPIO6 | `BAT_VOLT_PIN`: BAT+ through the board's own 100 kΩ / 100 kΩ divider (ADC1); nothing to wire |
| on-board | GPIO26 | `BAT_VOLT_PIN_EN`: load switch for that divider, high only while measuring; nothing to wire |
| native USB | GPIO13 / GPIO14 | USB D− / D+ |
| (user LED) | GPIO27 | yellow LED, active low: pulses on TX/RX (`CONFIG_MICROBU_STATUS_LED`) |
Only the cell's `+` and `−` go to the BAT pads; the cell's two extra wires (temperature / ID) stay unconnected, the firmware does not use them. The voltage measurement needs no further wiring. `BatteryMonitor` (`battery_monitor.cpp`) reports the calibrated cell voltage as `battery_mv` in
STATUS (octets 129-130, 0 = no cell or no sensor). `BatteryPolicy` (`battery_policy.hpp`, host-tested
in `test/host`) adds the protection, every 5 s: the smoothed voltage below 3.30 V for 30 s, more than
60 s after boot and with **no frame from a phone on USB in the last 5 s**, puts the board into deep
sleep; it wakes every 300 s, measures, and runs again from 3.60 V. A phone on USB powers the board,
so no shutdown happens then. Thresholds: `menuconfig -> micrOBU firmware`. The app shows
low/critical from the same voltage and its own thresholds. The first XIAO build read 0 mV because the divider had
no time to settle at `CONFIG_FREERTOS_HZ=100` (fixed 2026-11-11, see NOTES.md); it now reads 4.14-4.16 V on a charging
cell. There is no calibration trim and no multimeter reference on the glued-in cell: the plateau at full charge
should sit near the charger's 4.20 V.
## If the build fails
`idf.py fullclean`, then build again. A "provided by component(s) ... not in the requirements list"
error means a header moved to another IDF component: add it to `REQUIRES` in `main/CMakeLists.txt`.
## Going back
`firmware-backups/` (gitignored, on the lab laptop) holds full or 4 MB images of the boards as they
were before they were reflashed. Restore with
`python -m esptool --chip esp32c5 -p COM<N> write-flash 0 <image>`. The previous C firmware is in
git history (last commit before the port: `7285fa1`; the vanetza-idf port: `1c3a66e`).