Mercurial > avr_jtag
annotate py_avrjtag/cmd_proto.py @ 8:074e860d7d31
jtagdev is a desktop client to communicate with jtag device through Arduino.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 22 Feb 2009 22:30:20 +0800 |
parents | 61f27549de57 |
children | cc106f278d7d |
rev | line source |
---|---|
4
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
1 import struct |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
2 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
3 CPCMD_PING = 1 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
4 CPCMD_SHIFT_TDI = 2 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
5 CPCMD_SHIFT_TMS = 3 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
6 CPCMD_SHIFT_TDI_TDO = 4 |
7 | 7 CPCMD_TRST = 5 |
8 CPCMD_PONG = 6 | |
9 CPCMD_DATA = 7 | |
10 CPCMD_ACK = 8 | |
11 CPCMD_NAK = 9 | |
4
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
12 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 def csum_add(csum, c): |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
14 return (((csum << 3) | (csum >> 5)) ^ c) & 0xff |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
15 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
16 class cmd(object): |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
17 OFF_SEQ = 2 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
18 OFF_DSZ = 3 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 OFF_CODE = 4 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
20 FRAME_OVERHEAD = 5 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
21 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
22 def __init__(self, seq=0, code=0, data=''): |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
23 self.seq = seq |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
24 self.code = code |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
25 self.data = data |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
26 pass |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
27 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
28 def to_frame(self): |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
29 csum = 0 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
30 csum = csum_add(0, self.code) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
31 for c in self.data: |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
32 csum = csum_add(csum, ord(c)) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
33 pass |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
34 csum = csum_add(csum, 0) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 frame = struct.pack('BBBBB', ord('J'), ord('C'), self.seq, |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 len(self.data) + 1, self.code) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 frame = frame + self.data + chr(csum) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 return frame |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 def from_frame(self, frame): |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
42 if frame[:2] != 'JC': |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
43 return -1 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
45 csum = 0 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
46 for c in frame[self.OFF_CODE:]: |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
47 csum = csum_add(csum, ord(c)) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
48 pass |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
49 if csum: |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
50 return -1 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
51 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
52 data_sz = ord(frame[self.OFF_DSZ]) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
53 if data_sz != (len(frame) - self.FRAME_OVERHEAD): |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
54 return -1 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
55 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
56 if not data_sz: |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
57 return -1 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
58 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
59 self.seq = ord(frame[self.OFF_SEQ]) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
60 self.code = ord(frame[self.OFF_CODE]) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
61 self.data = frame[self.OFF_CODE + 1:-1] |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
62 pass |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
63 |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
64 def __repr__(self): |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
65 return '<%s {seq: %d, code: %d, data: %s}>' % \ |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
66 (self.__class__.__name__, self.seq, self.code, repr(self.data)) |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
67 pass |
6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
68 |
8
074e860d7d31
jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
7
diff
changeset
|
69 def prepend_nbits(nbits, data): |
074e860d7d31
jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
7
diff
changeset
|
70 r = chr(nbits & 0xff) + chr(nbits >> 8) + data |
074e860d7d31
jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
7
diff
changeset
|
71 return r |