3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for communication with pkernel
14 def recvByteTO(b): #with timeout
17 while len(i)==0 and j < 400:
24 if(recvByte() != 0xaa):
25 raise Exception #ok, something really failed
31 time.sleep(0.005) # just to get sure, wait 5ms
37 sendByte((word >> 8) & 0xFF)
40 sendByte(dword & 0xFF)
41 sendByte((dword >> 8) & 0xFF)
42 sendByte((dword >> 16) & 0xFF)
43 sendByte((dword >> 24) & 0xFF)
47 if (recvByte() != 0x45):
50 if (recvByte() != 0x23):
52 print "Chip erasing done."
54 def pkernERASE(address, size):
56 if (recvByte() != 0x11):
60 if (recvByte() != 0x18):
65 def pkernWRITE(address, size, data):
66 print "address:", hex(address), "size:", size
71 # tell desired address and size
75 # write binary stream of data
76 for i in range(0, size):
80 class FlashSequence(object):
81 def __init__(self, address, data):
82 self.address = address
85 # list of all our address/data pairs to flash
89 print "Initializing serial port..."
90 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
92 # check command line arguments
93 if len(sys.argv) != 2:
94 print "Usage: " + sys.argv[0] + " [mhx-file]"
97 # read in data from mhx-file before starting
99 fp = open(sys.argv[1], "r")
101 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
107 # get rid of newline characters
110 # we're only interested in S2 (data sequence with 3 address bytes) records by now
111 if line[0:2] == "S2":
112 byte_count = int(line[2:4], 16)
113 # just to get sure, check if byte count field is valid
114 if (len(line)-4) != (byte_count*2):
115 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
116 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
119 # address and checksum bytes are not needed
121 address = int(line[4:10], 16)
122 datastr = line[10:10+byte_count*2]
124 # convert data hex-byte-string to real byte data list
126 for i in range(0, len(datastr)/2):
127 data.append(int(datastr[2*i:2*i+2], 16))
129 # add flash sequence to our list
130 flashseqs.append(FlashSequence(address, data))
132 print "The following flash sequences have been read in:"
133 for seq in flashseqs:
134 print hex(seq.address) + ":", [hex(x) for x in seq.data]
139 for seq in flashseqs:
140 print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
141 pkernERASE(seq.address, len(seq.data))
147 for seq in flashseqs:
148 print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
149 pkernWRITE(seq.address, len(seq.data), seq.data)
151 #sendByte(0x99); #exit and wait
152 #print "Reset your board now to run code from Flash"
155 sendByte(0x97); #exit and restart