Revision 218
changed messages to have payload length. Also created manualTron
trunk/toolbox/main.c | ||
---|---|---|
166 | 166 |
uint8_t addr = eeprom_read_byte((void*)EEPROM_ADDR); |
167 | 167 |
uint8_t ms_timer=0; |
168 | 168 |
uint8_t mbuf[PROGD_PACKET_SIZE]; // For reading messages |
169 |
uint8_t len; |
|
169 | 170 |
|
170 | 171 |
/***** Start Start-up Sequence *****/ |
171 | 172 |
// We are initializing the pins and the RS485 in the bootloader |
... | ... | |
184 | 185 |
toggle_relay(OFF); |
185 | 186 |
|
186 | 187 |
// Wait for a packet |
187 |
resp = parse_packet(mbuf, addr); |
|
188 |
resp = parse_packet(mbuf, &len, addr);
|
|
188 | 189 |
|
189 | 190 |
// Turn on the tool |
190 | 191 |
if (resp == TT_ON) { |
trunk/toolbox/jumptable.h | ||
---|---|---|
5 | 5 |
// If the bootloader changes, you must change the jump table addresses! |
6 | 6 |
#define JT_ADDR 0x7FC |
7 | 7 |
#define JT_PARSE_PACKET ((void*)((JT_ADDR+0)/2)) |
8 |
#define JT_SEND_PACKET ((void*)((JT_ADDR+1)/2))
|
|
8 |
#define JT_SEND_PACKET ((void*)((JT_ADDR+2)/2))
|
|
9 | 9 |
|
10 | 10 |
#define JT_TYPE static __inline__ void |
11 | 11 |
typedef void (*PF_ARG1)(uint8_t); |
12 | 12 |
|
13 |
char (*parse_packet)(uint8_t*, uint8_t) = JT_PARSE_PACKET; |
|
13 |
char (*parse_packet)(uint8_t*, uint8_t*,uint8_t) = JT_PARSE_PACKET;
|
|
14 | 14 |
void (*send_packet)(uint8_t, uint8_t, uint8_t*, uint8_t) = JT_SEND_PACKET; |
15 | 15 |
|
16 | 16 |
#endif |
trunk/swipe/tooltron.py | ||
---|---|---|
81 | 81 |
TT_TO = 'f' |
82 | 82 |
TT_TIMEOUT = 't' |
83 | 83 |
TT_ON = 'o' |
84 |
TT_PING = 'g' |
|
84 | 85 |
|
86 |
MAX_PAYLOAD_LEN = 32 |
|
87 |
|
|
85 | 88 |
BAUD_RATE = 9600 |
86 | 89 |
|
87 | 90 |
#fails until a warning is sent |
... | ... | |
208 | 211 |
bus = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 2) |
209 | 212 |
bus.flushInput() |
210 | 213 |
print bus |
211 |
|
|
212 |
# ^ <src> <dest> <data> |
|
214 |
|
|
215 |
#tn is an actual tool number |
|
216 |
# ^ <src> <dest> <cmd> <plen> <payload> <crc> |
|
217 |
def sendMessage(tn, cmd, msg): |
|
218 |
if len(msg) > MAX_PAYLOAD_LEN: |
|
219 |
print "ERROR: message too long! max size is " + str(MAX_PAYLOAD_LEN) |
|
220 |
else: |
|
221 |
body = chr(1) + chr(tn) + cmd + chr(len(msg)) + msg |
|
222 |
crc = 0 |
|
223 |
for c in body: |
|
224 |
crc = crc ^ ord(c) |
|
225 |
msg = '^' + body + chr(crc) |
|
226 |
print "sending packet to tool",tn |
|
227 |
bus.write(msg) |
|
228 |
|
|
213 | 229 |
def sendTool(t): |
214 | 230 |
tn = keypad2toolID(t) |
215 |
msg = '^' + chr(1) + chr(tn) + TT_ON + chr(1 ^ tn ^ ord(TT_ON)) |
|
216 | 231 |
print "seding power to tool ID",tn |
217 |
bus.write(msg) |
|
218 |
return |
|
232 |
sendMessage(tn, TT_ON, "") |
|
219 | 233 |
|
220 | 234 |
def sendKeyRequest(): |
221 |
tn = 2 #2 is the ID for the cardbox |
|
222 |
msg = '^' + chr(1) + chr(tn) + TT_GET_KEY + chr(1 ^ tn ^ ord(TT_GET_KEY)) |
|
223 | 235 |
print "seding key request" |
224 |
bus.write(msg) |
|
225 |
return |
|
236 |
sendMessage(2, TT_GET_KEY, "") |
|
226 | 237 |
|
227 | 238 |
def sendAck(toolNum): |
228 |
msg = '^' + chr(1) + chr(toolNum) + TT_ACK + chr(1 ^ tn ^ ord(TT_ACK)) |
|
229 | 239 |
print "seding ACK to",toolNum |
230 |
bus.write(msg) |
|
231 |
return |
|
240 |
sendMessage(toolNum, TT_ACK, "") |
|
232 | 241 |
|
233 | 242 |
def sendNack(toolNum): |
234 |
msg = '^' + chr(1) + chr(toolNum) + TT_NACK + chr(1 ^ tn ^ ord(TT_NACK)) |
|
235 | 243 |
print "seding ACK to",toolNum |
236 |
bus.write(msg) |
|
237 |
return |
|
244 |
sendMessage(toolNum, TT_NACK, "") |
|
238 | 245 |
|
239 |
|
|
240 | 246 |
#returns key if the key_send packet is recived from tool ID 2 |
241 | 247 |
# otherwise returns 0 |
242 | 248 |
def readKey(): |
... | ... | |
251 | 257 |
packetType = bus.read(1) |
252 | 258 |
if packetType == 0: |
253 | 259 |
return 0 |
260 |
payloadLen = bus.read(1) |
|
261 |
if payloadLen != 1: |
|
262 |
print "ERROR: payload length wrong! Should be 1, was",payloadLen |
|
263 |
return 0 |
|
254 | 264 |
key = bus.read(1) |
255 | 265 |
if key == 0: |
256 | 266 |
return 0 |
... | ... | |
271 | 281 |
print "did not get start delim!: ", ord(startDelim) |
272 | 282 |
return 0 |
273 | 283 |
|
274 |
#returns [src, data] or [] on error
|
|
284 |
#returns [src, dest, command] or [] on error
|
|
275 | 285 |
def readTool(): |
276 | 286 |
ret = [0,0,0] |
277 | 287 |
|
... | ... | |
279 | 289 |
ret[0] = bus.read(1) |
280 | 290 |
ret[1] = bus.read(1) |
281 | 291 |
ret[2] = bus.read(1) |
292 |
plen = bus.read(1) |
|
293 |
if plen != chr(0): |
|
294 |
print "ERROR: tool responded with payload len",str(ord(plen)) |
|
295 |
bus.flushInput() |
|
282 | 296 |
x = bus.read(1) |
283 | 297 |
|
284 | 298 |
print "got packet",ret |
... | ... | |
298 | 312 |
return False |
299 | 313 |
|
300 | 314 |
|
301 |
return m[0] == chr(tn) and m[1] == chr(1) and m[2] == 'A'
|
|
315 |
return m[0] == chr(tn) and m[1] == chr(1) and m[2] == TT_ACK
|
|
302 | 316 |
|
303 | 317 |
|
304 | 318 |
while True: |
trunk/swipe/manualtron.py | ||
---|---|---|
1 |
#!/usr/bin/python -i |
|
2 |
|
|
3 |
""" |
|
4 |
This file is part of Tooltron. |
|
5 |
|
|
6 |
Tooltron is free software: you can redistribute it and/or modify |
|
7 |
it under the terms of the Lesser GNU General Public License as published by |
|
8 |
the Free Software Foundation, either version 3 of the License, or |
|
9 |
(at your option) any later version. |
|
10 |
|
|
11 |
Tooltron is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
Lesser GNU General Public License for more details. |
|
15 |
You should have received a copy of the Lesser GNU General Public License |
|
16 |
along with Tooltron. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
|
|
18 |
Copyright 2009 Bradford Neuman <bneuman@andrew.cmu.edu> |
|
19 |
|
|
20 |
""" |
|
21 |
|
|
22 |
# This allows you to interactively send crap to tooltron |
|
23 |
|
|
24 |
import re |
|
25 |
import sys |
|
26 |
import serial |
|
27 |
import MySQLdb |
|
28 |
import getpass |
|
29 |
from time import * |
|
30 |
|
|
31 |
keypadTimeout = 11 #in seconds |
|
32 |
|
|
33 |
TT_GET_KEY = 'k' |
|
34 |
TT_ACK = 'a' |
|
35 |
TT_NACK = 'n' |
|
36 |
TT_TO = 'f' |
|
37 |
TT_TIMEOUT = 't' |
|
38 |
TT_ON = 'o' |
|
39 |
TT_PING = 'g' |
|
40 |
|
|
41 |
MAX_PAYLOAD_LEN = 32 |
|
42 |
|
|
43 |
BAUD_RATE = 9600 |
|
44 |
|
|
45 |
#fails until a warning is sent |
|
46 |
MAX_TOOL_FAILS = 5 |
|
47 |
|
|
48 |
import time |
|
49 |
|
|
50 |
def printMsg(msg): |
|
51 |
if len(msg) < 5: |
|
52 |
print "ERROR: trying to print message thats too small!" |
|
53 |
return |
|
54 |
if msg[0] == '^': |
|
55 |
print '^', |
|
56 |
else: |
|
57 |
print str(ord(msg[0])) |
|
58 |
|
|
59 |
for c in msg[1:]: |
|
60 |
print str(ord(c)), |
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
if len(sys.argv) < 2: |
|
66 |
print "usage: manualtron.py /path/to/bus/device" |
|
67 |
|
|
68 |
else: |
|
69 |
|
|
70 |
bus = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 2) |
|
71 |
bus.flushInput() |
|
72 |
print bus |
|
73 |
|
|
74 |
#tn is an actual tool number |
|
75 |
# ^ <src> <dest> <cmd> <plen> <payload> <crc> |
|
76 |
def sendMessage(tn, cmd, msg): |
|
77 |
if len(msg) > MAX_PAYLOAD_LEN: |
|
78 |
print "ERROR: message too long! max size is " + str(MAX_PAYLOAD_LEN) |
|
79 |
else: |
|
80 |
body = chr(1) + chr(tn) + cmd + chr(len(msg)) + msg |
|
81 |
crc = 0 |
|
82 |
for c in body: |
|
83 |
crc = crc ^ ord(c) |
|
84 |
msg = '^' + body + chr(crc) |
|
85 |
print "sending packet to tool",tn |
|
86 |
printMsg(msg) |
|
87 |
bus.write(msg) |
|
88 |
|
|
89 |
def sendTool(t): |
|
90 |
tn = t |
|
91 |
print "seding power to tool ID",tn |
|
92 |
sendMessage(tn, TT_ON, "") |
|
93 |
|
|
94 |
def sendKeyRequest(): |
|
95 |
print "seding key request" |
|
96 |
sendMessage(2, TT_GET_KEY, "") |
|
97 |
|
|
98 |
def sendAck(toolNum): |
|
99 |
print "seding ACK to",toolNum |
|
100 |
sendMessage(toolNum, TT_ACK, "") |
|
101 |
|
|
102 |
def sendNack(toolNum): |
|
103 |
print "seding ACK to",toolNum |
|
104 |
sendMessage(toolNum, TT_NACK, "") |
|
105 |
|
|
106 |
#returns key if the key_send packet is recived from tool ID 2 |
|
107 |
# otherwise returns 0 |
|
108 |
def readKey(): |
|
109 |
startDelim = bus.read(1) |
|
110 |
if startDelim == '^': |
|
111 |
src = bus.read(1) |
|
112 |
if src == 0: |
|
113 |
return 0 |
|
114 |
dest = bus.read(1) |
|
115 |
if dest == 0: |
|
116 |
return 0 |
|
117 |
packetType = bus.read(1) |
|
118 |
if packetType == 0: |
|
119 |
return 0 |
|
120 |
payloadLen = bus.read(1) |
|
121 |
if payloadLen != 1: |
|
122 |
print "ERROR: payload length wrong! Should be 1, was",payloadLen |
|
123 |
return 0 |
|
124 |
key = bus.read(1) |
|
125 |
if key == 0: |
|
126 |
return 0 |
|
127 |
crc = bus.read(1) |
|
128 |
|
|
129 |
print "got packet" |
|
130 |
if src != 2 or dest != 1 or packetType != TT_KEY_SEND: |
|
131 |
print "Bad packet! ^",src,dest,packetType,key,crc |
|
132 |
|
|
133 |
if chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key)) == crc: |
|
134 |
return key |
|
135 |
else: |
|
136 |
print "xor fail. got", crc, "should have been got",(chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key))) |
|
137 |
|
|
138 |
return 0 |
|
139 |
|
|
140 |
else: |
|
141 |
print "did not get start delim!: ", ord(startDelim) |
|
142 |
return 0 |
|
143 |
|
|
144 |
#returns [src, dest, command] or [] on error |
|
145 |
def readTool(): |
|
146 |
ret = [0,0,0] |
|
147 |
|
|
148 |
if bus.read(1) == '^': |
|
149 |
ret[0] = bus.read(1) |
|
150 |
ret[1] = bus.read(1) |
|
151 |
ret[2] = bus.read(1) |
|
152 |
plen = bus.read(1) |
|
153 |
if plen != chr(0): |
|
154 |
print "ERROR: tool responded with payload len",str(ord(plen)) |
|
155 |
bus.flushInput() |
|
156 |
x = bus.read(1) |
|
157 |
|
|
158 |
print "got packet",ret |
|
159 |
|
|
160 |
if chr(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) == x: |
|
161 |
return ret |
|
162 |
else: |
|
163 |
print "xor fail. got", x, "should have been got",(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) |
|
164 |
|
|
165 |
return [] |
|
166 |
|
|
167 |
def checkAck(t): |
|
168 |
tn = t |
|
169 |
m = readTool() |
|
170 |
|
|
171 |
if m== []: |
|
172 |
return False |
|
173 |
|
|
174 |
|
|
175 |
return m[0] == chr(tn) and m[1] == chr(1) and m[2] == TT_ACK |
|
0 | 176 |
trunk/programmer/test/main.c | ||
---|---|---|
112 | 112 |
PORTB = 0x7; |
113 | 113 |
|
114 | 114 |
myaddr = eeprom_read_byte(1); |
115 |
if (myaddr == 11) {
|
|
115 |
if (myaddr == 15) {
|
|
116 | 116 |
PORTB = 0; |
117 |
while(1); |
|
117 |
while(1) { |
|
118 |
_delay_ms(1000); |
|
119 |
PORTB |= _BV(PORTB2); |
|
120 |
_delay_ms(1000); |
|
121 |
PORTB &= ~_BV(PORTB2); |
|
122 |
} |
|
118 | 123 |
} |
119 | 124 |
_delay_ms(1000); |
120 | 125 |
PORTB |= _BV(PORTB0); |
trunk/programmer/netprog.py | ||
---|---|---|
9 | 9 |
TT_BAD = 0 |
10 | 10 |
TT_PROGD = 'd' |
11 | 11 |
TT_RESET = 'r' |
12 |
TT_PING = 'g' |
|
12 | 13 |
|
13 | 14 |
MAX_PROG_SIZE = 1024-32 |
14 | 15 |
|
... | ... | |
16 | 17 |
|
17 | 18 |
BAUD_RATE = 9600 |
18 | 19 |
|
19 |
MAX_DATA_LEN = 32 |
|
20 |
MAX_PAYLOAD_LEN = 32 |
|
21 |
MAX_DATA_LEN = MAX_PAYLOAD_LEN |
|
20 | 22 |
|
21 | 23 |
if len(sys.argv) < 4: |
22 | 24 |
print "usage: netprog.py /path/to/bus/device toolnum binaryfile" |
... | ... | |
65 | 67 |
|
66 | 68 |
|
67 | 69 |
|
70 |
# ^ <src> <dest> <cmd> <plen> <payload> <crc> |
|
71 |
def sendMessage(tn, cmd, msg): |
|
72 |
if len(msg) > MAX_PAYLOAD_LEN: |
|
73 |
print "ERROR: message too long! max size is " + str(MAX_PAYLOAD_LEN) |
|
74 |
else: |
|
75 |
body = chr(1) + chr(tn) + cmd + chr(len(msg)) + msg |
|
76 |
crc = 0 |
|
77 |
for c in body: |
|
78 |
crc = crc ^ ord(c) |
|
79 |
msg = '^' + body + chr(crc) |
|
80 |
print "sending packet to tool",tn,":" |
|
81 |
printMsg(msg) |
|
82 |
bus.write(msg) |
|
83 |
|
|
68 | 84 |
def sendCommand(t, cmd): |
69 |
msg = '^' + chr(1) + chr(t) + cmd + chr(1 ^ t ^ ord(cmd)) |
|
70 |
print "sending..." |
|
71 |
printMsg(msg) |
|
72 |
bus.write(msg) |
|
85 |
sendMessage(t,cmd,"") |
|
73 | 86 |
|
74 | 87 |
def sendProgram(t, size): |
75 |
if size > 2**16:
|
|
88 |
if size > 65535:
|
|
76 | 89 |
print "ERROR: program too big!!" |
77 | 90 |
|
78 | 91 |
def lsb(s): return s & 0xFF |
79 | 92 |
def msb(s): return (s>>8) & 0xFF |
80 | 93 |
|
81 |
body = chr(1) + chr(t) + TT_PROGRAM_MODE + chr(lsb(size)) + chr(msb(size)) |
|
82 |
print 1,t,TT_PROGRAM_MODE,lsb(size),msb(size), |
|
83 |
xor = 0 |
|
84 |
for c in body: |
|
85 |
xor = xor ^ ord(c) |
|
86 |
print xor |
|
87 |
|
|
88 |
msg = '^' + body + chr(xor) |
|
89 | 94 |
print "sending P" |
90 |
printMsg(msg) |
|
91 |
bus.write(msg) |
|
95 |
sendMessage(t,TT_PROGRAM_MODE,chr(lsb(size))+chr(msb(size))) |
|
92 | 96 |
|
93 | 97 |
dCount = 0 |
94 | 98 |
|
95 | 99 |
def sendData(t, data): |
96 |
body = chr(1) + chr(t) + TT_PROGD + data
|
|
100 |
#PROGD messages must be 32 bytes, so expand if neccesary
|
|
97 | 101 |
n = len(data) |
98 | 102 |
while n < MAX_DATA_LEN: |
99 |
body = body + chr(0)
|
|
103 |
data = data + chr(0)
|
|
100 | 104 |
n = n + 1 |
101 |
xor = 0 |
|
102 |
for c in body: |
|
103 |
xor = xor ^ ord(c) |
|
104 |
msg = '^' + body + chr(xor) |
|
105 |
printMsg(msg) |
|
106 |
bus.write(msg) |
|
105 |
|
|
106 |
sendMessage(t,TT_PROGD,data) |
|
107 | 107 |
print '.', |
108 | 108 |
sys.stdout.flush() |
109 | 109 |
|
110 |
#NOTE: does not handle nonzero payloads |
|
110 | 111 |
def readPacket(): |
111 |
msg = bus.read(5)
|
|
112 |
if len(msg) < 5:
|
|
112 |
msg = bus.read(6)
|
|
113 |
if len(msg) < 6:
|
|
113 | 114 |
print "message too short! len =",len(msg) |
114 | 115 |
if len(msg) == 0: |
115 | 116 |
print "probably a timeout" |
... | ... | |
128 | 129 |
print "packet dest not 1!" |
129 | 130 |
return TT_BAD |
130 | 131 |
cmd = msg[3] |
131 |
if msg[4] != chr(ord(src) ^ 1 ^ ord(msg[3])): |
|
132 |
if msg[4] != chr(0): |
|
133 |
print "ERROR: readPacket got nonzero payload lengh",str(msg[4]) |
|
134 |
bus.flushInput() |
|
135 |
return TT_BAD |
|
136 |
if msg[5] != chr(ord(src) ^ 1 ^ ord(msg[3])): |
|
132 | 137 |
print "checksum was wrong!" |
133 | 138 |
print ord(msg[0]), ord(msg[1]), ord(msg[2]), ord(msg[3]), ord(msg[4]) |
134 | 139 |
print (ord(src) ^ 1 ^ ord(msg[3])) |
Also available in: Unified diff