annotate python/usb.py @ 114:f42268da614f

Connected to stm32f4discovery
author Windel Bouwman
date Sat, 05 Jan 2013 19:07:14 +0100
parents 1f40be088ee8
children 654093a9a1e3
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
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
110
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
111 buildfunc('libusb_get_device_descriptor',\
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
112 [libusb_device_p, libusb_device_descriptor_p])
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
113
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
114 # synchronous functions:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
115 buildfunc('libusb_bulk_transfer', [libusb_device_handle_p, c_uint8, \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
116 c_void_p, c_int, c_int_p, c_uint])
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
117
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
118 # pythonic API:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
119
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
120 class UsbError(Exception):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
121 def __init__(self, msg, errorcode):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
122 if errorcode in libusb_error.reverse_mapping:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
123 errorcode = libusb_error.reverse_mapping[errorcode]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
124 msg = msg + 'Error code: {0}'.format(errorcode)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
125 super().__init__(msg)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
126
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
127 class UsbContext(object):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
128 """ A usb context in case of multiple use """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
129 def __init__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
130 self.context_p = libusb_context_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
131 r = libusb_init(byref(self.context_p))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
132 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
133 raise UsbError('libusb_init error!', r)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
134 def getDeviceList(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
135 devlist = libusb_device_p_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
136 count = libusb_get_device_list(self.context_p, byref(devlist))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
137 if count < 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
138 raise UsbError('Error getting device list', count)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
139 l = [UsbDevice(self, device_p.contents) for device_p in devlist[0:count]]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
140 libusb_free_device_list(devlist, 0)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
141 return l
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
142 DeviceList = property(getDeviceList)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
143
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
144 class UsbDevice:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
145 """ A detected usb device """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
146 def __init__(self, context, device_p):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
147 self.context = context
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
148 self.dev_p = device_p
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
149 def __del__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
150 libusb_unref_device(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
151 def getBusNumber(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
152 return libusb_get_bus_number(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
153 BusNumber = property(getBusNumber)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
154 def getDeviceAddress(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
155 return libusb_get_device_address(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
156 DeviceAddress = property(getDeviceAddress)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
157 def getSpeed(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
158 s = libusb_get_device_speed(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
159 if s in libusb_speed.reverse_mapping:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
160 s = libusb_speed.reverse_mapping[s]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
161 return s
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
162 Speed = property(getSpeed)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
163 def getDescriptor(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
164 descriptor = libusb_device_descriptor()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
165 r = libusb_get_device_descriptor(self.dev_p, byref(descriptor))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
166 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
167 raise UsbError('Error getting descriptor', r)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
168 return descriptor
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
169 Descriptor = property(getDescriptor)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
170 VendorId = property(lambda self: self.Descriptor.idVendor)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
171 ProductId = property(lambda self: self.Descriptor.idProduct)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
172 NumConfigurations = property(lambda self: self.Descriptor.bNumConfigurations)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
173 def open(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
174 """ Opens this device and returns a handle """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
175 handle_p = libusb_device_handle_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
176 r = libusb_open(self.dev_p, byref(handle_p))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
177 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
178 raise UsbError('error opening device', r)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
179 return UsbDeviceHandle(self, handle_p)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
180 def __repr__(self):
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
181 r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X} speed {4}' \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
182 .format( \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
183 self.BusNumber, self.DeviceAddress, self.VendorId, \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
184 self.ProductId, self.Speed)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
185 return r2
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
186
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
187 USB_ENDPOINT_DIR_MASK = 0x80
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
188 USB_ENDPOINT_IN = 0x80
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
189 USB_ENDPOINT_OUT = 0x0
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
190
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
191 class UsbDeviceHandle:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
192 """ Handle to a detected usb device """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
193 def __init__(self, device, handle_p):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
194 self.device = device
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
195 self.handle_p = handle_p
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
196 def getConfiguration(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
197 config = c_int()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
198 r = libusb_get_configuration(self.handle_p, byref(config))
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
199 if r != 0: raise UsbError('Error getting configuration', r)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
200 return config.value
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
201 def setConfiguration(self, config):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
202 r = libusb_set_configuration(self.handle_p, config)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
203 if r != 0: raise UsbError('Error setting configuration', r)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
204 Configuration = property(getConfiguration, setConfiguration)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
205 def claimInterface(self, interface_number):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
206 r = libusb_claim_interface(self.handle_p, interface_number)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
207 if r != 0: raise UsbError('Error claiming interface', r)
114
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
208 def bulkWrite(self, endpoint, data, timeout=0):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
209 """ Synchronous bulk write """
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
210 assert type(data) is bytes
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
211 # assure the endpoint indicates the correct:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
212 endpoint = (endpoint & (~USB_ENDPOINT_DIR_MASK)) | USB_ENDPOINT_OUT
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
213 buf = create_string_buffer(data)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
214 transferred = c_int()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
215 r = libusb_bulk_transfer(self.handle_p, endpoint, buf, len(data), \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
216 byref(transferred), timeout)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
217 if r != 0:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
218 raise UsbError('Bulk write failed', r)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
219 if transferred.value != len(data):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
220 raise UsbError('Not all {0} transferred {1}'.format(len(data), \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
221 transferred.value))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
222 def bulkRead(self, endpoint, numbytes, timeout=0):
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
223 """ Synchronous bulk read """
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
224 # assure the endpoint indicates the correct:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
225 endpoint = (endpoint & (~USB_ENDPOINT_DIR_MASK)) | USB_ENDPOINT_IN
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
226 buf = create_string_buffer(numbytes)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
227 transferred = c_int()
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
228 r = libusb_bulk_transfer(self.handle_p, endpoint, buf, numbytes, \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
229 byref(transferred), timeout)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
230 if r != 0:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
231 raise UsbError('Bulk read failed', r)
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
232 if transferred.value != numbytes:
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
233 raise UsbError('Not all {0} transferred {1}'.format(numbytes, \
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
234 transferred.value))
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
235 data = buf.raw[0:numbytes]
f42268da614f Connected to stm32f4discovery
Windel Bouwman
parents: 113
diff changeset
236 return data
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
237
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
238 class UsbTransfer:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
239 def __init__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
240 libusb_alloc_transfer(0)