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 common.srdhelper import bitpack
27 [<data>] where <data> is the payload contained between the LFAST header and
28 the LFAST stop bit. It's an array of bytes.
31 ann_bit, ann_sync, ann_header, ann_payload, ann_stopbit, ann_warning = range(6)
32 state_sync, state_header, state_payload, state_stopbit = range(4)
34 class Decoder(srd.Decoder):
38 longname = 'NXP LFAST interface'
39 desc = 'Differential high-speed P2P interface'
43 tags = ['Embedded/industrial']
45 {'id': 'data', 'name': 'Data', 'desc': 'TXP or RXP'},
49 ('sync', 'Sync Pattern'),
51 ('payload', 'Payload'),
53 ('warning', 'Warning'),
56 ('bits', 'Bits', (ann_bit,)),
57 ('fields', 'Fields', (ann_sync, ann_header, ann_payload, ann_stopbit,)),
58 ('warnings', 'Warnings', (ann_warning,)),
62 decimal.getcontext().rounding = decimal.ROUND_HALF_UP
67 self.ss_payload = self.es_payload = 0
72 self.state = state_sync
74 def metadata(self, key, value):
78 self.out_python = self.register(srd.OUTPUT_PYTHON)
79 self.out_ann = self.register(srd.OUTPUT_ANN)
81 def put_ann(self, ss, es, ann_class, value):
82 self.put(ss, es, self.out_ann, [ann_class, value])
84 def put_payload(self):
85 self.put(self.ss_payload, self.es_payload, self.out_python, self.payload)
87 def handle_sync(self):
88 if len(self.bits) == 1:
89 self.ss_sync = self.ss_bit
91 if len(self.bits) == 16:
92 value = bitpack(self.bits)
94 self.put_ann(self.ss_sync, self.es_bit, ann_sync, ['Sync OK'])
96 self.put_ann(self.ss_sync, self.es_bit, ann_warning, ['Wrong Sync Value: {:2X}'.format(value)])
99 self.state = state_header
101 def handle_header(self):
102 if len(self.bits) == 1:
103 self.ss_header = self.ss_bit
105 if len(self.bits) == 8:
106 value = bitpack(self.bits)
107 self.put_ann(self.ss_header, self.es_bit, ann_header, ['{:2X}'.format(value)])
109 self.state = state_payload
111 def handle_payload(self):
112 # 8 bit times without state change are possible (8 low bits) but when
113 # there are 9 bit times without state change, we should have seen the
114 # stop bit - and only the stop bit
115 self.timeout = int(9 * self.bit_len)
117 if len(self.bits) == 1:
118 self.ss_byte = self.ss_bit
119 if self.ss_payload == 0:
120 self.ss_payload = self.ss_bit
122 if len(self.bits) == 8:
123 value = bitpack(self.bits)
124 self.put_ann(self.ss_byte, self.es_bit, ann_payload, ['{:2X}'.format(value)])
126 self.payload.append(value)
127 self.es_payload = self.es_bit
129 def handle_stopbit(self):
130 if len(self.bits) > 1:
131 self.put_ann(self.ss_bit, self.es_bit, ann_warning, ['Expected only the stop bit, got {} bits'.format(len(self.bits))])
133 if self.bits[0] == 1:
134 self.put_ann(self.ss_bit, self.es_bit, ann_stopbit, ['Stop Bit', 'Stop', 'S'])
136 self.put_ann(self.ss_bit, self.es_bit, ann_warning, ['Stop Bit must be 1', 'Stop not 1', 'S'])
138 # We send the payload out regardless of the stop bit's status so that
139 # any intermediate results can be decoded by a stacked decoder
146 self.state = state_sync
150 if self.timeout == 0:
151 rising_edge, = self.wait({0: 'e'})
153 rising_edge, = self.wait([{0: 'e'}, {'skip': self.timeout}])
155 # If this is the first bit, we only update ss
157 self.ss = self.samplenum
160 self.es = self.samplenum
162 # Check for the stop bit if this is a timeout condition
163 if (self.timeout > 0) and (self.es - self.ss >= self.timeout):
164 self.handle_stopbit()
167 # We use the first bit to deduce the bit length
168 if self.bit_len == 0:
169 self.bit_len = self.es - self.ss
171 # Determine number of bits covered by this edge
172 bit_count = (self.es - self.ss) / self.bit_len
173 bit_count = int(decimal.Decimal(bit_count).to_integral_value())
175 bit_value = '0' if rising_edge else '1'
177 divided_len = (self.es - self.ss) / bit_count
178 for i in range(bit_count):
179 self.ss_bit = int(self.ss + i * divided_len)
180 self.es_bit = int(self.ss_bit + divided_len)
181 self.put_ann(self.ss_bit, self.es_bit, ann_bit, [bit_value])
183 # Place the new bit at the front of the bit list
184 self.bits.insert(0, (0 if rising_edge else 1))
186 if self.state == state_sync:
188 elif self.state == state_header:
190 elif self.state == state_payload:
191 self.handle_payload()
193 self.ss = self.samplenum