root / trunk / programmer / netprog.py @ 213
History | View | Annotate | Download (4.67 KB)
1 | 157 | bneuman | #!/usr/bin/python
|
---|---|---|---|
2 | import sys |
||
3 | import serial |
||
4 | |||
5 | TT_BOOT = 'b'
|
||
6 | TT_PROGRAM_MODE = 'p'
|
||
7 | 208 | bneuman | TT_ACK = 'a'
|
8 | TT_NACK = 'n'
|
||
9 | 157 | bneuman | TT_BAD = 0
|
10 | 159 | bneuman | TT_PROGD = 'd'
|
11 | 208 | bneuman | TT_RESET = 'r'
|
12 | 157 | bneuman | |
13 | 208 | bneuman | MAX_PROG_SIZE = 1024-32 |
14 | |||
15 | 159 | bneuman | DUMB_SERIAL = 0
|
16 | |||
17 | BAUD_RATE = 9600
|
||
18 | |||
19 | 157 | bneuman | MAX_DATA_LEN = 32
|
20 | |||
21 | if len(sys.argv) < 4: |
||
22 | print "usage: netprog.py /path/to/bus/device toolnum binaryfile" |
||
23 | exit(1) |
||
24 | |||
25 | 208 | bneuman | #for testing, tool number 3 hardcoded
|
26 | 157 | bneuman | class dumbBus (serial.Serial): |
27 | def __init__(self): |
||
28 | self.step = 0 |
||
29 | def flushInput(self): |
||
30 | pass
|
||
31 | def write(self, s): |
||
32 | print '>', |
||
33 | def read(self, n): |
||
34 | if n == 5: |
||
35 | if self.step == 0: |
||
36 | #print ord('^'), 3,1,ord(TT_BOOT), (3 ^ 1 ^ ord(TT_BOOT))
|
||
37 | print '<', |
||
38 | self.step += 1 |
||
39 | return '^' + chr(3) + chr(1) + TT_BOOT + chr(3 ^ 1 ^ ord(TT_BOOT)) |
||
40 | else:
|
||
41 | #print ord('^'), 3,1,ord(TT_ACK), (3 ^ 1 ^ ord(TT_ACK))
|
||
42 | 159 | bneuman | print '<', |
43 | 157 | bneuman | return '^' + chr(3) + chr(1) + TT_ACK + chr(3 ^ 1 ^ ord(TT_ACK)) |
44 | else:
|
||
45 | return 0 |
||
46 | |||
47 | 159 | bneuman | if DUMB_SERIAL == 0: |
48 | bus = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 10) |
||
49 | else:
|
||
50 | bus = dumbBus() |
||
51 | |||
52 | 157 | bneuman | bus.flushInput() |
53 | |||
54 | 163 | bneuman | def printMsg(msg): |
55 | if len(msg) < 5: |
||
56 | print "ERROR: trying to print message thats too small!" |
||
57 | return
|
||
58 | if msg[0] == '^': |
||
59 | print '^', |
||
60 | else:
|
||
61 | print str(ord(msg[0])) |
||
62 | |||
63 | for c in msg[1:]: |
||
64 | print str(ord(c)), |
||
65 | |||
66 | print
|
||
67 | |||
68 | 157 | bneuman | def sendCommand(t, cmd): |
69 | msg = '^' + chr(1) + chr(t) + cmd + chr(1 ^ t ^ ord(cmd)) |
||
70 | print "sending..." |
||
71 | 163 | bneuman | printMsg(msg) |
72 | 157 | bneuman | bus.write(msg) |
73 | |||
74 | def sendProgram(t, size): |
||
75 | if size > 2**16: |
||
76 | print "ERROR: program too big!!" |
||
77 | |||
78 | def lsb(s): return s & 0xFF |
||
79 | def msb(s): return (s>>8) & 0xFF |
||
80 | |||
81 | body = chr(1) + chr(t) + TT_PROGRAM_MODE + chr(lsb(size)) + chr(msb(size)) |
||
82 | 159 | bneuman | print 1,t,TT_PROGRAM_MODE,lsb(size),msb(size), |
83 | 157 | bneuman | xor = 0
|
84 | 159 | bneuman | for c in body: |
85 | 157 | bneuman | xor = xor ^ ord(c)
|
86 | 159 | bneuman | print xor
|
87 | 157 | bneuman | |
88 | msg = '^' + body + chr(xor) |
||
89 | print "sending P" |
||
90 | 163 | bneuman | printMsg(msg) |
91 | 157 | bneuman | bus.write(msg) |
92 | |||
93 | 210 | bneuman | dCount = 0
|
94 | |||
95 | 157 | bneuman | def sendData(t, data): |
96 | 159 | bneuman | body = chr(1) + chr(t) + TT_PROGD + data |
97 | n = len(data)
|
||
98 | while n < MAX_DATA_LEN:
|
||
99 | body = body + chr(0) |
||
100 | n = n + 1
|
||
101 | 157 | bneuman | xor = 0
|
102 | 159 | bneuman | for c in body: |
103 | 157 | bneuman | xor = xor ^ ord(c)
|
104 | msg = '^' + body + chr(xor) |
||
105 | 163 | bneuman | printMsg(msg) |
106 | 157 | bneuman | bus.write(msg) |
107 | 159 | bneuman | print '.', |
108 | sys.stdout.flush() |
||
109 | 157 | bneuman | |
110 | def readPacket(): |
||
111 | msg = bus.read(5)
|
||
112 | 159 | bneuman | if len(msg) < 5: |
113 | print "message too short! len =",len(msg) |
||
114 | 170 | bneuman | if len(msg) == 0: |
115 | print "probably a timeout" |
||
116 | else:
|
||
117 | print "got:", |
||
118 | for i in range(len(msg)): |
||
119 | print (ord(msg[i]))," ", |
||
120 | print
|
||
121 | 159 | bneuman | return TT_BAD
|
122 | 157 | bneuman | if msg[0] != '^': |
123 | 210 | bneuman | print "did not get start delimeter! ",ord(msg[0])," packet size:",len(msg) |
124 | 157 | bneuman | return TT_BAD
|
125 | src = msg[1]
|
||
126 | #print "src: ", ord(src)
|
||
127 | if msg[2] != chr(1): |
||
128 | print "packet dest not 1!" |
||
129 | return TT_BAD
|
||
130 | cmd = msg[3]
|
||
131 | if msg[4] != chr(ord(src) ^ 1 ^ ord(msg[3])): |
||
132 | print "checksum was wrong!" |
||
133 | print ord(msg[0]), ord(msg[1]), ord(msg[2]), ord(msg[3]), ord(msg[4]) |
||
134 | print (ord(src) ^ 1 ^ ord(msg[3])) |
||
135 | return TT_BAD
|
||
136 | |||
137 | return cmd
|
||
138 | |||
139 | toolID = int(sys.argv[2]) |
||
140 | |||
141 | print "sending file: ", sys.argv[3], " to tool ", toolID |
||
142 | |||
143 | fin = open(sys.argv[3], "r") |
||
144 | fin.seek(0,2) |
||
145 | flen = fin.tell() |
||
146 | fin.seek(0,0) |
||
147 | |||
148 | 208 | bneuman | if flen > MAX_PROG_SIZE:
|
149 | print "Program too Large!" |
||
150 | print "prog size:",flen |
||
151 | print "max size:",MAX_PROG_SIZE |
||
152 | exit(-1) |
||
153 | |||
154 | print "prog size:",flen |
||
155 | |||
156 | print "sending RESET" |
||
157 | sendCommand(toolID, TT_RESET) |
||
158 | |||
159 | 157 | bneuman | print "waiting for boot packet..." |
160 | while readPacket() != TT_BOOT:
|
||
161 | 170 | bneuman | print "Didn't get a boot packet, bailing out" |
162 | 157 | bneuman | exit(-4) |
163 | |||
164 | 208 | bneuman | r = 0
|
165 | while r != TT_ACK:
|
||
166 | sendProgram(toolID, flen) |
||
167 | r = readPacket() |
||
168 | if r == TT_NACK:
|
||
169 | print "WARNING: got NACK, trying again" |
||
170 | 210 | bneuman | elif r == TT_BOOT:
|
171 | 208 | bneuman | print "ERROR: got stray boot packet" |
172 | #TODO: restart the whole state machine after the boot packet
|
||
173 | exit(-2) |
||
174 | 210 | bneuman | elif r != TT_ACK:
|
175 | 208 | bneuman | print "ERROR: got another packet:",r |
176 | exit(-1) |
||
177 | 157 | bneuman | |
178 | 159 | bneuman | print 'sending data', |
179 | sys.stdout.flush() |
||
180 | 157 | bneuman | while True: |
181 | data = fin.read(MAX_DATA_LEN) |
||
182 | #print data
|
||
183 | #print '-------------------'
|
||
184 | 208 | bneuman | if len(data) == 0: |
185 | 157 | bneuman | break
|
186 | |||
187 | 208 | bneuman | r = 0
|
188 | while r != TT_ACK:
|
||
189 | sendData(toolID, data) |
||
190 | r = readPacket() |
||
191 | if r == TT_NACK:
|
||
192 | print "WARNING: got NACK, trying again" |
||
193 | 210 | bneuman | elif r == TT_BOOT:
|
194 | 208 | bneuman | print "ERROR: got stray boot packet" |
195 | #TODO: restart the whole state machine after the boot packet
|
||
196 | exit(-2) |
||
197 | 210 | bneuman | elif r != TT_ACK:
|
198 | 208 | bneuman | print "ERROR: got another packet:",r |
199 | exit(-1) |
||
200 | |||
201 | 157 | bneuman | if len(data) < MAX_DATA_LEN: |
202 | break |