a call to the end of the IRAM (0x00033ffc)...
[pyfrprog.git] / frprog.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 initialization
8 INIT_BAUDRATE=9600
9 # baudrate used for communication after init
10 REAL_BAUDRATE=38400
11
12 # contains the last received checksum from a READ, WRITE or CHECKSUM command
13 last_checksum = 0
14
15 def sendByte(byte):
16         time.sleep(0.001) # just to get sure, wait 1ms
17         tty.write(chr(byte))
18         tty.flush()
19
20 def sendWord(word):
21         sendByte(word & 0xFF)
22         sendByte((word >> 8) & 0xFF)
23
24 def sendDWord(dword):
25         sendByte(dword & 0xFF)
26         sendByte((dword >> 8) & 0xFF)
27         sendByte((dword >> 16) & 0xFF)
28         sendByte((dword >> 24) & 0xFF)
29
30 def recvByte():
31         return ord(tty.read())
32
33 def recvChecksum():
34         global last_checksum
35         last_checksum = recvByte()
36         last_checksum |= (recvByte() << 8)
37
38 def cmdREAD(address, size):
39         # send READ command
40         sendByte(0x01)
41         if (recvByte() != 0xF1):
42                 raise Exception
43         sendByte(0x02)
44         if (recvByte() != 0x82):
45                 raise Exception
46         # tell desired address and size
47         sendDWord(address)
48         sendWord(size)
49         # get binary stream of data
50         data = []
51         for i in range(0, size):
52                 data.append(recvByte())
53         # get checksum
54         recvChecksum()
55         return data
56
57 def cmdWRITE(address, size, data):
58         # send WRITE command
59         sendByte(0x01)
60         if (recvByte() != 0xF1):
61                 raise Exception
62         sendByte(0x03)
63         if (recvByte() != 0x83):
64                 raise Exception
65         # tell desired address and size
66         sendDWord(address)
67         sendWord(size)
68         # write binary stream of data
69         for i in range(0, size):
70                 sendByte(data[i])
71         # get checksum
72         recvChecksum()
73
74 # TODO: test this function!
75 def cmdCALL(address):
76         # send CALL command
77         sendByte(0x01)
78         if (recvByte() != 0xF1):
79                 raise Exception
80         sendByte(0x04)
81         if (recvByte() != 0x84):
82                 raise Exception
83         # tell desired address
84         sendDWord(address)
85         # wait for return parameter - not needed here!
86         #return recvByte()
87
88 # TODO: test this function!
89 def cmdCHECKSUM():
90         # call CHECKSUM command
91         sendByte(0x01)
92         if (recvByte() != 0xF1):
93                 raise Exception
94         sendByte(0x05)
95         if (recvByte() != 0x84):
96                 raise Exception
97         # get checksum
98         recvChecksum()
99
100 def cmdBAUDRATE(baudrate):
101         global last_checksum
102
103         # send BAUDRATE command
104         sendByte(0x01)
105         if (recvByte() != 0xF1):
106                 raise Exception
107         sendByte(0x06)
108         if (recvByte() != 0x86):
109                 raise Exception
110         # send desired baudrate
111         sendByte(baudrate & 0xFF)
112         sendByte((baudrate >> 8) & 0xFF)
113         sendByte((baudrate >> 16) & 0xFF)
114         sendByte((baudrate >> 24) & 0xFF)
115
116 print "Initializing serial port..."
117 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
118
119 print "Please press RESET on your 1337 board..."
120
121 while 1:
122         tty.write('V')
123         tty.flush()
124         try: 
125                 if tty.read() == 'F':
126                         break
127         except SerialPortException: 
128                 # timeout happened, who cares ;-)
129                 pass
130
131 print "OK, trying to set baudrate..."
132
133 # set baudrate
134 cmdBAUDRATE(REAL_BAUDRATE)
135 tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
136 """
137 print
138 sendByte(0x01)
139 print recvByte()
140 sendByte(0x02)
141 print recvByte()
142 sys.exit(0)
143 """
144
145 #cmdCALL(0x00030000)
146 cmdCALL(0x00033ffc)
147 sys.exit(0)
148
149 # read something from the IRAM
150 #print cmdREAD(0x00030000, 32)
151
152 #data = []
153 #for i in range(0, 32):
154 #       data.append(i)
155 #cmdWRITE(0x00030000, 32, data)
156
157
158 # write something to the begin of the IRAM
159 data_wr = []
160 checksum = 0
161 for i in range(0, 0x400):
162         value = i%256
163         data_wr.append(value)
164         checksum = (checksum + value) % (2**16)
165
166 print "Calculated checksum:", checksum
167 print "Writing", data_wr, "to the IRAM..."
168 cmdWRITE(0x00030000, len(data_wr), data_wr)
169 print "Received Checksum:", last_checksum
170 print
171
172 print "Reading from the IRAM again..."
173 data_re = cmdREAD(0x00030000, len(data_wr))
174 print "Received data:", data_re, "Checksum:", last_checksum