-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathUartBoot.py
182 lines (171 loc) · 9.68 KB
/
UartBoot.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
#coding=utf-8
#!/usr/bin/env python
### ComSwireWriter.py ###
### Autor: pvvx ###
### Edited: Aaron Christophel ATCnetz.de ###
### Edit : Pila ###
import sys
import signal
import struct
import serial
import platform
import time
import argparse
import os
import io
import serial.tools.list_ports
import base64
boot_bin = base64.b64decode(b"""
JoA0EgAAAABLTkxUQAGIAB6AAAAAAAAAABQAAAAAAAAMZIGiCQsaQMAGwAbABsAGwAbABsAGwAbABsAGwAbABsAGwAbABsAGDGxwB28AgAASoMB
rEwiFBhOgwGsSCIUGAKASCRIKkQICyghQBLH6hwCgEgkSCpECAsoIUASx+ocNCQwICEABsEhADgkOCg8LmgIEyghYEFAEsQSy+IcBkB+YwEYAgI
QAgH6EAAwdhAB9H4QAFAAAAAwGgAAAFIQAABWEAPQTAAAAHYQADB2EABBlAPYA/goLHEgAohpACQsYQAkJQKMLQAGiC0gTAPzBBgoQSAGyE0ABC
xxAEG3ARkMGgAC4AIAAugCAALkAgAAQZQD2AP4J9gn+CgscSACiGkAJCxhAAbMZQAgJYKMLQAGiC0gTAPzBBAoTQAELHEAQbcBGQwaAALgAgAC6
AIAAAvIS/gwLGkAMCRCiC0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CAMHXgDULnALDwQSpAMEwgR6pAMHkgQOpAMF+gfyjAKGLh9Cjm/CcAh7A4KOb8JwCr8EL7xv2G/4BqwDIXoEGqQDB
tYEDqQDB6YA/owChdIcUqQDBHIEMqQDBr4EaqQDBT4HzowChaIcFqQDB8oADqQDBrIENqQDBRoHPowChXIcJqQDB+IAL7xv2G/4BqwDIu4EDqQD
BYYHPowChTYcg7AGh/5c1nv+jAKFGhwmpAMHWgBmpAMGQgQypAMEzgfOjAKE6h4YFgACoBYAAAgMAAAEDAAABAQAAAgIAAAECAAAaqQDBs4ADqQ
DBrYA/owChI4cL7xv2G/4BqwDI+4ADqQDBQIEVqQDBHIE/owChFIcVqQDBs4ADqQDBVYEEqQDBBIE/owChCIcFqQDBqoAGqQDBXoEXqQDB+4DPo
wCh/IYMqQDBj4ADqXLA86MAofSGGakAwbeAA6lkwM+jAKHshhipAMGpgAOpAMEegQ2pAMEAgfOjAKHghhupbsAGqQDBKIEDqQDB74A/owCh1YYF
qQDBj4ADqQDBBIEUqQDB1IDPowChyYYMqWDAA6lDwPyjAKHChhSpAMGRgAOpAMEhgQSpAMHNgM+jAKG2hgSpAMGLgBupAMHigBSpAMHNgPOjAKG
qhg2pXMAeqSfAz6MAoaOGCKlkwAOpAMHHgAapAMG+gPOjAKGYhh2pR8ADqQDB6YANqQDBj4DzowChjYY/o4ChiobPoxChh4Y/o0ChhIbzowihgY
b8owKhfobPoyChe4Y/o4CheIY/o0ChdYY/owChcobPowChb4Y/owChbIbzowChaYbzowShZob8owGhY4b8owChYIbPowChXYY/owChWobPowChV
4b8owChVIbzowChUYb8owChTobzowChS4bPoxChSIY/o0ChRYY/owChQobPowChP4bzowChPIbzowChOYbPowChNoY/owChM4b8owChMIb8owCh
LYbPowChKob8owChJ4bzowChJIb8owChIYY/owChHoY/owChG4bzowihGIbPoyChFYb8owKhEob8owKhD4b8owKhDIY/o4ChCYbzowihBoY/o4C
hA4bPoyChAIbzowih/YX8owKh+oU/o4Ch94XPoyCh9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el & 0x40) != 0:
d[1] &= 0xe8
if (el & 0x20) != 0:
d[1] &= 0x0f
if (el & 0x10) != 0:
d[2] &= 0xe8
if (el & 0x08) != 0:
d[2] &= 0x0f
if (el & 0x04) != 0:
d[3] &= 0xe8
if (el & 0x02) != 0:
d[3] &= 0x0f
if (el & 0x01) != 0:
d[4] &= 0xe8
pkt += d
d = bytearray([0xef,0xef,0xef,0xef,0xef])
return pkt
# encode command stop into 10-bit swire words
def sws_code_end():
return sws_encode_blk([0xff])
# encode the command for writing data into 10-bit swire words
def sws_wr_addr(addr, data):
return sws_encode_blk(bytearray([0x5a, (addr>>16)&0xff, (addr>>8)&0xff, addr & 0xff, 0x00]) + bytearray(data)) + sws_encode_blk([0xff])
# send block to USB-COM
def wr_usbcom_blk(serialPort, blk):
return serialPort.write(blk)
# send and receive block to USB-COM
def rd_wr_usbcom_blk(serialPort, blk):
i = wr_usbcom_blk(serialPort, blk)
return i == len(serialPort.read(i))
# send swire command write to USB-COM
def sws_wr_addr_usbcom(serialPort, addr, data):
return wr_usbcom_blk(serialPort, sws_wr_addr(addr, data))
# send and receive swire command write to USB-COM
def activate(serialPort, tact_ms):
serialPort.setDTR(True)
serialPort.setRTS(True)
time.sleep(0.05)
serialPort.setDTR(False)
serialPort.setRTS(False)
# Stop CPU|: [0x0602]=5
sws_wr_addr_usbcom(serialPort, 0x06f, bytearray([0x20])) # soft reset mcu
blk = sws_wr_addr(0x0602, bytearray([0x05]))
if tact_ms > 0:
tact = tact_ms/1000.0
t1 = time.time()
while time.time()-t1 < tact:
for i in range(5):
wr_usbcom_blk(serialPort, blk)
serialPort.reset_input_buffer()
#--------------------------------
# Duplication with syncronization
time.sleep(0.01)
serialPort.reset_input_buffer()
rd_wr_usbcom_blk(serialPort, sws_code_end())
rd_wr_usbcom_blk(serialPort, blk)
time.sleep(0.01)
serialPort.reset_input_buffer()
def load_ram(_port):
size = len(boot_bin)
addr = 0x40000
bin_addr = 0
while size > 0:
data = boot_bin[bin_addr:bin_addr+0x100]
if not len(data) > 0: # end of stream
break
_port.write(sws_wr_addr(addr, data))
_port.reset_input_buffer()
bin_addr += len(data)
addr += len(data)
size -= len(data)
# print('\rBin bytes writen:', bin_addr)
# print('CPU go Start...')
_port.write(sws_wr_addr(0x0602, b'\x88')) # cpu go Start
time.sleep(0.07)
_port.flushInput()
_port.flushOutput()
_port.reset_input_buffer()
_port.reset_output_buffer()
time.sleep(0.07)
return True
def uart_boot(serialPort):
activate(serialPort, 80)
return load_ram(serialPort)