Project

General

Profile

Statistics
| Revision:

root / trunk / programmer / netprog.py @ 303

History | View | Annotate | Download (3.64 KB)

1
#!/usr/bin/python
2
import sys
3
import serial
4
import sys
5
sys.path.append('../common')
6
import common
7

    
8
MAX_PROG_SIZE = 1024-32
9

    
10
DUMB_SERIAL = 0
11

    
12
MAX_DATA_LEN = common.MAX_PAYLOAD_LEN
13

    
14
if len(sys.argv) < 4:
15
    print "usage: netprog.py /path/to/bus/device toolnum binaryfile"
16
    exit(1)
17

    
18
#for testing, tool number 3 hardcoded
19
class dumbBus (serial.Serial):
20
    def __init__(self):
21
        self.step = 0
22
    def flushInput(self):
23
        pass
24
    def write(self, s):
25
        print '>',
26
    def read(self, n):
27
        if n == 5:
28
            if self.step == 0:
29
                #print ord('^'), 3,1,ord(common.TT_BOOT), (3 ^ 1 ^ ord(common.TT_BOOT))
30
                print '<',
31
                self.step += 1
32
                return '^' + chr(3) + chr(1) + common.TT_BOOT + chr(3 ^ 1 ^ ord(common.TT_BOOT))
33
            else:
34
                #print ord('^'), 3,1,ord(common.TT_ACK), (3 ^ 1 ^ ord(common.TT_ACK))
35
                print '<',
36
                return '^' + chr(3) + chr(1) + common.TT_ACK + chr(3 ^ 1 ^ ord(common.TT_ACK))
37
        else:
38
            return 0
39

    
40
common.initBus(sys.argv[1], timeout=10)
41

    
42
def sendCommand(t, cmd):
43
    common.sendMessage(t,cmd,"")
44

    
45
def sendProgram(t, size):
46
    if size > 65535:
47
        print "ERROR: program too big!!"
48

    
49
    def lsb(s): return s & 0xFF
50
    def msb(s): return (s>>8) & 0xFF
51

    
52
    print "sending P"
53
    common.sendMessage(t,common.TT_PROGRAM_MODE,chr(lsb(size))+chr(msb(size)))
54

    
55
dCount = 0
56

    
57
def sendData(t, data):
58
    #PROGD messages must be 32 bytes, so expand if neccesary
59
    n = len(data)
60
    while n < MAX_DATA_LEN:
61
        data = data + chr(0)
62
        n = n + 1
63

    
64
    common.sendMessage(t,common.TT_PROGD,data)
65
    print '.',
66
    sys.stdout.flush()
67

    
68
toolID = int(sys.argv[2])
69

    
70
print "sending file: ", sys.argv[3], " to tool ", toolID
71

    
72
fin = open(sys.argv[3], "r")
73
fin.seek(0,2)
74
flen = fin.tell()
75
fin.seek(0,0)
76

    
77
if flen > MAX_PROG_SIZE:
78
    print "Program too Large!"
79
    print "prog size:",flen
80
    print "max size:",MAX_PROG_SIZE
81
    exit(-1)
82

    
83
print "prog size:",flen
84

    
85
print "sending RESET"
86
sendCommand(toolID, common.TT_RESET)
87

    
88
print "waiting for boot packet..."
89

    
90
m = common.readMessage()
91

    
92
if m == None:
93
    print "Didn't get a packet (timeout?), bailing out"
94
    exit(-4)
95

    
96
[src,dest,cmd,data] = m
97
if cmd != common.TT_BOOT:
98
    print "Didn't get a boot packet, bailing out"
99
    print "got", cmd
100
    exit(-4)
101
else:
102
    print "got boot packet from " + str(ord(src))
103

    
104
r = 0
105
while r != common.TT_ACK:
106
    sendProgram(toolID, flen)
107
    m = common.readMessage()
108
    if m == None:
109
        print "ERROR: probably a timeout"
110
        exit(-1)
111
    [src, dest, r, data] = m
112
    if r == common.TT_NACK:
113
        print "WARNING: got NACK, trying again"
114
    elif r == common.TT_BOOT:
115
        print "ERROR: got stray boot packet"
116
        #TODO: restart the whole state machine after the boot packet
117
        exit(-2)
118
    elif r != common.TT_ACK:
119
        print "ERROR: got another packet:",r
120
        exit(-1)
121

    
122
print 'sending data',
123
sys.stdout.flush()
124
while True:
125
    data = fin.read(MAX_DATA_LEN)
126
    #print data
127
    #print '-------------------'
128
    if len(data) == 0:
129
        break
130

    
131
    r = 0
132
    while r != common.TT_ACK:
133
        sendData(toolID, data)
134
        m = common.readMessage()
135
        if m == None:
136
            print "ERROR: probably a timeout"
137
            exit(-1)
138
        [src, dest, r, data] = m
139

    
140
        if r == common.TT_NACK:
141
            print "WARNING: got NACK, trying again"
142
        elif r == common.TT_BOOT:
143
            print "ERROR: got stray boot packet from "+str(ord(src))
144
            #TODO: restart the whole state machine after the boot packet
145
            exit(-2)
146
        elif r != common.TT_ACK:
147
            print "ERROR: got another packet:",r
148
            exit(-1)