]> sigrok.org Git - libsigrokdecode.git/blob - decoders/sipi/pd.py
73c5eb9410b3dec9721a4b62d5c65e0823abc8fb
[libsigrokdecode.git] / decoders / sipi / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21 from binascii import crc_hqx
22
23 # See tc27xD_um_v2.2.pdf, Table 20-2
24 # (name, addr byte count, data byte count)
25 command_codes = {
26     0b00000: ('Read byte', 4, 0),
27     0b00001: ('Read 2 byte', 4, 0),
28     0b00010: ('Read 4 byte', 4, 0),
29     # Reserved
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),
33     # Reserved
34     0b01000: ('ACK', 0, 0),
35     0b01001: ('NACK (Target Error)', 0, 0),
36     0b01010: ('Read Answer with ACK', 4, 4),
37     # Reserved
38     0b01100: ('Trigger with ACK', 0, 0),
39     # Reserved
40     # Reserved
41     # Reserved
42     # Reserved
43     # Reserved
44     0b10010: ('Read 4-byte JTAG ID', 0, 0),
45     # Reserved
46     # Reserved
47     # Reserved
48     # Reserved
49     0b10111: ('Stream 32 byte with ACK', 0, 32)
50     # Rest is reserved
51 }
52
53
54
55 ann_header_tag, ann_header_cmd, ann_header_ch, ann_address, ann_data, \
56     ann_crc, ann_warning = range(7)
57
58 class Decoder(srd.Decoder):
59     api_version = 3
60     id = 'sipi'
61     name = 'SIPI (ZipWire)'
62     longname = 'NXP SIPI interface'
63     desc = 'Serial Inter-Processor Interface (SIPI) aka ZipWire, aka HSSL'
64     license = 'gplv2+'
65     inputs = ['lfast']
66     outputs = []
67     tags = ['Embedded/industrial']
68     annotations = (
69         ('header_tag', 'Transaction Tag'),
70         ('header_cmd', 'Command Code'),
71         ('header_ch', 'Channel'),
72         ('address', 'Address'),
73         ('data', 'Data'),
74         ('crc', 'CRC'),
75         ('warning', 'Warning'),
76     )
77     annotation_rows = (
78         ('fields', 'Fields', (ann_header_tag, ann_header_cmd,
79             ann_header_ch, ann_address, ann_data, ann_crc,)),
80         ('warnings', 'Warnings', (ann_warning,)),
81     )
82
83     def __init__(self):
84         self.reset()
85
86     def reset(self):
87         self.byte_len = 0
88         self.frame_len = 0
89
90     def start(self):
91         self.out_ann = self.register(srd.OUTPUT_ANN)
92         self.out_binary = self.register(srd.OUTPUT_BINARY)
93
94     def put_ann(self, ss, es, ann_class, value):
95         self.put(int(ss), int(es), self.out_ann, [ann_class, value])
96
97     def put_header(self, ss_header, es_header, value):
98         ss = ss_header
99         es = ss + 3 * self.bit_len
100         tag = (value & 0xE000) >> 13
101         self.put_ann(ss, es, ann_header_tag, ['{:02X}'.format(tag)])
102
103         ss = es
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])
110
111         # Bits 4..7 are reserved and should be 0, warn if they're not
112         ss = es
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'])
117
118         ss = es
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)])
122
123         # Bit 0 is reserved and should be 0, warn if it's not
124         if (value & 0x0001) == 0x0001:
125             ss = es
126             es = ss + self.bit_len
127             self.put_ann(ss, es, ann_warning, ['Reserved bit #0 should be 0'])
128
129     def put_payload(self, data):
130         byte_idx = 0
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
136
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)])
141
142     def put_crc(self, ss, es, crc_value, crc_payload_data):
143         crc_payload = []
144         for value_tuple in crc_payload_data:
145             crc_payload.append(value_tuple[2])
146
147         calculated_crc = crc_hqx(bytes(crc_payload), 0xFFFF)
148
149         if calculated_crc == crc_value:
150             self.put_ann(ss, es, ann_crc, ['CRC OK'])
151         else:
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'])
154
155     def decode(self, ss, es, data):
156         if len(data) == 1:
157             self.put_ann(ss, es, ann_warning, ['Header too short'])
158             return
159
160         # ss and es are now unused, we use them as local variables instead
161
162         self.bit_len = (data[0][1] - data[0][0]) / 8.0
163
164         byte_idx = 0
165
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])
169         byte_idx += 2
170
171         payload_len = self.frame_len - 2 - 2  # -Header -CRC
172         if payload_len > 0:
173             self.put_payload(data[byte_idx:-2])
174             byte_idx += payload_len
175
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])
181         else:
182             self.put_ann(ss, es, ann_warning, ['CRC incomplete or missing'])