]> sigrok.org Git - libsigrokdecode.git/blame - decoders/sdq/pd.py
sdq: use common bits to number conversion
[libsigrokdecode.git] / decoders / sdq / pd.py
CommitLineData
b094e813
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2019-2020 Philip Åkesson <philip.akesson@gmail.com>
5##
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.
10##
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.
15##
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/>.
18##
19
2bb149e6 20from common.srdhelper import bitpack
b094e813
21import sigrokdecode as srd
22
23class SamplerateError(Exception):
24 pass
25
26class Decoder(srd.Decoder):
27 api_version = 3
28 id = 'sdq'
29 name = 'SDQ'
30 longname = 'Texas Instruments SDQ'
31 desc = 'Texas Instruments SDQ. The SDQ protocol is also used by Apple.'
32 license = 'gplv2+'
33 inputs = ['logic']
34 outputs = []
35 tags = ['Embedded/industrial']
36 channels = (
37 {'id': 'sdq', 'name': 'SDQ', 'desc': 'Single wire SDQ data line.'},
38 )
39 options = (
40 {'id': 'bitrate', 'desc': 'Bit rate', 'default': 98425},
41 )
42 annotations = (
43 ('bit', 'Bit'),
44 ('byte', 'Byte'),
45 ('break', 'Break'),
46 )
47 annotation_rows = (
48 ('bits', 'Bits', (0,)),
49 ('bytes', 'Bytes', (1,)),
50 ('breaks', 'Breaks', (2,)),
51 )
52
53 def puts(self, data):
54 self.put(self.startsample, self.samplenum, self.out_ann, data)
55
56 def putetu(self, data):
57 self.put(self.startsample, self.startsample + int(self.bit_width), self.out_ann, data)
58
59 def putbetu(self, data):
60 self.put(self.bytepos, self.startsample + int(self.bit_width), self.out_ann, data)
61
b094e813
62 def __init__(self):
63 self.reset()
64
65 def reset(self):
66 self.samplerate = None
67 self.state = 'INIT'
68 self.startsample = 0
69 self.bits = []
70 self.bytepos = 0
71
72 def start(self):
73 self.out_ann = self.register(srd.OUTPUT_ANN)
74
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
81
82 def handle_bit(self, bit):
83 self.bits.append(bit)
84 self.putetu([0, ['Bit: %d' % bit, '%d' % bit]])
85
86 if len(self.bits) == 8:
2bb149e6 87 byte = bitpack(self.bits)
b094e813
88 self.putbetu([1, ['Byte: %#04x' % byte, '%#04x' % byte]])
89 self.bits = []
90 self.bytepos = 0
91
92 def handle_break(self):
93 self.puts([2, ['Break', 'BR']])
94 self.bits = []
95 self.startsample = self.samplenum
96 self.bytepos = 0
97
98 def decode(self):
99 if not self.samplerate:
100 raise SamplerateError('Cannot decode without samplerate.')
101
102 while True:
103 if self.state == 'INIT':
104 sdq, = self.wait({0: 'h'}) # Wait until the line is high before starting
105 self.state = 'DATA'
106
107 elif self.state == 'DATA':
108 sdq, = self.wait({0: 'f'}) # Falling edge
109
110 self.startsample = self.samplenum
111 if self.bytepos == 0:
112 self.bytepos = self.samplenum
113
114 sdq, = self.wait({0: 'r'}) # Rising edge
115
116 delta = self.samplenum - self.startsample
117 if delta > self.break_threshold:
118 self.state = 'BREAK'
119 elif delta > self.half_bit_width:
120 self.handle_bit(0)
121 else:
122 self.handle_bit(1)
123
124 elif self.state == 'BREAK':
125 self.handle_break()
126 self.state = 'DATA'