-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcal
executable file
·121 lines (97 loc) · 3.04 KB
/
cal
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
#!/usr/bin/env python
# Compile And Load a program into a GA144 and listen for output
# This script was originally based on work by James Bowman
# https://github.com/jamesbowman
#
from sys import argv
import subprocess
from subprocess import call
from serial import Serial
import time
from struct import unpack
def write(bs, serial):
# reboot by dropping RTS
if target:
target.setRTS(0)
target.setRTS(1)
serial.setRTS(0)
serial.setRTS(1)
serial.write(bs)
serial.flush()
#18bit words transmitted using 4 bytes with format:
# upper2 middle8 lower8 wordcode
#
# wordcodes:
# 0: receive 18 bit word
# 1: exit
def listen(port, speed, serial, verbose=True):
if verbose:
print "Listening. port={}, speed={}".format(port, speed)
def read_n( n ):
x = [ord(serial.read( 1 )) for _ in range( n ) ]
x.reverse()
word = 0
for byte in x:
word = ( word << 8 ) | byte
n -= 1
return word
while True:
n = read_n( 1 )
if n == 1:
print "[exit]"
return
if n == 0:
n = read_n( 3 )
print n & 0x3ffff
else:
print "ERROR -- unknown code:", s
def run_command(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
x = p.communicate()[0]
#print x
return eval(x)
default_speed = 460800
default_bootstream = 'async'
bootstream_types = [ "async", "2wire", "async-target" ]
def print_usage_and_exist():
print "usage: ./cal filename port [--speed (default: {})] [--bootstream (default: {})] [--no-listen]".format(default_speed, default_bootstream)
exit(1)
if __name__ == "__main__":
#speed = 921600
if len(argv) not in [3, 4, 5, 7]:
print_usage_and_exist()
filename = argv[1]
port = argv[2]
speed = default_speed
bootstream = default_bootstream
argv = argv[3:]
serial_listen = True
if "--no-listen" in argv:
arg.remove("--no-listen")
serial_listen = False
if not (len(argv) % 2 == 0):
print_usage_and_exist()
while argv:
arg = argv.pop(0)
if arg == "--speed" or arg == "-s":
speed = int(argv.pop(0))
elif arg == "--bootstream" or arg == "-b":
bootstream_type = argv.pop(0)
if bootstream_type not in bootstream_types:
print "invalid bootstream type:", bootstream_type
exit(1)
else:
print "invalid option: ", arg
cmd = "racket dump --bootstream {} {}".format(bootstream_type, filename)
compiled = run_command(cmd)
bootstream = compiled['bootstream']
print "Writing bootstream ({} bytes)...".format(len(bootstream))
host = Serial(port, speed)
host.reset_input_buffer()
# target chip serial for resetting
target = None #Serial("/dev/ttyUSB4", 921600)
write("".join(map(chr, bootstream)), host)
if serial_listen:
listen(port, speed, host)
host.close()
# ./cal test.aforth /dev/ttyUSB3 --speed 460800 --bootstream async-target