annotate python/usb.py @ 271:cf7d5fb7d9c8

Reorganization
author Windel Bouwman
date Tue, 20 Aug 2013 18:56:02 +0200
parents 654093a9a1e3
children
rev   line source
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
1 from ctypes import Structure, POINTER, CDLL, CFUNCTYPE
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
2 from ctypes import c_uint16, c_uint8, c_int, c_uint, c_ssize_t, c_void_p
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
3 from ctypes import byref, create_string_buffer
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
4
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
5 # libusb wrapper:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
6 libusb = CDLL('libusb-1.0.so')
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
7
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
8 # helper:
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
9 def buildfunc(name, argtypes, restype=c_int):
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
10 f = getattr(libusb, name)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
11 f.argtypes = argtypes
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
12 f.restype = restype
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
13 globals()[name] = f
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
14 return f
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
15 def enum(**enums):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
16 reverse = dict((value, key) for key, value in enums.items())
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
17 enums['reverse_mapping'] = reverse
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
18 return type('enum', (), enums)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
19
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
20 # enums
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
21 libusb_class_code = enum(PER_INTERFACE=0, AUDIO=1, COMM=2, HID=3, \
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
22 PHYSICAL=5, PRINTER=7, PTP=6, MASS_STORAGE=8, HUB=9, \
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
23 DATA=10, SMART_CARD=0xb, CONTENT_SECURITY=0xd, VIDEO=0xe, \
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
24 PERSONAL_HEALTHCARE=0xf, DIAGNOSTIC_DEVICE=0xdc, WIRELESS=0xe,\
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
25 APPLICATION=0xfe, VENDOR_SPEC=0xff)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
26 libusb_speed = enum(UNKNOWN=0, LOW=1, FULL=2, HIGH=3, SUPER=4)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
27 libusb_error = enum(SUCCES=0, ERROR_IO=-1, ERROR_INVALID_PARAM=-2, \
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
28 ERROR_ACCESS=-3, ERROR_NO_DEVICE=-4, ERROR_NOT_FOUND=-5, \
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
29 ERROR_BUSY=-6, ERROR_TIMEOUT=-7, ERROR_OVERFLOW=-8, \
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
30 ERROR_PIPE=-9, ERROR_INTERRUPTED=-10, ERROR_NO_MEM=-11, \
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
31 ERROR_NOT_SUPPORTED=-12, ERROR_OTHER=-99)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
32 libusb_transfer_status = enum(\
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
33 COMPLETED=0, ERROR=1, TIMED_OUT=2, \
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
34 CANCELLED=3, STALL=4, NO_DEVICE=5, OVERFLOW=6)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
35
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
36 # types
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
37 c_int_p = POINTER(c_int)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
38 class libusb_context(Structure):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
39 pass
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
40 libusb_context_p = POINTER(libusb_context)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
41 libusb_context_p_p = POINTER(libusb_context_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
42
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
43 class libusb_device(Structure):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
44 pass
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
45 libusb_device_p = POINTER(libusb_device)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
46 libusb_device_p_p = POINTER(libusb_device_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
47 libusb_device_p_p_p = POINTER(libusb_device_p_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
48
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
49 class libusb_device_handle(Structure):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
50 pass
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
51 libusb_device_handle_p = POINTER(libusb_device_handle)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
52 libusb_device_handle_p_p = POINTER(libusb_device_handle_p)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
53
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
54 class libusb_device_descriptor(Structure):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
55 _fields_ = [
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
56 ('bLength', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
57 ('bDescriptorType', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
58 ('bcdUSB', c_uint16),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
59 ('bDeviceClass', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
60 ('bDeviceSubClass', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
61 ('bDeviceProtocol', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
62 ('bMaxPacketSize0', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
63 ('idVendor', c_uint16),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
64 ('idProduct', c_uint16),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
65 ('bcdDevice', c_uint16),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
66 ('iManufacturer', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
67 ('iProduct', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
68 ('iSerialNumber', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
69 ('iNumConfigurations', c_uint8)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
70 ]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
71 libusb_device_descriptor_p = POINTER(libusb_device_descriptor)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
72
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
73 """
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
74 class libusb_transfer(Structure):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
75 pass
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
76 libusb_transfer_p = POINTER(libusb_transfer)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
77 libusb_transfer_cb_fn = CFUNCTYPE(None, libusb_transfer_p)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
78 libusb_transfer._fields_ = [
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
79 ('dev_handle', libusb_device_handle_p),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
80 ('flags', c_uint8),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
81 ('endpoint', c_uchar),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
82 ('type', c_uchar),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
83 ('timeout', c_uint),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
84 ('status', c_int), # enum libusb_transfer_status
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
85 ('length', c_int),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
86 ('actual_length', c_int),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
87 ('callback', libusb_transfer_cb_fn),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
88 ('userdata', c_void_p),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
89 ('buffer', c_void_p),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
90 ('num_iso_packets', c_int),
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
91 ('iso_packet_desc', libusb_iso_packet_descriptor)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
92 ]
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
93 """
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
94 # functions
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
95 buildfunc('libusb_init', [libusb_context_p_p], c_int)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
96
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
97 buildfunc('libusb_get_device_list', \
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
98 [libusb_context_p, libusb_device_p_p_p], c_ssize_t)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
99 buildfunc('libusb_free_device_list', [libusb_device_p_p, c_int], None)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
100 buildfunc('libusb_get_bus_number', [libusb_device_p], c_uint8)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
101 buildfunc('libusb_get_device_address', [libusb_device_p], c_uint8)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
102 buildfunc('libusb_get_device_speed', [libusb_device_p])
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
103 buildfunc('libusb_unref_device', [libusb_device_p], None)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
104 buildfunc('libusb_open', [libusb_device_p, libusb_device_handle_p_p])
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
105 buildfunc('libusb_close', [libusb_device_handle_p], None)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
106 buildfunc('libusb_get_configuration',[libusb_device_handle_p,POINTER(c_int)])
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
107 buildfunc('libusb_set_configuration', [libusb_device_handle_p, c_int])
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
108 buildfunc('libusb_claim_interface', [libusb_device_handle_p, c_int])
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
109
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
110 buildfunc('libusb_get_device_descriptor',\
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
111 [libusb_device_p, libusb_device_descriptor_p])
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
112
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
113 # synchronous functions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
114 buildfunc('libusb_bulk_transfer', [libusb_device_handle_p, c_uint8, \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
115 c_void_p, c_int, c_int_p, c_uint])
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
116
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
117 # pythonic API:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
118
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
119 class UsbError(Exception):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
120 def __init__(self, msg, errorcode):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
121 if errorcode in libusb_error.reverse_mapping:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
122 errorcode = libusb_error.reverse_mapping[errorcode]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
123 msg = msg + 'Error code: {0}'.format(errorcode)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
124 super().__init__(msg)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
125
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
126 class UsbContext(object):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
127 """ A usb context in case of multiple use """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
128 def __init__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
129 self.context_p = libusb_context_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
130 r = libusb_init(byref(self.context_p))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
131 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
132 raise UsbError('libusb_init error!', r)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
133 def getDeviceList(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
134 devlist = libusb_device_p_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
135 count = libusb_get_device_list(self.context_p, byref(devlist))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
136 if count < 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
137 raise UsbError('Error getting device list', count)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
138 l = [UsbDevice(self, device_p.contents) for device_p in devlist[0:count]]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
139 libusb_free_device_list(devlist, 0)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
140 return l
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
141 DeviceList = property(getDeviceList)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
142
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
143 class UsbDevice:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
144 """ A detected usb device """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
145 def __init__(self, context, device_p):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
146 self.context = context
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
147 self.dev_p = device_p
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
148 def __del__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
149 libusb_unref_device(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
150 def getBusNumber(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
151 return libusb_get_bus_number(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
152 BusNumber = property(getBusNumber)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
153 def getDeviceAddress(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
154 return libusb_get_device_address(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
155 DeviceAddress = property(getDeviceAddress)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
156 def getSpeed(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
157 s = libusb_get_device_speed(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
158 if s in libusb_speed.reverse_mapping:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
159 s = libusb_speed.reverse_mapping[s]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
160 return s
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
161 Speed = property(getSpeed)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
162 def getDescriptor(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
163 descriptor = libusb_device_descriptor()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
164 r = libusb_get_device_descriptor(self.dev_p, byref(descriptor))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
165 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
166 raise UsbError('Error getting descriptor', r)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
167 return descriptor
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
168 Descriptor = property(getDescriptor)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
169 VendorId = property(lambda self: self.Descriptor.idVendor)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
170 ProductId = property(lambda self: self.Descriptor.idProduct)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
171 NumConfigurations = property(lambda self: self.Descriptor.bNumConfigurations)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
172 def open(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
173 """ Opens this device and returns a handle """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
174 handle_p = libusb_device_handle_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
175 r = libusb_open(self.dev_p, byref(handle_p))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
176 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
177 raise UsbError('error opening device', r)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
178 return UsbDeviceHandle(self, handle_p)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
179 def __repr__(self):
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
180 r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X} speed {4}' \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
181 .format( \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
182 self.BusNumber, self.DeviceAddress, self.VendorId, \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
183 self.ProductId, self.Speed)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
184 return r2
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
185
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
186 USB_ENDPOINT_DIR_MASK = 0x80
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
187 USB_ENDPOINT_IN = 0x80
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
188 USB_ENDPOINT_OUT = 0x0
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
189
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
190 class UsbDeviceHandle:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
191 """ Handle to a detected usb device """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
192 def __init__(self, device, handle_p):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
193 self.device = device
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
194 self.handle_p = handle_p
130
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 114
diff changeset
195 def __del__(self):
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 114
diff changeset
196 self.close()
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 114
diff changeset
197 def close(self):
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 114
diff changeset
198 if self.handle_p:
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 114
diff changeset
199 libusb_close(self.handle_p)
654093a9a1e3 Added icons, improved device explorer
Windel Bouwman
parents: 114
diff changeset
200 self.handle_p = None
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
201 def getConfiguration(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
202 config = c_int()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
203 r = libusb_get_configuration(self.handle_p, byref(config))
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
204 if r != 0: raise UsbError('Error getting configuration', r)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
205 return config.value
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
206 def setConfiguration(self, config):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
207 r = libusb_set_configuration(self.handle_p, config)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
208 if r != 0: raise UsbError('Error setting configuration', r)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
209 Configuration = property(getConfiguration, setConfiguration)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
210 def claimInterface(self, interface_number):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
211 r = libusb_claim_interface(self.handle_p, interface_number)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
212 if r != 0: raise UsbError('Error claiming interface', r)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
213 def bulkWrite(self, endpoint, data, timeout=0):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
214 """ Synchronous bulk write """
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
215 assert type(data) is bytes
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
216 # assure the endpoint indicates the correct:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
217 endpoint = (endpoint & (~USB_ENDPOINT_DIR_MASK)) | USB_ENDPOINT_OUT
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
218 buf = create_string_buffer(data)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
219 transferred = c_int()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
220 r = libusb_bulk_transfer(self.handle_p, endpoint, buf, len(data), \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
221 byref(transferred), timeout)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
222 if r != 0:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
223 raise UsbError('Bulk write failed', r)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
224 if transferred.value != len(data):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
225 raise UsbError('Not all {0} transferred {1}'.format(len(data), \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
226 transferred.value))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
227 def bulkRead(self, endpoint, numbytes, timeout=0):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
228 """ Synchronous bulk read """
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
229 # assure the endpoint indicates the correct:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
230 endpoint = (endpoint & (~USB_ENDPOINT_DIR_MASK)) | USB_ENDPOINT_IN
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
231 buf = create_string_buffer(numbytes)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
232 transferred = c_int()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
233 r = libusb_bulk_transfer(self.handle_p, endpoint, buf, numbytes, \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
234 byref(transferred), timeout)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
235 if r != 0:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
236 raise UsbError('Bulk read failed', r)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
237 if transferred.value != numbytes:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
238 raise UsbError('Not all {0} transferred {1}'.format(numbytes, \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
239 transferred.value))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
240 data = buf.raw[0:numbytes]
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
241 return data
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
242
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
243 class UsbTransfer:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
244 def __init__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
245 libusb_alloc_transfer(0)