2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2019-2020 Philip Ã…kesson <philip.akesson@gmail.com>
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 from common.srdhelper import bitpack
21 import sigrokdecode as srd
23 class SamplerateError(Exception):
26 class Decoder(srd.Decoder):
30 longname = 'Texas Instruments SDQ'
31 desc = 'Texas Instruments SDQ. The SDQ protocol is also used by Apple.'
35 tags = ['Embedded/industrial']
37 {'id': 'sdq', 'name': 'SDQ', 'desc': 'Single wire SDQ data line.'},
40 {'id': 'bitrate', 'desc': 'Bit rate', 'default': 98425},
48 ('bits', 'Bits', (0,)),
49 ('bytes', 'Bytes', (1,)),
50 ('breaks', 'Breaks', (2,)),
54 self.put(self.startsample, self.samplenum, self.out_ann, data)
56 def putetu(self, data):
57 self.put(self.startsample, self.startsample + int(self.bit_width), self.out_ann, data)
59 def putbetu(self, data):
60 self.put(self.bytepos, self.startsample + int(self.bit_width), self.out_ann, data)
66 self.samplerate = None
73 self.out_ann = self.register(srd.OUTPUT_ANN)
75 def metadata(self, key, value):
76 if key == srd.SRD_CONF_SAMPLERATE:
77 self.samplerate = value
78 self.bit_width = float(self.samplerate) / float(self.options['bitrate'])
79 self.half_bit_width = self.bit_width / 2.0
80 self.break_threshold = self.bit_width * 1.2 # Break if the line is low for longer than this
82 def handle_bit(self, bit):
84 self.putetu([0, ['Bit: %d' % bit, '%d' % bit]])
86 if len(self.bits) == 8:
87 byte = bitpack(self.bits)
88 self.putbetu([1, ['Byte: %#04x' % byte, '%#04x' % byte]])
92 def handle_break(self):
93 self.puts([2, ['Break', 'BR']])
95 self.startsample = self.samplenum
99 if not self.samplerate:
100 raise SamplerateError('Cannot decode without samplerate.')
103 if self.state == 'INIT':
104 sdq, = self.wait({0: 'h'}) # Wait until the line is high before starting
107 elif self.state == 'DATA':
108 sdq, = self.wait({0: 'f'}) # Falling edge
110 self.startsample = self.samplenum
111 if self.bytepos == 0:
112 self.bytepos = self.samplenum
114 sdq, = self.wait({0: 'r'}) # Rising edge
116 delta = self.samplenum - self.startsample
117 if delta > self.break_threshold:
119 elif delta > self.half_bit_width:
124 elif self.state == 'BREAK':