]> sigrok.org Git - libsigrokdecode.git/blob - decoders/sdq/pd.py
sdq: simplify decode routine, no state machine required
[libsigrokdecode.git] / decoders / sdq / pd.py
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
20 from common.srdhelper import bitpack
21 import sigrokdecode as srd
22
23 class SamplerateError(Exception):
24     pass
25
26 class 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
62     def __init__(self):
63         self.reset()
64
65     def reset(self):
66         self.samplerate = None
67         self.startsample = 0
68         self.bits = []
69         self.bytepos = 0
70
71     def start(self):
72         self.out_ann = self.register(srd.OUTPUT_ANN)
73
74     def metadata(self, key, value):
75         if key == srd.SRD_CONF_SAMPLERATE:
76             self.samplerate = value
77
78     def handle_bit(self, bit):
79         self.bits.append(bit)
80         self.putetu([0, ['Bit: %d' % bit, '%d' % bit]])
81
82         if len(self.bits) == 8:
83             byte = bitpack(self.bits)
84             self.putbetu([1, ['Byte: %#04x' % byte, '%#04x' % byte]])
85             self.bits = []
86             self.bytepos = 0
87
88     def handle_break(self):
89         self.puts([2, ['Break', 'BR']])
90         self.bits = []
91         self.startsample = self.samplenum
92         self.bytepos = 0
93
94     def decode(self):
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
101
102         # Wait until the line is high before inspecting input data.
103         sdq, = self.wait({0: 'h'})
104         while True:
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'})
111
112             # Check for 0 or 1 data bits, or the BREAK symbol.
113             delta = self.samplenum - self.startsample
114             if delta > break_threshold:
115                 self.handle_break()
116             elif delta > self.half_bit_width:
117                 self.handle_bit(0)
118             else:
119                 self.handle_bit(1)