Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
112:056face59ee7 | 113:1f40be088ee8 |
---|---|
8 # helper: | 8 # helper: |
9 def buildfunc(name, argtypes, restype): | 9 def buildfunc(name, argtypes, restype): |
10 f = getattr(libusb, name) | 10 f = getattr(libusb, name) |
11 f.argtypes = argtypes | 11 f.argtypes = argtypes |
12 f.restype = restype | 12 f.restype = restype |
13 globals()[name] = f | |
13 return f | 14 return f |
14 def enum(**enums): | 15 def enum(**enums): |
15 reverse = dict((value, key) for key, value in enums.items()) | 16 reverse = dict((value, key) for key, value in enums.items()) |
16 enums['reverse_mapping'] = reverse | 17 enums['reverse_mapping'] = reverse |
17 return type('enum', (), enums) | 18 return type('enum', (), enums) |
42 pass | 43 pass |
43 libusb_device_p = POINTER(libusb_device) | 44 libusb_device_p = POINTER(libusb_device) |
44 libusb_device_p_p = POINTER(libusb_device_p) | 45 libusb_device_p_p = POINTER(libusb_device_p) |
45 libusb_device_p_p_p = POINTER(libusb_device_p_p) | 46 libusb_device_p_p_p = POINTER(libusb_device_p_p) |
46 | 47 |
48 class libusb_device_handle(Structure): | |
49 pass | |
50 libusb_device_handle_p = POINTER(libusb_device_handle) | |
51 libusb_device_handle_p_p = POINTER(libusb_device_handle_p) | |
52 | |
47 class libusb_device_descriptor(Structure): | 53 class libusb_device_descriptor(Structure): |
48 _fields_ = [ | 54 _fields_ = [ |
49 ('bLength', c_uint8), | 55 ('bLength', c_uint8), |
50 ('bDescriptorType', c_uint8), | 56 ('bDescriptorType', c_uint8), |
51 ('bcdUSB', c_uint16), | 57 ('bcdUSB', c_uint16), |
62 ('iNumConfigurations', c_uint8) | 68 ('iNumConfigurations', c_uint8) |
63 ] | 69 ] |
64 libusb_device_descriptor_p = POINTER(libusb_device_descriptor) | 70 libusb_device_descriptor_p = POINTER(libusb_device_descriptor) |
65 | 71 |
66 # functions | 72 # functions |
67 libusb_init = buildfunc('libusb_init', [libusb_context_p_p], c_int) | 73 buildfunc('libusb_init', [libusb_context_p_p], c_int) |
68 | 74 |
69 libusb_get_device_list = buildfunc('libusb_get_device_list', \ | 75 buildfunc('libusb_get_device_list', \ |
70 [libusb_context_p, libusb_device_p_p_p], c_ssize_t) | 76 [libusb_context_p, libusb_device_p_p_p], c_ssize_t) |
71 libusb_free_device_list = buildfunc('libusb_free_device_list',\ | 77 buildfunc('libusb_free_device_list', [libusb_device_p_p, c_int], None) |
72 [libusb_device_p_p, c_int], None) | 78 buildfunc('libusb_get_bus_number', [libusb_device_p], c_uint8) |
73 libusb_get_bus_number = buildfunc('libusb_get_bus_number', \ | 79 buildfunc('libusb_get_device_address', [libusb_device_p], c_uint8) |
74 [libusb_device_p], c_uint8) | 80 buildfunc('libusb_get_device_speed', [libusb_device_p], c_int) |
75 libusb_get_device_address = buildfunc('libusb_get_device_address', \ | 81 buildfunc('libusb_unref_device', [libusb_device_p], None) |
76 [libusb_device_p], c_uint8) | 82 buildfunc('libusb_open', [libusb_device_p, libusb_device_handle_p_p], c_int) |
77 libusb_get_device_speed = buildfunc('libusb_get_device_speed', \ | 83 buildfunc('libusb_close', [libusb_device_handle_p], None) |
78 [libusb_device_p], c_int) | 84 buildfunc('libusb_get_configuration', \ |
79 libusb_unref_device = buildfunc('libusb_unref_device', \ | 85 [libusb_device_handle_p, POINTER(c_int)], c_int) |
80 [libusb_device_p], None) | 86 buildfunc('libusb_set_configuration', [libusb_device_handle_p, c_int], c_int) |
87 buildfunc('libusb_claim_interface', [libusb_device_handle_p, c_int], c_int) | |
81 | 88 |
82 libusb_get_device_descriptor = buildfunc('libusb_get_device_descriptor', [libusb_device_p, libusb_device_descriptor_p], c_int) | |
83 | 89 |
84 # Quick hack function: | 90 buildfunc('libusb_get_device_descriptor',\ |
85 #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) | 91 [libusb_device_p, libusb_device_descriptor_p], c_int) |
86 | 92 |
87 # pythonic API: | 93 # pythonic API: |
88 | 94 |
89 class UsbError(Exception): | 95 class UsbError(Exception): |
90 def __init__(self, msg, errorcode): | 96 def __init__(self, msg, errorcode): |
143 """ Opens this device and returns a handle """ | 149 """ Opens this device and returns a handle """ |
144 handle_p = libusb_device_handle_p() | 150 handle_p = libusb_device_handle_p() |
145 r = libusb_open(self.dev_p, byref(handle_p)) | 151 r = libusb_open(self.dev_p, byref(handle_p)) |
146 if r != 0: | 152 if r != 0: |
147 raise UsbError('error opening device', r) | 153 raise UsbError('error opening device', r) |
148 return UseDeviceHandle(self, handle_p) | 154 return UsbDeviceHandle(self, handle_p) |
149 def __repr__(self): | 155 def __repr__(self): |
150 r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X}'.format(self.BusNumber, self.DeviceAddress, self.VendorId, self.ProductId) | 156 r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X} speed {4}' \ |
157 .format( \ | |
158 self.BusNumber, self.DeviceAddress, self.VendorId, \ | |
159 self.ProductId, self.Speed) | |
151 return r2 | 160 return r2 |
152 | 161 |
153 class UsbDeviceHandle: | 162 class UsbDeviceHandle: |
154 """ Handle to a detected usb device """ | 163 """ Handle to a detected usb device """ |
155 def __init__(self, device, handle_p): | 164 def __init__(self, device, handle_p): |
156 self.device = device | 165 self.device = device |
157 self.handle_p = handle_p | 166 self.handle_p = handle_p |
167 def getConfiguration(self): | |
168 config = c_int() | |
169 r = libusb_get_configuration(self.handle_p, byref(config)) | |
170 if r != 0: raise UsbError('Error getting configuration', r) | |
171 return config.value | |
172 def setConfiguration(self, config): | |
173 r = libusb_set_configuration(self.handle_p, config) | |
174 if r != 0: raise UsbError('Error setting configuration', r) | |
175 Configuration = property(getConfiguration, setConfiguration) | |
176 def claimInterface(self, interface_number): | |
177 r = libusb_claim_interface(self.handle_p, interface_number) | |
178 if r != 0: raise UsbError('Error claiming interface', r) | |
179 | |
158 | 180 |
159 class UsbTransfer: | 181 class UsbTransfer: |
160 def __init__(self): | 182 def __init__(self): |
161 libusb_alloc_transfer(0) | 183 libusb_alloc_transfer(0) |