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
72 self.out_ann = self.register(srd.OUTPUT_ANN)
74 def metadata(self, key, value):
75 if key == srd.SRD_CONF_SAMPLERATE:
76 self.samplerate = value
78 def handle_bit(self, bit):
80 self.putetu([0, ['Bit: %d' % bit, '%d' % bit]])
82 if len(self.bits) == 8:
83 byte = bitpack(self.bits)
84 self.putbetu([1, ['Byte: %#04x' % byte, '%#04x' % byte]])
88 def handle_break(self):
89 self.puts([2, ['Break', 'BR']])
91 self.startsample = self.samplenum
95 if not self.samplerate:
96 raise SamplerateError('Cannot decode without samplerate.')
97 self.bit_width = float(self.samplerate) / float(self.options['bitrate'])
98 self.half_bit_width = self.bit_width / 2.0
99 # BREAK if the line is low for longer than this.
100 break_threshold = self.bit_width * 1.2
102 # Wait until the line is high before inspecting input data.
103 sdq, = self.wait({0: 'h'})
105 # Get the length of a low pulse (falling to rising edge).
106 sdq, = self.wait({0: 'f'})
107 self.startsample = self.samplenum
108 if self.bytepos == 0:
109 self.bytepos = self.samplenum
110 sdq, = self.wait({0: 'r'})
112 # Check for 0 or 1 data bits, or the BREAK symbol.
113 delta = self.samplenum - self.startsample
114 if delta > break_threshold:
116 elif delta > self.half_bit_width: