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