2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
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.
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.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
21 from binascii import crc_hqx
23 # See tc27xD_um_v2.2.pdf, Table 20-2
24 # (name, addr byte count, data byte count)
26 0b00000: ('Read byte', 4, 0),
27 0b00001: ('Read 2 byte', 4, 0),
28 0b00010: ('Read 4 byte', 4, 0),
30 0b00100: ('Write byte with ACK', 4, 4),
31 0b00101: ('Write 2 byte with ACK', 4, 4),
32 0b00110: ('Write 4 byte with ACK', 4, 4),
34 0b01000: ('ACK', 0, 0),
35 0b01001: ('NACK (Target Error)', 0, 0),
36 0b01010: ('Read Answer with ACK', 4, 4),
38 0b01100: ('Trigger with ACK', 0, 0),
44 0b10010: ('Read 4-byte JTAG ID', 0, 0),
49 0b10111: ('Stream 32 byte with ACK', 0, 32)
54 ann_header_tag, ann_header_cmd, ann_header_ch, ann_address, ann_data, \
55 ann_crc, ann_warning = range(7)
57 class Decoder(srd.Decoder):
60 name = 'SIPI (Zipwire)'
61 longname = 'NXP SIPI interface'
62 desc = 'Serial Inter-Processor Interface (SIPI) aka Zipwire, aka HSSL'
66 tags = ['Embedded/industrial']
68 ('header_tag', 'Transaction Tag'),
69 ('header_cmd', 'Command Code'),
70 ('header_ch', 'Channel'),
71 ('address', 'Address'),
74 ('warning', 'Warning'),
77 ('fields', 'Fields', (ann_header_tag, ann_header_cmd,
78 ann_header_ch, ann_address, ann_data, ann_crc,)),
79 ('warnings', 'Warnings', (ann_warning,)),
90 self.out_ann = self.register(srd.OUTPUT_ANN)
91 self.out_binary = self.register(srd.OUTPUT_BINARY)
93 def put_ann(self, ss, es, ann_class, value):
94 self.put(int(ss), int(es), self.out_ann, [ann_class, value])
96 def put_header(self, ss_header, es_header, value):
98 es = ss + 3 * self.bit_len
99 tag = (value & 0xE000) >> 13
100 self.put_ann(ss, es, ann_header_tag, ['{:02X}'.format(tag)])
103 es = ss + 5 * self.bit_len
104 cmd_id = (value & 0x1F00) >> 8
105 cmd_name, self.addr_len, self.data_len = \
106 command_codes.get(cmd_id, ('Reserved ({:02X})'.format(cmd_id), 0, 0))
107 self.frame_len = 2 + 2 + self.addr_len + self.data_len # +Header +CRC
108 self.put_ann(ss, es, ann_header_cmd, [cmd_name])
110 # Bits 4..7 are reserved and should be 0, warn if they're not
112 es = ss + 4 * self.bit_len
113 reserved_bits = (value & 0x00F0) >> 4
114 if reserved_bits > 0:
115 self.put_ann(ss, es, ann_warning, ['Reserved bits #4..7 should be 0'])
118 es = ss + 3 * self.bit_len
119 ch = (value & 0x000E) >> 1 # See tc27xD_um_v2.2.pdf, Table 20-1
120 self.put_ann(ss, es, ann_header_ch, [str(ch)])
122 # Bit 0 is reserved and should be 0, warn if it's not
123 if (value & 0x0001) == 0x0001:
125 es = ss + self.bit_len
126 self.put_ann(ss, es, ann_warning, ['Reserved bit #0 should be 0'])
128 def put_payload(self, data):
130 if self.addr_len > 0:
131 for value_tuple in data[:self.addr_len]:
132 ss, es, value = value_tuple
133 self.put_ann(ss, es, ann_address, ['{:02X}'.format(value)])
134 byte_idx = self.addr_len
136 if self.data_len > 0:
137 for value_tuple in data[byte_idx:]:
138 ss, es, value = value_tuple
139 self.put_ann(ss, es, ann_data, ['{:02X}'.format(value)])
141 def put_crc(self, ss, es, crc_value, crc_payload_data):
143 for value_tuple in crc_payload_data:
144 crc_payload.append(value_tuple[2])
146 calculated_crc = crc_hqx(bytes(crc_payload), 0xFFFF)
148 if calculated_crc == crc_value:
149 self.put_ann(ss, es, ann_crc, ['CRC OK'])
151 self.put_ann(ss, es, ann_crc, ['Have {:02X} but calculated {:02X}'.format(crc_value, calculated_crc)])
152 self.put_ann(ss, es, ann_warning, ['CRC mismatch'])
154 def decode(self, ss, es, data):
156 self.put_ann(ss, es, ann_warning, ['Header too short'])
159 # ss and es are now unused, we use them as local variables instead
161 self.bit_len = (data[0][1] - data[0][0]) / 8.0
165 ss = data[byte_idx][0]
166 es = data[byte_idx + 1][1]
167 self.put_header(ss, es, (data[byte_idx][2] << 8) + data[byte_idx + 1][2])
170 payload_len = self.frame_len - 2 - 2 # -Header -CRC
172 self.put_payload(data[byte_idx:-2])
173 byte_idx += payload_len
175 ss = data[byte_idx][0]
176 es = data[byte_idx + 1][1]
177 if byte_idx == len(data) - 2:
178 # CRC is calculated over header + payload bytes
179 self.put_crc(ss, es, (data[byte_idx][2] << 8) + data[byte_idx + 1][2], data[0:-2])
181 self.put_ann(ss, es, ann_warning, ['CRC incomplete or missing'])