-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTBXX_Flash_Tool.py
385 lines (281 loc) · 12.5 KB
/
TBXX_Flash_Tool.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import sys
import io
import os
import struct
import time
import base64
from PyQt5.QtWidgets import QPushButton,QApplication,QLineEdit,QWidget,QTextEdit,QVBoxLayout,QHBoxLayout,QComboBox,QFileDialog,QProgressBar
from PyQt5.QtCore import Qt,QThread,pyqtSignal
from PyQt5.QtGui import QIcon
from Telink_Tools import get_port_list,tl_open_port,connect_chip,get_chip_info,telink_flash_write,telink_flash_erase
from aithinker_png import aithinker_png as logo
__version__ = "V2.1.1"
class TelinkThread(QThread):
pressbarSignal = pyqtSignal(int)
textSignal = pyqtSignal(str)
def __init__(self, _port_name, action, args):
super(TelinkThread, self).__init__()
self._port_name = _port_name
self.action = action
self.args = args
def run(self):
try:
_port = tl_open_port(self._port_name)
except Exception:
self.textSignal.emit("串口 " + self._port_name + " 打开失败,请检查串口是否被占用!!!")
self.pressbarSignal.emit(200)
return
self.pressbarSignal.emit(1000)
self.textSignal.emit("打开串口成功!!!")
if self.action == "reset":#复位模组,进入运行模式
_port.setRTS(True)
_port.setDTR(False)
time.sleep(0.1)
_port.setRTS(False)
self.textSignal.emit("模组已复位!!!")
self.pressbarSignal.emit(100)
_port.close()
return
if not connect_chip(_port): #连接芯片,进入烧录模式
self.textSignal.emit('''连接芯片失败!!! 请检查接线:
USB-TTL <--------> TB Moudle
/ ---------------SWS
Tx ----------+
\\----------------Rx
Rx ----------------------------Tx
RTS(或DTR)---------------RST(或VCC)
''')
self.pressbarSignal.emit(200)
_port.close()
return
self.textSignal.emit("连接芯片成功!")
info = get_chip_info(_port) #获取芯片信息
if len(info) != 5:
self.textSignal.emit("获取芯片信息失败!!!")
self.pressbarSignal.emit(200)
_port.close()
return
jedecid = hex((info[0]<<16) | (info[1]<<8) | info[2])
fsize = str((1<<info[2])>>10) + " KBytes"
chip = hex(info[3]*256 + info[4])
self.textSignal.emit("Chip Type:" + chip + " Flash ID:" + jedecid + " Size:" + fsize)
if self.action == "burn": #烧录固件
self.textSignal.emit("擦除固件 ... ... ")
if not telink_flash_erase(_port, 0, 48):
self.textSignal.emit("擦除固件失败!!!")
self.pressbarSignal.emit(200)
_port.close()
return
self.textSignal.emit("擦除固件成功!")
self.textSignal.emit("烧录固件 :" + self.args.file_name)
fo = open(self.args.file_name, "rb")
firmware_addr = 0
firmware_size = os.path.getsize(self.args.file_name)
percent=0
if(firmware_size >= 192 * 1024): # 固件大于192KB,说明是合并好的固件,要跳过Boot
fo.seek(16 * 1024, 0)
firmware_addr = 16 * 1024
while True:
data = fo.read(256)
if len(data) < 1: break
if not telink_flash_write(_port, firmware_addr, data):
self.textSignal.emit("烧录固件失败!!!")
self.pressbarSignal.emit(200)
break
firmware_addr += len(data)
percent = (int)(firmware_addr *100 / firmware_size)
self.pressbarSignal.emit(percent) # 跟新进度条
if percent == 100 : self.textSignal.emit("烧录固件完成!")
fo.close()
elif self.action == "burn_triad": #烧录三元组
self.textSignal.emit("擦除三元组 ... ... ")
if not telink_flash_erase(_port, 0x78000, 1):
self.textSignal.emit("擦除三元组失败!!!")
self.pressbarSignal.emit(200)
_port.close()
return
self.textSignal.emit("擦除三元组成功!")
self.textSignal.emit("烧录三元组 :" + str(self.args.triad))
if telink_flash_write(_port, 0x78000, self.args.triad):
self.textSignal.emit("烧录三元组成功!")
self.pressbarSignal.emit(100)
else:
self.textSignal.emit("烧录三元组失败!!!")
self.pressbarSignal.emit(200)
elif self.action == "erase":#擦除Flash
self.textSignal.emit("擦除Flash: 从 " + hex(self.args.addr) + " 擦除 " + str(self.args.len_t) + " 扇区... ... ")
if telink_flash_erase(_port, self.args.addr, self.args.len_t):
self.textSignal.emit("擦除成功!")
self.pressbarSignal.emit(100)
else:
self.textSignal.emit("擦除失败!!!")
self.pressbarSignal.emit(200)
_port.close()
class TB_Tools(QWidget):
def __init__(self,parent=None):
super().__init__(parent)
self.setWindowTitle("安信可TB模块烧录工具 " + __version__)
# if not os.path.exists('aithinker.png'):
# tmp = open('aithinker.png', 'wb+')
# tmp.write(base64.b64decode(logo))
# tmp.close()
self.setWindowIcon(QIcon("aithinker.png"))
#self.setWindowFlags(Qt.FramelessWindowHint)
self.resize(500,300)
self.layout=QVBoxLayout()
self.tbox_log=QTextEdit()
line_1=QHBoxLayout()
self.serial_cb=QComboBox()
btn_refresh_p=QPushButton("刷新串口")
btn_erase_fw =QPushButton("擦除固件")
btn_erase_key=QPushButton("擦除Mesh Key")
btn_erase_all=QPushButton("整片擦除")
btn_rst_chip =QPushButton("复位芯片")
btn_clean_scn=QPushButton("清空窗口")
btn_refresh_p.clicked.connect(self.refresh_p_fn)
btn_erase_fw.clicked.connect (lambda:self.erase_fn("fw"))
btn_erase_key.clicked.connect(lambda:self.erase_fn("key"))
btn_erase_all.clicked.connect(lambda:self.erase_fn("all"))
btn_rst_chip.clicked.connect(self.rst_chip_fn)
btn_clean_scn.clicked.connect(self.clean_screen_fn)
line_1.addWidget(self.serial_cb)
line_1.addWidget(btn_refresh_p)
line_1.addWidget(btn_erase_fw)
line_1.addWidget(btn_erase_key)
line_1.addWidget(btn_erase_all)
line_1.addWidget(btn_rst_chip)
line_1.addWidget(btn_clean_scn)
line_1.setContentsMargins(0, 0, 0, 0)
line_2=QHBoxLayout()
self.tbox_file = QLineEdit()
btn_open_file =QPushButton("···")
btn_burn=QPushButton("烧录固件")
btn_open_file.clicked.connect(self.open_file_fn)
btn_burn.clicked.connect(self.burn_fn)
line_2.addWidget(self.tbox_file)
line_2.addWidget(btn_open_file)
line_2.addWidget(btn_burn)
line_2.setContentsMargins(0, 0, 0, 0)
line_3=QHBoxLayout()
self.tbox_ali_pID = QLineEdit()
self.tbox_ali_Mac = QLineEdit()
self.tbox_ali_Sct = QLineEdit()
self.tbox_ali_pID.setPlaceholderText("Product ID")
self.tbox_ali_Mac.setPlaceholderText("Device Name")
self.tbox_ali_Sct.setPlaceholderText("Device Secret")
btn_burn_triad=QPushButton("烧录三元组")
btn_burn_triad.clicked.connect(self.burn_triad_fn)
line_3.addWidget(self.tbox_ali_pID)
line_3.addWidget(self.tbox_ali_Mac)
line_3.addWidget(self.tbox_ali_Sct)
line_3.addWidget(btn_burn_triad)
line_3.setContentsMargins(0, 0, 0, 0)
line_3.setStretch(0, 2)
line_3.setStretch(1, 2)
line_3.setStretch(2, 5)
line_3.setStretch(3, 1)
self.layout.addWidget(self.tbox_log)
self.layout.addLayout(line_1)
self.layout.addLayout(line_2)
self.layout.addLayout(line_3)
self.pbar = QProgressBar(self)
self.layout.addWidget(self.pbar)
self.setLayout(self.layout)
self.refresh_p_fn()
def refresh_p_fn(self): #刷新串口号
self.serial_cb.clear()
plist = get_port_list()
for i in range(0, len(plist)):
plist_0 = list(plist[i])
self.serial_cb.addItem(str(plist_0[0]))
def clean_screen_fn(self):
self.tbox_log.clear()
self.tbox_log.setStyleSheet("background-color:white;")
def erase_fn(self, action):#擦除Flash
if not len(self.serial_cb.currentText()) > 0 :
self.log_string("请选择串口号!!!")
return
args = argparse.Namespace()
if action == "fw":
args.addr = 0
args.len_t = 48
elif action == "key":
args.addr = 0x30000
args.len_t = 16
elif action == "all":
args.addr = 0
args.len_t = 128
self.mThread = TelinkThread(_port_name=self.serial_cb.currentText(), action="erase", args=args)
self.mThread.pressbarSignal.connect(self.pressBar_refresh)
self.mThread.textSignal.connect(self.log_string)
self.mThread.start()
def rst_chip_fn(self):#复位新片
if not len(self.serial_cb.currentText()) > 0 :
self.log_string("请选择串口号!!!")
return
args = argparse.Namespace()
self.mThread = TelinkThread(_port_name=self.serial_cb.currentText(), action="reset", args=args)
self.mThread.pressbarSignal.connect(self.pressBar_refresh)
self.mThread.textSignal.connect(self.log_string)
self.mThread.start()
def open_file_fn(self): #选择固件
directory = QFileDialog.getOpenFileName(self, "选择要烧录的固件",'',"固件 (*.bin)")
if len(str(directory[0])) > 5 :
self.tbox_file.setText(str(directory[0]))
self.tbox_file.setStyleSheet("background-color:LightGreen;")
def burn_fn(self):#烧录固件
if not len(self.tbox_file.text()) > 5 :
self.log_string("请选择要烧录的固件!!!")
self.tbox_file.setStyleSheet("background-color:red;")
return
if not os.path.exists(self.tbox_file.text()):
self.log_string("固件不存在!!!")
self.tbox_file.setStyleSheet("background-color:red;")
return
if not len(self.serial_cb.currentText()) > 0 :
self.log_string("请选择串口号!!!")
return
args = argparse.Namespace()
args.file_name = self.tbox_file.text()
self.mThread = TelinkThread(_port_name=self.serial_cb.currentText(), action="burn", args=args)
self.mThread.pressbarSignal.connect(self.pressBar_refresh)
self.mThread.textSignal.connect(self.log_string)
self.mThread.start()
def burn_triad_fn(self):#烧录三元组
try:
data = struct.pack('<I', int(self.tbox_ali_pID.text())) + bytearray.fromhex(self.tbox_ali_Mac.text()) + bytearray.fromhex(self.tbox_ali_Sct.text())
except Exception:
self.log_string("三元组格式错误!!!")
return
if(len(data) != 26):
self.log_string("三元组长度不对!!!")
return
if not len(self.serial_cb.currentText()) > 0 :
self.log_string("请选择串口号!!!")
return
args = argparse.Namespace()
args.triad = data
self.mThread = TelinkThread(_port_name=self.serial_cb.currentText(), action="burn_triad", args=args)
self.mThread.pressbarSignal.connect(self.pressBar_refresh)
self.mThread.textSignal.connect(self.log_string)
self.mThread.start()
def log_string(self, s): #Log窗口日志输出
self.tbox_log.append(s)
def pressBar_refresh(self, a):#刷新进度条及Log串口颜色
if a < 101 :
self.pbar.setValue(a)
if(a == 100):
self.tbox_log.setStyleSheet("background-color:LawnGreen;")
if(a == 200):
self.tbox_log.setStyleSheet("background-color:red;")
if(a == 1000):
self.tbox_log.setStyleSheet("background-color:white;")
self.pbar.setValue(0)
if __name__=="__main__":
app=QApplication(sys.argv)
win=TB_Tools()
win.show()
sys.exit(app.exec_())