]>
Commit | Line | Data |
---|---|---|
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 | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
2ab416c2 JH |
18 | ## |
19 | ||
2ab416c2 | 20 | import sigrokdecode as srd |
7f214f53 | 21 | import struct |
2ab416c2 | 22 | |
4adb11a9 | 23 | ''' |
c515eed7 | 24 | OUTPUT_PYTHON format: |
4adb11a9 UH |
25 | |
26 | Packet: | |
27 | [<ptype>, <pdata>] | |
28 | ||
29 | <ptype>, <pdata>: | |
30 | - 'DATA', [<channel>, <value>] | |
31 | ||
32 | <channel>: 'L' or 'R' | |
33 | <value>: integer | |
34 | ''' | |
35 | ||
2ab416c2 | 36 | class Decoder(srd.Decoder): |
07e40f34 | 37 | api_version = 3 |
2ab416c2 | 38 | id = 'i2s' |
270a59d8 | 39 | name = 'I²S' |
2ab416c2 | 40 | longname = 'Integrated Interchip Sound' |
a465436e | 41 | desc = 'Serial bus for connecting digital audio devices.' |
2ab416c2 JH |
42 | license = 'gplv2+' |
43 | inputs = ['logic'] | |
44 | outputs = ['i2s'] | |
d6d8a8a4 | 45 | tags = ['Audio', 'PC'] |
6a15597a | 46 | channels = ( |
2ab416c2 JH |
47 | {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'}, |
48 | {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'}, | |
ee3e279c | 49 | {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'}, |
da9bcbd9 BV |
50 | ) |
51 | annotations = ( | |
52 | ('left', 'Left channel'), | |
53 | ('right', 'Right channel'), | |
54 | ('warnings', 'Warnings'), | |
55 | ) | |
e54f222c UH |
56 | binary = ( |
57 | ('wav', 'WAV file'), | |
58 | ) | |
2ab416c2 | 59 | |
92b7b49f | 60 | def __init__(self): |
10aeb8ea GS |
61 | self.reset() |
62 | ||
63 | def reset(self): | |
f372d597 | 64 | self.samplerate = None |
2ab416c2 JH |
65 | self.oldws = 1 |
66 | self.bitcount = 0 | |
67 | self.data = 0 | |
68 | self.samplesreceived = 0 | |
780a5bee | 69 | self.first_sample = None |
486b19ce | 70 | self.ss_block = None |
abbbd2ba | 71 | self.wordlength = -1 |
e54f222c | 72 | self.wrote_wav_header = False |
2ab416c2 | 73 | |
f372d597 | 74 | def start(self): |
c515eed7 | 75 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
2f370328 | 76 | self.out_binary = self.register(srd.OUTPUT_BINARY) |
be465111 | 77 | self.out_ann = self.register(srd.OUTPUT_ANN) |
2ab416c2 | 78 | |
f372d597 BV |
79 | def metadata(self, key, value): |
80 | if key == srd.SRD_CONF_SAMPLERATE: | |
81 | self.samplerate = value | |
82 | ||
2b2b7c30 | 83 | def putpb(self, data): |
486b19ce | 84 | self.put(self.ss_block, self.samplenum, self.out_python, data) |
2b2b7c30 | 85 | |
e54f222c | 86 | def putbin(self, data): |
2f370328 | 87 | self.put(self.ss_block, self.samplenum, self.out_binary, data) |
e54f222c | 88 | |
2b2b7c30 | 89 | def putb(self, data): |
486b19ce | 90 | self.put(self.ss_block, self.samplenum, self.out_ann, data) |
2b2b7c30 | 91 | |
2ab416c2 | 92 | def report(self): |
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 \ |
72cced49 GS |
97 | self.ss_block > self.first_sample and \ |
98 | self.samplerate: | |
ee3e279c | 99 | samplerate = '%d' % (self.samplesreceived * |
486b19ce | 100 | self.samplerate / (self.ss_block - |
780a5bee JH |
101 | self.first_sample)) |
102 | ||
ea48f543 | 103 | return 'I²S: %d %d-bit samples received at %sHz' % \ |
780a5bee | 104 | (self.samplesreceived, self.wordlength, samplerate) |
2ab416c2 | 105 | |
e54f222c UH |
106 | def wav_header(self): |
107 | # Chunk descriptor | |
108 | h = b'RIFF' | |
109 | h += b'\x24\x80\x00\x00' # Chunk size (2084) | |
110 | h += b'WAVE' | |
111 | # Fmt subchunk | |
112 | h += b'fmt ' | |
113 | h += b'\x10\x00\x00\x00' # Subchunk size (16 bytes) | |
114 | h += b'\x01\x00' # Audio format (0x0001 == PCM) | |
115 | h += b'\x02\x00' # Number of channels (2) | |
116 | h += b'\x80\x3e\x00\x00' # Samplerate (16000) | |
7f214f53 | 117 | h += b'\x00\xfa\x00\x00' # Byterate (64000) |
e54f222c | 118 | h += b'\x04\x00' # Blockalign (4) |
7f214f53 | 119 | h += b'\x20\x00' # Bits per sample (32) |
e54f222c UH |
120 | # Data subchunk |
121 | h += b'data' | |
65658c0a | 122 | h += b'\xff\xff\xff\xff' # Subchunk size (4G bytes) TODO |
e54f222c UH |
123 | return h |
124 | ||
125 | def wav_sample(self, sample): | |
30c855f6 | 126 | return struct.pack('<I', self.data) |
e54f222c | 127 | |
07e40f34 | 128 | def decode(self): |
07e40f34 UH |
129 | while True: |
130 | # Wait for a rising edge on the SCK pin. | |
131 | sck, ws, sd = self.wait({0: 'r'}) | |
2ab416c2 JH |
132 | |
133 | self.data = (self.data << 1) | sd | |
134 | self.bitcount += 1 | |
135 | ||
ee3e279c | 136 | # This was not the LSB unless WS has flipped. |
2ab416c2 JH |
137 | if ws == self.oldws: |
138 | continue | |
139 | ||
ee3e279c | 140 | # Only submit the sample, if we received the beginning of it. |
486b19ce | 141 | if self.ss_block is not None: |
e54f222c UH |
142 | |
143 | if not self.wrote_wav_header: | |
2f370328 | 144 | self.put(0, 0, self.out_binary, [0, self.wav_header()]) |
e54f222c UH |
145 | self.wrote_wav_header = True |
146 | ||
2ab416c2 | 147 | self.samplesreceived += 1 |
750a808a | 148 | |
c78c8f6f WZ |
149 | sck = self.wait({0: 'f'}) |
150 | ||
151 | idx = 0 if not self.oldws else 1 | |
152 | c1 = 'Left channel' if not self.oldws else 'Right channel' | |
153 | c2 = 'Left' if not self.oldws else 'Right' | |
154 | c3 = 'L' if not self.oldws else 'R' | |
750a808a UH |
155 | v = '%08x' % self.data |
156 | self.putpb(['DATA', [c3, self.data]]) | |
157 | self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v), | |
158 | '%s: %s' % (c3, v), c3]]) | |
2824e811 | 159 | self.putbin([0, self.wav_sample(self.data)]) |
abbbd2ba | 160 | |
ee3e279c | 161 | # Check that the data word was the correct length. |
abbbd2ba | 162 | if self.wordlength != -1 and self.wordlength != self.bitcount: |
2b2b7c30 UH |
163 | self.putb([2, ['Received %d-bit word, expected %d-bit ' |
164 | 'word' % (self.bitcount, self.wordlength)]]) | |
abbbd2ba JH |
165 | |
166 | self.wordlength = self.bitcount | |
c78c8f6f WZ |
167 | else: |
168 | sck = self.wait({0: 'f'}) | |
2ab416c2 JH |
169 | |
170 | # Reset decoder state. | |
171 | self.data = 0 | |
172 | self.bitcount = 0 | |
486b19ce | 173 | self.ss_block = self.samplenum |
780a5bee | 174 | |
ee3e279c | 175 | # Save the first sample position. |
35b380b1 | 176 | if self.first_sample is None: |
2b2b7c30 | 177 | self.first_sample = self.samplenum |
780a5bee | 178 | |
2ab416c2 | 179 | self.oldws = ws |