Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the use of libusb-package optional #1332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pyocd/probe/picoprobe.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

from time import sleep
from usb import core, util
import libusb_package
try:
from libusb_package import find as usb_find
except ImportError:
from usb.core import find as usb_find

import platform
import errno
Expand Down Expand Up @@ -108,7 +111,7 @@ def enumerate_picoprobes(cls, uid=None) -> List["PicoLink"]:
"""@brief Find and return all Picoprobes """
try:
# Use a custom matcher to make sure the probe is a Picoprobe and accessible.
return [PicoLink(probe) for probe in libusb_package.find(find_all=True, custom_match=FindPicoprobe(uid))]
return [PicoLink(probe) for probe in usb_find(find_all=True, custom_match=FindPicoprobe(uid))]
except core.NoBackendError:
show_no_libusb_warning()
return []
Expand Down
10 changes: 7 additions & 3 deletions pyocd/probe/pydapaccess/interface/pyusb_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@
TRACE.setLevel(logging.CRITICAL)

try:
import libusb_package
import usb.core
import usb.util
try:
from libusb_package import find as usb_find
except ImportError:
from usb.core import find as usb_find
except ImportError:
IS_AVAILABLE = False
else:
IS_AVAILABLE = True


class PyUSB(Interface):
"""@brief CMSIS-DAP USB interface class using pyusb for the backend."""

Expand Down Expand Up @@ -81,7 +85,7 @@ def open(self):
assert self.closed is True

# Get device handle
dev = libusb_package.find(custom_match=FindDap(self.serial_number))
dev = usb_find(custom_match=FindDap(self.serial_number))
if dev is None:
raise DAPAccessIntf.DeviceError(f"Probe {self.serial_number} not found")

Expand Down Expand Up @@ -180,7 +184,7 @@ def get_all_connected_interfaces():
"""
# find all cmsis-dap devices
try:
all_devices = libusb_package.find(find_all=True, custom_match=FindDap())
all_devices = usb_find(find_all=True, custom_match=FindDap())
except usb.core.NoBackendError:
if not PyUSB.did_show_no_libusb_warning:
LOG.warning("CMSIS-DAPv1 probes may not be detected because no libusb library was found.")
Expand Down
10 changes: 7 additions & 3 deletions pyocd/probe/pydapaccess/interface/pyusb_v2_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@
TRACE.setLevel(logging.CRITICAL)

try:
import libusb_package
import usb.core
import usb.util
try:
from libusb_package import find as usb_find
except ImportError:
from usb.core import find as usb_find
except ImportError:
IS_AVAILABLE = False
else:
IS_AVAILABLE = True


class PyUSBv2(Interface):
"""@brief CMSIS-DAPv2 interface using pyUSB."""

Expand Down Expand Up @@ -95,7 +99,7 @@ def open(self):
assert self.closed is True

# Get device handle
dev = libusb_package.find(custom_match=HasCmsisDapv2Interface(self.serial_number))
dev = usb_find(custom_match=HasCmsisDapv2Interface(self.serial_number))
if dev is None:
raise DAPAccessIntf.DeviceError(f"Probe {self.serial_number} not found")

Expand Down Expand Up @@ -204,7 +208,7 @@ def get_all_connected_interfaces():
"""@brief Returns all the connected devices with a CMSIS-DAPv2 interface."""
# find all cmsis-dap devices
try:
all_devices = libusb_package.find(find_all=True, custom_match=HasCmsisDapv2Interface())
all_devices = usb_find(find_all=True, custom_match=HasCmsisDapv2Interface())
except usb.core.NoBackendError:
common.show_no_libusb_warning()
return []
Expand Down
7 changes: 5 additions & 2 deletions pyocd/probe/stlink/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import libusb_package
try:
from libusb_package import find as usb_find
except ImportError:
from usb.core import find as usb_find
import usb.core
import usb.util
import logging
Expand Down Expand Up @@ -101,7 +104,7 @@ def _usb_match(cls, dev):
@classmethod
def get_all_connected_devices(cls):
try:
devices = libusb_package.find(find_all=True, custom_match=cls._usb_match)
devices = usb_find(find_all=True, custom_match=cls._usb_match)
except usb.core.NoBackendError:
common.show_no_libusb_warning()
return []
Expand Down
Loading