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