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