diff python/usb.py @ 113:1f40be088ee8

Added first start stlink
author Windel Bouwman
date Sat, 05 Jan 2013 00:06:27 +0100
parents 056face59ee7
children f42268da614f
line wrap: on
line diff
--- a/python/usb.py	Fri Jan 04 18:40:05 2013 +0100
+++ b/python/usb.py	Sat Jan 05 00:06:27 2013 +0100
@@ -10,6 +10,7 @@
    f = getattr(libusb, name)
    f.argtypes = argtypes
    f.restype = restype
+   globals()[name] = f
    return f
 def enum(**enums):
    reverse = dict((value, key) for key, value in enums.items())
@@ -44,6 +45,11 @@
 libusb_device_p_p = POINTER(libusb_device_p)
 libusb_device_p_p_p = POINTER(libusb_device_p_p)
 
+class libusb_device_handle(Structure):
+   pass
+libusb_device_handle_p = POINTER(libusb_device_handle)
+libusb_device_handle_p_p = POINTER(libusb_device_handle_p)
+
 class libusb_device_descriptor(Structure):
    _fields_ = [
                ('bLength', c_uint8),
@@ -64,25 +70,25 @@
 libusb_device_descriptor_p = POINTER(libusb_device_descriptor)
 
 # functions
-libusb_init = buildfunc('libusb_init', [libusb_context_p_p], c_int)
+buildfunc('libusb_init', [libusb_context_p_p], c_int)
 
-libusb_get_device_list = buildfunc('libusb_get_device_list', \
+buildfunc('libusb_get_device_list', \
    [libusb_context_p, libusb_device_p_p_p], c_ssize_t)
-libusb_free_device_list = buildfunc('libusb_free_device_list',\
-   [libusb_device_p_p, c_int], None)
-libusb_get_bus_number = buildfunc('libusb_get_bus_number', \
-   [libusb_device_p], c_uint8)
-libusb_get_device_address = buildfunc('libusb_get_device_address', \
-   [libusb_device_p], c_uint8)
-libusb_get_device_speed = buildfunc('libusb_get_device_speed', \
-   [libusb_device_p], c_int)
-libusb_unref_device = buildfunc('libusb_unref_device', \
-   [libusb_device_p], None)
+buildfunc('libusb_free_device_list', [libusb_device_p_p, c_int], None)
+buildfunc('libusb_get_bus_number', [libusb_device_p], c_uint8)
+buildfunc('libusb_get_device_address', [libusb_device_p], c_uint8)
+buildfunc('libusb_get_device_speed', [libusb_device_p], c_int)
+buildfunc('libusb_unref_device', [libusb_device_p], None)
+buildfunc('libusb_open', [libusb_device_p, libusb_device_handle_p_p], c_int)
+buildfunc('libusb_close', [libusb_device_handle_p], None)
+buildfunc('libusb_get_configuration', \
+   [libusb_device_handle_p, POINTER(c_int)], c_int)
+buildfunc('libusb_set_configuration', [libusb_device_handle_p, c_int], c_int)
+buildfunc('libusb_claim_interface', [libusb_device_handle_p, c_int], c_int)
 
-libusb_get_device_descriptor = buildfunc('libusb_get_device_descriptor', [libusb_device_p, libusb_device_descriptor_p], c_int)
 
-# Quick hack function:
-#libusb_open_device_with_vid_pid = buildfunc('libusb_open_device_with_vid_pid', [libusb_context_p, ctypes.c_uint16, ctypes.c_uint16], libusb_device_handle_p)
+buildfunc('libusb_get_device_descriptor',\
+   [libusb_device_p, libusb_device_descriptor_p], c_int)
 
 # pythonic API:
 
@@ -145,9 +151,12 @@
       r = libusb_open(self.dev_p, byref(handle_p))
       if r != 0:
          raise UsbError('error opening device', r)
-      return UseDeviceHandle(self, handle_p)
+      return UsbDeviceHandle(self, handle_p)
    def __repr__(self):
-      r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X}'.format(self.BusNumber, self.DeviceAddress, self.VendorId, self.ProductId)
+      r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X} speed {4}' \
+         .format( \
+         self.BusNumber, self.DeviceAddress, self.VendorId, \
+         self.ProductId, self.Speed)
       return r2
 
 class UsbDeviceHandle:
@@ -155,6 +164,19 @@
    def __init__(self, device, handle_p):
       self.device = device
       self.handle_p = handle_p
+   def getConfiguration(self):
+      config = c_int()
+      r = libusb_get_configuration(self.handle_p, byref(config))
+      if r != 0: raise UsbError('Error getting configuration', r)
+      return config.value
+   def setConfiguration(self, config):
+      r = libusb_set_configuration(self.handle_p, config)
+      if r != 0: raise UsbError('Error setting configuration', r)
+   Configuration = property(getConfiguration, setConfiguration)
+   def claimInterface(self, interface_number):
+      r = libusb_claim_interface(self.handle_p, interface_number)
+      if r != 0: raise UsbError('Error claiming interface', r)
+
 
 class UsbTransfer:
    def __init__(self):