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
|
#!/usr/bin/env python
import serial
import array
#=========================================================
PORT = '/dev/ttyUSB1'
FILE = 'build/ch.bin'
RAM_ADDR = 0x10000300
PAGE_SIZE = 0x40
SECTOR_SIZE = 0x400
FLASH_SIZE = 0x4000
#=========================================================
ser = serial.Serial( PORT, 115200, timeout=1)
data = array.array('B')
f = file( FILE, 'rb')
try:
data.fromfile(f, FLASH_SIZE)
except:
pass
f.close()
## pad out to next whole page
data.fromstring( chr(0xff)*(PAGE_SIZE - (data.buffer_info()[1]%PAGE_SIZE)) )
#=========================================================
## fix-up LPC boot checksum
csum = 0;
for i in range(7):
csum = csum + \
(data[(i*4)] ) + \
(data[(i*4)+1]<<8 ) + \
(data[(i*4)+2]<<16) + \
(data[(i*4)+3]<<24); \
csum = -csum
data[28] = csum & 0xff
data[29] = csum>>8 & 0xff
data[30] = csum>>16 & 0xff
data[31] = csum>>24 & 0xff
#=========================================================
##
ser.write('?')
resp = ser.readline()
if resp.strip() <> 'Synchronized':
print 'No Response "?"'
exit(1)
ser.write('Synchronized\r\n')
resp = ser.readline()
resp = ser.readline()
if resp.strip() <> 'OK':
print 'Not Synchronized'
exit(1)
ser.write('12000\r\n')
resp = ser.readline()
resp = ser.readline()
if resp.strip() <> 'OK':
print 'No Response "12000"'
exit(1)
ser.write('A 0\r\n')
resp = ser.readline()
resp = ser.readline()
if resp.strip() <> '0':
print 'Error Response "A"', resp
exit(1)
ser.write('J\r\n')
resp = ser.readline()
if resp.strip() <> '0':
print 'Error Response "J"', resp
exit(1)
resp = ser.readline()
print 'Device ID: ', hex(int(resp))
ser.write('U 23130\r\n')
resp = ser.readline()
if resp.strip() <> '0':
print 'Error Response "U"', resp
exit(1)
## Erase whole device
ser.write('P 0 7\r\n')
resp = ser.readline()
if resp.strip() <> '0':
print 'Error Response "P"', resp
exit(1)
ser.write('E 0 7\r\n')
resp = ser.readline()
if resp.strip() <> '0':
print 'Error Response "P"', resp
exit(1)
#=========================================================
address = 0
while data.buffer_info()[1]:
ser.write( "W %d %d\r\n"%(RAM_ADDR, PAGE_SIZE) )
resp = ser.readline()
if resp.strip() <> '0':
print 'Error Response "W"', resp
exit(1)
for i in range(PAGE_SIZE):
ser.write( chr(data.pop(0)) )
#print('P %x %x\r\n'%( address/SECTOR_SIZE, address/SECTOR_SIZE ))
#print('C %x %x 0xff\r\n'%( address, RAM_ADDR ))
## Program page
ser.write('P %d %d\r\n'%( address/SECTOR_SIZE, address/SECTOR_SIZE ))
resp = ser.readline()
if resp.strip() <> '0':
print 'Error Response "P"', resp
exit(1)
ser.write( 'C %d %d %d\r\n'%(address, RAM_ADDR, PAGE_SIZE) )
resp = ser.readline()
if resp.strip() <> '0':
print 'Error Response "C"', resp
exit(1)
print '.',
address = address + PAGE_SIZE
if (address%SECTOR_SIZE) == 0:
print ''
#=========================================================
#=========================================================
|