annotate python/stlink.py @ 113:1f40be088ee8

Added first start stlink
author Windel Bouwman
date Sat, 05 Jan 2013 00:06:27 +0100
parents
children f42268da614f
rev   line source
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
1 from usb import UsbContext
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
2
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
3 class STLinkException(Exception):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
4 pass
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
5
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
6 def checkDevice(device):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
7 ST_VID=0x0483
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
8 STLINK2_PID=0x3748
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
9 return device.VendorId == ST_VID and device.ProductId == STLINK2_PID
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
10
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
11 XFER_TO_DEV=0
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
12 XFER_FROM_DEV=0x80
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
13
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
14 class STLink(object):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
15 DFU_MODE = 222
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
16 def __init__(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
17 self.context = UsbContext()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
18 def open(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
19 context = UsbContext()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
20 stlink2s = list(filter(checkDevice, context.DeviceList))
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
21 if not stlink2s:
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
22 raise STLinkException('Could not find an ST link')
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
23 if len(stlink2s) > 1:
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
24 print('More then one stlink2 found, picking first one')
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
25 stlink2 = stlink2s[0]
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
26 dev = stlink2.open()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
27 if dev.Configuration != 1:
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
28 dev.Configuration = 1
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
29 dev.claimInterface(0)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
30 def getCurrentMode(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
31 print('get cur mode')
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
32 rep_len = 2
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
33 self.fillCommand(self, XFER_FROM_DEV, rep_len)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
34 size = self.send_recv(1, cmd, data)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
35 return self.q_buf[0]
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
36 CurrentMode = property(getCurrentMode)
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
37 def fillCommand(self, di, rl):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
38 bytes(b'USBC')
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
39 pass
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
40 def reset(self):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
41 pass
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
42 def send_recv(self, txbuf, rxbuf):
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
43 pass
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
44