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