]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/i2s/pd.py
srd_decoder_unload_all(): Fix a -Wcast-function-type compiler warning.
[libsigrokdecode.git] / decoders / i2s / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
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, see <http://www.gnu.org/licenses/>.
18##
19
20import sigrokdecode as srd
21import struct
22
23'''
24OUTPUT_PYTHON format:
25
26Packet:
27[<ptype>, <pdata>]
28
29<ptype>, <pdata>:
30 - 'DATA', [<channel>, <value>]
31
32<channel>: 'L' or 'R'
33<value>: integer
34'''
35
36class Decoder(srd.Decoder):
37 api_version = 3
38 id = 'i2s'
39 name = 'I²S'
40 longname = 'Integrated Interchip Sound'
41 desc = 'Serial bus for connecting digital audio devices.'
42 license = 'gplv2+'
43 inputs = ['logic']
44 outputs = ['i2s']
45 channels = (
46 {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'},
47 {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'},
48 {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'},
49 )
50 annotations = (
51 ('left', 'Left channel'),
52 ('right', 'Right channel'),
53 ('warnings', 'Warnings'),
54 )
55 binary = (
56 ('wav', 'WAV file'),
57 )
58
59 def __init__(self):
60 self.reset()
61
62 def reset(self):
63 self.samplerate = None
64 self.oldws = 1
65 self.bitcount = 0
66 self.data = 0
67 self.samplesreceived = 0
68 self.first_sample = None
69 self.ss_block = None
70 self.wordlength = -1
71 self.wrote_wav_header = False
72
73 def start(self):
74 self.out_python = self.register(srd.OUTPUT_PYTHON)
75 self.out_binary = self.register(srd.OUTPUT_BINARY)
76 self.out_ann = self.register(srd.OUTPUT_ANN)
77
78 def metadata(self, key, value):
79 if key == srd.SRD_CONF_SAMPLERATE:
80 self.samplerate = value
81
82 def putpb(self, data):
83 self.put(self.ss_block, self.samplenum, self.out_python, data)
84
85 def putbin(self, data):
86 self.put(self.ss_block, self.samplenum, self.out_binary, data)
87
88 def putb(self, data):
89 self.put(self.ss_block, self.samplenum, self.out_ann, data)
90
91 def report(self):
92 # Calculate the sample rate.
93 samplerate = '?'
94 if self.ss_block is not None and \
95 self.first_sample is not None and \
96 self.ss_block > self.first_sample and \
97 self.samplerate:
98 samplerate = '%d' % (self.samplesreceived *
99 self.samplerate / (self.ss_block -
100 self.first_sample))
101
102 return 'I²S: %d %d-bit samples received at %sHz' % \
103 (self.samplesreceived, self.wordlength, samplerate)
104
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\xfa\x00\x00' # Byterate (64000)
117 h += b'\x04\x00' # Blockalign (4)
118 h += b'\x20\x00' # Bits per sample (32)
119 # Data subchunk
120 h += b'data'
121 h += b'\xff\xff\xff\xff' # Subchunk size (4G bytes) TODO
122 return h
123
124 def wav_sample(self, sample):
125 return struct.pack('<I', self.data)
126
127 def decode(self):
128 while True:
129 # Wait for a rising edge on the SCK pin.
130 sck, ws, sd = self.wait({0: 'r'})
131
132 self.data = (self.data << 1) | sd
133 self.bitcount += 1
134
135 # This was not the LSB unless WS has flipped.
136 if ws == self.oldws:
137 continue
138
139 # Only submit the sample, if we received the beginning of it.
140 if self.ss_block is not None:
141
142 if not self.wrote_wav_header:
143 self.put(0, 0, self.out_binary, [0, self.wav_header()])
144 self.wrote_wav_header = True
145
146 self.samplesreceived += 1
147
148 idx = 0 if self.oldws else 1
149 c1 = 'Left channel' if self.oldws else 'Right channel'
150 c2 = 'Left' if self.oldws else 'Right'
151 c3 = 'L' if self.oldws else 'R'
152 v = '%08x' % self.data
153 self.putpb(['DATA', [c3, self.data]])
154 self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
155 '%s: %s' % (c3, v), c3]])
156 self.putbin([0, self.wav_sample(self.data)])
157
158 # Check that the data word was the correct length.
159 if self.wordlength != -1 and self.wordlength != self.bitcount:
160 self.putb([2, ['Received %d-bit word, expected %d-bit '
161 'word' % (self.bitcount, self.wordlength)]])
162
163 self.wordlength = self.bitcount
164
165 # Reset decoder state.
166 self.data = 0
167 self.bitcount = 0
168 self.ss_block = self.samplenum
169
170 # Save the first sample position.
171 if self.first_sample is None:
172 self.first_sample = self.samplenum
173
174 self.oldws = ws