Mercurial > lcfOS
comparison python/stlink.py @ 129:9e350a7dde98
Changed architecture and updated the util
author | Windel Bouwman |
---|---|
date | Fri, 18 Jan 2013 12:52:11 +0100 |
parents | 51cc127648e4 |
children | 654093a9a1e3 |
comparison
equal
deleted
inserted
replaced
128:51cc127648e4 | 129:9e350a7dde98 |
---|---|
1 import struct, time | 1 import struct, time |
2 from usb import UsbContext | 2 from usb import UsbContext, UsbDevice |
3 from devices import Interface, STLinkException, registerInterface | 3 from devices import Interface, STLinkException, registerInterface |
4 | 4 |
5 ST_VID, STLINK2_PID = 0x0483, 0x3748 | 5 ST_VID, STLINK2_PID = 0x0483, 0x3748 |
6 | 6 |
7 def checkDevice(device): | 7 def checkDevice(device): |
43 | 43 |
44 @registerInterface((ST_VID, STLINK2_PID)) | 44 @registerInterface((ST_VID, STLINK2_PID)) |
45 class STLink2(Interface): | 45 class STLink2(Interface): |
46 """ STlink2 interface implementation. """ | 46 """ STlink2 interface implementation. """ |
47 def __init__(self, stlink2=None): | 47 def __init__(self, stlink2=None): |
48 self.devHandle = None | |
48 if not stlink2: | 49 if not stlink2: |
49 context = UsbContext() | 50 context = UsbContext() |
50 stlink2s = list(filter(checkDevice, context.DeviceList)) | 51 stlink2s = list(filter(checkDevice, context.DeviceList)) |
51 if not stlink2s: | 52 if not stlink2s: |
52 raise STLinkException('Could not find an ST link 2 interface') | 53 raise STLinkException('Could not find an ST link 2 interface') |
53 if len(stlink2s) > 1: | 54 if len(stlink2s) > 1: |
54 print('More then one stlink2 found, picking first one') | 55 print('More then one stlink2 found, picking first one') |
55 stlink2 = stlink2s[0] | 56 stlink2 = stlink2s[0] |
57 assert isinstance(stlink2, UsbDevice) # Nifty type checking | |
56 assert checkDevice(stlink2) | 58 assert checkDevice(stlink2) |
57 self.stlink2 = stlink2 | 59 self.stlink2 = stlink2 |
60 def __str__(self): | |
61 if self.devHandle: | |
62 return 'STlink2 device version {0}'.format(self.Version) | |
63 else: | |
64 return 'STlink2 device' | |
58 def open(self): | 65 def open(self): |
66 if self.devHandle: | |
67 return | |
59 self.devHandle = self.stlink2.open() | 68 self.devHandle = self.stlink2.open() |
60 if self.devHandle.Configuration != 1: | 69 if self.devHandle.Configuration != 1: |
61 self.devHandle.Configuration = 1 | 70 self.devHandle.Configuration = 1 |
62 self.devHandle.claimInterface(0) | 71 self.devHandle.claimInterface(0) |
63 | 72 |
202 #assert len(tx) == 16 | 211 #assert len(tx) == 16 |
203 self.devHandle.bulkWrite(2, tx) # write to endpoint 2 | 212 self.devHandle.bulkWrite(2, tx) # write to endpoint 2 |
204 if rxsize > 0: | 213 if rxsize > 0: |
205 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1 | 214 return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1 |
206 | 215 |
207 knownChipIds = {0x1: 'x'} | |
208 | |
209 if __name__ == '__main__': | 216 if __name__ == '__main__': |
210 # Test program | 217 # Test program |
211 sl = STLink2() | 218 sl = STLink2() |
212 sl.open() | 219 sl.open() |
213 print('version:', sl.Version) | 220 print('version:', sl.Version) |
216 sl.exitDfuMode() | 223 sl.exitDfuMode() |
217 sl.enterSwdMode() | 224 sl.enterSwdMode() |
218 print('mode after entering swd mode:', sl.CurrentModeString) | 225 print('mode after entering swd mode:', sl.CurrentModeString) |
219 | 226 |
220 i = sl.ChipId | 227 i = sl.ChipId |
221 if i in knownChipIds: | 228 print('chip id: 0x{0:X}'.format(i)) |
222 print('chip id: 0x{0:X} -> {1}'.format(i, knownChipIds[i])) | |
223 else: | |
224 print('chip id: 0x{0:X}'.format(i)) | |
225 print('cpu: {0}'.format(sl.CpuId)) | 229 print('cpu: {0}'.format(sl.CpuId)) |
226 | 230 |
227 print('status: {0}'.format(sl.StatusString)) | 231 print('status: {0}'.format(sl.StatusString)) |
228 | 232 |
229 # test registers: | 233 # test registers: |