Project

General

Profile

Statistics
| Revision:

root / trunk / programmer / netprog.py @ 225

History | View | Annotate | Download (3.55 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])
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

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

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

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

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