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