-
Notifications
You must be signed in to change notification settings - Fork 10
/
ComSwireWriter826x.py
193 lines (182 loc) · 5.03 KB
/
ComSwireWriter826x.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
#!/usr/bin/env python
### ComSwireWriter.py ###
### Autor: pvvx ###
import sys
import struct
import serial
import platform
import time
import argparse
import os
import io
__progname__ = 'TLSR826x ComSwireWriter Utility'
__filename__ = 'ComSwireWriter'
__version__ = "25.02.20"
class FatalError(RuntimeError):
def __init__(self, message):
RuntimeError.__init__(self, message)
@staticmethod
def WithResult(message, result):
message += " (result was %s)" % hexify(result)
return FatalError(message)
def arg_auto_int(x):
return int(x, 0)
def sws_code_blk(blk):
pkt=[]
d = bytearray([0xd8,0xdf,0xdf,0xdf,0xdf])
for el in blk:
if (el & 0x80) != 0:
d[0] &= 0x1f
if (el & 0x40) != 0:
d[1] &= 0xf8
if (el & 0x20) != 0:
d[1] &= 0x1f
if (el & 0x10) != 0:
d[2] &= 0xf8
if (el & 0x08) != 0:
d[2] &= 0x1f
if (el & 0x04) != 0:
d[3] &= 0xf8
if (el & 0x02) != 0:
d[3] &= 0x1f
if (el & 0x01) != 0:
d[4] &= 0xf8
pkt += d
d = bytearray([0xdf,0xdf,0xdf,0xdf,0xdf])
return pkt
def sws_rd_addr(addr):
return sws_code_blk(bytearray([0x5a, (addr>>8)&0xff, addr & 0xff, 0x80]))
def sws_code_end():
return sws_code_blk([0xff])
def sws_wr_addr(addr, data):
return sws_code_blk(bytearray([0x5a, (addr>>8)&0xff, addr & 0xff, 0x00]) + bytearray(data)) + sws_code_blk([0xff])
def main():
parser = argparse.ArgumentParser(description='%s version %s' % (__progname__, __version__), prog=__filename__)
parser.add_argument(
'--port', '-p',
help='Serial port device (default: COM1)',
default='COM1')
parser.add_argument(
'--tact','-t',
help='Time Activation ms (0-off, default: 600 ms)',
type=arg_auto_int,
default=600)
parser.add_argument(
'--file','-f',
help='Filename to load (default: floader.bin)',
default='floader.bin')
parser.add_argument(
'--baud','-b',
help='UART Baud Rate (default: 230400)',
type=arg_auto_int,
default=230400)
args = parser.parse_args()
print('%s version %s' % (__progname__, __version__))
print('------------------------------------------------')
print ('Open %s, %d baud...' % (args.port, args.baud))
try:
serialPort = serial.Serial(args.port, args.baud, \
serial.EIGHTBITS,\
serial.PARITY_NONE, \
serial.STOPBITS_ONE)
# serialPort.flushInput()
# serialPort.flushOutput()
serialPort.timeout = 100*12/args.baud
except:
print ('Error: Open %s, %d baud!' % (args.port, args.baud))
sys.exit(1)
#--------------------------------
try:
stream = open(args.file, 'rb')
size = os.path.getsize(args.file)
except:
serialPort.close
print('Error: Not open input file <%s>!' % args.file)
sys.exit(2)
if size < 1:
stream.close
serialPort.close
print('Error: File size = 0!')
sys.exit(3)
warn = 0
#--------------------------------
# issue reset-to-bootloader:
# RTS = either RESET (active low = chip in reset)
# DTR = active low
print('Reset module (RTS low)...')
serialPort.setDTR(True)
serialPort.setRTS(True)
time.sleep(0.05)
serialPort.setDTR(False)
serialPort.setRTS(False)
#--------------------------------
# Stop CPU|: [0x0602]=5
print('Activate (%d ms)...' % args.tact)
tact = args.tact/1000.0
blk = sws_wr_addr(0x0602, bytearray([5]))
byteSent = serialPort.write(blk)
if args.tact != 0:
t1 = time.time()
while time.time()-t1 < tact:
for i in range(5):
byteSent += serialPort.write(blk)
serialPort.reset_input_buffer()
time.sleep(byteSent*12/args.baud - tact)
while len(serialPort.read(1000)):
continue
#--------------------------------
# Set SWS speed low: [0x00b2]=50
byteSent = serialPort.write(sws_wr_addr(0x00b2, [45]))
read = serialPort.read(byteSent+33)
byteRead = len(read)
if byteRead != byteSent:
byteSent = 0
warn += 1
print('Warning: Wrong RX-TX connection?')
#--------------------------------
# Test read bytes [0x00b2]
byteSent += serialPort.write(sws_rd_addr(0x00b2))
# start read
byteSent += serialPort.write([0xff])
read = serialPort.read(byteSent-byteRead+33)
byteRead += len(read)
if byteRead <= byteSent:
print('Warning: Pin RX no connection to the module?')
warn += 1
else:
print('Connection...')
# stop read
byteSent += serialPort.write(sws_code_end())
read = serialPort.read(byteSent-byteRead+33)
byteRead += len(read)
#--------------------------------
# Load floader.bin
binWrite = 0
rdsize = 0x100
addr = 0x8000
print('Load <%s> to 0x%04x...' % (args.file, addr))
while size > 0:
print('\r0x%04x' % addr, end = '')
data = stream.read(rdsize)
if not data: # end of stream
print('send: at EOF')
break
byteSent += serialPort.write(sws_wr_addr(addr, data))
serialPort.reset_input_buffer()
binWrite += len(data)
addr += len(data)
size -= len(data)
stream.close
print('\rBin bytes writen:', binWrite)
print('CPU go Start...')
byteSent += serialPort.write(sws_wr_addr(0x0602, [0x88])) # cpu go Start
serialPort.close
print('COM bytes sent:', byteSent)
print('------------------------------------------------')
if warn == 0:
print('Done!')
else:
print('(%d) Warning' % warn)
sys.exit(0)
if __name__ == '__main__':
main()