changeset 124:d38729d35c4d stm32f4discovery flash with python

New implementation of hexfile viewer
author Windel Bouwman
date Sat, 12 Jan 2013 15:43:10 +0100
parents 9f1094b1587a
children 1ee5f36b34ed
files python/hexviewer.py python/stlink.py
diffstat 2 files changed, 72 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/python/hexviewer.py	Sat Jan 12 12:52:18 2013 +0100
+++ b/python/hexviewer.py	Sat Jan 12 15:43:10 2013 +0100
@@ -60,11 +60,62 @@
       self.hexfile = hf
       self.update()
 
+class HexFileModel(QAbstractTableModel):
+   def __init__(self):
+      super().__init__()
+      self.hexFile = None
+   def setHexFile(self, hf):
+      self.hexFile = hf
+      self.modelReset.emit()
+   def getHexFile(self):
+      return self.hexFile
+   HexFile = property(getHexFile, setHexFile)
+   def rowCount(self, parent):
+      if self.hexFile:
+         region = self.hexFile.regions[-1]
+         r = len(region.data)
+         s = r >> 4
+         if r % 16 != 0:
+            s += 1
+         return s
+      return 0
+   def columnCount(self, parent):
+      return 16 + 1
+   def headerData(self, section, orientation, role):
+      if role == Qt.DisplayRole:
+         if orientation == Qt.Horizontal:
+            if section in range(16):
+               return '{0:X}'.format(section)
+            elif section == 16:
+               return 'Ascii'
+         elif orientation == Qt.Vertical:
+            region = self.hexFile.regions[-1]
+            addr = region.address + 16 * section
+            return '0x{0:X}'.format(addr)
+   def data(self, index, role):
+      if index.isValid():
+         row = index.row()
+         col = index.column()
+         region = self.hexFile.regions[-1]
+         chunk = region.data[row * 16: row * 16 + 16]
+
+         if role == Qt.DisplayRole:
+            if col in range(16):
+               return '{0:02X}'.format(chunk[col])
+            else:
+               s = chunk.decode(encoding='ascii', errors='replace')
+               return s
+
 class BinViewMain(QMainWindow):
    def __init__(self):
       super().__init__()
       self.bv = BinViewer()
-      self.setCentralWidget(self.bv)
+      #self.setCentralWidget(self.bv)
+      tableView = QTableView()
+      self.setCentralWidget(tableView)
+      self.hfm = HexFileModel()
+      self.hfm.modelReset.connect(tableView.resizeColumnsToContents)
+      tableView.setModel(self.hfm)
       mb = self.menuBar()
       fileMenu = mb.addMenu("File")
       
@@ -86,9 +137,11 @@
    app = QApplication(sys.argv)
    bv = BinViewMain()
    bv.show()
-   bv.bv.setHexFile(hexfile.HexFile('audio.hex'))
+   hf = hexfile.HexFile('audio.hex')
+   #bv.bv.setHexFile(
+   bv.hfm.HexFile = hf
    qpv = QtPropertyViewer()
    qpv.propertyModel.InspectedWidget = bv
-   qpv.show()
+   #qpv.show()
    app.exec_()
 
--- a/python/stlink.py	Sat Jan 12 12:52:18 2013 +0100
+++ b/python/stlink.py	Sat Jan 12 15:43:10 2013 +0100
@@ -29,6 +29,7 @@
 DEBUG_ENTER_SWD = 0xa3
 DEBUG_GETSTATUS = 0x01
 DEBUG_RESETSYS = 0x03
+DEBUG_READALLREGS = 0x04
 DEBUG_READREG = 0x5
 DEBUG_WRITEREG = 0x6
 DEBUG_READMEM_32BIT = 0x7
@@ -104,6 +105,7 @@
          if len(stlink2s) > 1:
             print('More then one stlink2 found, picking first one')
          stlink2 = stlink2s[0]
+      assert checkDevice(stlink2)
       self.stlink2 = stlink2
    def open(self):
       self.devHandle = self.stlink2.open()
@@ -395,6 +397,12 @@
       cmd[0:3] = DEBUG_COMMAND, DEBUG_READREG, reg
       reply = self.send_recv(cmd, 4)
       return struct.unpack('<I', reply)[0]
+   def read_all_regs(self):
+      cmd = bytearray(16)
+      cmd[0:2] = DEBUG_COMMAND, DEBUG_READALLREGS
+      reply = self.send_recv(cmd, 84)
+      fmt = '<' + 'I' * 21 # unpack 21 register values
+      return list(struct.unpack(fmt, reply))
    def write_mem32(self, address, content):
       assert len(content) % 4 == 0
       cmd = bytearray(16)
@@ -443,12 +451,18 @@
    print('status: {0}'.format(sl.StatusString))
 
    # test registers:
+   sl.write_reg(0, 0xdeadbeef)
+   sl.write_reg(1, 0xcafebabe)
+   sl.write_reg(2, 0xc0ffee)
    sl.write_reg(3, 0x1337)
-   sl.write_reg(2, 0x1332)
+   sl.write_reg(5, 0x1332)
    sl.write_reg(6, 0x12345)
    assert sl.read_reg(3) == 0x1337
-   assert sl.read_reg(2) == 0x1332
+   assert sl.read_reg(5) == 0x1332
    assert sl.read_reg(6) == 0x12345
+   regs = sl.read_all_regs()
+   for i in range(len(regs)):
+      print('R{0}=0x{1:X}'.format(i, regs[i]))
 
    sl.exitDebugMode()
    print('mode at end:', sl.CurrentModeString)