]> sigrok.org Git - libsigrokdecode.git/blob - decoders/sipi/pd.py
lfast/sipi: Fix typo
[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 ann_header_tag, ann_header_cmd, ann_header_ch, ann_address, ann_data, \
55     ann_crc, ann_warning = range(7)
56
57 class Decoder(srd.Decoder):
58     api_version = 3
59     id = 'sipi'
60     name = 'SIPI (Zipwire)'
61     longname = 'NXP SIPI interface'
62     desc = 'Serial Inter-Processor Interface (SIPI) aka Zipwire, aka HSSL'
63     license = 'gplv2+'
64     inputs = ['lfast']
65     outputs = []
66     tags = ['Embedded/industrial']
67     annotations = (
68         ('header_tag', 'Transaction Tag'),
69         ('header_cmd', 'Command Code'),
70         ('header_ch', 'Channel'),
71         ('address', 'Address'),
72         ('data', 'Data'),
73         ('crc', 'CRC'),
74         ('warning', 'Warning'),
75     )
76     annotation_rows = (
77         ('fields', 'Fields', (ann_header_tag, ann_header_cmd,
78             ann_header_ch, ann_address, ann_data, ann_crc,)),
79         ('warnings', 'Warnings', (ann_warning,)),
80     )
81
82     def __init__(self):
83         self.reset()
84
85     def reset(self):
86         self.byte_len = 0
87         self.frame_len = 0
88
89     def start(self):
90         self.out_ann = self.register(srd.OUTPUT_ANN)
91         self.out_binary = self.register(srd.OUTPUT_BINARY)
92
93     def put_ann(self, ss, es, ann_class, value):
94         self.put(int(ss), int(es), self.out_ann, [ann_class, value])
95
96     def put_header(self, ss_header, es_header, value):
97         ss = ss_header
98         es = ss + 3 * self.bit_len
99         tag = (value & 0xE000) >> 13
100         self.put_ann(ss, es, ann_header_tag, ['{:02X}'.format(tag)])
101
102         ss = es
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])
109
110         # Bits 4..7 are reserved and should be 0, warn if they're not
111         ss = es
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'])
116
117         ss = es
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)])
121
122         # Bit 0 is reserved and should be 0, warn if it's not
123         if (value & 0x0001) == 0x0001:
124             ss = es
125             es = ss + self.bit_len
126             self.put_ann(ss, es, ann_warning, ['Reserved bit #0 should be 0'])
127
128     def put_payload(self, data):
129         byte_idx = 0
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
135
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)])
140
141     def put_crc(self, ss, es, crc_value, crc_payload_data):
142         crc_payload = []
143         for value_tuple in crc_payload_data:
144             crc_payload.append(value_tuple[2])
145
146         calculated_crc = crc_hqx(bytes(crc_payload), 0xFFFF)
147
148         if calculated_crc == crc_value:
149             self.put_ann(ss, es, ann_crc, ['CRC OK'])
150         else:
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'])
153
154     def decode(self, ss, es, data):
155         if len(data) == 1:
156             self.put_ann(ss, es, ann_warning, ['Header too short'])
157             return
158
159         # ss and es are now unused, we use them as local variables instead
160
161         self.bit_len = (data[0][1] - data[0][0]) / 8.0
162
163         byte_idx = 0
164
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])
168         byte_idx += 2
169
170         payload_len = self.frame_len - 2 - 2  # -Header -CRC
171         if payload_len > 0:
172             self.put_payload(data[byte_idx:-2])
173             byte_idx += payload_len
174
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])
180         else:
181             self.put_ann(ss, es, ann_warning, ['CRC incomplete or missing'])