-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathMCUProg.py
415 lines (313 loc) · 14.9 KB
/
MCUProg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#! python3
import os
import re
import sys
import collections
import configparser
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QThread
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QFileDialog
import jlink
import xlink
import device
import device.chip
os.environ['PATH'] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'libusb-1.0.24/MinGW64/dll') + os.pathsep + os.environ['PATH']
zero_if = lambda i: 0 if i == -1 else i
'''
from MCUProg_UI import Ui_MCUProg
class MCUProg(QWidget, Ui_MCUProg):
def __init__(self, parent=None):
super(MCUProg, self).__init__(parent)
self.setupUi(self)
'''
class MCUProg(QWidget):
def __init__(self, parent=None):
super(MCUProg, self).__init__(parent)
uic.loadUi('MCUProg.ui', self)
self.prgInfo.setVisible(False)
self.table.setVisible(False)
self.resize(self.width(), 160)
self.table.horizontalHeader().setStretchLastSection(True)
self.initSetting()
self.tmrDAP = QtCore.QTimer()
self.tmrDAP.setInterval(1000)
self.tmrDAP.timeout.connect(self.on_tmrDAP_timeout)
self.tmrDAP.start()
def initSetting(self):
if not os.path.exists('setting.ini'):
open('setting.ini', 'w', encoding='utf-8')
self.conf = configparser.ConfigParser()
self.conf.read('setting.ini', encoding='utf-8')
if not self.conf.has_section('globals'):
self.conf.add_section('globals')
self.conf.set('globals', 'mcu', 'NUM480')
self.conf.set('globals', 'addr', '0 K')
self.conf.set('globals', 'size', '16 K')
self.conf.set('globals', 'link', '')
self.conf.set('globals', 'dllpath', '')
self.conf.set('globals', 'hexpath', '[]')
self.conf.set('globals', 'savpath', '')
self.cmbMCU.addItems(self.parse_devices())
self.cmbMCU.setCurrentIndex(zero_if(self.cmbMCU.findText(self.conf.get('globals', 'mcu'))))
self.cmbAddr.setCurrentIndex(zero_if(self.cmbAddr.findText(self.conf.get('globals', 'addr'))))
self.cmbSize.setCurrentIndex(zero_if(self.cmbSize.findText(self.conf.get('globals', 'size'))))
self.cmbDLL.addItem(self.conf.get('globals', 'dllpath'))
self.on_tmrDAP_timeout() # add DAPLink
self.cmbDLL.setCurrentIndex(zero_if(self.cmbDLL.findText(self.conf.get('globals', 'link'))))
self.cmbHEX.addItems(eval(self.conf.get('globals', 'hexpath')))
self.savPath = self.conf.get('globals', 'savpath')
def parse_devices(self):
cwd = os.getcwd()
try:
for line in open(os.path.join(cwd, 'devices.txt')):
match = re.match(r'(\w+)\s+(.+)\n', line)
if match:
name = match.group(1)
addr = 0x20000000
size = 0x1000
path = os.path.join(cwd, match.group(2).strip())
device.Devices[name] = (name, addr, size, path)
match = re.match(r'(\w+)\s+(0x[0-9a-fA-F]+)\s+(0x[0-9a-fA-F]+)\s+(.+)\n', line)
if match:
name = match.group(1)
addr = int(match.group(2), 16)
size = int(match.group(3), 16)
path = os.path.join(cwd, match.group(4).strip())
device.Devices[name] = (name, addr, size, path)
except Exception as e:
print(e)
return device.Devices.keys()
def on_tmrDAP_timeout(self):
if not self.isEnabled(): # link working
return
try:
from pyocd.probe import aggregator
self.daplinks = aggregator.DebugProbeAggregator.get_all_connected_probes()
if len(self.daplinks) != self.cmbDLL.count() - 1:
for i in range(1, self.cmbDLL.count()):
self.cmbDLL.removeItem(1)
for i, daplink in enumerate(self.daplinks):
self.cmbDLL.addItem(f'{daplink.product_name} ({daplink.unique_id})')
except Exception as e:
pass
def device(self, name, xlink):
dev = device.Devices[name]
if isinstance(dev, tuple):
return device.chip.Chip(xlink, dev)
else:
return dev(xlink)
def connect(self):
try:
if self.cmbDLL.currentIndex() == 0:
self.xlk = xlink.XLink(jlink.JLink(self.cmbDLL.currentText(), self.device(self.cmbMCU.currentText(), None).CHIP_CORE))
else:
from pyocd.coresight import dap, ap, cortex_m
daplink = self.daplinks[self.cmbDLL.currentIndex() - 1]
daplink.open()
_dp = dap.DebugPort(daplink, None)
_dp.init()
_dp.power_up_debug()
_ap = ap.AHB_AP(_dp, 0)
_ap.init()
self.xlk = xlink.XLink(cortex_m.CortexM(None, _ap))
self.dev = self.device(self.cmbMCU.currentText(), self.xlk)
except Exception as e:
QMessageBox.critical(self, '连接失败', str(e), QMessageBox.Yes)
return False
return True
@pyqtSlot()
def on_btnChipErase_clicked(self):
if self.connect():
self.setEnabled(False)
self.prgInfo.setVisible(True)
self.threadErase = ThreadAsync(self.dev.chip_erase)
self.threadErase.taskFinished.connect(self.on_btnErase_finished)
self.threadErase.start()
@pyqtSlot()
def on_btnErase_clicked(self):
if self.connect():
self.setEnabled(False)
self.prgInfo.setVisible(True)
self.threadErase = ThreadAsync(self.dev.sect_erase, self.addr, self.size)
self.threadErase.taskFinished.connect(self.on_btnErase_finished)
self.threadErase.start()
def on_btnErase_finished(self):
QMessageBox.information(self, '擦除完成', ' 芯片擦除完成 ', QMessageBox.Yes)
self.xlk.reset()
self.xlk.close()
self.setEnabled(True)
self.prgInfo.setVisible(False)
@pyqtSlot()
def on_btnWrite_clicked(self):
if self.connect():
self.setEnabled(False)
self.prgInfo.setVisible(True)
fpath = self.cmbHEX.currentText()
if not os.path.exists(fpath):
QMessageBox.warning(self, '文件不存在', fpath, QMessageBox.Yes)
return
if fpath.endswith('.ini'):
self.wrdata = []
for i in range(self.table.rowCount()):
if self.table.item(i, 0).checkState():
fpath = self.table.item(i, 2).text()
if not os.path.exists(fpath):
QMessageBox.warning(self, '文件不存在', fpath, QMessageBox.Yes)
return
addr = int(self.table.item(i, 1).text(), 16) - self.dev.CHIP_BASE
if fpath.endswith('.hex'):
self.wrdata.append((addr, parseHex(fpath)))
else:
self.wrdata.append((addr, open(fpath, 'rb').read()))
elif fpath.endswith('.hex'):
self.wrdata = [(self.addr, parseHex(fpath))]
else:
self.wrdata = [(self.addr, open(fpath, 'rb').read())]
self.threadWrite = ThreadAsync(self.dev.chip_write, *self.wrdata.pop())
self.threadWrite.taskFinished.connect(self.on_btnWrite_finished)
self.threadWrite.start()
def on_btnWrite_finished(self):
if len(self.wrdata):
self.threadWrite.args = self.wrdata.pop()
self.threadWrite.start()
return
QMessageBox.information(self, '烧写完成', ' 程序烧写完成 ', QMessageBox.Yes)
self.xlk.reset()
self.xlk.close()
self.setEnabled(True)
self.prgInfo.setVisible(False)
@pyqtSlot()
def on_btnRead_clicked(self):
if self.connect():
self.setEnabled(False)
self.prgInfo.setVisible(True)
self.rdbuff = [] # bytes 无法 extend,因此用 list
self.threadRead = ThreadAsync(self.dev.chip_read, self.addr, self.size, self.rdbuff)
self.threadRead.taskFinished.connect(self.on_btnRead_finished)
self.threadRead.start()
def on_btnRead_finished(self):
binpath, filter = QFileDialog.getSaveFileName(caption='将读取到的数据保存到文件', filter='程序文件 (*.bin)', directory=self.savPath)
if binpath:
self.savPath = binpath
with open(binpath, 'wb') as f:
f.write(bytes(self.rdbuff))
self.xlk.reset()
self.xlk.close()
self.setEnabled(True)
self.prgInfo.setVisible(False)
@property
def addr(self):
return int(self.cmbAddr.currentText().split()[0]) * 1024
@property
def size(self):
return int(self.cmbSize.currentText().split()[0]) * 1024
@pyqtSlot(int)
def on_cmbMCU_currentIndexChanged(self, index):
dev = self.device(self.cmbMCU.currentText(), None)
addr = self.cmbAddr.currentText()
self.cmbAddr.clear()
for i in range(dev.SECT_SKIP // dev.SECT_SIZE, dev.CHIP_SIZE // dev.SECT_SIZE):
if (dev.SECT_SIZE * i) % 1024 == 0:
self.cmbAddr.addItem('%d K' %(dev.SECT_SIZE * i // 1024))
self.cmbAddr.setCurrentIndex(zero_if(self.cmbAddr.findText(addr)))
if dev.falgo['pc_EraseChip'] > 0xFFFFFFFF:
self.btnChipErase.setEnabled(False)
else:
self.btnChipErase.setEnabled(True)
@pyqtSlot(int)
def on_cmbAddr_currentIndexChanged(self, index):
if self.cmbAddr.currentText() == '': return
dev = self.device(self.cmbMCU.currentText(), None)
size = self.cmbSize.currentText()
self.cmbSize.clear()
for i in range((dev.CHIP_SIZE - self.addr) // dev.SECT_SIZE):
if (dev.SECT_SIZE * (i+1)) % 1024 == 0:
self.cmbSize.addItem('%d K' %(dev.SECT_SIZE * (i+1) // 1024))
self.cmbSize.setCurrentIndex(zero_if(self.cmbSize.findText(size)))
@pyqtSlot()
def on_btnDLL_clicked(self):
dllpath, filter = QFileDialog.getOpenFileName(caption='JLink_x64.dll 路径', filter='动态链接库 (*.dll *.so)', directory=self.cmbDLL.itemText(0))
if dllpath:
self.cmbDLL.setItemText(0, dllpath)
@pyqtSlot()
def on_btnHEX_clicked(self):
hexpath, filter = QFileDialog.getOpenFileName(caption='程序文件路径', filter='程序文件 (*.bin *.hex *.ini);;任意文件 (*.*)', directory=self.cmbHEX.currentText())
if hexpath:
self.cmbHEX.insertItem(0, hexpath)
self.cmbHEX.setCurrentIndex(0)
@pyqtSlot(str)
def on_cmbHEX_currentIndexChanged(self, text):
if text.endswith('.ini'):
self.table.setVisible(True)
self.resize(self.width(), 270)
conf = configparser.ConfigParser()
conf.read(text, encoding='utf-8')
self.table.setRowCount(len(conf.sections()))
for i,section in enumerate(conf.sections()):
checkbox = QtWidgets.QTableWidgetItem(section)
checkbox.setCheckState(QtCore.Qt.Checked)
self.table.setItem(i, 0, checkbox)
self.table.setItem(i, 1, QtWidgets.QTableWidgetItem(conf.get(section, 'addr')))
self.table.setItem(i, 2, QtWidgets.QTableWidgetItem(conf.get(section, 'path')))
else:
self.table.setVisible(False)
self.resize(self.width(), 160)
@pyqtSlot(int, int)
def on_table_cellDoubleClicked(self, row, column):
if column != 2: # 只能设置文件路径
return
hexpath, filter = QFileDialog.getOpenFileName(caption='程序文件路径', filter='程序文件 (*.bin *.hex);;任意文件 (*.*)', directory=self.table.item(row, column).text())
if hexpath:
self.table.setItem(row, column, QtWidgets.QTableWidgetItem(hexpath))
conf = configparser.ConfigParser()
conf.read(self.cmbHEX.currentText(), encoding='utf-8')
conf.set(self.table.item(row, 0).text(), 'path', hexpath)
conf.write(open(self.cmbHEX.currentText(), 'w', encoding='utf-8'))
def closeEvent(self, evt):
self.conf.set('globals', 'mcu', self.cmbMCU.currentText())
self.conf.set('globals', 'addr', self.cmbAddr.currentText())
self.conf.set('globals', 'size', self.cmbSize.currentText())
self.conf.set('globals', 'link', self.cmbDLL.currentText())
self.conf.set('globals', 'dllpath', self.cmbDLL.itemText(0))
self.conf.set('globals', 'savpath', self.savPath)
hexpath = [self.cmbHEX.currentText()] + [self.cmbHEX.itemText(i) for i in range(self.cmbHEX.count())]
self.conf.set('globals', 'hexpath', repr(list(collections.OrderedDict.fromkeys(hexpath)))) # 保留顺序去重
self.conf.write(open('setting.ini', 'w', encoding='utf-8'))
class ThreadAsync(QThread):
taskFinished = pyqtSignal()
def __init__(self, func, *args):
super(ThreadAsync, self).__init__()
self.func = func
self.args = args
def run(self):
self.func(*self.args)
self.taskFinished.emit()
def parseHex(file):
''' 解析 .hex 文件,提取出程序代码,没有值的地方填充0xFF '''
data = ''
currentAddr = 0
extSegAddr = 0 # 扩展段地址
for line in open(file, 'rb').readlines():
line = line.strip()
if len(line) == 0: continue
len_ = int(line[1:3],16)
addr = int(line[3:7],16) + extSegAddr
type = int(line[7:9],16)
if type == 0x00:
if currentAddr != addr:
if currentAddr != 0:
data += '\xFF' * (addr - currentAddr)
currentAddr = addr
for i in range(len_):
data += chr(int(line[9+2*i:11+2*i], 16))
currentAddr += len_
elif type == 0x02:
extSegAddr = int(line[9:9+4], 16) * 16
elif type == 0x04:
extSegAddr = int(line[9:9+4], 16) * 65536
return data.encode('latin')
if __name__ == "__main__":
app = QApplication(sys.argv)
mcu = MCUProg()
mcu.show()
app.exec()