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.
88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
"""Print the micrOBU's STATUS (incl. the battery fields) from its native USB Serial/JTAG port.
|
|
|
|
py -3.11 tools/station_status_watch.py COM10 [seconds]
|
|
|
|
Speaks the station-link framing of obu-firmware (0xAA55, type, u16 length, payload, CRC-16/CCITT-FALSE
|
|
over type..payload, little endian; frame type 0x10 = LINK) and asks for STATUS once a second. The
|
|
board also broadcasts STATUS itself every second. Opening the port resets the board (DTR/RTS), so
|
|
the first frames come after the boot. Needs pyserial (the ESP-IDF python env has it).
|
|
"""
|
|
import struct
|
|
import sys
|
|
import time
|
|
|
|
import serial
|
|
|
|
LINK = 0x10
|
|
STATUS_REQUEST = 0x0C
|
|
STATUS = 0x84
|
|
|
|
|
|
def crc16(data: bytes) -> int:
|
|
crc = 0xFFFF
|
|
for b in data:
|
|
crc ^= b << 8
|
|
for _ in range(8):
|
|
crc = ((crc << 1) ^ 0x1021) & 0xFFFF if crc & 0x8000 else (crc << 1) & 0xFFFF
|
|
return crc
|
|
|
|
|
|
def frame(type_: int, payload: bytes) -> bytes:
|
|
head = bytes([type_]) + struct.pack('<H', len(payload)) + payload
|
|
return b'\xaa\x55' + head + struct.pack('<H', crc16(head))
|
|
|
|
|
|
def parse(buffer: bytearray):
|
|
"""Yield (type, payload) for each complete, CRC-valid frame; consumes the buffer."""
|
|
while True:
|
|
i = buffer.find(b'\xaa\x55')
|
|
if i < 0 or len(buffer) < i + 5:
|
|
if i > 0:
|
|
del buffer[:i]
|
|
return
|
|
length = buffer[i + 3] | (buffer[i + 4] << 8)
|
|
end = i + 5 + length + 2
|
|
if len(buffer) < end:
|
|
if i > 0:
|
|
del buffer[:i]
|
|
return
|
|
head, crc = bytes(buffer[i + 2:end - 2]), buffer[end - 2] | (buffer[end - 1] << 8)
|
|
del buffer[:end if crc16(head) == crc else i + 2]
|
|
if crc16(head) == crc:
|
|
yield head[0], head[3:]
|
|
|
|
|
|
def describe(body: bytes) -> str:
|
|
uptime, configured = struct.unpack_from('<IB', body, 0)
|
|
text = f"len {len(body)} uptime {uptime / 1000:.1f}s configured {configured}"
|
|
if len(body) >= 106:
|
|
mv, flags = struct.unpack_from('<HB', body, 103)
|
|
text += f" battery {mv} mV flags 0x{flags:02x}" + (" LOW" if flags & 1 else "") + (" CRITICAL" if flags & 2 else "")
|
|
else:
|
|
text += " (no battery fields)"
|
|
return text
|
|
|
|
|
|
def main():
|
|
port = sys.argv[1] if len(sys.argv) > 1 else 'COM10'
|
|
seconds = float(sys.argv[2]) if len(sys.argv) > 2 else 15
|
|
buffer = bytearray()
|
|
sequence = 0
|
|
with serial.Serial(port, 115200, timeout=0.2) as s:
|
|
s.dtr = True # a CDC host asserts DTR; the firmware may gate TX on it
|
|
end = time.time() + seconds
|
|
next_request = 0.0
|
|
while time.time() < end:
|
|
if time.time() >= next_request:
|
|
sequence += 1
|
|
s.write(frame(LINK, bytes([STATUS_REQUEST, 0]) + struct.pack('<H', sequence)))
|
|
next_request = time.time() + 1.0
|
|
buffer += s.read(512)
|
|
for type_, payload in parse(buffer):
|
|
if type_ == LINK and len(payload) >= 4 and payload[0] == STATUS:
|
|
print(time.strftime('%H:%M:%S'), describe(payload[4:]), flush=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|