-
Notifications
You must be signed in to change notification settings - Fork 31
/
send_xsvf
executable file
·138 lines (120 loc) · 3.12 KB
/
send_xsvf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
#
# The JTAG Whisperer https://github.com/sowbug/JTAGWhisperer
# Copyright 2012 Mike Tsao http://www.sowbug.com/
#
# See LICENSE file for BSD-style licensing information.
#
import binascii
from time import time
import getopt
import sys
try:
import serial
except ImportError:
print """
You don't have pyserial installed. Please install it and try again.
"easy_install pyserial" or "pip install pyserial" will probably work.
"""
total_bytes_sent = 0
need_lf = False
def send_xsvf_chunk(s, xsvf):
global total_bytes_sent, need_lf
if len(xsvf) == 0:
print 'Problem: tried to send empty xsvf.'
return xsvf
chunk = xsvf[:32]
xsvf = xsvf[len(chunk):]
bytes_to_write = len(chunk)
while bytes_to_write > 0:
bytes_written = s.write(chunk)
total_bytes_sent += bytes_written
chunk = chunk[bytes_written:]
bytes_to_write -= bytes_written
print '\rSent: %8d bytes, %8d remaining' % (total_bytes_sent, len(xsvf)),
need_lf = True
sys.stdout.flush()
return xsvf
def get_xsvf_checksum(xsvf):
xsvf_sum = 0
for c in xsvf:
xsvf_sum += ord(c)
return (xsvf_sum, len(xsvf))
def maybe_print_lf():
global need_lf
if need_lf:
need_lf = False
print
def program(xsvf_filename, port, bps):
global total_bytes_sent
start_time = 0
f = open(xsvf_filename, 'rb')
xsvf = f.read()
f.close()
print 'Ready to send file of size %d bytes.' % (len(xsvf))
(xsvf_sum, xsvf_len) = get_xsvf_checksum(xsvf)
s = serial.Serial(port=port, baudrate=bps, rtscts=True)
s.flushInput()
s.flushOutput()
is_device_ready = False
while True:
line = s.readline().strip()
if len(line) == 0:
continue
command = line[0]
text = line[1:].strip()
if command == 'R':
if text == 'XSVF':
if not is_device_ready:
is_device_ready = True
print 'Device is ready.'
start_time = time()
continue
elif text == 'SEND':
xsvf = send_xsvf_chunk(s, xsvf)
continue
else:
print 'Unrecognized ready command:', text
elif command == 'Q':
maybe_print_lf()
print 'Received device quit:', text
break
elif command == 'D':
maybe_print_lf()
print 'Device:', text
elif command == '!':
maybe_print_lf()
print 'IMPORTANT:', text
else:
maybe_print_lf()
print 'Unrecognized line:', line
s.close()
print 'Expected checksum: %lx/%lx.' % (xsvf_sum, xsvf_len)
if start_time > 0:
print 'Elapsed time: %.02f seconds.' % (time() - start_time)
return 0
def usage():
print 'send_xsvf [-b bps] -p /dev/your_arduino_serial_port xsvf_filename'
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'b:p:', ['bps=', 'port='])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
xsvf_filename = args[0]
port = None
bps = 115200
for o, a in opts:
if o in ['-b', '--bps']:
bps = int(a)
elif o in ['-p', '--port']:
port = a
else:
assert False, 'unhandled option'
if port is None:
usage()
sys.exit(2)
sys.exit(program(xsvf_filename, port, bps))
if __name__ == '__main__':
main()