added further exit function, so the board restarts after reset
[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         # write binary stream of data
60         for i in range(0, size):
61                 sendByte(data[i])
62
63         if (recvByte() != 0x28):
64                 raise Exception
65         print "Flashing done."
66
67
68 class FlashSequence(object):
69         def __init__(self, address, data):
70                 self.address = address
71                 self.data = data
72
73 # list of all our address/data pairs to flash
74 flashseqs = []
75
76
77 print "Initializing serial port..."
78 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
79
80 # check command line arguments
81 if len(sys.argv) != 2:
82         print "Usage: " + sys.argv[0] + " [mhx-file]"
83         sys.exit(1)
84
85 # read in data from mhx-file before starting
86 try:
87         fp = open(sys.argv[1], "r")
88 except IOError:
89         print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
90         sys.exit(1)
91
92 linecount = 0
93 for line in fp:
94         linecount += 1
95         # get rid of newline characters
96         line = line.strip()
97
98         # we're only interested in S2 (data sequence with 3 address bytes) records by now
99         if line[0:2] == "S2":
100                 byte_count = int(line[2:4], 16)
101                 # just to get sure, check if byte count field is valid
102                 if (len(line)-4) != (byte_count*2):
103                         print sys.argv[0] + ": Warning - inavlid byte count field in " + \
104                                 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
105                         continue
106
107                 # address and checksum bytes are not needed
108                 byte_count -= 4
109                 address = int(line[4:10], 16)
110                 datastr = line[10:10+byte_count*2]
111
112                 # convert data hex-byte-string to real byte data list
113                 data = []
114                 for i in range(0, len(datastr)/2):
115                         data.append(int(datastr[2*i:2*i+2], 16))
116
117                 # add flash sequence to our list
118                 flashseqs.append(FlashSequence(address, data))
119
120 print "The following flash sequences have been read in:"
121 for seq in flashseqs:
122         print hex(seq.address) + ":", [hex(x) for x in seq.data]
123
124
125 # let the fun begin!
126 """
127 for seq in flashseqs:
128         print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
129         pkernERASE(seq.address, len(seq.data))
130 """
131 print "ChipErase..."
132 pkernCHIPERASE()
133
134
135 for seq in flashseqs:
136         print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
137         pkernWRITE(seq.address, len(seq.data), seq.data)
138
139 """
140 sendByte(0x99) #exit and wait
141 print "Reset your board now to run code from Flash"
142 """
143
144 sendByte(0x97) #exit and restart