]> sigrok.org Git - libsigrokdecode.git/blame - decoders/sdq/pd.py
sdq: prefer Python's .format() method for string formatting
[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
65c6eed8
GS
26class Pin:
27 SDQ, = range(1)
28
29class Ann:
30 BIT, BYTE, BREAK, = range(3)
31
b094e813
32class Decoder(srd.Decoder):
33 api_version = 3
34 id = 'sdq'
35 name = 'SDQ'
36 longname = 'Texas Instruments SDQ'
37 desc = 'Texas Instruments SDQ. The SDQ protocol is also used by Apple.'
38 license = 'gplv2+'
39 inputs = ['logic']
40 outputs = []
41 tags = ['Embedded/industrial']
42 channels = (
43 {'id': 'sdq', 'name': 'SDQ', 'desc': 'Single wire SDQ data line.'},
44 )
45 options = (
46 {'id': 'bitrate', 'desc': 'Bit rate', 'default': 98425},
47 )
48 annotations = (
49 ('bit', 'Bit'),
50 ('byte', 'Byte'),
51 ('break', 'Break'),
52 )
53 annotation_rows = (
65c6eed8
GS
54 ('bits', 'Bits', (Ann.BIT,)),
55 ('bytes', 'Bytes', (Ann.BYTE,)),
56 ('breaks', 'Breaks', (Ann.BREAK,)),
b094e813
57 )
58
59 def puts(self, data):
60 self.put(self.startsample, self.samplenum, self.out_ann, data)
61
62 def putetu(self, data):
63 self.put(self.startsample, self.startsample + int(self.bit_width), self.out_ann, data)
64
65 def putbetu(self, data):
66 self.put(self.bytepos, self.startsample + int(self.bit_width), self.out_ann, data)
67
b094e813
68 def __init__(self):
69 self.reset()
70
71 def reset(self):
72 self.samplerate = None
b094e813
73 self.startsample = 0
74 self.bits = []
75 self.bytepos = 0
76
77 def start(self):
78 self.out_ann = self.register(srd.OUTPUT_ANN)
79
80 def metadata(self, key, value):
81 if key == srd.SRD_CONF_SAMPLERATE:
82 self.samplerate = value
b094e813
83
84 def handle_bit(self, bit):
85 self.bits.append(bit)
168b01ad
GS
86 self.putetu([Ann.BIT, [
87 'Bit: {:d}'.format(bit),
88 '{:d}'.format(bit),
89 ]])
b094e813
90
91 if len(self.bits) == 8:
2bb149e6 92 byte = bitpack(self.bits)
168b01ad
GS
93 self.putbetu([Ann.BYTE, [
94 'Byte: 0x{:02x}'.format(byte),
95 '0x{:02x}'.format(byte),
96 ]])
b094e813
97 self.bits = []
98 self.bytepos = 0
99
100 def handle_break(self):
65c6eed8 101 self.puts([Ann.BREAK, ['Break', 'BR']])
b094e813
102 self.bits = []
103 self.startsample = self.samplenum
104 self.bytepos = 0
105
106 def decode(self):
107 if not self.samplerate:
108 raise SamplerateError('Cannot decode without samplerate.')
d7c23408
GS
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.
b6975c68 112 break_threshold = self.bit_width * 1.2
b094e813 113
b6975c68 114 # Wait until the line is high before inspecting input data.
65c6eed8 115 sdq, = self.wait({Pin.SDQ: 'h'})
b094e813 116 while True:
b6975c68 117 # Get the length of a low pulse (falling to rising edge).
65c6eed8 118 sdq, = self.wait({Pin.SDQ: 'f'})
b6975c68
GS
119 self.startsample = self.samplenum
120 if self.bytepos == 0:
121 self.bytepos = self.samplenum
65c6eed8 122 sdq, = self.wait({Pin.SDQ: 'r'})
b6975c68
GS
123
124 # Check for 0 or 1 data bits, or the BREAK symbol.
125 delta = self.samplenum - self.startsample
126 if delta > break_threshold:
b094e813 127 self.handle_break()
b6975c68
GS
128 elif delta > self.half_bit_width:
129 self.handle_bit(0)
130 else:
131 self.handle_bit(1)