Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def open(self):
assert self.closed is True
# Get device handle
dev = usb.core.find(custom_match=FindDap(self.serial_number))
if dev is None:
raise DAPAccessIntf.DeviceError("Device %s not found" %
self.serial_number)
# get active config
config = dev.get_active_configuration()
# Get hid interface
interface = None
interface_number = None
for interface in config:
if interface.bInterfaceClass == 0x03:
interface_number = interface.bInterfaceNumber
break
if interface_number is None or interface is None:
raise DAPAccessIntf.DeviceError("Device %s has no hid interface" %
self.serial_number)
# Check validity of backend env var.
if USB_BACKEND and ((USB_BACKEND not in INTERFACE) or (not INTERFACE[USB_BACKEND].isAvailable)):
LOG.error("Invalid USB backend specified in PYOCD_USB_BACKEND: " + USB_BACKEND)
USB_BACKEND = ""
# Select backend based on OS and availability.
if not USB_BACKEND:
if os.name == "nt":
# Prefer hidapi over pyWinUSB for Windows, since pyWinUSB has known bug(s)
if HidApiUSB.isAvailable:
USB_BACKEND = "hidapiusb"
elif PyWinUSB.isAvailable:
USB_BACKEND = "pywinusb"
else:
raise DAPAccessIntf.DeviceError("No USB backend found")
elif os.name == "posix":
# Select hidapi for OS X and pyUSB for Linux.
if os.uname()[0] == 'Darwin':
USB_BACKEND = "hidapiusb"
else:
USB_BACKEND = "pyusb"
else:
raise DAPAccessIntf.DeviceError("No USB backend found")
USB_BACKEND_V2 = "pyusb_v2"
def open(self):
assert self.closed is True
# Get device handle
dev = usb.core.find(custom_match=FindDap(self.serial_number))
if dev is None:
raise DAPAccessIntf.DeviceError("Device %s not found" % self.serial_number)
# get active config
config = dev.get_active_configuration()
# Get CMSIS-DAPv1 interface
interface = usb.util.find_descriptor(config, custom_match=_match_cmsis_dap_v1_interface)
if interface is None:
raise DAPAccessIntf.DeviceError("Device %s has no CMSIS-DAPv1 interface" %
self.serial_number)
interface_number = interface.bInterfaceNumber
# Find endpoints
ep_in, ep_out = None, None
for endpoint in interface:
if endpoint.bEndpointAddress & usb.util.ENDPOINT_IN:
ep_in = endpoint
def __init__(self, faultAddress=None):
super(DAPAccessIntf.TransferFaultError, self).__init__(faultAddress)
self._address = faultAddress
def jtag_id_code(self, index=0):
cmd = []
cmd.append(Command.DAP_JTAG_IDCODE)
cmd.append(index)
self.interface.write(cmd)
resp = self.interface.read()
if resp[0] != Command.DAP_JTAG_IDCODE:
# Response is to a different command
raise DAPAccessIntf.DeviceError()
if resp[1] != DAP_OK:
# Operation failed
raise DAPAccessIntf.CommandError()
return (resp[2] << 0) | \
(resp[3] << 8) | \
(resp[4] << 16) | \
(resp[5] << 24)
def _abort_all_transfers(self, exception):
"""! @brief Abort any ongoing transfers and clear all buffers
"""
pending_reads = len(self._commands_to_read)
# invalidate _transfer_list
for transfer in self._transfer_list:
transfer.add_error(exception)
# clear all deferred buffers
self._init_deferred_buffers()
# finish all pending reads and ignore the data
# Only do this if the error is a tranfer error.
# Otherwise this could cause another exception
if isinstance(exception, DAPAccessIntf.TransferError):
for _ in range(pending_reads):
self._interface.read()