view python/st-util.py @ 138:14e739ed03ab

Added halt stub
author Windel Bouwman
date Sat, 26 Jan 2013 10:04:32 +0100
parents 0a540ce31cd5
children 1cc59ac80950
line wrap: on
line source

#!/usr/bin/python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import stlink, devices, stm32
from devices import Interface, Device
from hexedit import HexEdit

class InformationDialog(QDialog):
   def __init__(self, parent):
      super().__init__(parent)
      self.setWindowTitle('Info')
      fl = QFormLayout(self)
      if parent.stl:
         fl.addRow('ST link version:', QLabel(parent.stl.Version))
         fl.addRow('Chip id:', QLabel('0x{0:X}'.format(parent.stl.ChipId)))
         fl.addRow('Current mode:', QLabel(parent.stl.CurrentModeString))
         fl.addRow('Status:', QLabel(parent.stl.StatusString))

class RegisterModel(QAbstractTableModel):
   def __init__(self):
      super().__init__()
      self.regCount = 15
      self.device = None
   def rowCount(self, parent):
      if parent.isValid():
         return 0
      if self.device:
         return 21 # TODO make variable
      else:
         return 0
   def setDevice(self, dev):
      self.device = dev
      self.modelReset.emit()
   def columnCount(self, parent):
      if parent.isValid():
         return 0
      return 2
   def data(self, index, role):
      if index.isValid():
         row, col = index.row(), index.column()
         if role == Qt.DisplayRole:
            if col == 0:
               return 'R{0}'.format(row)
            elif col == 1:
               v = self.device.iface.read_reg(row)
               return '0x{0:X}'.format(v)
   def setData(self, index, value, role):
      if index.isValid():
         row = index.row()
         col = index.column()
         if role == Qt.EditRole and col == 1:
            value = int(value, 16)
            print(row, value, type(row), type(value))
            self.device.iface.write_reg(row, value)
            return True
      return False
   def flags(self, index):
      if index.isValid():
         row = index.row()
         col = index.column()
         if col == 1:
            return super().flags(index) | Qt.ItemIsEditable
      return super().flags(index)

class RegisterView(QTableView):
   def __init__(self):
      super().__init__()
      self.mdl = RegisterModel()
      self.setModel(self.mdl)

class MemoryView(QWidget):
   def __init__(self):
      super().__init__()
      l = QVBoxLayout(self)
      l2 = QHBoxLayout()
      l2.addWidget(QLabel('Address'))
      self.addressLine = QLineEdit()
      self.addressLine.setInputMask('Hhhhhhhh')
      self.addressLine.setText('08000000')
      l2.addWidget(self.addressLine)
      l.addLayout(l2)
      self.device = None
      self.hexEdit = HexEdit()
      self.loadAddress(0x8000000)
      l.addWidget(self.hexEdit)
      self.addressLine.returnPressed.connect(self.doLoad)
   def doLoad(self):
      txt = self.addressLine.text()
      self.loadAddress(int(txt, 16))
   def loadAddress(self, address):
      size = 512
      if self.device:
         data = self.device.iface.read_mem32(address, size)
      else:
         data = bytearray(size)
      self.hexEdit.bv.Data = data
      self.hexEdit.bv.Offset = address
   def setDevice(self, dev):
      self.device = dev
      self.loadAddress(0x8000000)

class DebugToolbar(QToolBar):
   def __init__(self):
      super().__init__()
      self.device = None
      # generate actions:
      def genAction(name, callback):
         a = QAction(name, self)
         a.triggered.connect(callback)
         self.addAction(a)
         return a
      self.stepAction = genAction('Step', self.doStep)
      self.runAction = genAction('Run', self.doRun)
      self.stopAction = genAction('Stop', self.doHalt)
      self.resetAction = genAction('Reset', self.doReset)
      self.updateEnables()
   def updateEnables(self):
      if self.device:
         self.resetAction.setEnabled(True)
         if self.device.Running:
            self.runAction.setEnabled(False)
            self.stepAction.setEnabled(False)
            self.stopAction.setEnabled(True)
         else:
            self.runAction.setEnabled(True)
            self.stepAction.setEnabled(True)
            self.stopAction.setEnabled(False)
      else:
         self.resetAction.setEnabled(False)
         self.runAction.setEnabled(False)
         self.stepAction.setEnabled(False)
         self.stopAction.setEnabled(False)
   def doStep(self):
      self.device.iface.step()
      self.updateEnables()
   def doReset(self):
      self.device.iface.reset()
      self.updateEnables()
   def doRun(self):
      self.device.iface.run()
      self.updateEnables()
   def doHalt(self):
      self.device.iface.halt()
      self.updateEnables()
   def setDevice(self, dev):
      self.device = dev
      self.updateEnables()

class FlashTool(QWidget):
   def __init__(self):
      super().__init__()
      # TODO!

class DeviceTreeModel(QAbstractItemModel):
   def __init__(self):
      super().__init__()
      self.chipPixmap = QPixmap('chip.png').scaled(32, 32)
      self.hardwarePixmap = QPixmap('hardware.png').scaled(32, 32)
      self.refresh()
   def refresh(self):
      """ Check all usb interfaces for interfaces """
      self.interfaces = devices.createInterfaces()
      self.devices = []
      self.modelReset.emit()
   def addDevice(self, device):
      if device.iface in [lambda d: d.iface for d in self.devices]:
         print('device already open')
         return
      self.devices.append(device)
      self.modelReset.emit()
   def index(self, row, column, parent):
      if parent.isValid():
         ip = parent.internalPointer()
         if isinstance(ip, Interface):
            devs = [d for d in self.devices if d.iface is ip]
            return self.createIndex(row, column, devs[row])
      else:
         iface = self.interfaces[row]
         return self.createIndex(row, column, iface)
      return idx
   def parent(self, index):
      if index.isValid():
         ip = index.internalPointer()
         if isinstance(ip, Interface):
            return QModelIndex()
         elif isinstance(ip, Device):
            iface = ip.iface
            row = self.interfaces.index(iface)
            return self.createIndex(row, 0, iface)
         return QModelIndex()
   def rowCount(self, parent):
      if parent.isValid():
         # non-root level:
         ip = parent.internalPointer()
         if isinstance(ip, Interface):
            devs = [d for d in self.devices if d.iface is ip]
            return len(devs)
      else:
         # root level:
         return len(self.interfaces)
      return 0
   def columnCount(self, parent):
      return 1
   def data(self, index, role):
      if index.isValid():
         ip = index.internalPointer()
         if role == Qt.DisplayRole:
            return str(ip)
         elif role == Qt.DecorationRole:
            if isinstance(ip, Interface):
               return self.hardwarePixmap
            if isinstance(ip, Device):
               return self.chipPixmap

class DeviceExplorer(QTreeView):
   """ Lists all interfaces plugged in and allows selection """
   deviceSelected = pyqtSignal(Device)
   def __init__(self):
      super().__init__()
      self.mdl = DeviceTreeModel()
      self.setModel(self.mdl)
      self.activated.connect(self.openItem)
      self.header().close()
      self.customContextMenuRequested.connect(self.openMenu)
      self.setContextMenuPolicy(Qt.CustomContextMenu)
   def openItem(self, idx):
      if idx.isValid():
         ip = idx.internalPointer()
         if isinstance(ip, Interface):
            if not ip.IsOpen:
               ip.open()
               # Try to get a device:
               self.mdl.addDevice(ip.createDevice())
         if isinstance(ip, Device):
            self.deviceSelected.emit(ip)
   def openMenu(self, pt):
      idx = self.indexAt(pt)
      menu = QMenu()
      menu.addAction('Refresh', self.mdl.refresh)
      if idx.isValid():
         ip = idx.internalPointer()
         if isinstance(ip, Interface):
            if ip.IsOpen:
               def closeInterface():
                  self.mdl.closeInterface(ip)
               menu.addAction('Close', closeInterface)
            else:
               def openInterface():
                  ip.open()
                  # Try to get a device:
                  self.mdl.addDevice(ip.createDevice())
               menu.addAction('Open', openInterface)
         elif isinstance(ip, Device):
            def selectDevice():
               self.deviceSelected.emit(ip)
            menu.addAction('Select', selectDevice)
      menu.exec(self.mapToGlobal(pt))

class StUtil(QMainWindow):
   connected = pyqtSignal(bool)
   def __init__(self):
      super().__init__()
      self.stl = None
      def buildAction(name, callback, shortcut=None):
         a = QAction(name, self)
         a.triggered.connect(callback)
         if shortcut:
            a.setShortcut(shortcut)
         return a
      mb = self.menuBar()
      fileMenu = mb.addMenu("File")
      self.connectAction = buildAction('Connect', self.connect)
      fileMenu.addAction(self.connectAction)
      self.disconnectAction = buildAction('Disconnect', self.disconnect)
      fileMenu.addAction(self.disconnectAction)

      self.miscMenu = mb.addMenu("Misc")
      infoAction = buildAction('Info', self.info)
      self.miscMenu.addAction(infoAction)

      sb = self.statusBar()

      devexplr = DeviceExplorer()
      self.setCentralWidget(devexplr)
      devexplr.deviceSelected.connect(self.handleDeviceSelected)

      self.connected.connect(self.handleConnectedChange)
      self.connected.emit(False)
   def handleDeviceSelected(self, dev):
      self.dev = dev
      self.handleConnectedChange(True)
   def handleConnectedChange(self, state):
      self.miscMenu.setEnabled(state)
      self.connectAction.setEnabled(not state)
      self.disconnectAction.setEnabled(state)
      msg = 'Connected!' if state else 'Disconnected!'
      self.statusBar().showMessage(msg)
   def info(self):
      infoDialog = InformationDialog(self)
      infoDialog.exec()
   def connect(self):
      try:
         self.stl = stlink.STLink()
         self.stl.open()
      except (stlink.STLinkException, usb.UsbError) as e:
         QMessageBox.warning(self, "Error", str(e))
         self.stl = None
      if self.stl:
         self.connected.emit(True)
   def disconnect(self):
      if self.stl:
         self.stl.close()
         self.connected.emit(False)
      self.stl = None

if __name__ == '__main__':
   app = QApplication(sys.argv)
   stu = StUtil()
   stu.show()
   app.exec()