]> sigrok.org Git - libsigrokdecode.git/blame - decoders/jtag_stm32/pd.py
jtag_stm32: Always switch state on new instruction
[libsigrokdecode.git] / decoders / jtag_stm32 / pd.py
CommitLineData
66a8517e 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
66a8517e 3##
6cdc1e09 4## Copyright (C) 2012-2015 Uwe Hermann <uwe@hermann-uwe.de>
66a8517e
UH
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
4539e9ca 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
66a8517e
UH
18##
19
66a8517e
UH
20import sigrokdecode as srd
21
22# JTAG debug port data registers (in IR[3:0]) and their sizes (in bits)
457acc28
UH
23# Note: The ARM DAP-DP is not IEEE 1149.1 (JTAG) compliant (as per ARM docs),
24# as it does not implement the EXTEST, SAMPLE, and PRELOAD instructions.
25# Instead, BYPASS is decoded for any of these instructions.
66a8517e
UH
26ir = {
27 '1111': ['BYPASS', 1], # Bypass register
28 '1110': ['IDCODE', 32], # ID code register
29 '1010': ['DPACC', 35], # Debug port access register
30 '1011': ['APACC', 35], # Access port access register
e9656a0c 31 '1000': ['ABORT', 35], # Abort register # TODO: 32 bits? Datasheet typo?
66a8517e
UH
32}
33
bf1eeb93
GH
34# Boundary scan data registers (in IR[8:4]) and their sizes (in bits)
35bs_ir = {
36 '11111': ['BYPASS', 1], # Bypass register
37}
38
66a8517e
UH
39# ARM Cortex-M3 r1p1-01rel0 ID code
40cm3_idcode = 0x3ba00477
41
e4bafb8d
UH
42# http://infocenter.arm.com/help/topic/com.arm.doc.ddi0413c/Chdjibcg.html
43cm3_idcode_ver = {
44 0x3: 'JTAG-DP',
45 0x2: 'SW-DP',
46}
47cm3_idcode_part = {
48 0xba00: 'JTAG-DP',
49 0xba10: 'SW-DP',
50}
51
52# http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka14408.html
53jedec_id = {
54 5: {
55 0x3b: 'ARM Ltd.',
56 },
57}
58
66a8517e
UH
59# JTAG ID code in the STM32F10xxx BSC (boundary scan) TAP
60jtag_idcode = {
61 0x06412041: 'Low-density device, rev. A',
62 0x06410041: 'Medium-density device, rev. A',
63 0x16410041: 'Medium-density device, rev. B/Z/Y',
64 0x06414041: 'High-density device, rev. A/Z/Y',
65 0x06430041: 'XL-density device, rev. A',
66 0x06418041: 'Connectivity-line device, rev. A/Z',
67}
68
d274e1bf 69# ACK[2:0] in the DPACC/APACC registers (unlisted values are reserved)
66a8517e 70ack_val = {
66a8517e
UH
71 '001': 'WAIT',
72 '010': 'OK/FAULT',
66a8517e
UH
73}
74
75# 32bit debug port registers (addressed via A[3:2])
d274e1bf 76dp_reg = {
66a8517e
UH
77 '00': 'Reserved', # Must be kept at reset value
78 '01': 'DP CTRL/STAT',
79 '10': 'DP SELECT',
80 '11': 'DP RDBUFF',
81}
82
d274e1bf
UH
83# APB-AP registers (each of them 32 bits wide)
84apb_ap_reg = {
85 0x00: ['CSW', 'Control/status word'],
86 0x04: ['TAR', 'Transfer address'],
87 # 0x08: Reserved SBZ
88 0x0c: ['DRW', 'Data read/write'],
89 0x10: ['BD0', 'Banked data 0'],
90 0x14: ['BD1', 'Banked data 1'],
91 0x18: ['BD2', 'Banked data 2'],
92 0x1c: ['BD3', 'Banked data 3'],
93 # 0x20-0xf4: Reserved SBZ
94 0x800000000: ['ROM', 'Debug ROM address'],
95 0xfc: ['IDR', 'Identification register'],
96}
97
d274e1bf 98# TODO: Split off generic ARM/Cortex-M3 parts into another protocol decoder?
e9656a0c 99
457acc28
UH
100# Bits[31:28]: Version (here: 0x3)
101# JTAG-DP: 0x3, SW-DP: 0x2
102# Bits[27:12]: Part number (here: 0xba00)
103# JTAG-DP: 0xba00, SW-DP: 0xba10
104# Bits[11:1]: JEDEC (JEP-106) manufacturer ID (here: 0x23b)
e4bafb8d
UH
105# Bits[11:8]: Continuation code ('ARM Ltd.': 0x04)
106# Bits[7:1]: Identity code ('ARM Ltd.': 0x3b)
457acc28
UH
107# Bits[0:0]: Reserved (here: 0x1)
108def decode_device_id_code(bits):
109 id_hex = '0x%x' % int('0b' + bits, 2)
e4bafb8d
UH
110 ver = cm3_idcode_ver.get(int('0b' + bits[-32:-28], 2), 'UNKNOWN')
111 part = cm3_idcode_part.get(int('0b' + bits[-28:-12], 2), 'UNKNOWN')
112 ids = jedec_id.get(int('0b' + bits[-12:-8], 2) + 1, {})
113 manuf = ids.get(int('0b' + bits[-7:-1], 2), 'UNKNOWN')
114 return (id_hex, manuf, ver, part)
457acc28 115
d274e1bf
UH
116# DPACC is used to access debug port registers (CTRL/STAT, SELECT, RDBUFF).
117# APACC is used to access all Access Port (AHB-AP) registers.
118
119# APACC/DPACC, when transferring data IN:
120# Bits[34:3] = DATA[31:0]: 32bit data to transfer (write request)
121# Bits[2:1] = A[3:2]: 2-bit address (debug/access port register)
122# Bits[0:0] = RnW: Read request (1) or write request (0)
123def data_in(instruction, bits):
c840e704
UH
124 data, a, rnw = bits[:-3], bits[-3:-1], bits[-1]
125 data_hex = '0x%x' % int('0b' + data, 2)
126 r = 'Read request' if (rnw == '1') else 'Write request'
d274e1bf
UH
127 # reg = dp_reg[a] if (instruction == 'DPACC') else apb_ap_reg[a]
128 reg = dp_reg[a] if (instruction == 'DPACC') else a # TODO
129 return 'New transaction: DATA: %s, A: %s, RnW: %s' % (data_hex, reg, r)
130
131# APACC/DPACC, when transferring data OUT:
132# Bits[34:3] = DATA[31:0]: 32bit data which is read (read request)
133# Bits[2:0] = ACK[2:0]: 3-bit acknowledge
134def data_out(bits):
c840e704
UH
135 data, ack = bits[:-3], bits[-3:]
136 data_hex = '0x%x' % int('0b' + data, 2)
d274e1bf
UH
137 ack_meaning = ack_val.get(ack, 'Reserved')
138 return 'Previous transaction result: DATA: %s, ACK: %s' \
139 % (data_hex, ack_meaning)
c840e704 140
66a8517e 141class Decoder(srd.Decoder):
b197383c 142 api_version = 3
66a8517e
UH
143 id = 'jtag_stm32'
144 name = 'JTAG / STM32'
145 longname = 'Joint Test Action Group / ST STM32'
146 desc = 'ST STM32-specific JTAG protocol.'
147 license = 'gplv2+'
148 inputs = ['jtag']
149 outputs = ['jtag_stm32']
da9bcbd9 150 annotations = (
d011c2e1
UH
151 ('item', 'Item'),
152 ('field', 'Field'),
153 ('command', 'Command'),
154 ('warning', 'Warning'),
155 )
156 annotation_rows = (
157 ('items', 'Items', (0,)),
158 ('fields', 'Fields', (1,)),
159 ('commands', 'Commands', (2,)),
160 ('warnings', 'Warnings', (3,)),
da9bcbd9 161 )
66a8517e 162
92b7b49f 163 def __init__(self):
10aeb8ea
GS
164 self.reset()
165
166 def reset(self):
66a8517e 167 self.state = 'IDLE'
a4dd548f 168 self.samplenums = None
66a8517e 169
8915b346 170 def start(self):
be465111 171 self.out_ann = self.register(srd.OUTPUT_ANN)
66a8517e 172
0edb5d58
UH
173 def putx(self, data):
174 self.put(self.ss, self.es, self.out_ann, data)
175
d011c2e1
UH
176 def putf(self, s, e, data):
177 self.put(self.samplenums[s][0], self.samplenums[e][1], self.out_ann, data)
178
d274e1bf 179 def handle_reg_bypass(self, cmd, bits):
0edb5d58 180 self.putx([0, ['BYPASS: ' + bits]])
66a8517e 181
d274e1bf 182 def handle_reg_idcode(self, cmd, bits):
bf1eeb93
GH
183 bits = bits[1:]
184
185 id_hex, manuf, ver, part = decode_device_id_code(bits)
186 cc = '0x%x' % int('0b' + bits[-12:-8], 2)
187 ic = '0x%x' % int('0b' + bits[-7:-1], 2)
188
189 self.putf(0, 0, [1, ['Reserved', 'Res', 'R']])
190 self.putf(8, 11, [0, ['Continuation code: %s' % cc, 'CC', 'C']])
191 self.putf(1, 7, [0, ['Identity code: %s' % ic, 'IC', 'I']])
192 self.putf(1, 11, [1, ['Manufacturer: %s' % manuf, 'Manuf', 'M']])
193 self.putf(12, 27, [1, ['Part: %s' % part, 'Part', 'P']])
194 self.putf(28, 31, [1, ['Version: %s' % ver, 'Version', 'V']])
195 self.putf(32, 32, [1, ['BYPASS (BS TAP)', 'BS', 'B']])
196
e4bafb8d 197 self.putx([2, ['IDCODE: %s (%s: %s/%s)' % \
bf1eeb93 198 decode_device_id_code(bits)]])
66a8517e 199
d274e1bf 200 def handle_reg_dpacc(self, cmd, bits):
bf1eeb93 201 bits = bits[1:]
d274e1bf 202 s = data_in('DPACC', bits) if (cmd == 'DR TDI') else data_out(bits)
d011c2e1 203 self.putx([2, [s]])
d274e1bf
UH
204
205 def handle_reg_apacc(self, cmd, bits):
bf1eeb93 206 bits = bits[1:]
d274e1bf 207 s = data_in('APACC', bits) if (cmd == 'DR TDI') else data_out(bits)
d011c2e1 208 self.putx([2, [s]])
d274e1bf
UH
209
210 def handle_reg_abort(self, cmd, bits):
bf1eeb93 211 bits = bits[1:]
66a8517e
UH
212 # Bits[31:1]: reserved. Bit[0]: DAPABORT.
213 a = '' if (bits[0] == '1') else 'No '
214 s = 'DAPABORT = %s: %sDAP abort generated' % (bits[0], a)
d011c2e1 215 self.putx([2, [s]])
66a8517e 216
e9656a0c 217 # Warn if DAPABORT[31:1] contains non-zero bits.
66a8517e 218 if (bits[:-1] != ('0' * 31)):
d011c2e1 219 self.putx([3, ['WARNING: DAPABORT[31:1] reserved!']])
e9656a0c 220
d274e1bf 221 def handle_reg_unknown(self, cmd, bits):
bf1eeb93 222 bits = bits[1:]
d011c2e1 223 self.putx([2, ['Unknown instruction: %s' % bits]])
66a8517e
UH
224
225 def decode(self, ss, es, data):
66a8517e
UH
226 cmd, val = data
227
228 self.ss, self.es = ss, es
229
a4dd548f 230 if cmd != 'NEW STATE':
a4dd548f 231 # The right-most char in the 'val' bitstring is the LSB.
d011c2e1 232 val, self.samplenums = val
e4bafb8d 233 self.samplenums.reverse()
66a8517e 234
a3ac52d1 235 if cmd == 'IR TDI':
e9656a0c 236 # Switch to the state named after the instruction, or 'UNKNOWN'.
6cdc1e09
UH
237 # The STM32F10xxx has two serially connected JTAG TAPs, the
238 # boundary scan tap (5 bits) and the Cortex-M3 TAP (4 bits).
239 # See UM 31.5 "STM32F10xxx JTAG TAP connection" for details.
bf1eeb93
GH
240 self.state = ir.get(val[5:9], ['UNKNOWN', 0])[0]
241 bstap_ir = bs_ir.get(val[:5], ['UNKNOWN', 0])[0]
242 self.putf(4, 8, [1, ['IR (BS TAP): ' + bstap_ir]])
243 self.putf(0, 3, [1, ['IR (M3 TAP): ' + self.state]])
d011c2e1 244 self.putx([2, ['IR: %s' % self.state]])
a3ac52d1
GH
245
246 # State machine
247 if self.state == 'BYPASS':
d274e1bf 248 # Here we're interested in incoming bits (TDI).
e9656a0c
UH
249 if cmd != 'DR TDI':
250 return
251 handle_reg = getattr(self, 'handle_reg_%s' % self.state.lower())
d274e1bf 252 handle_reg(cmd, val)
e9656a0c 253 self.state = 'IDLE'
d274e1bf
UH
254 elif self.state in ('IDCODE', 'ABORT', 'UNKNOWN'):
255 # Here we're interested in outgoing bits (TDO).
457acc28 256 if cmd != 'DR TDO':
e9656a0c
UH
257 return
258 handle_reg = getattr(self, 'handle_reg_%s' % self.state.lower())
d274e1bf 259 handle_reg(cmd, val)
e9656a0c 260 self.state = 'IDLE'
d274e1bf
UH
261 elif self.state in ('DPACC', 'APACC'):
262 # Here we're interested in incoming and outgoing bits (TDI/TDO).
263 if cmd not in ('DR TDI', 'DR TDO'):
264 return
265 handle_reg = getattr(self, 'handle_reg_%s' % self.state.lower())
266 handle_reg(cmd, val)
0edb5d58 267 if cmd == 'DR TDO': # Assumes 'DR TDI' comes before 'DR TDO'.
d274e1bf 268 self.state = 'IDLE'