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