]> sigrok.org Git - libsigrokdecode.git/blame - decoders/spdif/pd.py
ssi32: rename reset() helper method
[libsigrokdecode.git] / decoders / spdif / pd.py
CommitLineData
cb6d4c6d
GW
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2014 Guenther Wenninger <robin@bitschubbser.org>
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
4539e9ca 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
cb6d4c6d
GW
18##
19
20import sigrokdecode as srd
21
22class SamplerateError(Exception):
23 pass
24
25class Decoder(srd.Decoder):
5e39fa54 26 api_version = 3
cb6d4c6d
GW
27 id = 'spdif'
28 name = 'S/PDIF'
29 longname = 'Sony/Philips Digital Interface Format'
30 desc = 'Serial bus for connecting digital audio devices.'
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = ['spdif']
34 channels = (
35 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
36 )
37 annotations = (
38 ('bitrate', 'Bitrate / baudrate'),
39 ('preamble', 'Preamble'),
40 ('bits', 'Bits'),
41 ('aux', 'Auxillary-audio-databits'),
42 ('samples', 'Audio Samples'),
43 ('validity', 'Data Valid'),
44 ('subcode', 'Subcode data'),
45 ('chan_stat', 'Channnel Status'),
46 ('parity', 'Parity Bit'),
47 )
48 annotation_rows = (
49 ('info', 'Info', (0, 1, 3, 5, 6, 7, 8)),
50 ('bits', 'Bits', (2,)),
51 ('samples', 'Samples', (4,)),
52 )
53
54 def putx(self, ss, es, data):
55 self.put(ss, es, self.out_ann, data)
56
cc16130e
UH
57 def puty(self, data):
58 self.put(self.ss_edge, self.samplenum, self.out_ann, data)
59
92b7b49f 60 def __init__(self):
5302d1e9 61 self.state = 'GET FIRST PULSE WIDTH'
cb6d4c6d
GW
62 self.ss_edge = None
63 self.first_edge = True
5e39fa54 64 self.samplenum_prev_edge = 0
cb6d4c6d
GW
65 self.pulse_width = 0
66
67 self.clocks = []
68 self.range1 = 0
69 self.range2 = 0
70
71 self.preamble_state = 0
72 self.preamble = []
73 self.seen_preamble = False
74 self.last_preamble = 0
75
76 self.first_one = True
77 self.subframe = []
78
79 def start(self):
80 self.out_ann = self.register(srd.OUTPUT_ANN)
81
82 def metadata(self, key, value):
83 if key == srd.SRD_CONF_SAMPLERATE:
84 self.samplerate = value
85
58636105 86 def get_pulse_type(self):
cb6d4c6d
GW
87 if self.range1 == 0 or self.range2 == 0:
88 return -1
58636105 89 if self.pulse_width >= self.range2:
cb6d4c6d 90 return 2
58636105 91 elif self.pulse_width >= self.range1:
cb6d4c6d
GW
92 return 0
93 else:
94 return 1
95
eb148e7a
UH
96 def find_first_pulse_width(self):
97 if self.pulse_width != 0:
98 self.clocks.append(self.pulse_width)
5302d1e9 99 self.state = 'GET SECOND PULSE WIDTH'
eb148e7a
UH
100
101 def find_second_pulse_width(self):
102 if self.pulse_width > (self.clocks[0] * 1.3) or \
103 self.pulse_width < (self.clocks[0] * 0.7):
104 self.clocks.append(self.pulse_width)
5302d1e9 105 self.state = 'GET THIRD PULSE WIDTH'
eb148e7a
UH
106
107 def find_third_pulse_width(self):
108 if not ((self.pulse_width > (self.clocks[0] * 1.3) or \
109 self.pulse_width < (self.clocks[0] * 0.7)) \
110 and (self.pulse_width > (self.clocks[1] * 1.3) or \
111 self.pulse_width < (self.clocks[1] * 0.7))):
112 return
113
114 self.clocks.append(self.pulse_width)
115 self.clocks.sort()
116 self.range1 = (self.clocks[0] + self.clocks[1]) / 2
117 self.range2 = (self.clocks[1] + self.clocks[2]) / 2
118 spdif_bitrate = int(self.samplerate / (self.clocks[2] / 1.5))
119 self.ss_edge = 0
120
cc16130e
UH
121 self.puty([0, ['Signal Bitrate: %d Mbit/s (=> %d kHz)' % \
122 (spdif_bitrate, (spdif_bitrate/ (2 * 32)))]])
eb148e7a
UH
123
124 clock_period_nsec = 1000000000 / spdif_bitrate
125
126 self.last_preamble = self.samplenum
127
128 # We are done recovering the clock, now let's decode the data stream.
5302d1e9 129 self.state = 'DECODE STREAM'
eb148e7a
UH
130
131 def decode_stream(self):
58636105 132 pulse = self.get_pulse_type()
eb148e7a
UH
133
134 if not self.seen_preamble:
135 # This is probably the start of a preamble, decode it.
136 if pulse == 2:
58636105 137 self.preamble.append(self.get_pulse_type())
5302d1e9 138 self.state = 'DECODE PREAMBLE'
eb148e7a
UH
139 self.ss_edge = self.samplenum - self.pulse_width - 1
140 return
141
142 # We've seen a preamble.
143 if pulse == 1 and self.first_one:
144 self.first_one = False
145 self.subframe.append([pulse, self.samplenum - \
146 self.pulse_width - 1, self.samplenum])
147 elif pulse == 1 and not self.first_one:
148 self.subframe[-1][2] = self.samplenum
149 self.putx(self.subframe[-1][1], self.samplenum, [2, ['1']])
150 self.bitcount += 1
151 self.first_one = True
152 else:
153 self.subframe.append([pulse, self.samplenum - \
154 self.pulse_width - 1, self.samplenum])
155 self.putx(self.samplenum - self.pulse_width - 1,
156 self.samplenum, [2, ['0']])
157 self.bitcount += 1
158
159 if self.bitcount == 28:
160 aux_audio_data = self.subframe[0:4]
161 sam, sam_rot = '', ''
162 for a in aux_audio_data:
163 sam = sam + str(a[0])
164 sam_rot = str(a[0]) + sam_rot
165 sample = self.subframe[4:24]
166 for s in sample:
167 sam = sam + str(s[0])
168 sam_rot = str(s[0]) + sam_rot
169 validity = self.subframe[24:25]
170 subcode_data = self.subframe[25:26]
171 channel_status = self.subframe[26:27]
172 parity = self.subframe[27:28]
173
174 self.putx(aux_audio_data[0][1], aux_audio_data[3][2], \
175 [3, ['Aux 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]])
176 self.putx(sample[0][1], sample[19][2], \
177 [3, ['Sample 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]])
178 self.putx(aux_audio_data[0][1], sample[19][2], \
179 [4, ['Audio 0x%x' % int(sam_rot, 2), '0x%x' % int(sam_rot, 2)]])
180 if validity[0][0] == 0:
181 self.putx(validity[0][1], validity[0][2], [5, ['V']])
182 else:
183 self.putx(validity[0][1], validity[0][2], [5, ['E']])
184 self.putx(subcode_data[0][1], subcode_data[0][2],
185 [6, ['S: %d' % subcode_data[0][0]]])
186 self.putx(channel_status[0][1], channel_status[0][2],
187 [7, ['C: %d' % channel_status[0][0]]])
188 self.putx(parity[0][1], parity[0][2], [8, ['P: %d' % parity[0][0]]])
189
190 self.subframe = []
191 self.seen_preamble = False
192 self.bitcount = 0
193
5302d1e9 194 def decode_preamble(self):
eb148e7a 195 if self.preamble_state == 0:
58636105 196 self.preamble.append(self.get_pulse_type())
eb148e7a
UH
197 self.preamble_state = 1
198 elif self.preamble_state == 1:
58636105 199 self.preamble.append(self.get_pulse_type())
eb148e7a
UH
200 self.preamble_state = 2
201 elif self.preamble_state == 2:
58636105 202 self.preamble.append(self.get_pulse_type())
eb148e7a 203 self.preamble_state = 0
5302d1e9 204 self.state = 'DECODE STREAM'
eb148e7a 205 if self.preamble == [2, 0, 1, 0]:
cc16130e 206 self.puty([1, ['Preamble W', 'W']])
eb148e7a 207 elif self.preamble == [2, 2, 1, 1]:
cc16130e 208 self.puty([1, ['Preamble M', 'M']])
eb148e7a 209 elif self.preamble == [2, 1, 1, 2]:
cc16130e 210 self.puty([1, ['Preamble B', 'B']])
eb148e7a 211 else:
868fd207 212 self.puty([1, ['Unknown Preamble', 'Unknown Prea.', 'U']])
eb148e7a
UH
213 self.preamble = []
214 self.seen_preamble = True
215 self.bitcount = 0
216 self.first_one = True
217
218 self.last_preamble = self.samplenum
219
5e39fa54 220 def decode(self):
cb6d4c6d
GW
221 if not self.samplerate:
222 raise SamplerateError('Cannot decode without samplerate.')
223
5e39fa54
UH
224 # Throw away first detected edge as it might be mangled data.
225 self.wait({0: 'e'})
226
227 while True:
228 # Wait for any edge (rising or falling).
229 (data,) = self.wait({0: 'e'})
230 self.pulse_width = self.samplenum - self.samplenum_prev_edge - 1
231 self.samplenum_prev_edge = self.samplenum
232
233 if self.state == 'GET FIRST PULSE WIDTH':
234 self.find_first_pulse_width()
235 elif self.state == 'GET SECOND PULSE WIDTH':
236 self.find_second_pulse_width()
237 elif self.state == 'GET THIRD PULSE WIDTH':
238 self.find_third_pulse_width()
239 elif self.state == 'DECODE STREAM':
240 self.decode_stream()
241 elif self.state == 'DECODE PREAMBLE':
242 self.decode_preamble()