Skip to content

Commit

Permalink
Merge pull request #527 from Josverl/libusb_on_windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Josverl authored Mar 12, 2024
2 parents 08b61bf + b05527b commit e37309a
Show file tree
Hide file tree
Showing 11 changed files with 483 additions and 170 deletions.
174 changes: 174 additions & 0 deletions src/mpflash/libusb_flash.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<DEVICE ID 2109:2812 on Bus 002 Address 004>,\n",
" <DEVICE ID f055:9800 on Bus 002 Address 040>,\n",
" <DEVICE ID 045e:0c1e on Bus 002 Address 002>,\n",
" <DEVICE ID 043e:9a39 on Bus 002 Address 035>,\n",
" <DEVICE ID 0b0e:245e on Bus 002 Address 032>,\n",
" <DEVICE ID 045e:0927 on Bus 002 Address 010>,\n",
" <DEVICE ID 2109:2812 on Bus 002 Address 007>,\n",
" <DEVICE ID 045e:07b2 on Bus 002 Address 033>,\n",
" <DEVICE ID 1000:2000 on Bus 002 Address 031>,\n",
" <DEVICE ID 046d:085e on Bus 002 Address 011>,\n",
" <DEVICE ID 0bda:5401 on Bus 002 Address 006>,\n",
" <DEVICE ID 0bda:5411 on Bus 002 Address 003>,\n",
" <DEVICE ID 0a05:7211 on Bus 002 Address 013>,\n",
" <DEVICE ID 0a05:7211 on Bus 002 Address 012>,\n",
" <DEVICE ID 8086:a0ed on Bus 002 Address 000>,\n",
" <DEVICE ID 8087:0029 on Bus 002 Address 018>,\n",
" <DEVICE ID 8086:9a13 on Bus 001 Address 000>]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import usb.core\n",
"import usb.util\n",
"import usb.backend.libusb1 as libusb1\n",
"from pathlib import Path\n",
"import platform\n",
"\n",
"if platform.system() == \"Windows\":\n",
" # on windows you need to use the libusb1 backend\n",
" import libusb\n",
"\n",
" arch = \"x64\" if platform.architecture()[0] == \"64bit\" else \"x86\"\n",
" libusb1_dll = Path(libusb.__file__).parent / f\"_platform\\\\_windows\\\\{arch}\\\\libusb-1.0.dll\"\n",
"\n",
" backend = libusb1.get_backend(find_library=lambda x: libusb1_dll.as_posix())\n",
"usb_devices = usb.core.find(backend=backend, find_all=True)\n",
"\n",
"list(usb_devices)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No DFU devices found\n"
]
}
],
"source": [
"from mpflash.vendored import pydfu as pydfu\n",
"\n",
"try:\n",
" pydfu.list_dfu_devices()\n",
"except SystemExit:\n",
" print(\"No DFU devices found\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"pydfu.init()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Read DFU file...\n",
"File: C:\\Users\\josverl\\Downloads\\firmware\\stm32\\PYBV11-THREAD-v1.23.0-preview.203.dfu\n",
" b'DfuSe' v1, image size: 365621, targets: 1\n",
" b'Target' 0, alt setting: 0, name: \"ST...\", size: 365336, elements: 2\n",
" 0, address: 0x08000000, size: 14712\n",
" 1, address: 0x08020000, size: 350608\n",
" usb: 0483:df11, device: 0x0000, dfu: 0x011a, b'UFD', 16, 0xd114e190\n"
]
}
],
"source": [
"dfu_file = Path(\"C:\\\\Users\\\\josverl\\\\Downloads\\\\firmware\\\\stm32\\\\PYBV11-THREAD-v1.23.0-preview.203.dfu\")\n",
"\n",
"print(\"Read DFU file...\")\n",
"elements = pydfu.read_dfu_file(dfu_file)\n",
"if not elements:\n",
" print(\"No data in dfu file\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing memory...\n",
"0x08000000 14712 [=========================] 100% \n",
"0x08020000 350608 [=========================] 100% \n"
]
}
],
"source": [
"print(\"Writing memory...\")\n",
"pydfu.write_elements(elements, False, progress=pydfu.cli_progress)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Exiting DFU...\n"
]
}
],
"source": [
"print(\"Exiting DFU...\")\n",
"pydfu.exit_dfu()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
4 changes: 2 additions & 2 deletions src/mpflash/mpflash/basic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .vendored import pydfu as pydfu
from argparse import Namespace
from pathlib import Path

from .vendored import pydfu as pydfu


def main():
print("Hello, DFU!")
Expand Down
32 changes: 25 additions & 7 deletions src/mpflash/mpflash/common.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import time
from typing import TypedDict

from github import Github
from loguru import logger as log
from packaging.version import parse
from rich.progress import track

from mpflash.mpremoteboard.mpremoteboard import MPRemoteBoard

PORT_FWTYPES = {
"stm32": [".hex", ".dfu"], # .hex for cube cli, but need .dfu for pydfu.py
"stm32": [".dfu"], # need .dfu for pydfu.py - .hex for cube cli/GUI
"esp32": [".bin"],
"esp8266": [".bin"],
"rp2": [".uf2"],
Expand All @@ -19,11 +22,12 @@

class FWInfo(TypedDict):
filename: str
port:str
board:str
variant:str
port: str
board: str
variant: str
preview: bool
version:str
version: str


#############################################################
# Version handling copied from stubber/utils/versions.py
Expand Down Expand Up @@ -122,8 +126,22 @@ def micropython_versions(minver: str = "v1.9.2"):
def get_stable_version() -> str:
# read the versions from the git tags
all_versions = micropython_versions(minver="v1.17")
stable_version = [v for v in all_versions if not v.endswith(V_PREVIEW)][-1]
return stable_version
return [v for v in all_versions if not v.endswith(V_PREVIEW)][-1]


#############################################################
def wait_for_restart(mcu: MPRemoteBoard, timeout: int = 10):
"""wait for the board to restart"""
for _ in track(
range(timeout),
description="Waiting for the board to restart",
transient=True,
get_time=lambda: time.time(),
show_speed=False,
):
time.sleep(1)
try:
mcu.get_mcu_info()
break
except ConnectionError:
pass
7 changes: 3 additions & 4 deletions src/mpflash/mpflash/flash_esp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from loguru import logger as log

from .mpremoteboard.mpremoteboard import MPRemoteBoard

from .common import wait_for_restart

def flash_esp(
mcu: MPRemoteBoard, fw_file: Path, *, erase: bool = True
Expand Down Expand Up @@ -59,7 +59,6 @@ def flash_esp(
return None

log.info("Done flashing, resetting the board and wait for it to restart")
time.sleep(5)
mcu.get_mcu_info()
log.success(f"Flashed {mcu.version} to {mcu.board} on {mcu.serialport} done")
wait_for_restart(mcu)
log.success(f"Flashed {mcu.version} to {mcu.board}")
return mcu
27 changes: 18 additions & 9 deletions src/mpflash/mpflash/flash_stm32.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
"""Flash STM32 boards using either STM32CubeProgrammer CLI or dfu-util"""

from pathlib import Path
from .flash_stm32_dfu import flash_stm32_dfu
from .flash_stm32_cube import flash_stm32_cubecli

from loguru import logger as log

from mpflash.common import wait_for_restart

# from .flash_stm32_cube import flash_stm32_cubecli
from .flash_stm32_dfu import dfu_init, flash_stm32_dfu
from .mpremoteboard.mpremoteboard import MPRemoteBoard


def flash_stm32(mcu: MPRemoteBoard, fw_file: Path, *, erase: bool, stm32_hex: bool):
"""Flash STM32 boards using either STM32CubeProgrammer CLI or dfu-util"""
# TODO: Check installed utilities / Lazy import
if stm32_hex:
updated = flash_stm32_cubecli(mcu, fw_file=fw_file, erase=erase)
else:
updated = flash_stm32_dfu(mcu, fw_file=fw_file, erase=erase)
def flash_stm32(mcu: MPRemoteBoard, fw_file: Path, *, erase: bool, stm32_dfu: bool = True):
# sourcery skip: lift-return-into-if
log.info("Using dfu-util")
dfu_init()
updated = flash_stm32_dfu(mcu, fw_file=fw_file, erase=erase)
# if stm32_dfu:
# else:
# log.info("Using STM32CubeProgrammer CLI")
# updated = flash_stm32_cubecli(mcu, fw_file=fw_file, erase=erase)

wait_for_restart(mcu)
log.success(f"Flashed {mcu.version} to {mcu.board}")
return updated
Loading

0 comments on commit e37309a

Please sign in to comment.