Merge branch 'master' of wien.tomnetworks.com:pyfrprog
[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 recvByteTO(b): #with timeout
15         j = 0
16         i = tty.read()
17         while len(i)==0 and j < 400:
18                 time.sleep(0.03)
19                 i = tty.read()
20                 j = j + 1
21         if j == 400:
22                 print "ack it!"
23                 sendByte(0x23)
24                 if(recvByte() != 0xaa):
25                         raise Exception #ok, something really failed
26         else:
27                 if(ord(i) != b):
28                         raise Exception
29
30 def sendByte(byte):
31         time.sleep(0.005) # just to get sure, wait 5ms
32         tty.write(chr(byte))
33         tty.flush()
34
35 def sendWord(word):
36         sendByte(word & 0xFF)
37         sendByte((word >> 8) & 0xFF)
38
39 def sendDWord(dword):
40         sendByte(dword & 0xFF)
41         sendByte((dword >> 8) & 0xFF)
42         sendByte((dword >> 16) & 0xFF)
43         sendByte((dword >> 24) & 0xFF)
44
45 def pkernCHIPERASE():
46         sendByte(0x15)
47         if (recvByte() != 0x45):
48                 raise Exception
49         print "wait..."
50         if (recvByte() != 0x23):
51                 raise Exception
52         print "Chip erasing done."
53
54 def pkernERASE(address, size):
55         sendByte(0x12)
56         if (recvByte() != 0x11):
57                 raise Exception
58         sendDWord(address)
59         sendWord(size)
60         if (recvByte() != 0x18):
61                 raise Exception
62         print "Erasing done."
63
64
65 def pkernWRITE(address, size, data):
66         print "address:", hex(address), "size:", size
67         # send WRITE command
68         sendByte(0x13)
69         recvByteTO(0x37)
70
71         # tell desired address and size
72         sendDWord(address)
73         sendWord(size)
74
75         # write binary stream of data
76         for i in range(0, size):
77                 sendByte(data[i])
78
79
80 class FlashSequence(object):
81         def __init__(self, address, data):
82                 self.address = address
83                 self.data = data
84
85 # list of all our address/data pairs to flash
86 flashseqs = []
87
88
89 print "Initializing serial port..."
90 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
91
92 # check command line arguments
93 if len(sys.argv) != 2:
94         print "Usage: " + sys.argv[0] + " [mhx-file]"
95         sys.exit(1)
96
97 # read in data from mhx-file before starting
98 try:
99         fp = open(sys.argv[1], "r")
100 except IOError:
101         print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
102         sys.exit(1)
103
104 linecount = 0
105 for line in fp:
106         linecount += 1
107         # get rid of newline characters
108         line = line.strip()
109
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!"
117                         continue
118
119                 # address and checksum bytes are not needed
120                 byte_count -= 4
121                 address = int(line[4:10], 16)
122                 datastr = line[10:10+byte_count*2]
123
124                 # convert data hex-byte-string to real byte data list
125                 data = []
126                 for i in range(0, len(datastr)/2):
127                         data.append(int(datastr[2*i:2*i+2], 16))
128
129                 # add flash sequence to our list
130                 flashseqs.append(FlashSequence(address, data))
131
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]
135
136
137 # let the fun begin!
138 """
139 for seq in flashseqs:
140         print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
141         pkernERASE(seq.address, len(seq.data))
142 """
143 print "ChipErase..."
144 pkernCHIPERASE()
145
146
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)
150
151 #sendByte(0x99); #exit and wait
152 #print "Reset your board now to run code from Flash"
153
154
155 sendByte(0x97); #exit and restart