2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2014 Guenther Wenninger <robin@bitschubbser.org>
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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 import sigrokdecode as srd
23 class SamplerateError(Exception):
26 class Decoder(srd.Decoder):
30 longname = 'Sony/Philips Digital Interface Format'
31 desc = 'Serial bus for connecting digital audio devices.'
36 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
39 ('bitrate', 'Bitrate / baudrate'),
40 ('preamble', 'Preamble'),
42 ('aux', 'Auxillary-audio-databits'),
43 ('samples', 'Audio Samples'),
44 ('validity', 'Data Valid'),
45 ('subcode', 'Subcode data'),
46 ('chan_stat', 'Channnel Status'),
47 ('parity', 'Parity Bit'),
50 ('info', 'Info', (0, 1, 3, 5, 6, 7, 8)),
51 ('bits', 'Bits', (2,)),
52 ('samples', 'Samples', (4,)),
55 def putx(self, ss, es, data):
56 self.put(ss, es, self.out_ann, data)
58 def __init__(self, **kwargs):
62 self.first_edge = True
69 self.preamble_state = 0
71 self.seen_preamble = False
72 self.last_preamble = 0
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 get_pulse_type(self, pulse):
85 if self.range1 == 0 or self.range2 == 0:
87 if pulse >= self.range2:
89 elif pulse >= self.range1:
94 def decode(self, ss, es, data):
95 if not self.samplerate:
96 raise SamplerateError('Cannot decode without samplerate.')
98 for (self.samplenum, pins) in data:
101 # Initialize first self.olddata with the first sample value.
102 if self.olddata == None:
106 # First we need to recover the clock.
107 if self.olddata == data:
108 self.pulse_width += 1
110 # Found rising or falling edge.
112 # Throw away first detected edge as it might be mangled data.
113 self.first_edge = False
117 # Find first pulse width.
118 if self.pulse_width != 0:
119 self.clocks.append(self.pulse_width)
122 elif self.state == 1:
123 # Find second pulse width.
124 if self.pulse_width > (self.clocks[0] * 1.3) or self.pulse_width < (self.clocks[0] * 0.7):
125 self.clocks.append(self.pulse_width)
128 elif self.state == 2:
129 # Find third pulse width.
130 if (self.pulse_width > (self.clocks[0] * 1.3) or self.pulse_width < (self.clocks[0] * 0.7)) \
131 and (self.pulse_width > (self.clocks[1] * 1.3) or self.pulse_width < (self.clocks[1] * 0.7)):
132 self.clocks.append(self.pulse_width)
134 self.range1 = (self.clocks[0] + self.clocks[1]) / 2
135 self.range2 = (self.clocks[1] + self.clocks[2]) / 2
136 spdif_bitrate = int(self.samplerate / (self.clocks[2] / 1.5))
139 self.putx(self.ss_edge, self.samplenum, [0, \
140 ['Signal Bitrate: %d Mbit/s (=> %d kHz)' % \
141 (spdif_bitrate, (spdif_bitrate/ (2 * 32)))]])
143 clock_period_nsec = 1000000000 / spdif_bitrate
145 self.last_preamble = self.samplenum
146 # We are done recovering the clock, now let's decode the data stream.
149 elif self.state == 3:
151 pulse = self.get_pulse_type(self.pulse_width)
153 if self.seen_preamble:
154 if pulse == 1 and self.first_one:
155 self.first_one = False
156 self.subframe.append([pulse, self.samplenum - self.pulse_width - 1, self.samplenum])
157 elif pulse == 1 and not self.first_one:
158 self.subframe[-1][2] = self.samplenum
159 self.putx(self.subframe[-1][1], self.samplenum, [2, ['1']])
161 self.first_one = True
163 self.subframe.append([pulse, self.samplenum - self.pulse_width - 1, self.samplenum])
164 self.putx(self.samplenum - self.pulse_width - 1, self.samplenum, [2, ['0']])
167 if self.bitcount == 28:
168 aux_audio_data = self.subframe[0:4]
169 sam, sam_rot = '', ''
170 for a in aux_audio_data:
171 sam = sam + str(a[0])
172 sam_rot = str(a[0]) + sam_rot
173 sample = self.subframe[4:24]
175 sam = sam + str(s[0])
176 sam_rot = str(s[0]) + sam_rot
177 validity = self.subframe[24:25]
178 subcode_data = self.subframe[25:26]
179 channel_status = self.subframe[26:27]
180 parity = self.subframe[27:28]
182 self.putx(aux_audio_data[0][1], aux_audio_data[3][2], \
183 [3, ['Aux 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]])
184 self.putx(sample[0][1], sample[19][2], \
185 [3, ['Sample 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]])
186 self.putx(aux_audio_data[0][1], sample[19][2], \
187 [4, ['Audio 0x%x' % int(sam_rot, 2), '0x%x' % int(sam_rot, 2)]])
188 if validity[0][0] == 0:
189 self.putx(validity[0][1], validity[0][2], [5, ['V']])
191 self.putx(validity[0][1], validity[0][2], [5, ['E']])
192 self.putx(subcode_data[0][1], subcode_data[0][2], [6, ['S: %d' % subcode_data[0][0]]])
193 self.putx(channel_status[0][1], channel_status[0][2], [7, ['C: %d' % channel_status[0][0]]])
194 self.putx(parity[0][1], parity[0][2], [8, ['P: %d' % parity[0][0]]])
197 self.seen_preamble = False
200 # This is probably the start of a preamble, so go
201 # ahead and decode it.
203 self.preamble.append(self.get_pulse_type(self.pulse_width))
204 self.state = 4 # Decode a preamble.
205 self.ss_edge = self.samplenum - self.pulse_width - 1
207 elif self.state == 4:
208 if self.preamble_state == 0:
209 self.preamble.append(self.get_pulse_type(self.pulse_width))
210 self.preamble_state = 1
211 elif self.preamble_state == 1:
212 self.preamble.append(self.get_pulse_type(self.pulse_width))
213 self.preamble_state = 2
214 elif self.preamble_state == 2:
215 self.preamble.append(self.get_pulse_type(self.pulse_width))
216 self.preamble_state = 0
218 if self.preamble == [2, 0, 1, 0]:
219 self.putx(self.ss_edge, self.samplenum, [1, ['Preamble W', 'W']])
220 elif self.preamble == [2, 2, 1, 1]:
221 self.putx(self.ss_edge, self.samplenum, [1, ['Preamble M', 'M']])
222 elif self.preamble == [2, 1, 1, 2]:
223 self.putx(self.ss_edge, self.samplenum, [1, ['Preamble B', 'B']])
225 self.putx(self.ss_edge, self.samplenum, [1, ['Unknown Preamble', 'Unkown Prea.', 'U']])
227 self.seen_preamble = True
229 self.first_one = True
231 self.last_preamble = self.samplenum