]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2s/pd.py
i2s: Convert to PD API version 3.
[libsigrokdecode.git] / decoders / i2s / pd.py
CommitLineData
2ab416c2 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
2ab416c2
JH
3##
4## Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
2ab416c2
JH
21import sigrokdecode as srd
22
4adb11a9 23'''
c515eed7 24OUTPUT_PYTHON format:
4adb11a9
UH
25
26Packet:
27[<ptype>, <pdata>]
28
29<ptype>, <pdata>:
30 - 'DATA', [<channel>, <value>]
31
32<channel>: 'L' or 'R'
33<value>: integer
34'''
35
21cda951
UH
36class SamplerateError(Exception):
37 pass
38
2ab416c2 39class Decoder(srd.Decoder):
07e40f34 40 api_version = 3
2ab416c2 41 id = 'i2s'
270a59d8 42 name = 'I²S'
2ab416c2 43 longname = 'Integrated Interchip Sound'
a465436e 44 desc = 'Serial bus for connecting digital audio devices.'
2ab416c2
JH
45 license = 'gplv2+'
46 inputs = ['logic']
47 outputs = ['i2s']
6a15597a 48 channels = (
2ab416c2
JH
49 {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'},
50 {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'},
ee3e279c 51 {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'},
da9bcbd9
BV
52 )
53 annotations = (
54 ('left', 'Left channel'),
55 ('right', 'Right channel'),
56 ('warnings', 'Warnings'),
57 )
e54f222c
UH
58 binary = (
59 ('wav', 'WAV file'),
60 )
2ab416c2 61
92b7b49f 62 def __init__(self):
f372d597 63 self.samplerate = None
2ab416c2
JH
64 self.oldws = 1
65 self.bitcount = 0
66 self.data = 0
67 self.samplesreceived = 0
780a5bee 68 self.first_sample = None
486b19ce 69 self.ss_block = None
abbbd2ba 70 self.wordlength = -1
e54f222c 71 self.wrote_wav_header = False
2ab416c2 72
f372d597 73 def start(self):
c515eed7 74 self.out_python = self.register(srd.OUTPUT_PYTHON)
2f370328 75 self.out_binary = self.register(srd.OUTPUT_BINARY)
be465111 76 self.out_ann = self.register(srd.OUTPUT_ANN)
2ab416c2 77
f372d597
BV
78 def metadata(self, key, value):
79 if key == srd.SRD_CONF_SAMPLERATE:
80 self.samplerate = value
81
2b2b7c30 82 def putpb(self, data):
486b19ce 83 self.put(self.ss_block, self.samplenum, self.out_python, data)
2b2b7c30 84
e54f222c 85 def putbin(self, data):
2f370328 86 self.put(self.ss_block, self.samplenum, self.out_binary, data)
e54f222c 87
2b2b7c30 88 def putb(self, data):
486b19ce 89 self.put(self.ss_block, self.samplenum, self.out_ann, data)
2b2b7c30 90
2ab416c2 91 def report(self):
780a5bee 92
ee3e279c 93 # Calculate the sample rate.
780a5bee 94 samplerate = '?'
486b19ce 95 if self.ss_block is not None and \
35b380b1 96 self.first_sample is not None and \
486b19ce 97 self.ss_block > self.first_sample:
ee3e279c 98 samplerate = '%d' % (self.samplesreceived *
486b19ce 99 self.samplerate / (self.ss_block -
780a5bee
JH
100 self.first_sample))
101
ea48f543 102 return 'I²S: %d %d-bit samples received at %sHz' % \
780a5bee 103 (self.samplesreceived, self.wordlength, samplerate)
2ab416c2 104
e54f222c
UH
105 def wav_header(self):
106 # Chunk descriptor
107 h = b'RIFF'
108 h += b'\x24\x80\x00\x00' # Chunk size (2084)
109 h += b'WAVE'
110 # Fmt subchunk
111 h += b'fmt '
112 h += b'\x10\x00\x00\x00' # Subchunk size (16 bytes)
113 h += b'\x01\x00' # Audio format (0x0001 == PCM)
114 h += b'\x02\x00' # Number of channels (2)
115 h += b'\x80\x3e\x00\x00' # Samplerate (16000)
116 h += b'\x00\x7d\x00\x00' # Byterate (32000)
117 h += b'\x04\x00' # Blockalign (4)
118 h += b'\x10\x00' # Bits per sample (16)
119 # Data subchunk
120 h += b'data'
121 h += b'\xff\xff\x00\x00' # Subchunk size (65535 bytes) TODO
122 return h
123
124 def wav_sample(self, sample):
125 # TODO: This currently assumes U32 samples, and converts to S16.
126 s = sample >> 16
127 if s >= 0x8000:
128 s -= 0x10000
129 lo, hi = s & 0xff, (s >> 8) & 0xff
130 return bytes([lo, hi])
131
07e40f34 132 def decode(self):
21cda951
UH
133 if not self.samplerate:
134 raise SamplerateError('Cannot decode without samplerate.')
07e40f34
UH
135 while True:
136 # Wait for a rising edge on the SCK pin.
137 sck, ws, sd = self.wait({0: 'r'})
2ab416c2
JH
138
139 self.data = (self.data << 1) | sd
140 self.bitcount += 1
141
ee3e279c 142 # This was not the LSB unless WS has flipped.
2ab416c2
JH
143 if ws == self.oldws:
144 continue
145
ee3e279c 146 # Only submit the sample, if we received the beginning of it.
486b19ce 147 if self.ss_block is not None:
e54f222c
UH
148
149 if not self.wrote_wav_header:
2f370328 150 self.put(0, 0, self.out_binary, [0, self.wav_header()])
e54f222c
UH
151 self.wrote_wav_header = True
152
2ab416c2 153 self.samplesreceived += 1
750a808a
UH
154
155 idx = 0 if self.oldws else 1
156 c1 = 'Left channel' if self.oldws else 'Right channel'
157 c2 = 'Left' if self.oldws else 'Right'
158 c3 = 'L' if self.oldws else 'R'
159 v = '%08x' % self.data
160 self.putpb(['DATA', [c3, self.data]])
161 self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
162 '%s: %s' % (c3, v), c3]])
2824e811 163 self.putbin([0, self.wav_sample(self.data)])
abbbd2ba 164
ee3e279c 165 # Check that the data word was the correct length.
abbbd2ba 166 if self.wordlength != -1 and self.wordlength != self.bitcount:
2b2b7c30
UH
167 self.putb([2, ['Received %d-bit word, expected %d-bit '
168 'word' % (self.bitcount, self.wordlength)]])
abbbd2ba
JH
169
170 self.wordlength = self.bitcount
2ab416c2
JH
171
172 # Reset decoder state.
173 self.data = 0
174 self.bitcount = 0
486b19ce 175 self.ss_block = self.samplenum
780a5bee 176
ee3e279c 177 # Save the first sample position.
35b380b1 178 if self.first_sample is None:
2b2b7c30 179 self.first_sample = self.samplenum
780a5bee 180
2ab416c2 181 self.oldws = ws