annotate 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
rev   line source
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
1 from ctypes import Structure, POINTER, CDLL
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
2 from ctypes import c_uint16, c_uint8, c_int, c_ssize_t
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
3 from ctypes import byref
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:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
9 def buildfunc(name, argtypes, restype):
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
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
37 class libusb_context(Structure):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
38 pass
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
39 libusb_context_p = POINTER(libusb_context)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
40 libusb_context_p_p = POINTER(libusb_context_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
41
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
42 class libusb_device(Structure):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
43 pass
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
44 libusb_device_p = POINTER(libusb_device)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
45 libusb_device_p_p = POINTER(libusb_device_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
46 libusb_device_p_p_p = POINTER(libusb_device_p_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
47
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
48 class libusb_device_handle(Structure):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
49 pass
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
50 libusb_device_handle_p = POINTER(libusb_device_handle)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
51 libusb_device_handle_p_p = POINTER(libusb_device_handle_p)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
52
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
53 class libusb_device_descriptor(Structure):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
54 _fields_ = [
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
55 ('bLength', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
56 ('bDescriptorType', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
57 ('bcdUSB', c_uint16),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
58 ('bDeviceClass', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
59 ('bDeviceSubClass', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
60 ('bDeviceProtocol', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
61 ('bMaxPacketSize0', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
62 ('idVendor', c_uint16),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
63 ('idProduct', c_uint16),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
64 ('bcdDevice', c_uint16),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
65 ('iManufacturer', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
66 ('iProduct', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
67 ('iSerialNumber', c_uint8),
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
68 ('iNumConfigurations', c_uint8)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
69 ]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
70 libusb_device_descriptor_p = POINTER(libusb_device_descriptor)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
71
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
72 # functions
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
73 buildfunc('libusb_init', [libusb_context_p_p], c_int)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
74
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
75 buildfunc('libusb_get_device_list', \
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
76 [libusb_context_p, libusb_device_p_p_p], c_ssize_t)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
77 buildfunc('libusb_free_device_list', [libusb_device_p_p, c_int], None)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
78 buildfunc('libusb_get_bus_number', [libusb_device_p], c_uint8)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
79 buildfunc('libusb_get_device_address', [libusb_device_p], c_uint8)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
80 buildfunc('libusb_get_device_speed', [libusb_device_p], c_int)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
81 buildfunc('libusb_unref_device', [libusb_device_p], None)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
82 buildfunc('libusb_open', [libusb_device_p, libusb_device_handle_p_p], c_int)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
83 buildfunc('libusb_close', [libusb_device_handle_p], None)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
84 buildfunc('libusb_get_configuration', \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
85 [libusb_device_handle_p, POINTER(c_int)], c_int)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
86 buildfunc('libusb_set_configuration', [libusb_device_handle_p, c_int], c_int)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
87 buildfunc('libusb_claim_interface', [libusb_device_handle_p, c_int], c_int)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
88
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
89
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
90 buildfunc('libusb_get_device_descriptor',\
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
91 [libusb_device_p, libusb_device_descriptor_p], c_int)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
92
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
93 # pythonic API:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
94
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
95 class UsbError(Exception):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
96 def __init__(self, msg, errorcode):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
97 if errorcode in libusb_error.reverse_mapping:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
98 errorcode = libusb_error.reverse_mapping[errorcode]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
99 msg = msg + 'Error code: {0}'.format(errorcode)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
100 super().__init__(msg)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
101
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
102 class UsbContext(object):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
103 """ A usb context in case of multiple use """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
104 def __init__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
105 self.context_p = libusb_context_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
106 r = libusb_init(byref(self.context_p))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
107 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
108 raise UsbError('libusb_init error!', r)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
109 def getDeviceList(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
110 devlist = libusb_device_p_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
111 count = libusb_get_device_list(self.context_p, byref(devlist))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
112 if count < 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
113 raise UsbError('Error getting device list', count)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
114 l = [UsbDevice(self, device_p.contents) for device_p in devlist[0:count]]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
115 libusb_free_device_list(devlist, 0)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
116 return l
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
117 DeviceList = property(getDeviceList)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
118
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
119 class UsbDevice:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
120 """ A detected usb device """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
121 def __init__(self, context, device_p):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
122 self.context = context
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
123 self.dev_p = device_p
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
124 def __del__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
125 libusb_unref_device(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
126 def getBusNumber(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
127 return libusb_get_bus_number(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
128 BusNumber = property(getBusNumber)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
129 def getDeviceAddress(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
130 return libusb_get_device_address(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
131 DeviceAddress = property(getDeviceAddress)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
132 def getSpeed(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
133 s = libusb_get_device_speed(self.dev_p)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
134 if s in libusb_speed.reverse_mapping:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
135 s = libusb_speed.reverse_mapping[s]
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
136 return s
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
137 Speed = property(getSpeed)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
138 def getDescriptor(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
139 descriptor = libusb_device_descriptor()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
140 r = libusb_get_device_descriptor(self.dev_p, byref(descriptor))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
141 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
142 raise UsbError('Error getting descriptor', r)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
143 return descriptor
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
144 Descriptor = property(getDescriptor)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
145 VendorId = property(lambda self: self.Descriptor.idVendor)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
146 ProductId = property(lambda self: self.Descriptor.idProduct)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
147 NumConfigurations = property(lambda self: self.Descriptor.bNumConfigurations)
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
148 def open(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
149 """ Opens this device and returns a handle """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
150 handle_p = libusb_device_handle_p()
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
151 r = libusb_open(self.dev_p, byref(handle_p))
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
152 if r != 0:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
153 raise UsbError('error opening device', r)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
154 return UsbDeviceHandle(self, handle_p)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
155 def __repr__(self):
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
156 r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X} speed {4}' \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
157 .format( \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
158 self.BusNumber, self.DeviceAddress, self.VendorId, \
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
159 self.ProductId, self.Speed)
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
160 return r2
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
161
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
162 class UsbDeviceHandle:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
163 """ Handle to a detected usb device """
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
164 def __init__(self, device, handle_p):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
165 self.device = device
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
166 self.handle_p = handle_p
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
167 def getConfiguration(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
168 config = c_int()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
169 r = libusb_get_configuration(self.handle_p, byref(config))
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
170 if r != 0: raise UsbError('Error getting configuration', r)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
171 return config.value
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
172 def setConfiguration(self, config):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
173 r = libusb_set_configuration(self.handle_p, config)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
174 if r != 0: raise UsbError('Error setting configuration', r)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
175 Configuration = property(getConfiguration, setConfiguration)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
176 def claimInterface(self, interface_number):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
177 r = libusb_claim_interface(self.handle_p, interface_number)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
178 if r != 0: raise UsbError('Error claiming interface', r)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents: 112
diff changeset
179
112
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
180
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
181 class UsbTransfer:
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
182 def __init__(self):
056face59ee7 Added lsusb script
Windel Bouwman
parents:
diff changeset
183 libusb_alloc_transfer(0)