3ebda56f3ad0952f12990f1d951df6ce1532b488
[pyfrprog.git] / pkernel / kernel.py
1 #!/usr/bin/env python
2 import sys, time
3 from SerialPort_linux import *
4
5 # serial device to communicate with
6 DEVICE="/dev/ttyUSB0"
7 # baudrate used for communication with pkernel
8 KERNEL_BAUDRATE=38400
9
10 def recvByte():
11         i = tty.read()
12         while len(i)==0:
13                 time.sleep(0.03)
14                 i = tty.read()
15         return ord(i)
16
17 def sendByte(byte):
18         time.sleep(0.005) # just to get sure, wait 5ms
19         tty.write(chr(byte))
20         tty.flush()
21
22 def sendWord(word):
23         sendByte(word & 0xFF)
24         sendByte((word >> 8) & 0xFF)
25
26 def sendDWord(dword):
27         sendByte(dword & 0xFF)
28         sendByte((dword >> 8) & 0xFF)
29         sendByte((dword >> 16) & 0xFF)
30         sendByte((dword >> 24) & 0xFF)
31
32 def pkernCHIPERASE():
33         sendByte(0x15)
34         if (recvByte() != 0x45):
35                 raise Exception
36         print "wait..."
37         if (recvByte() != 0x23):
38                 raise Exception
39         print "Chip erasing done."
40
41 def pkernERASE(address, size):
42         sendByte(0x12)
43         if (recvByte() != 0x11):
44                 raise Exception
45         sendDWord(address)
46         sendWord(size)
47         if (recvByte() != 0x18):
48                 raise Exception
49         print "Erasing done."
50
51
52 def pkernWRITE(address, size, data):
53         print "address:", hex(address), "size:", size
54         # send WRITE command
55         sendByte(0x13)
56         if (recvByte() != 0x37):
57                 raise Exception
58         # tell desired address and size
59         sendDWord(address)
60         sendWord(size)
61
62         if (recvByte() != 0x04):
63                 raise Exception
64         print "Received Metadata."
65
66         # write binary stream of data
67         for i in range(0, size):
68                 sendByte(data[i])
69
70         if (recvByte() != 0x08):
71                 raise Exception
72         print "Received Data."
73
74         if (recvByte() != 0x28):
75                 raise Exception
76         print "Flashing done."
77
78
79 class FlashSequence(object):
80         def __init__(self, address, data):
81                 self.address = address
82                 self.data = data
83
84 # list of all our address/data pairs to flash
85 flashseqs = []
86
87
88 print "Initializing serial port..."
89 tty = SerialPort(DEVICE, 0, KERNEL_BAUDRATE)
90
91 # check command line arguments
92 if len(sys.argv) != 2:
93         print "Usage: " + sys.argv[0] + " [mhx-file]"
94         sys.exit(1)
95
96 # read in data from mhx-file before starting
97 try:
98         fp = open(sys.argv[1], "r")
99 except IOError:
100         print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
101         sys.exit(1)
102
103 linecount = 0
104 for line in fp:
105         linecount += 1
106         # get rid of newline characters
107         line = line.strip()
108
109         # we're only interested in S2 (data sequence with 3 address bytes) records by now
110         if line[0:2] == "S2":
111                 byte_count = int(line[2:4], 16)
112                 # just to get sure, check if byte count field is valid
113                 if (len(line)-4) != (byte_count*2):
114                         print sys.argv[0] + ": Warning - inavlid byte count field in " + \
115                                 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
116                         continue
117
118                 # address and checksum bytes are not needed
119                 byte_count -= 4
120                 address = int(line[4:10], 16)
121                 datastr = line[10:10+byte_count*2]
122
123                 # convert data hex-byte-string to real byte data list
124                 data = []
125                 for i in range(0, len(datastr)/2):
126                         data.append(int(datastr[2*i:2*i+2], 16))
127
128                 # add flash sequence to our list
129                 flashseqs.append(FlashSequence(address, data))
130
131 print "The following flash sequences have been read in:"
132 for seq in flashseqs:
133         print hex(seq.address) + ":", [hex(x) for x in seq.data]
134
135
136 # let the fun begin!
137 """
138 for seq in flashseqs:
139         print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
140         pkernERASE(seq.address, len(seq.data))
141 """
142 print "ChipErase..."
143 pkernCHIPERASE()
144
145
146 for seq in flashseqs:
147         print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
148         pkernWRITE(seq.address, len(seq.data), seq.data)
149
150 sendByte(0x99);
151
152 print "Reset your board now to run code from Flash"