diff --git a/python/usb.py b/python/usb.py index a43bb51d..0ea0020b 100644 --- a/python/usb.py +++ b/python/usb.py @@ -34,9 +34,15 @@ class STBootloaderUSBHandle(BaseSTBootloaderHandle): def __init__(self, libusb_device, libusb_handle): self._libusb_handle = libusb_handle - # TODO: Find a way to detect F4 vs F2 - # TODO: also check F4 BCD, don't assume in else - self._mcu_type = McuType.H7 if libusb_device.getbcdDevice() == 512 else McuType.F4 + # lsusb -v | grep Flash + # iInterface 4 @Internal Flash /0x08000000/04*016Kg,01*064Kg,011*128Kg + out = libusb_handle.controlRead(0x80, 0x06, 0x0300 | 4, 0, 255) + flash_desc = bytes(out[2:]).decode('utf-16le') + sector_count = sum([int(s.split('*')[0]) for s in flash_desc.split('/')[-1].split(',')]) + + mcu_by_sector_count = {len(m.config.sector_sizes): m for m in McuType} + assert sector_count in mcu_by_sector_count, f"Unkown MCU: {sector_count=}" + self._mcu_type = mcu_by_sector_count[sector_count] def _status(self) -> None: while 1: