Skip to content

Commit 20491df

Browse files
committed
Switch to libusb_package
Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
1 parent 98c1e0b commit 20491df

3 files changed

Lines changed: 455 additions & 521 deletions

File tree

mpflash/flash/stm32_dfu.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ def init_libusb_windows():
1313
"""
1414
Initializes the libusb backend on Windows and caches it module-wide.
1515
16+
Uses libusb_package, which bundles pre-built libusb shared libraries and
17+
exposes a ready-to-use pyusb backend, removing the need to locate the DLL.
18+
1619
Returns:
1720
The usb backend object if successful, None otherwise.
1821
"""
1922
global _libusb_backend
2023
if _libusb_backend is not None:
2124
return _libusb_backend
25+
try:
26+
import libusb_package # type: ignore
27+
except ImportError:
28+
log.error("libusb_package not found, please install it with 'pip install libusb-package'")
29+
return None
2230

23-
import libusb # type: ignore
24-
import usb.backend.libusb1 as libusb1
25-
26-
arch = "x86_64" if platform.architecture()[0] == "64bit" else "x86"
27-
libusb1_dll = Path(libusb.__file__).parent / "_platform" / "windows" / arch / "libusb-1.0.dll"
28-
if not libusb1_dll.exists():
29-
raise FileNotFoundError(f"libusb1.dll not found at {libusb1_dll}")
30-
_libusb_backend = libusb1.get_backend(find_library=lambda x: libusb1_dll.as_posix())
31+
_libusb_backend = libusb_package.get_libusb1_backend()
3132
return _libusb_backend
3233

3334

@@ -95,11 +96,11 @@ def flash_stm32_dfu(
9596
try:
9697
pydfu.list_dfu_devices(**kwargs)
9798
except ValueError as e:
98-
log.error(f"Insuffient permissions to access usb DFU devices: {e}")
99+
log.error(f"Insufficient permissions to access usb DFU devices: {e}")
99100
return None
100101

101102
# Needs to be a list of serial ports
102-
log.debug("Inititialize pydfu...")
103+
log.debug("Initialize pydfu...")
103104
pydfu.init(**kwargs)
104105

105106
if erase:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ dependencies = [
3737
"questionary>=2.0.0",
3838
"jsonlines>=4.0.0",
3939
"jsons>=1.6.3",
40-
"libusb>=1.0.27; sys_platform == 'win32'",
4140
"pywin32>=310; sys_platform == 'win32'",
4241
"loguru>=0.7.2",
4342
"mpremote>=1.22.0",
@@ -54,6 +53,7 @@ dependencies = [
5453
"tomli>=2.2.1",
5554
"pytest-cov>=7.0.0",
5655
"peewee>=4.0.3",
56+
"libusb-package>=1.0.26.0; sys_platform == 'win32'",
5757
]
5858

5959
[project.optional-dependencies]

0 commit comments

Comments
 (0)