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