127
|
1 #!/usr/bin/python
|
|
2
|
|
3 import sys
|
333
|
4 from qtwrapper import QtGui, QtCore, QtWidgets, pyqtSignal, get_icon, Qt
|
173
|
5 import stlink, devices, stm32, usb
|
129
|
6 from devices import Interface, Device
|
136
|
7 from hexedit import HexEdit
|
127
|
8
|
292
|
9
|
333
|
10 class InformationDialog(QtWidgets.QDialog):
|
127
|
11 def __init__(self, parent):
|
|
12 super().__init__(parent)
|
|
13 self.setWindowTitle('Info')
|
|
14 fl = QFormLayout(self)
|
|
15 if parent.stl:
|
|
16 fl.addRow('ST link version:', QLabel(parent.stl.Version))
|
|
17 fl.addRow('Chip id:', QLabel('0x{0:X}'.format(parent.stl.ChipId)))
|
|
18 fl.addRow('Current mode:', QLabel(parent.stl.CurrentModeString))
|
|
19 fl.addRow('Status:', QLabel(parent.stl.StatusString))
|
|
20
|
292
|
21
|
333
|
22 class RegisterModel(QtCore.QAbstractTableModel):
|
279
|
23 def __init__(self):
|
131
|
24 super().__init__()
|
|
25 self.regCount = 15
|
|
26 self.device = None
|
279
|
27
|
|
28 def rowCount(self, parent):
|
131
|
29 if parent.isValid():
|
|
30 return 0
|
|
31 if self.device:
|
|
32 return 21 # TODO make variable
|
|
33 else:
|
|
34 return 0
|
279
|
35
|
|
36 def setDevice(self, dev):
|
131
|
37 self.device = dev
|
|
38 self.modelReset.emit()
|
279
|
39
|
|
40 def columnCount(self, parent):
|
131
|
41 if parent.isValid():
|
|
42 return 0
|
|
43 return 2
|
279
|
44
|
|
45 def data(self, index, role):
|
131
|
46 if index.isValid():
|
|
47 row, col = index.row(), index.column()
|
|
48 if role == Qt.DisplayRole:
|
|
49 if col == 0:
|
279
|
50 if row == 15:
|
|
51 return 'PC'
|
|
52 elif row == 14:
|
|
53 return 'LR'
|
|
54 elif row == 13:
|
|
55 return 'SP'
|
|
56 else:
|
|
57 return 'R{0}'.format(row)
|
131
|
58 elif col == 1:
|
|
59 v = self.device.iface.read_reg(row)
|
|
60 return '0x{0:X}'.format(v)
|
279
|
61
|
|
62 def setData(self, index, value, role):
|
138
|
63 if index.isValid():
|
|
64 row = index.row()
|
|
65 col = index.column()
|
|
66 if role == Qt.EditRole and col == 1:
|
|
67 value = int(value, 16)
|
|
68 self.device.iface.write_reg(row, value)
|
|
69 return True
|
|
70 return False
|
279
|
71
|
|
72 def flags(self, index):
|
138
|
73 if index.isValid():
|
|
74 row = index.row()
|
|
75 col = index.column()
|
|
76 if col == 1:
|
|
77 return super().flags(index) | Qt.ItemIsEditable
|
|
78 return super().flags(index)
|
279
|
79
|
|
80 def refresh(self):
|
143
|
81 if self.device:
|
|
82 fromIndex = self.index(0, 1)
|
|
83 toIndex = self.index(21, 1)
|
|
84 self.dataChanged.emit(fromIndex, toIndex)
|
131
|
85
|
247
|
86
|
333
|
87 class RegisterView(QtWidgets.QTableView):
|
279
|
88 def __init__(self):
|
131
|
89 super().__init__()
|
|
90 self.mdl = RegisterModel()
|
|
91 self.setModel(self.mdl)
|
279
|
92
|
|
93 def refresh(self):
|
144
|
94 if self.mdl.device:
|
|
95 self.setEnabled(not self.mdl.device.Running)
|
143
|
96 self.mdl.refresh()
|
131
|
97
|
279
|
98
|
333
|
99 class MemoryView(QtWidgets.QWidget):
|
240
|
100 BlockSize = 0x100
|
|
101 def __init__(self):
|
136
|
102 super().__init__()
|
333
|
103 l = QtWidgets.QVBoxLayout(self)
|
|
104 l2 = QtWidgets.QHBoxLayout()
|
|
105 l2.addWidget(QtWidgets.QLabel('Address'))
|
|
106 self.addressLine = QtWidgets.QLineEdit()
|
136
|
107 self.addressLine.setInputMask('Hhhhhhhh')
|
|
108 l2.addWidget(self.addressLine)
|
333
|
109 upButton = QtWidgets.QPushButton('up')
|
143
|
110 l2.addWidget(upButton)
|
|
111 upButton.clicked.connect(self.doUp)
|
333
|
112 downButton = QtWidgets.QPushButton('down')
|
143
|
113 downButton.clicked.connect(self.doDown)
|
|
114 l2.addWidget(downButton)
|
136
|
115 l.addLayout(l2)
|
|
116 self.device = None
|
|
117 self.hexEdit = HexEdit()
|
143
|
118 self.Address = 0x8000000
|
136
|
119 l.addWidget(self.hexEdit)
|
240
|
120 self.addressLine.returnPressed.connect(self.refresh)
|
|
121
|
|
122 def refresh(self):
|
|
123 address = self.Address
|
|
124 if self.device:
|
|
125 data = self.device.iface.read_mem32(address, self.BlockSize)
|
|
126 else:
|
|
127 data = bytearray(self.BlockSize)
|
|
128 self.hexEdit.bv.Data = data
|
|
129 self.hexEdit.bv.Offset = address
|
|
130
|
|
131 def getAddress(self):
|
|
132 txt = self.addressLine.text()
|
|
133 return int(txt, 16)
|
|
134
|
|
135 def doUp(self):
|
|
136 self.Address -= self.BlockSize
|
|
137
|
|
138 def doDown(self):
|
|
139 self.Address += self.BlockSize
|
|
140
|
|
141 def setAddress(self, address):
|
|
142 self.addressLine.setText('{0:08X}'.format(address))
|
|
143 self.refresh()
|
|
144 Address = property(getAddress, setAddress)
|
|
145 def setDevice(self, dev):
|
136
|
146 self.device = dev
|
143
|
147 self.Address = 0x8000000
|
132
|
148
|
247
|
149
|
333
|
150 class DebugToolbar(QtWidgets.QToolBar):
|
|
151 statusChange = pyqtSignal()
|
|
152 codePosition = pyqtSignal(int)
|
|
153
|
|
154 def __init__(self):
|
137
|
155 super().__init__()
|
|
156 self.device = None
|
|
157 # generate actions:
|
|
158 def genAction(name, callback):
|
333
|
159 a = QtWidgets.QAction(name, self)
|
137
|
160 a.triggered.connect(callback)
|
|
161 self.addAction(a)
|
|
162 return a
|
|
163 self.stepAction = genAction('Step', self.doStep)
|
|
164 self.runAction = genAction('Run', self.doRun)
|
138
|
165 self.stopAction = genAction('Stop', self.doHalt)
|
137
|
166 self.resetAction = genAction('Reset', self.doReset)
|
144
|
167 self.enableTraceAction = genAction('Enable trace', self.doEnableTrace)
|
137
|
168 self.updateEnables()
|
333
|
169
|
|
170 def updateEnables(self):
|
137
|
171 if self.device:
|
|
172 self.resetAction.setEnabled(True)
|
144
|
173 self.enableTraceAction.setEnabled(True)
|
143
|
174 self.runAction.setEnabled(not self.device.Running)
|
|
175 self.stepAction.setEnabled(not self.device.Running)
|
|
176 self.stopAction.setEnabled(self.device.Running)
|
|
177 self.statusChange.emit()
|
247
|
178 if not self.device.Running:
|
|
179 PC = 15
|
|
180 v = self.device.iface.read_reg(PC)
|
|
181 self.codePosition.emit(v)
|
137
|
182 else:
|
|
183 self.resetAction.setEnabled(False)
|
144
|
184 self.enableTraceAction.setEnabled(False)
|
138
|
185 self.runAction.setEnabled(False)
|
137
|
186 self.stepAction.setEnabled(False)
|
138
|
187 self.stopAction.setEnabled(False)
|
333
|
188
|
|
189 def doStep(self):
|
137
|
190 self.device.iface.step()
|
138
|
191 self.updateEnables()
|
333
|
192
|
|
193 def doReset(self):
|
137
|
194 self.device.iface.reset()
|
138
|
195 self.updateEnables()
|
333
|
196
|
|
197 def doRun(self):
|
137
|
198 self.device.iface.run()
|
138
|
199 self.updateEnables()
|
333
|
200
|
|
201 def doHalt(self):
|
138
|
202 self.device.iface.halt()
|
|
203 self.updateEnables()
|
333
|
204
|
|
205 def doEnableTrace(self):
|
144
|
206 self.device.iface.traceEnable()
|
|
207 self.updateEnables()
|
333
|
208
|
|
209 def setDevice(self, dev):
|
137
|
210 self.device = dev
|
|
211 self.updateEnables()
|
|
212
|
247
|
213
|
333
|
214 class FlashTool(QtWidgets.QWidget):
|
132
|
215 def __init__(self):
|
|
216 super().__init__()
|
137
|
217 # TODO!
|
132
|
218
|
247
|
219
|
333
|
220 class DeviceTreeModel(QtCore.QAbstractItemModel):
|
129
|
221 def __init__(self):
|
|
222 super().__init__()
|
333
|
223 self.chipPixmap = get_icon('chip.png').scaled(32, 32)
|
|
224 self.hardwarePixmap = get_icon('hardware.png').scaled(32, 32)
|
129
|
225 self.refresh()
|
|
226 def refresh(self):
|
|
227 """ Check all usb interfaces for interfaces """
|
|
228 self.interfaces = devices.createInterfaces()
|
|
229 self.devices = []
|
|
230 self.modelReset.emit()
|
|
231 def addDevice(self, device):
|
|
232 if device.iface in [lambda d: d.iface for d in self.devices]:
|
|
233 print('device already open')
|
|
234 return
|
|
235 self.devices.append(device)
|
|
236 self.modelReset.emit()
|
|
237 def index(self, row, column, parent):
|
|
238 if parent.isValid():
|
|
239 ip = parent.internalPointer()
|
|
240 if isinstance(ip, Interface):
|
|
241 devs = [d for d in self.devices if d.iface is ip]
|
|
242 return self.createIndex(row, column, devs[row])
|
|
243 else:
|
|
244 iface = self.interfaces[row]
|
|
245 return self.createIndex(row, column, iface)
|
|
246 return idx
|
|
247 def parent(self, index):
|
|
248 if index.isValid():
|
|
249 ip = index.internalPointer()
|
|
250 if isinstance(ip, Interface):
|
|
251 return QModelIndex()
|
|
252 elif isinstance(ip, Device):
|
|
253 iface = ip.iface
|
|
254 row = self.interfaces.index(iface)
|
|
255 return self.createIndex(row, 0, iface)
|
|
256 return QModelIndex()
|
|
257 def rowCount(self, parent):
|
|
258 if parent.isValid():
|
|
259 # non-root level:
|
|
260 ip = parent.internalPointer()
|
|
261 if isinstance(ip, Interface):
|
|
262 devs = [d for d in self.devices if d.iface is ip]
|
|
263 return len(devs)
|
|
264 else:
|
|
265 # root level:
|
|
266 return len(self.interfaces)
|
|
267 return 0
|
|
268 def columnCount(self, parent):
|
|
269 return 1
|
|
270 def data(self, index, role):
|
|
271 if index.isValid():
|
|
272 ip = index.internalPointer()
|
|
273 if role == Qt.DisplayRole:
|
|
274 return str(ip)
|
|
275 elif role == Qt.DecorationRole:
|
|
276 if isinstance(ip, Interface):
|
130
|
277 return self.hardwarePixmap
|
129
|
278 if isinstance(ip, Device):
|
|
279 return self.chipPixmap
|
|
280
|
333
|
281 class DeviceExplorer(QtWidgets.QTreeView):
|
130
|
282 """ Lists all interfaces plugged in and allows selection """
|
|
283 deviceSelected = pyqtSignal(Device)
|
|
284 def __init__(self):
|
|
285 super().__init__()
|
|
286 self.mdl = DeviceTreeModel()
|
|
287 self.setModel(self.mdl)
|
|
288 self.activated.connect(self.openItem)
|
|
289 self.header().close()
|
|
290 self.customContextMenuRequested.connect(self.openMenu)
|
|
291 self.setContextMenuPolicy(Qt.CustomContextMenu)
|
|
292 def openItem(self, idx):
|
|
293 if idx.isValid():
|
|
294 ip = idx.internalPointer()
|
|
295 if isinstance(ip, Interface):
|
|
296 if not ip.IsOpen:
|
173
|
297 try:
|
|
298 ip.open()
|
|
299 except usb.UsbError as e:
|
|
300 QMessageBox.critical(self, "Error", 'Error opening interface: "{0}"'.format(e))
|
|
301 else:
|
|
302 # Try to get a device:
|
|
303 self.mdl.addDevice(ip.createDevice())
|
130
|
304 if isinstance(ip, Device):
|
|
305 self.deviceSelected.emit(ip)
|
|
306 def openMenu(self, pt):
|
|
307 idx = self.indexAt(pt)
|
|
308 menu = QMenu()
|
|
309 menu.addAction('Refresh', self.mdl.refresh)
|
|
310 if idx.isValid():
|
|
311 ip = idx.internalPointer()
|
|
312 if isinstance(ip, Interface):
|
|
313 if ip.IsOpen:
|
|
314 def closeInterface():
|
|
315 self.mdl.closeInterface(ip)
|
|
316 menu.addAction('Close', closeInterface)
|
|
317 else:
|
|
318 def openInterface():
|
|
319 ip.open()
|
|
320 # Try to get a device:
|
|
321 self.mdl.addDevice(ip.createDevice())
|
|
322 menu.addAction('Open', openInterface)
|
|
323 elif isinstance(ip, Device):
|
|
324 def selectDevice():
|
|
325 self.deviceSelected.emit(ip)
|
|
326 menu.addAction('Select', selectDevice)
|
|
327 menu.exec(self.mapToGlobal(pt))
|
|
328
|
292
|
329
|
333
|
330 class StUtil(QtWidgets.QMainWindow):
|
127
|
331 connected = pyqtSignal(bool)
|
|
332 def __init__(self):
|
|
333 super().__init__()
|
|
334 self.stl = None
|
|
335 def buildAction(name, callback, shortcut=None):
|
|
336 a = QAction(name, self)
|
|
337 a.triggered.connect(callback)
|
|
338 if shortcut:
|
|
339 a.setShortcut(shortcut)
|
|
340 return a
|
|
341 mb = self.menuBar()
|
|
342 fileMenu = mb.addMenu("File")
|
|
343 self.connectAction = buildAction('Connect', self.connect)
|
|
344 fileMenu.addAction(self.connectAction)
|
|
345 self.disconnectAction = buildAction('Disconnect', self.disconnect)
|
|
346 fileMenu.addAction(self.disconnectAction)
|
|
347
|
|
348 self.miscMenu = mb.addMenu("Misc")
|
|
349 infoAction = buildAction('Info', self.info)
|
|
350 self.miscMenu.addAction(infoAction)
|
|
351
|
|
352 sb = self.statusBar()
|
|
353
|
130
|
354 devexplr = DeviceExplorer()
|
|
355 self.setCentralWidget(devexplr)
|
|
356 devexplr.deviceSelected.connect(self.handleDeviceSelected)
|
129
|
357
|
127
|
358 self.connected.connect(self.handleConnectedChange)
|
|
359 self.connected.emit(False)
|
130
|
360 def handleDeviceSelected(self, dev):
|
|
361 self.dev = dev
|
|
362 self.handleConnectedChange(True)
|
127
|
363 def handleConnectedChange(self, state):
|
|
364 self.miscMenu.setEnabled(state)
|
|
365 self.connectAction.setEnabled(not state)
|
|
366 self.disconnectAction.setEnabled(state)
|
|
367 msg = 'Connected!' if state else 'Disconnected!'
|
|
368 self.statusBar().showMessage(msg)
|
|
369 def info(self):
|
|
370 infoDialog = InformationDialog(self)
|
|
371 infoDialog.exec()
|
|
372 def connect(self):
|
|
373 try:
|
|
374 self.stl = stlink.STLink()
|
|
375 self.stl.open()
|
|
376 except (stlink.STLinkException, usb.UsbError) as e:
|
|
377 QMessageBox.warning(self, "Error", str(e))
|
|
378 self.stl = None
|
|
379 if self.stl:
|
|
380 self.connected.emit(True)
|
|
381 def disconnect(self):
|
|
382 if self.stl:
|
|
383 self.stl.close()
|
|
384 self.connected.emit(False)
|
|
385 self.stl = None
|
|
386
|
|
387 if __name__ == '__main__':
|
|
388 app = QApplication(sys.argv)
|
|
389 stu = StUtil()
|
|
390 stu.show()
|
|
391 app.exec()
|
|
392
|