annotate py_avrjtag/jtagdev.py @ 11:520f45b72ba7

Add debug_frame flag to enable print-out frame when received
author Thinker K.F. Li <thinker@branda.to>
date Tue, 24 Feb 2009 14:33:23 +0800
parents cc106f278d7d
children 68ecd42850d3
rev   line source
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 import tms_ptns
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 import cmd_proto
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3
11
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
4 debug_frame = 0
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
5
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 class jtagdev(object):
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
7 ST_RESET = 0
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 ST_IDLE = 1
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 ST_SHIFT_DR = 2
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 ST_SHIFT_IR = 3
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
11 ST_EXIT1_DR = 4
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
12 ST_EXIT1_IR = 5
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 def __init__(self, fo):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 self.fo = fo
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
16 self.state = self.ST_IDLE
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 self.seq = 0
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 def _send_cmd(self, code, data):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 cmd = cmd_proto.cmd(self.seq, code, data)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 frame = cmd.to_frame()
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 self.fo.write(frame)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 self.fo.flush()
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 def _send_nbits_data_cmd(self, code, nbits, data):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 nbits_data = cmd_proto.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:
diff changeset
30 self._send_cmd(code, nbits_data)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 def _send_tms(self, ptn, nbits):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 self._send_nbits_data_cmd(cmd_proto.CPCMD_SHIFT_TMS,
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 nbits, ptn)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 def wait_reply(self, tmo=None):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 import select
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
41 frame = ''
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
42 while True:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
43 if tmo == None:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
44 rlist, wlist, xlist = select.select((self.fo,), (), ())
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
45 else:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
46 rlist, wlist, xlist = select.select((self.fo,), (), (), tmo)
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
47 pass
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
48 if not rlist:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
49 break
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
50 frame = frame + self.fo.read()
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
51 if len(frame) >= 4 and \
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
52 len(frame) >= (ord(frame[3]) +
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
53 cmd_proto.cmd.FRAME_OVERHEAD):
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
54 break
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
55 tmo = 0.05
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 pass
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
57
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
58 if not frame:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
59 return None
11
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
60 if debug_frame:
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
61 print repr(frame)
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
62 pass
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
63
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 cmd = cmd_proto.cmd()
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
65 r = cmd.from_frame(frame)
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
66 if r:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
67 return None
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 return cmd
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 def idle(self):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 self.seq = (self.seq + 1) % 256
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
73 if self.state == self.ST_RESET:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
74 ptn, nbits = tms_ptns.TMS_RESET_2_IDLE
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
75 else:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
76 ptn, nbits = tms_ptns.TMS_IDLE_SEQ
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
77 pass
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
78 self._send_tms(ptn, nbits)
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 self.state = self.ST_IDLE
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 def go_shift_IR(self):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 if self.state == self.ST_SHIFT_IR:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 return
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 if self.state == self.ST_IDLE:
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
88 ptn, ptn_nbits = tms_ptns.TMS_SHIFT_IR
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
89 self._send_tms(ptn, ptn_nbits)
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
90 elif self.state == self.ST_EXIT1_DR:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
91 ptn, ptn_nbits = tms_ptns.TMS_EXIT1_DR_2_SHIFT_IR
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
92 self._send_tms(ptn, ptn_nbits)
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 else:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 raise RuntimeError, 'Transite to shift IR state from invalid state'
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 self.state = self.ST_SHIFT_IR
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 def go_shift_DR(self):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 if self.state == self.ST_SHIFT_DR:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 return
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 if self.state == self.ST_IDLE:
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
103 ptn, ptn_nbits = tms_ptns.TMS_SHIFT_DR
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
104 self._send_tms(ptn, ptn_nbits)
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
105 elif self.state == self.ST_EXIT1_IR:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
106 ptn, ptn_nbits = tms_ptns.TMS_EXIT1_IR_2_SHIFT_DR
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
107 self._send_tms(ptn, ptn_nbits)
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 else:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109 raise RuntimeError, 'Transite to shift DR state from invalid state'
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 self.state = self.ST_SHIFT_DR
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114 def shift_IR(self, data, nbits):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 self.seq = (self.seq + 1) % 256
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 if self.state != self.ST_SHIFT_IR:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 raise RuntimeError, 'Invalid state'
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 self._send_nbits_data_cmd(cmd_proto.CPCMD_SHIFT_TDI,
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 nbits, data)
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
122 self.state = self.ST_EXIT1_IR
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124
11
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
125 def shift_IR_n_out(self, data, nbits):
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
126 self.seq = (self.seq + 1) % 256
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
127
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
128 if self.state != self.ST_SHIFT_IR:
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
129 raise RuntimeError, 'Invalid state'
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
130
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
131 self._send_nbits_data_cmd(cmd_proto.CPCMD_SHIFT_TDI_TDO,
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
132 nbits, data)
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
133 self.state = self.ST_EXIT1_IR
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
134 pass
520f45b72ba7 Add debug_frame flag to enable print-out frame when received
Thinker K.F. Li <thinker@branda.to>
parents: 10
diff changeset
135
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
136 def shift_DR(self, data, nbits):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
137 self.seq = (self.seq + 1) % 256
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
139 if self.state != self.ST_SHIFT_DR:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140 raise RuntimeError, 'Invalid state'
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
141
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
142 self._send_nbits_data_cmd(cmd_proto.CPCMD_SHIFT_TDI,
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
143 nbits, data)
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
144 self.state = self.ST_EXIT1_DR
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
145 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
146
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
147 def shift_DR_n_out(self, data, nbits):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
148 self.seq = (self.seq + 1) % 256
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
149
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
150 if self.state != self.ST_SHIFT_DR:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
151 raise RuntimeError, 'Invalid state'
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
152
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
153 self._send_nbits_data_cmd(cmd_proto.CPCMD_SHIFT_TDI_TDO,
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
154 nbits, data)
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
155 self.state = self.ST_EXIT1_DR
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
156 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
157
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
158 def reset(self):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
159 self._send_cmd(cmd_proto.CPCMD_TRST, '')
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
160 self.state = self.ST_RESET
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
161 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
162 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
163
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
164 def _extract_nbits_data(nbits_data):
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
165 nbits = ord(nbits_data[0]) | (ord(nbits_data[1]) << 8)
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
166 return nbits, nbits_data[2:]
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
167
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
168 def _get_bit(data, bit_idx):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
169 byte_off = bit_idx / 8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
170 bit_off = bit_idx % 8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
171 return (ord(data[byte_off]) >> bit_off) & 0x1
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173 def identify_components(dev):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
174 dev.go_shift_IR()
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
175 reply = dev.wait_reply()
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
176 if reply.code != cmd_proto.CPCMD_ACK:
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
177 raise RuntimeError, 'invalid reply code 0x%02x' % (reply.code)
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
178
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
179 dev.shift_IR('\x02', 4)
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
180 reply = dev.wait_reply()
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
181 if reply.code != cmd_proto.CPCMD_ACK:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
182 raise RuntimeError, 'invalid reply code 0x%02x' % (reply.code)
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
183
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
184 dev.go_shift_DR()
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
185 reply = dev.wait_reply()
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
186 if reply.code != cmd_proto.CPCMD_ACK:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
187 raise RuntimeError, 'invalid reply code 0x%02x' % (reply.code)
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
188
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
189 dev.shift_DR_n_out('\xff' * 252, 252 * 8)
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
190 reply = dev.wait_reply()
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
191 if not reply:
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
192 raise RuntimeError, 'Invalid replied message'
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
193 if reply.code != cmd_proto.CPCMD_DATA:
10
cc106f278d7d Get identify of components
Thinker K.F. Li <thinker@branda.to>
parents: 8
diff changeset
194 raise RuntimeError, 'invalid replied code 0x%02x %s' % (reply.code, repr(reply))
8
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
195
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
196 nbits, data = _extract_nbits_data(reply.data)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
197
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
198 components = []
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
199 i = 0
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
200 while i < nbits:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
201 bit = _get_bit(data, i)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
202 if bit == 0:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
203 components.append(0)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
204 i = i + 1
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
205 else:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
206 comp_id = 0
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
207 for j in range(32):
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
208 bit = _get_bit(data, i + j)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
209 comp_id = comp_id | (bit << j)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
210 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
211 if comp_id == 0xffffffff:
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
212 break
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
213 components.append(comp_id)
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
214 i = i + 32
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
215 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
216 pass
074e860d7d31 jtagdev is a desktop client to communicate with jtag device through Arduino.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
217 return components