add a test with some more data and local chksum
[pyfrprog.git] / frprog.py
1 #!/usr/bin/env python
2 import sys, time
3 from SerialPort_linux import *
4
5 # contains the last received checksum from a READ, WRITE or CHECKSUM command
6 last_checksum = 0
7
8 def sendByte(byte):
9         time.sleep(0.001) # just to get sure, wait 1ms
10         tty.write(chr(byte))
11         tty.flush()
12
13 def sendWord(word):
14         sendByte(word & 0xFF)
15         sendByte((word >> 8) & 0xFF)
16
17 def sendDWord(dword):
18         sendByte(dword & 0xFF)
19         sendByte((dword >> 8) & 0xFF)
20         sendByte((dword >> 16) & 0xFF)
21         sendByte((dword >> 24) & 0xFF)
22
23 def recvByte():
24         return ord(tty.read())
25
26 def recvChecksum():
27         global last_checksum
28         # get checksum
29         last_checksum = recvByte()
30         last_checksum |= (recvByte() << 8)
31
32 def cmdREAD(address, size):
33         # send READ command
34         sendByte(0x01)
35         if (recvByte() != 0xF1):
36                 raise Exception
37         sendByte(0x02)
38         if (recvByte() != 0x82):
39                 raise Exception
40         # tell desired address and size
41         sendDWord(address)
42         sendWord(size)
43         # get binary stream of data
44         data = []
45         for i in range(0, size):
46                 data.append(recvByte())
47         # get checksum
48         recvChecksum()
49         return data
50
51 def cmdWRITE(address, size, data):
52         # send WRITE command
53         sendByte(0x01)
54         if (recvByte() != 0xF1):
55                 raise Exception
56         sendByte(0x03)
57         if (recvByte() != 0x83):
58                 raise Exception
59         # tell desired address and size
60         sendDWord(address)
61         sendWord(size)
62         # write binary stream of data
63         for i in range(0, size):
64                 sendByte(data[i])
65         # get checksum
66         recvChecksum()
67
68 def cmdBAUDRATE(baudrate):
69         global last_checksum
70
71         # send BAUDRATE command
72         sendByte(0x01)
73         if (recvByte() != 0xF1):
74                 raise Exception
75         sendByte(0x06)
76         if (recvByte() != 0x86):
77                 raise Exception
78         # send desired baudrate
79         sendByte(baudrate & 0xFF)
80         sendByte((baudrate >> 8) & 0xFF)
81         sendByte((baudrate >> 16) & 0xFF)
82         sendByte((baudrate >> 24) & 0xFF)
83
84 print "Initializing serial port..."
85 tty = SerialPort("/dev/ttyUSB0", 100, 9600)
86
87 print "Please press RESET on your 1337 board..."
88
89 while 1:
90         tty.write('V')
91         tty.flush()
92         try: 
93                 if tty.read() == 'F':
94                         break
95         except SerialPortException: 
96                 # timeout happened, who cares ;-)
97                 pass
98
99 print "OK, trying to set baudrate..."
100
101 # set baudrate
102 cmdBAUDRATE(38400)
103 tty = SerialPort("/dev/ttyUSB0", 100, 38400)
104 """
105 print
106 sendByte(0x01)
107 print recvByte()
108 sendByte(0x02)
109 print recvByte()
110 sys.exit(0)
111 """
112
113
114 # write something to the begin of the IRAM
115 data_wr = []
116 checksum = 0
117 for i in range(0, 0x400):
118         value = i%256
119         data_wr.append(value)
120         checksum = (checksum + value) % (2**16)
121
122 print "Calculated checksum:", checksum
123 print "Writing", data_wr, "to the IRAM..."
124 cmdWRITE(0x00030000, len(data_wr), data_wr)
125 print "Received Checksum:", last_checksum
126 print
127
128 print "Reading from the IRAM again..."
129 data_re = cmdREAD(0x00030000, len(data_wr))
130 print "Received data:", data_re, "Checksum:", last_checksum