Project

General

Profile

Revision 157

first draft of network programmer, no error checking!

View differences:

trunk/programmer/netprog.py
1
#!/usr/bin/python
2
import sys
3
import serial
4

  
5
TT_BOOT = 'b'
6
TT_PROGRAM_MODE = 'p'
7
TT_ACK     = 'a'
8
TT_NACK    = 'n'
9
TT_BAD = 0
10

  
11
MAX_DATA_LEN = 32
12

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

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

  
39
bus = dumbBus() #serial.Serial(sys.argv[1], BAUD_RATE, timeout = 0) 
40
bus.flushInput()
41
#print bus
42

  
43
def sendCommand(t, cmd):
44
    msg = '^' + chr(1) + chr(t) + cmd + chr(1 ^ t ^ ord(cmd))
45
    print "sending..."
46
    bus.write(msg)
47

  
48
def sendProgram(t, size):
49
    if size > 2**16:
50
        print "ERROR: program too big!!"
51

  
52
    def lsb(s): return s & 0xFF
53
    def msb(s): return (s>>8) & 0xFF
54

  
55
    body = chr(1) + chr(t) + TT_PROGRAM_MODE + chr(lsb(size)) + chr(msb(size))
56
    xor = 0
57
    for c in body[1:]:
58
        xor = xor ^ ord(c)
59

  
60
    msg = '^' + body + chr(xor)
61
    print "sending P"
62
    bus.write(msg)
63

  
64
def sendData(t, data):
65
    body = chr(1) + chr(t) + data
66
    xor = 0
67
    for c in body[1:]:
68
        xor = xor ^ ord(c)
69

  
70
    msg = '^' + body + chr(xor)
71
    print "sending data"
72
    bus.write(msg)
73

  
74
def readPacket():
75
    msg = bus.read(5)
76
    if msg[0] != '^':
77
        print "did not get start delimeter!"
78
        return TT_BAD
79
    src = msg[1]
80
    #print "src: ", ord(src)
81
    if msg[2] != chr(1):
82
        print "packet dest not 1!"
83
        return TT_BAD
84
    cmd = msg[3]
85
    if msg[4] != chr(ord(src) ^ 1 ^ ord(msg[3])):
86
        print "checksum was wrong!"
87
        print ord(msg[0]), ord(msg[1]), ord(msg[2]), ord(msg[3]), ord(msg[4])
88
        print (ord(src) ^ 1 ^ ord(msg[3]))
89
        return TT_BAD
90

  
91
    return cmd
92

  
93
toolID = int(sys.argv[2])
94

  
95
print "sending file: ", sys.argv[3], " to tool ", toolID
96

  
97
fin = open(sys.argv[3], "r")
98
fin.seek(0,2)
99
flen = fin.tell()
100
fin.seek(0,0)
101

  
102
print "waiting for boot packet..."
103
while readPacket() != TT_BOOT:
104
    print "got some other byte ???"
105
    exit(-4)
106

  
107
sendProgram(toolID, flen)
108
if readPacket() != TT_ACK:
109
    print "ERROR: tool did not ACK!!!"
110
    exit(-1)
111

  
112
while True:
113
    data = fin.read(MAX_DATA_LEN)
114
    #print data
115
    #print '-------------------'
116

  
117
    sendData(toolID, data)
118
    if readPacket() != TT_ACK:
119
        print "last data packet not ACK'd!!"
120
        break
121

  
122
    if len(data) < MAX_DATA_LEN:
123
        break
0 124

  

Also available in: Unified diff