annotate python/devices.py @ 223:85c8105318e7

Fixup of parser
author Windel Bouwman
date Tue, 09 Jul 2013 17:42:52 +0200
parents 9e350a7dde98
children
rev   line source
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
1 import sys
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
2 import usb
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
3
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
4 # Global device list to which devices are registered.
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
5 devices = {}
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
6
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
7 def registerDevice(chipId):
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
8 """ Decorator to register a device """
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
9 def wrapper(dev):
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
10 devices[chipId] = dev
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
11 return dev
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
12 return wrapper
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
13
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
14 # Global interface dictionary.
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
15 interfaces = {}
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
16
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
17 def registerInterface(vid_pid):
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
18 def wrapper(iface):
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
19 interfaces[vid_pid] = iface
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
20 return iface
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
21 return wrapper
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
22
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
23 def createInterfaces():
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
24 """ Create a list of detected interfaces """
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
25 ctx = usb.UsbContext()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
26
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
27 # Retrieve all usb devices:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
28 devs = ctx.DeviceList
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
29 keys = interfaces.keys()
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
30
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
31 # Filter function to filter only registered interfaces:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
32 def filt(usbiface):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
33 return (usbiface.VendorId, usbiface.ProductId) in keys
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
34 def buildInterface(usbiface):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
35 key = (usbiface.VendorId, usbiface.ProductId)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
36 iface = interfaces[key]
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
37 return iface(usbiface)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
38 return [buildInterface(uif) for uif in filter(filt, devs)]
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
39
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
40 class Device:
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
41 """
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
42 Base class for a device possibly connected via an interface.
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
43 """
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
44 def __init__(self, iface):
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
45 # Store the interface through which this device is connected:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
46 assert isinstance(iface, Interface)
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
47 self.iface = iface
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
48
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
49 class Interface:
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
50 """
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
51 Generic interface class. Connected via Usb to a JTAG interface.
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
52 Possibly is connected with a certain chip.
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
53 """
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
54 def createDevice(self):
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
55 """ Try to get the device connected to this interface """
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
56 if self.ChipId in devices:
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
57 return devices[self.ChipId](self)
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
58 raise STLinkException('No device found!')
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
59
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
60 class STLinkException(Exception):
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
61 """ Exception used for interfaces and devices """
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
62 pass
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents:
diff changeset
63