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