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