]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ssi32/pd.py
all decoders: introduce a reset() method
[libsigrokdecode.git] / decoders / ssi32 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2016 Robert Bosch Car Multimedia GmbH
5 ## Authors: Oleksij Rempel
6 ##              <fixed-term.Oleksij.Rempel@de.bosch.com>
7 ##              <linux@rempel-privat.de>
8 ##
9 ## This program is free software; you can redistribute it and/or modify
10 ## it under the terms of the GNU General Public License as published by
11 ## the Free Software Foundation; either version 2 of the License, or
12 ## (at your option) any later version.
13 ##
14 ## This program is distributed in the hope that it will be useful,
15 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ## GNU General Public License for more details.
18 ##
19 ## You should have received a copy of the GNU General Public License
20 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
21 ##
22
23 import sigrokdecode as srd
24
25 class Decoder(srd.Decoder):
26     api_version = 3
27     id = 'ssi32'
28     name = 'SSI32'
29     longname = 'Synchronous Serial Interface (32bit)'
30     desc = 'Synchronous Serial Interface (32bit) protocol.'
31     license = 'gplv2+'
32     inputs = ['spi']
33     outputs = ['ssi32']
34     options = (
35         {'id': 'msgsize', 'desc': 'Message size', 'default': 64},
36     )
37     annotations = (
38         ('ctrl-tx', 'CTRL TX'),
39         ('ack-tx', 'ACK TX'),
40         ('ctrl-rx', 'CTRL RX'),
41         ('ack-rx', 'ACK RX'),
42     )
43     annotation_rows = (
44         ('tx', 'TX', (0, 1)),
45         ('rx', 'RX', (2, 3)),
46     )
47
48     def __init__(self):
49         self.reset()
50
51     def reset(self):
52         self.ss_cmd, self.es_cmd = 0, 0
53         self.mosi_bytes = []
54         self.miso_bytes = []
55         self.es_array = []
56         self.rx_size = 0
57         self.tx_size = 0
58
59     def start(self):
60         self.out_ann = self.register(srd.OUTPUT_ANN)
61
62     def putx(self, data):
63         self.put(self.ss_cmd, self.es_cmd, self.out_ann, data)
64
65     def reset_data(self):
66         self.mosi_bytes = []
67         self.miso_bytes = []
68         self.es_array = []
69
70     def handle_ack(self):
71         # Only first byte should have ACK data, other 3 bytes are reserved.
72         self.es_cmd = self.es_array[0]
73         self.putx([1, ['> ACK:0x%02x' % (self.mosi_bytes[0])]])
74         self.putx([3, ['< ACK:0x%02x' % (self.miso_bytes[0])]])
75
76     def handle_ctrl(self):
77         mosi = miso = ''
78         self.tx_size = self.mosi_bytes[2]
79         self.rx_size = self.miso_bytes[2]
80
81         if self.tx_size > 0:
82             mosi = ', DATA:0x' + ''.join(format(x, '02x') for x in self.mosi_bytes[4:self.tx_size + 4])
83         if self.rx_size > 0:
84             miso = ', DATA:0x' + ''.join(format(x, '02x') for x in self.miso_bytes[4:self.rx_size + 4])
85
86         self.es_cmd = self.es_array[self.tx_size + 3]
87         self.putx([0, ['> CTRL:0x%02x, LUN:0x%02x, SIZE:0x%02x, CRC:0x%02x%s'
88                    % (self.mosi_bytes[0], self.mosi_bytes[1],
89                       self.mosi_bytes[2], self.mosi_bytes[3], mosi)]])
90
91         self.es_cmd = self.es_array[self.rx_size + 3]
92         self.putx([2, ['< CTRL:0x%02x, LUN:0x%02x, SIZE:0x%02x, CRC:0x%02x%s'
93                    % (self.miso_bytes[0], self.miso_bytes[1],
94                       self.miso_bytes[2], self.miso_bytes[3], miso)]])
95
96     def decode(self, ss, es, data):
97         ptype = data[0]
98         if ptype == 'CS-CHANGE':
99             self.reset_data()
100             return
101
102         # Don't care about anything else.
103         if ptype != 'DATA':
104             return
105         mosi, miso = data[1:]
106
107         self.ss, self.es = ss, es
108
109         if len(self.mosi_bytes) == 0:
110             self.ss_cmd = ss
111         self.mosi_bytes.append(mosi)
112         self.miso_bytes.append(miso)
113         self.es_array.append(es)
114
115         if self.mosi_bytes[0] & 0x80:
116             if len(self.mosi_bytes) < 4:
117                 return
118
119             self.handle_ack()
120             self.reset_data()
121         else:
122             if len(self.mosi_bytes) < self.options['msgsize']:
123                 return
124
125             self.handle_ctrl()
126             self.reset_data()