]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | import sigrokdecode as srd | |
21 | ||
22 | class SamplerateError(Exception): | |
23 | pass | |
24 | ||
25 | class 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): |
10aeb8ea GS |
61 | self.reset() |
62 | ||
63 | def reset(self): | |
5302d1e9 | 64 | self.state = 'GET FIRST PULSE WIDTH' |
cb6d4c6d GW |
65 | self.ss_edge = None |
66 | self.first_edge = True | |
5e39fa54 | 67 | self.samplenum_prev_edge = 0 |
cb6d4c6d GW |
68 | self.pulse_width = 0 |
69 | ||
70 | self.clocks = [] | |
71 | self.range1 = 0 | |
72 | self.range2 = 0 | |
73 | ||
74 | self.preamble_state = 0 | |
75 | self.preamble = [] | |
76 | self.seen_preamble = False | |
77 | self.last_preamble = 0 | |
78 | ||
79 | self.first_one = True | |
80 | self.subframe = [] | |
81 | ||
82 | def start(self): | |
83 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
84 | ||
85 | def metadata(self, key, value): | |
86 | if key == srd.SRD_CONF_SAMPLERATE: | |
87 | self.samplerate = value | |
88 | ||
58636105 | 89 | def get_pulse_type(self): |
cb6d4c6d GW |
90 | if self.range1 == 0 or self.range2 == 0: |
91 | return -1 | |
58636105 | 92 | if self.pulse_width >= self.range2: |
cb6d4c6d | 93 | return 2 |
58636105 | 94 | elif self.pulse_width >= self.range1: |
cb6d4c6d GW |
95 | return 0 |
96 | else: | |
97 | return 1 | |
98 | ||
eb148e7a UH |
99 | def find_first_pulse_width(self): |
100 | if self.pulse_width != 0: | |
101 | self.clocks.append(self.pulse_width) | |
5302d1e9 | 102 | self.state = 'GET SECOND PULSE WIDTH' |
eb148e7a UH |
103 | |
104 | def find_second_pulse_width(self): | |
105 | if self.pulse_width > (self.clocks[0] * 1.3) or \ | |
106 | self.pulse_width < (self.clocks[0] * 0.7): | |
107 | self.clocks.append(self.pulse_width) | |
5302d1e9 | 108 | self.state = 'GET THIRD PULSE WIDTH' |
eb148e7a UH |
109 | |
110 | def find_third_pulse_width(self): | |
111 | if not ((self.pulse_width > (self.clocks[0] * 1.3) or \ | |
112 | self.pulse_width < (self.clocks[0] * 0.7)) \ | |
113 | and (self.pulse_width > (self.clocks[1] * 1.3) or \ | |
114 | self.pulse_width < (self.clocks[1] * 0.7))): | |
115 | return | |
116 | ||
117 | self.clocks.append(self.pulse_width) | |
118 | self.clocks.sort() | |
119 | self.range1 = (self.clocks[0] + self.clocks[1]) / 2 | |
120 | self.range2 = (self.clocks[1] + self.clocks[2]) / 2 | |
121 | spdif_bitrate = int(self.samplerate / (self.clocks[2] / 1.5)) | |
122 | self.ss_edge = 0 | |
123 | ||
cc16130e UH |
124 | self.puty([0, ['Signal Bitrate: %d Mbit/s (=> %d kHz)' % \ |
125 | (spdif_bitrate, (spdif_bitrate/ (2 * 32)))]]) | |
eb148e7a UH |
126 | |
127 | clock_period_nsec = 1000000000 / spdif_bitrate | |
128 | ||
129 | self.last_preamble = self.samplenum | |
130 | ||
131 | # We are done recovering the clock, now let's decode the data stream. | |
5302d1e9 | 132 | self.state = 'DECODE STREAM' |
eb148e7a UH |
133 | |
134 | def decode_stream(self): | |
58636105 | 135 | pulse = self.get_pulse_type() |
eb148e7a UH |
136 | |
137 | if not self.seen_preamble: | |
138 | # This is probably the start of a preamble, decode it. | |
139 | if pulse == 2: | |
58636105 | 140 | self.preamble.append(self.get_pulse_type()) |
5302d1e9 | 141 | self.state = 'DECODE PREAMBLE' |
eb148e7a UH |
142 | self.ss_edge = self.samplenum - self.pulse_width - 1 |
143 | return | |
144 | ||
145 | # We've seen a preamble. | |
146 | if pulse == 1 and self.first_one: | |
147 | self.first_one = False | |
148 | self.subframe.append([pulse, self.samplenum - \ | |
149 | self.pulse_width - 1, self.samplenum]) | |
150 | elif pulse == 1 and not self.first_one: | |
151 | self.subframe[-1][2] = self.samplenum | |
152 | self.putx(self.subframe[-1][1], self.samplenum, [2, ['1']]) | |
153 | self.bitcount += 1 | |
154 | self.first_one = True | |
155 | else: | |
156 | self.subframe.append([pulse, self.samplenum - \ | |
157 | self.pulse_width - 1, self.samplenum]) | |
158 | self.putx(self.samplenum - self.pulse_width - 1, | |
159 | self.samplenum, [2, ['0']]) | |
160 | self.bitcount += 1 | |
161 | ||
162 | if self.bitcount == 28: | |
163 | aux_audio_data = self.subframe[0:4] | |
164 | sam, sam_rot = '', '' | |
165 | for a in aux_audio_data: | |
166 | sam = sam + str(a[0]) | |
167 | sam_rot = str(a[0]) + sam_rot | |
168 | sample = self.subframe[4:24] | |
169 | for s in sample: | |
170 | sam = sam + str(s[0]) | |
171 | sam_rot = str(s[0]) + sam_rot | |
172 | validity = self.subframe[24:25] | |
173 | subcode_data = self.subframe[25:26] | |
174 | channel_status = self.subframe[26:27] | |
175 | parity = self.subframe[27:28] | |
176 | ||
177 | self.putx(aux_audio_data[0][1], aux_audio_data[3][2], \ | |
178 | [3, ['Aux 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]]) | |
179 | self.putx(sample[0][1], sample[19][2], \ | |
180 | [3, ['Sample 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]]) | |
181 | self.putx(aux_audio_data[0][1], sample[19][2], \ | |
182 | [4, ['Audio 0x%x' % int(sam_rot, 2), '0x%x' % int(sam_rot, 2)]]) | |
183 | if validity[0][0] == 0: | |
184 | self.putx(validity[0][1], validity[0][2], [5, ['V']]) | |
185 | else: | |
186 | self.putx(validity[0][1], validity[0][2], [5, ['E']]) | |
187 | self.putx(subcode_data[0][1], subcode_data[0][2], | |
188 | [6, ['S: %d' % subcode_data[0][0]]]) | |
189 | self.putx(channel_status[0][1], channel_status[0][2], | |
190 | [7, ['C: %d' % channel_status[0][0]]]) | |
191 | self.putx(parity[0][1], parity[0][2], [8, ['P: %d' % parity[0][0]]]) | |
192 | ||
193 | self.subframe = [] | |
194 | self.seen_preamble = False | |
195 | self.bitcount = 0 | |
196 | ||
5302d1e9 | 197 | def decode_preamble(self): |
eb148e7a | 198 | if self.preamble_state == 0: |
58636105 | 199 | self.preamble.append(self.get_pulse_type()) |
eb148e7a UH |
200 | self.preamble_state = 1 |
201 | elif self.preamble_state == 1: | |
58636105 | 202 | self.preamble.append(self.get_pulse_type()) |
eb148e7a UH |
203 | self.preamble_state = 2 |
204 | elif self.preamble_state == 2: | |
58636105 | 205 | self.preamble.append(self.get_pulse_type()) |
eb148e7a | 206 | self.preamble_state = 0 |
5302d1e9 | 207 | self.state = 'DECODE STREAM' |
eb148e7a | 208 | if self.preamble == [2, 0, 1, 0]: |
cc16130e | 209 | self.puty([1, ['Preamble W', 'W']]) |
eb148e7a | 210 | elif self.preamble == [2, 2, 1, 1]: |
cc16130e | 211 | self.puty([1, ['Preamble M', 'M']]) |
eb148e7a | 212 | elif self.preamble == [2, 1, 1, 2]: |
cc16130e | 213 | self.puty([1, ['Preamble B', 'B']]) |
eb148e7a | 214 | else: |
868fd207 | 215 | self.puty([1, ['Unknown Preamble', 'Unknown Prea.', 'U']]) |
eb148e7a UH |
216 | self.preamble = [] |
217 | self.seen_preamble = True | |
218 | self.bitcount = 0 | |
219 | self.first_one = True | |
220 | ||
221 | self.last_preamble = self.samplenum | |
222 | ||
5e39fa54 | 223 | def decode(self): |
cb6d4c6d GW |
224 | if not self.samplerate: |
225 | raise SamplerateError('Cannot decode without samplerate.') | |
226 | ||
5e39fa54 UH |
227 | # Throw away first detected edge as it might be mangled data. |
228 | self.wait({0: 'e'}) | |
229 | ||
230 | while True: | |
231 | # Wait for any edge (rising or falling). | |
232 | (data,) = self.wait({0: 'e'}) | |
233 | self.pulse_width = self.samplenum - self.samplenum_prev_edge - 1 | |
234 | self.samplenum_prev_edge = self.samplenum | |
235 | ||
236 | if self.state == 'GET FIRST PULSE WIDTH': | |
237 | self.find_first_pulse_width() | |
238 | elif self.state == 'GET SECOND PULSE WIDTH': | |
239 | self.find_second_pulse_width() | |
240 | elif self.state == 'GET THIRD PULSE WIDTH': | |
241 | self.find_third_pulse_width() | |
242 | elif self.state == 'DECODE STREAM': | |
243 | self.decode_stream() | |
244 | elif self.state == 'DECODE PREAMBLE': | |
245 | self.decode_preamble() |