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):
30 BIT, BYTE, BREAK, = range(3)
32 class Decoder(srd.Decoder):
36 longname = 'Texas Instruments SDQ'
37 desc = 'Texas Instruments SDQ. The SDQ protocol is also used by Apple.'
41 tags = ['Embedded/industrial']
43 {'id': 'sdq', 'name': 'SDQ', 'desc': 'Single wire SDQ data line.'},
46 {'id': 'bitrate', 'desc': 'Bit rate', 'default': 98425},
54 ('bits', 'Bits', (Ann.BIT,)),
55 ('bytes', 'Bytes', (Ann.BYTE,)),
56 ('breaks', 'Breaks', (Ann.BREAK,)),
60 self.put(self.startsample, self.samplenum, self.out_ann, data)
62 def putetu(self, data):
63 self.put(self.startsample, self.startsample + int(self.bit_width), self.out_ann, data)
65 def putbetu(self, data):
66 self.put(self.bytepos, self.startsample + int(self.bit_width), self.out_ann, data)
72 self.samplerate = None
78 self.out_ann = self.register(srd.OUTPUT_ANN)
80 def metadata(self, key, value):
81 if key == srd.SRD_CONF_SAMPLERATE:
82 self.samplerate = value
84 def handle_bit(self, bit):
86 self.putetu([Ann.BIT, [
87 'Bit: {:d}'.format(bit),
91 if len(self.bits) == 8:
92 byte = bitpack(self.bits)
93 self.putbetu([Ann.BYTE, [
94 'Byte: 0x{:02x}'.format(byte),
95 '0x{:02x}'.format(byte),
100 def handle_break(self):
101 self.puts([Ann.BREAK, ['Break', 'BR']])
103 self.startsample = self.samplenum
107 if not self.samplerate:
108 raise SamplerateError('Cannot decode without samplerate.')
109 self.bit_width = float(self.samplerate) / float(self.options['bitrate'])
110 self.half_bit_width = self.bit_width / 2.0
111 # BREAK if the line is low for longer than this.
112 break_threshold = self.bit_width * 1.2
114 # Wait until the line is high before inspecting input data.
115 sdq, = self.wait({Pin.SDQ: 'h'})
117 # Get the length of a low pulse (falling to rising edge).
118 sdq, = self.wait({Pin.SDQ: 'f'})
119 self.startsample = self.samplenum
120 if self.bytepos == 0:
121 self.bytepos = self.samplenum
122 sdq, = self.wait({Pin.SDQ: 'r'})
124 # Check for 0 or 1 data bits, or the BREAK symbol.
125 delta = self.samplenum - self.startsample
126 if delta > break_threshold:
128 elif delta > self.half_bit_width: