]> sigrok.org Git - libsigrokdecode.git/blame - decoders/jtag/pd.py
i2c: Add Python tests.
[libsigrokdecode.git] / decoders / jtag / pd.py
CommitLineData
557a143d 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
557a143d
UH
3##
4## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 2 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
21# JTAG protocol decoder
22
23import sigrokdecode as srd
24
25class Decoder(srd.Decoder):
26 api_version = 1
27 id = 'jtag'
28 name = 'JTAG'
b7a7e6f5 29 longname = 'Joint Test Action Group (IEEE 1149.1)'
6e7a0087 30 desc = 'Protocol for testing, debugging, and flashing ICs.'
557a143d
UH
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = ['jtag']
34 probes = [
35 {'id': 'tdi', 'name': 'TDI', 'desc': 'Test data input'},
36 {'id': 'tdo', 'name': 'TDO', 'desc': 'Test data output'},
37 {'id': 'tck', 'name': 'TCK', 'desc': 'Test clock'},
38 {'id': 'tms', 'name': 'TMS', 'desc': 'Test mode select'},
557a143d 39 ]
6d990fe1 40 optional_probes = [
1cc590f7
UH
41 {'id': 'trst', 'name': 'TRST#', 'desc': 'Test reset'},
42 {'id': 'srst', 'name': 'SRST#', 'desc': 'System reset'},
43 {'id': 'rtck', 'name': 'RTCK', 'desc': 'Return clock signal'},
6d990fe1 44 ]
557a143d
UH
45 options = {}
46 annotations = [
ee3e279c 47 ['Text', 'Human-readable text'],
557a143d
UH
48 ]
49
50 def __init__(self, **kwargs):
e5edf39f
UH
51 # self.state = 'TEST-LOGIC-RESET'
52 self.state = 'RUN-TEST/IDLE'
53 self.oldstate = None
6d990fe1 54 self.oldpins = (-1, -1, -1, -1)
557a143d
UH
55 self.oldtck = -1
56 self.bits_tdi = []
57 self.bits_tdo = []
58
8915b346 59 def start(self):
be465111
BV
60 self.out_proto = self.register(srd.OUTPUT_PYTHON)
61 self.out_ann = self.register(srd.OUTPUT_ANN)
557a143d 62
557a143d 63 def advance_state_machine(self, tms):
e5edf39f
UH
64 self.oldstate = self.state
65
557a143d
UH
66 # Intro "tree"
67 if self.state == 'TEST-LOGIC-RESET':
68 self.state = 'TEST-LOGIC-RESET' if (tms) else 'RUN-TEST/IDLE'
69 elif self.state == 'RUN-TEST/IDLE':
70 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
71
72 # DR "tree"
73 elif self.state == 'SELECT-DR-SCAN':
74 self.state = 'SELECT-IR-SCAN' if (tms) else 'CAPTURE-DR'
75 elif self.state == 'CAPTURE-DR':
76 self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
77 elif self.state == 'SHIFT-DR':
78 self.state = 'EXIT1-DR' if (tms) else 'SHIFT-DR'
79 elif self.state == 'EXIT1-DR':
80 self.state = 'UPDATE-DR' if (tms) else 'PAUSE-DR'
81 elif self.state == 'PAUSE-DR':
82 self.state = 'EXIT2-DR' if (tms) else 'PAUSE-DR'
83 elif self.state == 'EXIT2-DR':
84 self.state = 'UPDATE-DR' if (tms) else 'SHIFT-DR'
85 elif self.state == 'UPDATE-DR':
86 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
87
88 # IR "tree"
89 elif self.state == 'SELECT-IR-SCAN':
90 self.state = 'TEST-LOGIC-RESET' if (tms) else 'CAPTURE-IR'
91 elif self.state == 'CAPTURE-IR':
92 self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
93 elif self.state == 'SHIFT-IR':
94 self.state = 'EXIT1-IR' if (tms) else 'SHIFT-IR'
95 elif self.state == 'EXIT1-IR':
96 self.state = 'UPDATE-IR' if (tms) else 'PAUSE-IR'
97 elif self.state == 'PAUSE-IR':
98 self.state = 'EXIT2-IR' if (tms) else 'PAUSE-IR'
99 elif self.state == 'EXIT2-IR':
100 self.state = 'UPDATE-IR' if (tms) else 'SHIFT-IR'
101 elif self.state == 'UPDATE-IR':
102 self.state = 'SELECT-DR-SCAN' if (tms) else 'RUN-TEST/IDLE'
103
104 else:
105 raise Exception('Invalid state: %s' % self.state)
106
6d990fe1 107 def handle_rising_tck_edge(self, tdi, tdo, tck, tms):
557a143d
UH
108 # Rising TCK edges always advance the state machine.
109 self.advance_state_machine(tms)
110
111 # Output the state we just switched to.
112 self.put(self.ss, self.es, self.out_ann,
113 [0, ['New state: %s' % self.state]])
e5edf39f
UH
114 self.put(self.ss, self.es, self.out_proto,
115 ['NEW STATE', self.state])
116
117 # If we went from SHIFT-IR to SHIFT-IR, or SHIFT-DR to SHIFT-DR,
118 # collect the current TDI/TDO values (upon rising TCK edge).
119 if self.state.startswith('SHIFT-') and self.oldstate == self.state:
120 self.bits_tdi.insert(0, tdi)
121 self.bits_tdo.insert(0, tdo)
122 # TODO: ANN/PROTO output.
123 # self.put(self.ss, self.es, self.out_ann,
124 # [0, ['TDI add: ' + str(tdi)]])
125 # self.put(self.ss, self.es, self.out_ann,
126 # [0, ['TDO add: ' + str(tdo)]])
127
128 # Output all TDI/TDO bits if we just switched from SHIFT-* to EXIT1-*.
129 if self.oldstate.startswith('SHIFT-') and \
130 self.state.startswith('EXIT1-'):
131
132 t = self.state[-2:] + ' TDI'
133 b = ''.join(map(str, self.bits_tdi))
8189738e
UH
134 h = ' (0x%x' % int('0b' + b, 2) + ')'
135 s = t + ': ' + b + h + ', ' + str(len(self.bits_tdi)) + ' bits'
e5edf39f
UH
136 self.put(self.ss, self.es, self.out_ann, [0, [s]])
137 self.put(self.ss, self.es, self.out_proto, [t, b])
138 self.bits_tdi = []
557a143d 139
e5edf39f
UH
140 t = self.state[-2:] + ' TDO'
141 b = ''.join(map(str, self.bits_tdo))
8189738e
UH
142 h = ' (0x%x' % int('0b' + b, 2) + ')'
143 s = t + ': ' + b + h + ', ' + str(len(self.bits_tdo)) + ' bits'
557a143d 144 self.put(self.ss, self.es, self.out_ann, [0, [s]])
e5edf39f 145 self.put(self.ss, self.es, self.out_proto, [t, b])
557a143d
UH
146 self.bits_tdo = []
147
148 def decode(self, ss, es, data):
149 for (samplenum, pins) in data:
150
151 # If none of the pins changed, there's nothing to do.
152 if self.oldpins == pins:
153 continue
154
155 # Store current pin values for the next round.
156 self.oldpins = pins
157
158 # Get individual pin values into local variables.
1cc590f7
UH
159 # Unused probes will have a value of > 1.
160 (tdi, tdo, tck, tms, trst, srst, rtck) = pins
557a143d
UH
161
162 # We only care about TCK edges (either rising or falling).
163 if (self.oldtck == tck):
164 continue
165
166 # Store start/end sample for later usage.
167 self.ss, self.es = ss, es
168
e5edf39f 169 # self.put(self.ss, self.es, self.out_ann,
6d990fe1
UH
170 # [0, ['tdi:%s, tdo:%s, tck:%s, tms:%s' \
171 # % (tdi, tdo, tck, tms)]])
e5edf39f 172
557a143d 173 if (self.oldtck == 0 and tck == 1):
6d990fe1 174 self.handle_rising_tck_edge(tdi, tdo, tck, tms)
557a143d
UH
175
176 self.oldtck = tck
177