Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
interface_number = interface.bInterfaceNumber
# Find endpoints. CMSIS-DAPv2 endpoints are in a fixed order.
try:
ep_out = interface.endpoints()[0]
ep_in = interface.endpoints()[1]
ep_swo = interface.endpoints()[2] if len(interface.endpoints()) > 2 else None
except IndexError:
raise DAPAccessIntf.DeviceError("CMSIS-DAPv2 device %s is missing endpoints" %
self.serial_number)
# Explicitly claim the interface
try:
usb.util.claim_interface(dev, interface_number)
except usb.core.USBError as exc:
raise six.raise_from(DAPAccessIntf.DeviceError("Unable to open device"), exc)
# Update all class variables if we made it here
self.ep_out = ep_out
self.ep_in = ep_in
self.ep_swo = ep_swo
self.dev = dev
self.intf_number = interface_number
# Start RX thread as the last step
self.closed = False
self.start_rx()
def set_swj_pins(self, output, pins, wait=0):
cmd = []
cmd.append(Command.DAP_SWJ_PINS)
cmd.append(output & 0xff)
cmd.append(pins)
cmd.append(wait & 0xff)
cmd.append((wait >> 8) & 0xff)
cmd.append((wait >> 16) & 0xff)
cmd.append((wait >> 24) & 0xff)
self.interface.write(cmd)
resp = self.interface.read()
if resp[0] != Command.DAP_SWJ_PINS:
# Response is to a different command
raise DAPAccessIntf.DeviceError()
return resp[1]
def swo_mode(self, mode):
cmd = []
cmd.append(Command.DAP_SWO_MODE)
cmd.append(mode)
self.interface.write(cmd)
resp = self.interface.read()
if resp[0] != Command.DAP_SWO_MODE:
# Response is to a different command
raise DAPAccessIntf.DeviceError()
if resp[1] != DAP_OK:
# Operation failed
raise DAPAccessIntf.CommandError()
return resp[1]
def open(self):
assert self.closed is True
# Get device handle
dev = usb.core.find(custom_match=HasCmsisDapv2Interface(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-DAPv2 interface
interface = usb.util.find_descriptor(config, custom_match=match_cmsis_dap_interface_name)
if interface is None:
raise DAPAccessIntf.DeviceError("Device %s has no CMSIS-DAPv2 interface" %
self.serial_number)
interface_number = interface.bInterfaceNumber
# Find endpoints. CMSIS-DAPv2 endpoints are in a fixed order.
try:
ep_out = interface.endpoints()[0]
ep_in = interface.endpoints()[1]
ep_swo = interface.endpoints()[2] if len(interface.endpoints()) > 2 else None
except IndexError:
raise DAPAccessIntf.DeviceError("CMSIS-DAPv2 device %s is missing endpoints" %
self.serial_number)
# Explicitly claim the interface
try:
usb.util.claim_interface(dev, interface_number)
except usb.core.USBError as exc:
config = dev.get_active_configuration()
# Get CMSIS-DAPv2 interface
interface = usb.util.find_descriptor(config, custom_match=_match_cmsis_dap_v2_interface)
if interface is None:
raise DAPAccessIntf.DeviceError("Device %s has no CMSIS-DAPv2 interface" %
self.serial_number)
interface_number = interface.bInterfaceNumber
# Find endpoints. CMSIS-DAPv2 endpoints are in a fixed order.
try:
ep_out = interface.endpoints()[0]
ep_in = interface.endpoints()[1]
ep_swo = interface.endpoints()[2] if len(interface.endpoints()) > 2 else None
except IndexError:
raise DAPAccessIntf.DeviceError("CMSIS-DAPv2 device %s is missing endpoints" %
self.serial_number)
# Explicitly claim the interface
try:
usb.util.claim_interface(dev, interface_number)
except usb.core.USBError as exc:
raise six.raise_from(DAPAccessIntf.DeviceError("Unable to open device"), exc)
# Update all class variables if we made it here
self.ep_out = ep_out
self.ep_in = ep_in
self.ep_swo = ep_swo
self.dev = dev
self.intf_number = interface_number
# Start RX thread as the last step
# Detach kernel driver
self.kernel_driver_was_attached = False
try:
if dev.is_kernel_driver_active(interface_number):
dev.detach_kernel_driver(interface_number)
self.kernel_driver_was_attached = True
except NotImplementedError as e:
# Some implementations don't don't have kernel attach/detach
pass
# Explicitly claim the interface
try:
usb.util.claim_interface(dev, interface_number)
except usb.core.USBError as exc:
raise six.raise_from(DAPAccessIntf.DeviceError("Unable to open device"), exc)
# Update all class variables if we made it here
self.ep_out = ep_out
self.ep_in = ep_in
self.dev = dev
self.intf_number = interface_number
# Start RX thread as the last step
self.closed = False
self.start_rx()
if interface_number is None or interface is None:
raise DAPAccessIntf.DeviceError("Device %s has no hid interface" %
self.serial_number)
# Find endpoints
ep_in, ep_out = None, None
for endpoint in interface:
if endpoint.bEndpointAddress & 0x80:
ep_in = endpoint
else:
ep_out = endpoint
# If there is no EP for OUT then we can use CTRL EP.
# The IN EP is required
if not ep_in:
raise DAPAccessIntf.DeviceError("Unable to open device -"
" no endpoints")
# Detach kernel driver
kernel_driver_was_attached = False
try:
if dev.is_kernel_driver_active(interface_number):
dev.detach_kernel_driver(interface_number)
kernel_driver_was_attached = True
except NotImplementedError as e:
# Some implementations don't don't have kernel attach/detach
log.debug('Exception detaching kernel driver: %s' %
str(e))
# Explicitly claim the interface
try:
usb.util.claim_interface(dev, interface_number)