add constants for device and baudrate
[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         # get checksum
36         last_checksum = recvByte()
37         last_checksum |= (recvByte() << 8)
38
39 def cmdREAD(address, size):
40         # send READ command
41         sendByte(0x01)
42         if (recvByte() != 0xF1):
43                 raise Exception
44         sendByte(0x02)
45         if (recvByte() != 0x82):
46                 raise Exception
47         # tell desired address and size
48         sendDWord(address)
49         sendWord(size)
50         # get binary stream of data
51         data = []
52         for i in range(0, size):
53                 data.append(recvByte())
54         # get checksum
55         recvChecksum()
56         return data
57
58 def cmdWRITE(address, size, data):
59         # send WRITE command
60         sendByte(0x01)
61         if (recvByte() != 0xF1):
62                 raise Exception
63         sendByte(0x03)
64         if (recvByte() != 0x83):
65                 raise Exception
66         # tell desired address and size
67         sendDWord(address)
68         sendWord(size)
69         # write binary stream of data
70         for i in range(0, size):
71                 sendByte(data[i])
72         # get checksum
73         recvChecksum()
74
75 def cmdBAUDRATE(baudrate):
76         global last_checksum
77
78         # send BAUDRATE command
79         sendByte(0x01)
80         if (recvByte() != 0xF1):
81                 raise Exception
82         sendByte(0x06)
83         if (recvByte() != 0x86):
84                 raise Exception
85         # send desired baudrate
86         sendByte(baudrate & 0xFF)
87         sendByte((baudrate >> 8) & 0xFF)
88         sendByte((baudrate >> 16) & 0xFF)
89         sendByte((baudrate >> 24) & 0xFF)
90
91 print "Initializing serial port..."
92 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
93
94 print "Please press RESET on your 1337 board..."
95
96 while 1:
97         tty.write('V')
98         tty.flush()
99         try: 
100                 if tty.read() == 'F':
101                         break
102         except SerialPortException: 
103                 # timeout happened, who cares ;-)
104                 pass
105
106 print "OK, trying to set baudrate..."
107
108 # set baudrate
109 cmdBAUDRATE(REAL_BAUDRATE)
110 tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
111 """
112 print
113 sendByte(0x01)
114 print recvByte()
115 sendByte(0x02)
116 print recvByte()
117 sys.exit(0)
118 """
119
120
121 # write something to the begin of the IRAM
122 data_wr = []
123 checksum = 0
124 for i in range(0, 0x400):
125         value = i%256
126         data_wr.append(value)
127         checksum = (checksum + value) % (2**16)
128
129 print "Calculated checksum:", checksum
130 print "Writing", data_wr, "to the IRAM..."
131 cmdWRITE(0x00030000, len(data_wr), data_wr)
132 print "Received Checksum:", last_checksum
133 print
134
135 print "Reading from the IRAM again..."
136 data_re = cmdREAD(0x00030000, len(data_wr))
137 print "Received data:", data_re, "Checksum:", last_checksum