diff 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
line wrap: on
line diff
--- a/python/stlink.py	Sun Jan 13 17:31:35 2013 +0100
+++ b/python/stlink.py	Fri Jan 18 12:52:11 2013 +0100
@@ -1,5 +1,5 @@
 import struct, time
-from usb import UsbContext
+from usb import UsbContext, UsbDevice
 from devices import Interface, STLinkException, registerInterface
 
 ST_VID, STLINK2_PID = 0x0483, 0x3748
@@ -45,6 +45,7 @@
 class STLink2(Interface):
    """ STlink2 interface implementation. """
    def __init__(self, stlink2=None):
+      self.devHandle = None
       if not stlink2:
          context = UsbContext()
          stlink2s = list(filter(checkDevice, context.DeviceList))
@@ -53,9 +54,17 @@
          if len(stlink2s) > 1:
             print('More then one stlink2 found, picking first one')
          stlink2 = stlink2s[0]
+      assert isinstance(stlink2, UsbDevice) # Nifty type checking
       assert checkDevice(stlink2)
       self.stlink2 = stlink2
+   def __str__(self):
+      if self.devHandle:
+         return 'STlink2 device version {0}'.format(self.Version)
+      else:
+         return 'STlink2 device'
    def open(self):
+      if self.devHandle:
+         return
       self.devHandle = self.stlink2.open()
       if self.devHandle.Configuration != 1:
          self.devHandle.Configuration = 1
@@ -204,8 +213,6 @@
       if rxsize > 0:
          return self.devHandle.bulkRead(1, rxsize) # read from endpoint 1
 
-knownChipIds = {0x1: 'x'}
-
 if __name__ == '__main__':
    # Test program
    sl = STLink2()
@@ -218,10 +225,7 @@
    print('mode after entering swd mode:', sl.CurrentModeString)
 
    i = sl.ChipId
-   if i in knownChipIds:
-      print('chip id: 0x{0:X} -> {1}'.format(i, knownChipIds[i]))
-   else:
-      print('chip id: 0x{0:X}'.format(i))
+   print('chip id: 0x{0:X}'.format(i))
    print('cpu: {0}'.format(sl.CpuId))
 
    print('status: {0}'.format(sl.StatusString))