3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for communication with pkernel
15 time.sleep(0.005) # just to get sure, wait 5ms
21 sendByte((word >> 8) & 0xFF)
24 sendByte(dword & 0xFF)
25 sendByte((dword >> 8) & 0xFF)
26 sendByte((dword >> 16) & 0xFF)
27 sendByte((dword >> 24) & 0xFF)
31 if (recvByte() != 0x45):
34 if (recvByte() != 0x23):
36 print "Chip erasing done."
38 def pkernERASE(address, size):
40 if (recvByte() != 0x11):
44 if (recvByte() != 0x18):
49 def pkernWRITE(address, size, data):
50 print "address:", hex(address), "size:", size
53 if (recvByte() != 0x37):
55 # tell desired address and size
59 # write binary stream of data
60 for i in range(0, size):
63 if (recvByte() != 0x28):
65 print "Flashing done."
68 class FlashSequence(object):
69 def __init__(self, address, data):
70 self.address = address
73 # list of all our address/data pairs to flash
77 print "Initializing serial port..."
78 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
80 # check command line arguments
81 if len(sys.argv) != 2:
82 print "Usage: " + sys.argv[0] + " [mhx-file]"
85 # read in data from mhx-file before starting
87 fp = open(sys.argv[1], "r")
89 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
95 # get rid of newline characters
98 # we're only interested in S2 (data sequence with 3 address bytes) records by now
100 byte_count = int(line[2:4], 16)
101 # just to get sure, check if byte count field is valid
102 if (len(line)-4) != (byte_count*2):
103 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
104 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
107 # address and checksum bytes are not needed
109 address = int(line[4:10], 16)
110 datastr = line[10:10+byte_count*2]
112 # convert data hex-byte-string to real byte data list
114 for i in range(0, len(datastr)/2):
115 data.append(int(datastr[2*i:2*i+2], 16))
117 # add flash sequence to our list
118 flashseqs.append(FlashSequence(address, data))
120 print "The following flash sequences have been read in:"
121 for seq in flashseqs:
122 print hex(seq.address) + ":", [hex(x) for x in seq.data]
127 for seq in flashseqs:
128 print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
129 pkernERASE(seq.address, len(seq.data))
135 for seq in flashseqs:
136 print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
137 pkernWRITE(seq.address, len(seq.data), seq.data)
140 sendByte(0x99) #exit and wait
141 print "Reset your board now to run code from Flash"
144 sendByte(0x97) #exit and restart