]> sigrok.org Git - libsigrokdecode.git/blob - decoders/sdq/pd.py
sdq: only use samplerate and options values after validity check
[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.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
79     def handle_bit(self, bit):
80         self.bits.append(bit)
81         self.putetu([0, ['Bit: %d' % bit, '%d' % bit]])
82
83         if len(self.bits) == 8:
84             byte = bitpack(self.bits)
85             self.putbetu([1, ['Byte: %#04x' % byte, '%#04x' % byte]])
86             self.bits = []
87             self.bytepos = 0
88
89     def handle_break(self):
90         self.puts([2, ['Break', 'BR']])
91         self.bits = []
92         self.startsample = self.samplenum
93         self.bytepos = 0
94
95     def decode(self):
96         if not self.samplerate:
97             raise SamplerateError('Cannot decode without samplerate.')
98         self.bit_width = float(self.samplerate) / float(self.options['bitrate'])
99         self.half_bit_width = self.bit_width / 2.0
100         # BREAK if the line is low for longer than this.
101         self.break_threshold = self.bit_width * 1.2
102
103         while True:
104             if self.state == 'INIT':
105                 sdq, = self.wait({0: 'h'}) # Wait until the line is high before starting
106                 self.state = 'DATA'
107
108             elif self.state == 'DATA':
109                 sdq, = self.wait({0: 'f'}) # Falling edge
110
111                 self.startsample = self.samplenum
112                 if self.bytepos == 0:
113                     self.bytepos = self.samplenum
114
115                 sdq, = self.wait({0: 'r'}) # Rising edge
116
117                 delta = self.samplenum - self.startsample
118                 if delta > self.break_threshold:
119                     self.state = 'BREAK'
120                 elif delta > self.half_bit_width:
121                     self.handle_bit(0)
122                 else:
123                     self.handle_bit(1)
124
125             elif self.state == 'BREAK':
126                 self.handle_break()
127                 self.state = 'DATA'