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)
55 ann_header_tag, ann_header_cmd, ann_header_ch, ann_address, ann_data, \
56 ann_crc, ann_warning = range(7)
58 class Decoder(srd.Decoder):
61 name = 'SIPI (ZipWire)'
62 longname = 'NXP SIPI interface'
63 desc = 'Serial Inter-Processor Interface (SIPI) aka ZipWire, aka HSSL'
67 tags = ['Embedded/industrial']
69 ('header_tag', 'Transaction Tag'),
70 ('header_cmd', 'Command Code'),
71 ('header_ch', 'Channel'),
72 ('address', 'Address'),
75 ('warning', 'Warning'),
78 ('fields', 'Fields', (ann_header_tag, ann_header_cmd,
79 ann_header_ch, ann_address, ann_data, ann_crc,)),
80 ('warnings', 'Warnings', (ann_warning,)),
91 self.out_ann = self.register(srd.OUTPUT_ANN)
92 self.out_binary = self.register(srd.OUTPUT_BINARY)
94 def put_ann(self, ss, es, ann_class, value):
95 self.put(int(ss), int(es), self.out_ann, [ann_class, value])
97 def put_header(self, ss_header, es_header, value):
99 es = ss + 3 * self.bit_len
100 tag = (value & 0xE000) >> 13
101 self.put_ann(ss, es, ann_header_tag, ['{:02X}'.format(tag)])
104 es = ss + 5 * self.bit_len
105 cmd_id = (value & 0x1F00) >> 8
106 cmd_name, self.addr_len, self.data_len = \
107 command_codes.get(cmd_id, ('Reserved ({:02X})'.format(cmd_id), 0, 0))
108 self.frame_len = 2 + 2 + self.addr_len + self.data_len # +Header +CRC
109 self.put_ann(ss, es, ann_header_cmd, [cmd_name])
111 # Bits 4..7 are reserved and should be 0, warn if they're not
113 es = ss + 4 * self.bit_len
114 reserved_bits = (value & 0x00F0) >> 4
115 if reserved_bits > 0:
116 self.put_ann(ss, es, ann_warning, ['Reserved bits #4..7 should be 0'])
119 es = ss + 3 * self.bit_len
120 ch = (value & 0x000E) >> 1 # See tc27xD_um_v2.2.pdf, Table 20-1
121 self.put_ann(ss, es, ann_header_ch, [str(ch)])
123 # Bit 0 is reserved and should be 0, warn if it's not
124 if (value & 0x0001) == 0x0001:
126 es = ss + self.bit_len
127 self.put_ann(ss, es, ann_warning, ['Reserved bit #0 should be 0'])
129 def put_payload(self, data):
131 if self.addr_len > 0:
132 for value_tuple in data[:self.addr_len]:
133 ss, es, value = value_tuple
134 self.put_ann(ss, es, ann_address, ['{:02X}'.format(value)])
135 byte_idx = self.addr_len
137 if self.data_len > 0:
138 for value_tuple in data[byte_idx:]:
139 ss, es, value = value_tuple
140 self.put_ann(ss, es, ann_data, ['{:02X}'.format(value)])
142 def put_crc(self, ss, es, crc_value, crc_payload_data):
144 for value_tuple in crc_payload_data:
145 crc_payload.append(value_tuple[2])
147 calculated_crc = crc_hqx(bytes(crc_payload), 0xFFFF)
149 if calculated_crc == crc_value:
150 self.put_ann(ss, es, ann_crc, ['CRC OK'])
152 self.put_ann(ss, es, ann_crc, ['Have {:02X} but calculated {:02X}'.format(crc_value, calculated_crc)])
153 self.put_ann(ss, es, ann_warning, ['CRC mismatch'])
155 def decode(self, ss, es, data):
157 self.put_ann(ss, es, ann_warning, ['Header too short'])
160 # ss and es are now unused, we use them as local variables instead
162 self.bit_len = (data[0][1] - data[0][0]) / 8.0
166 ss = data[byte_idx][0]
167 es = data[byte_idx + 1][1]
168 self.put_header(ss, es, (data[byte_idx][2] << 8) + data[byte_idx + 1][2])
171 payload_len = self.frame_len - 2 - 2 # -Header -CRC
173 self.put_payload(data[byte_idx:-2])
174 byte_idx += payload_len
176 ss = data[byte_idx][0]
177 es = data[byte_idx + 1][1]
178 if byte_idx == len(data) - 2:
179 # CRC is calculated over header + payload bytes
180 self.put_crc(ss, es, (data[byte_idx][2] << 8) + data[byte_idx + 1][2], data[0:-2])
182 self.put_ann(ss, es, ann_warning, ['CRC incomplete or missing'])