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