127
|
1 #!/usr/bin/python
|
|
2
|
|
3 import sys
|
|
4 from PyQt4.QtCore import *
|
|
5 from PyQt4.QtGui import *
|
129
|
6 import stlink, devices, stm32
|
|
7 from devices import Interface, Device
|
127
|
8
|
|
9 class InformationDialog(QDialog):
|
|
10 def __init__(self, parent):
|
|
11 super().__init__(parent)
|
|
12 self.setWindowTitle('Info')
|
|
13 fl = QFormLayout(self)
|
|
14 if parent.stl:
|
|
15 fl.addRow('ST link version:', QLabel(parent.stl.Version))
|
|
16 fl.addRow('Chip id:', QLabel('0x{0:X}'.format(parent.stl.ChipId)))
|
|
17 fl.addRow('Current mode:', QLabel(parent.stl.CurrentModeString))
|
|
18 fl.addRow('Status:', QLabel(parent.stl.StatusString))
|
|
19
|
131
|
20 class RegisterModel(QAbstractTableModel):
|
|
21 def __init__(self):
|
|
22 super().__init__()
|
|
23 self.regCount = 15
|
|
24 self.device = None
|
|
25 def rowCount(self, parent):
|
|
26 if parent.isValid():
|
|
27 return 0
|
|
28 if self.device:
|
|
29 return 21 # TODO make variable
|
|
30 else:
|
|
31 return 0
|
|
32 def setDevice(self, dev):
|
|
33 self.device = dev
|
|
34 self.modelReset.emit()
|
|
35 def columnCount(self, parent):
|
|
36 if parent.isValid():
|
|
37 return 0
|
|
38 return 2
|
|
39 def data(self, index, role):
|
|
40 if index.isValid():
|
|
41 row, col = index.row(), index.column()
|
|
42 if role == Qt.DisplayRole:
|
|
43 if col == 0:
|
|
44 return 'R{0}'.format(row)
|
|
45 elif col == 1:
|
|
46 v = self.device.iface.read_reg(row)
|
|
47 return '0x{0:X}'.format(v)
|
|
48
|
|
49 class RegisterView(QTableView):
|
|
50 def __init__(self):
|
|
51 super().__init__()
|
|
52 self.mdl = RegisterModel()
|
|
53 self.setModel(self.mdl)
|
|
54
|
132
|
55 class MemoryViewer(QWidget):
|
|
56 pass
|
|
57
|
|
58 class FlashTool(QWidget):
|
|
59 def __init__(self):
|
|
60 super().__init__()
|
|
61
|
129
|
62 class DeviceTreeModel(QAbstractItemModel):
|
|
63 def __init__(self):
|
|
64 super().__init__()
|
|
65 self.chipPixmap = QPixmap('chip.png').scaled(32, 32)
|
130
|
66 self.hardwarePixmap = QPixmap('hardware.png').scaled(32, 32)
|
129
|
67 self.refresh()
|
|
68 def refresh(self):
|
|
69 """ Check all usb interfaces for interfaces """
|
|
70 self.interfaces = devices.createInterfaces()
|
|
71 self.devices = []
|
|
72 self.modelReset.emit()
|
|
73 def addDevice(self, device):
|
|
74 if device.iface in [lambda d: d.iface for d in self.devices]:
|
|
75 print('device already open')
|
|
76 return
|
|
77 self.devices.append(device)
|
|
78 self.modelReset.emit()
|
|
79 def index(self, row, column, parent):
|
|
80 if parent.isValid():
|
|
81 ip = parent.internalPointer()
|
|
82 if isinstance(ip, Interface):
|
|
83 devs = [d for d in self.devices if d.iface is ip]
|
|
84 return self.createIndex(row, column, devs[row])
|
|
85 else:
|
|
86 iface = self.interfaces[row]
|
|
87 return self.createIndex(row, column, iface)
|
|
88 return idx
|
|
89 def parent(self, index):
|
|
90 if index.isValid():
|
|
91 ip = index.internalPointer()
|
|
92 if isinstance(ip, Interface):
|
|
93 return QModelIndex()
|
|
94 elif isinstance(ip, Device):
|
|
95 iface = ip.iface
|
|
96 row = self.interfaces.index(iface)
|
|
97 return self.createIndex(row, 0, iface)
|
|
98 return QModelIndex()
|
|
99 def rowCount(self, parent):
|
|
100 if parent.isValid():
|
|
101 # non-root level:
|
|
102 ip = parent.internalPointer()
|
|
103 if isinstance(ip, Interface):
|
|
104 devs = [d for d in self.devices if d.iface is ip]
|
|
105 return len(devs)
|
|
106 else:
|
|
107 # root level:
|
|
108 return len(self.interfaces)
|
|
109 return 0
|
|
110 def columnCount(self, parent):
|
|
111 return 1
|
|
112 def data(self, index, role):
|
|
113 if index.isValid():
|
|
114 ip = index.internalPointer()
|
|
115 if role == Qt.DisplayRole:
|
|
116 return str(ip)
|
|
117 elif role == Qt.DecorationRole:
|
|
118 if isinstance(ip, Interface):
|
130
|
119 return self.hardwarePixmap
|
129
|
120 if isinstance(ip, Device):
|
|
121 return self.chipPixmap
|
|
122
|
130
|
123 class DeviceExplorer(QTreeView):
|
|
124 """ Lists all interfaces plugged in and allows selection """
|
|
125 deviceSelected = pyqtSignal(Device)
|
|
126 def __init__(self):
|
|
127 super().__init__()
|
|
128 self.mdl = DeviceTreeModel()
|
|
129 self.setModel(self.mdl)
|
|
130 self.activated.connect(self.openItem)
|
|
131 self.header().close()
|
|
132 self.customContextMenuRequested.connect(self.openMenu)
|
|
133 self.setContextMenuPolicy(Qt.CustomContextMenu)
|
|
134 def openItem(self, idx):
|
|
135 if idx.isValid():
|
|
136 ip = idx.internalPointer()
|
|
137 if isinstance(ip, Interface):
|
|
138 if not ip.IsOpen:
|
|
139 ip.open()
|
|
140 # Try to get a device:
|
|
141 self.mdl.addDevice(ip.createDevice())
|
|
142 if isinstance(ip, Device):
|
|
143 self.deviceSelected.emit(ip)
|
|
144 def openMenu(self, pt):
|
|
145 idx = self.indexAt(pt)
|
|
146 menu = QMenu()
|
|
147 menu.addAction('Refresh', self.mdl.refresh)
|
|
148 if idx.isValid():
|
|
149 ip = idx.internalPointer()
|
|
150 if isinstance(ip, Interface):
|
|
151 if ip.IsOpen:
|
|
152 def closeInterface():
|
|
153 self.mdl.closeInterface(ip)
|
|
154 menu.addAction('Close', closeInterface)
|
|
155 else:
|
|
156 def openInterface():
|
|
157 ip.open()
|
|
158 # Try to get a device:
|
|
159 self.mdl.addDevice(ip.createDevice())
|
|
160 menu.addAction('Open', openInterface)
|
|
161 elif isinstance(ip, Device):
|
|
162 def selectDevice():
|
|
163 self.deviceSelected.emit(ip)
|
|
164 menu.addAction('Select', selectDevice)
|
|
165 menu.exec(self.mapToGlobal(pt))
|
|
166
|
127
|
167 class StUtil(QMainWindow):
|
|
168 connected = pyqtSignal(bool)
|
|
169 def __init__(self):
|
|
170 super().__init__()
|
|
171 self.stl = None
|
|
172 def buildAction(name, callback, shortcut=None):
|
|
173 a = QAction(name, self)
|
|
174 a.triggered.connect(callback)
|
|
175 if shortcut:
|
|
176 a.setShortcut(shortcut)
|
|
177 return a
|
|
178 mb = self.menuBar()
|
|
179 fileMenu = mb.addMenu("File")
|
|
180 self.connectAction = buildAction('Connect', self.connect)
|
|
181 fileMenu.addAction(self.connectAction)
|
|
182 self.disconnectAction = buildAction('Disconnect', self.disconnect)
|
|
183 fileMenu.addAction(self.disconnectAction)
|
|
184
|
|
185 self.miscMenu = mb.addMenu("Misc")
|
|
186 infoAction = buildAction('Info', self.info)
|
|
187 self.miscMenu.addAction(infoAction)
|
|
188
|
|
189 sb = self.statusBar()
|
|
190
|
130
|
191 devexplr = DeviceExplorer()
|
|
192 self.setCentralWidget(devexplr)
|
|
193 devexplr.deviceSelected.connect(self.handleDeviceSelected)
|
129
|
194
|
127
|
195 self.connected.connect(self.handleConnectedChange)
|
|
196 self.connected.emit(False)
|
130
|
197 def handleDeviceSelected(self, dev):
|
|
198 self.dev = dev
|
|
199 self.handleConnectedChange(True)
|
127
|
200 def handleConnectedChange(self, state):
|
|
201 self.miscMenu.setEnabled(state)
|
|
202 self.connectAction.setEnabled(not state)
|
|
203 self.disconnectAction.setEnabled(state)
|
|
204 msg = 'Connected!' if state else 'Disconnected!'
|
|
205 self.statusBar().showMessage(msg)
|
|
206 def info(self):
|
|
207 infoDialog = InformationDialog(self)
|
|
208 infoDialog.exec()
|
|
209 def connect(self):
|
|
210 try:
|
|
211 self.stl = stlink.STLink()
|
|
212 self.stl.open()
|
|
213 except (stlink.STLinkException, usb.UsbError) as e:
|
|
214 QMessageBox.warning(self, "Error", str(e))
|
|
215 self.stl = None
|
|
216 if self.stl:
|
|
217 self.connected.emit(True)
|
|
218 def disconnect(self):
|
|
219 if self.stl:
|
|
220 self.stl.close()
|
|
221 self.connected.emit(False)
|
|
222 self.stl = None
|
|
223
|
|
224 if __name__ == '__main__':
|
|
225 app = QApplication(sys.argv)
|
|
226 stu = StUtil()
|
|
227 stu.show()
|
|
228 app.exec()
|
|
229
|