]>
Commit | Line | Data |
---|---|---|
2ab416c2 JH |
1 | ## |
2 | ## This file is part of the sigrok 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, write to the Free Software | |
18 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ## | |
20 | ||
21 | # I2S protocol decoder | |
22 | ||
23 | import sigrokdecode as srd | |
24 | ||
25 | # Annotation formats | |
26 | ANN_HEX = 0 | |
27 | ||
28 | class Decoder(srd.Decoder): | |
29 | api_version = 1 | |
30 | id = 'i2s' | |
31 | name = 'I2S' | |
32 | longname = 'Integrated Interchip Sound' | |
a465436e | 33 | desc = 'Serial bus for connecting digital audio devices.' |
2ab416c2 JH |
34 | license = 'gplv2+' |
35 | inputs = ['logic'] | |
36 | outputs = ['i2s'] | |
37 | probes = [ | |
38 | {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'}, | |
39 | {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'}, | |
ee3e279c | 40 | {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'}, |
2ab416c2 | 41 | ] |
ee3e279c UH |
42 | optional_probes = [] |
43 | options = {} | |
2ab416c2 | 44 | annotations = [ |
ee3e279c | 45 | ['Hex', 'Annotations in hex format'], |
2ab416c2 JH |
46 | ] |
47 | ||
48 | def __init__(self, **kwargs): | |
49 | self.oldsck = 1 | |
50 | self.oldws = 1 | |
51 | self.bitcount = 0 | |
52 | self.data = 0 | |
53 | self.samplesreceived = 0 | |
780a5bee | 54 | self.first_sample = None |
2ab416c2 JH |
55 | self.start_sample = None |
56 | self.samplenum = -1 | |
abbbd2ba | 57 | self.wordlength = -1 |
2ab416c2 JH |
58 | |
59 | def start(self, metadata): | |
780a5bee | 60 | self.samplerate = metadata['samplerate'] |
2ab416c2 JH |
61 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2s') |
62 | self.out_ann = self.add(srd.OUTPUT_ANN, 'i2s') | |
63 | ||
64 | def report(self): | |
780a5bee | 65 | |
ee3e279c | 66 | # Calculate the sample rate. |
780a5bee JH |
67 | samplerate = '?' |
68 | if self.start_sample != None and \ | |
69 | self.first_sample != None and \ | |
70 | self.start_sample > self.first_sample: | |
ee3e279c | 71 | samplerate = '%d' % (self.samplesreceived * |
780a5bee JH |
72 | self.samplerate / (self.start_sample - |
73 | self.first_sample)) | |
74 | ||
75 | return 'I2S: %d %d-bit samples received at %sHz' % \ | |
76 | (self.samplesreceived, self.wordlength, samplerate) | |
2ab416c2 JH |
77 | |
78 | def decode(self, ss, es, data): | |
79 | for samplenum, (sck, ws, sd) in data: | |
80 | ||
81 | # Ignore sample if the bit clock hasn't changed. | |
82 | if sck == self.oldsck: | |
83 | continue | |
84 | ||
85 | self.oldsck = sck | |
ee3e279c | 86 | if sck == 0: # Ignore the falling clock edge. |
2ab416c2 JH |
87 | continue |
88 | ||
89 | self.data = (self.data << 1) | sd | |
90 | self.bitcount += 1 | |
91 | ||
ee3e279c | 92 | # This was not the LSB unless WS has flipped. |
2ab416c2 JH |
93 | if ws == self.oldws: |
94 | continue | |
95 | ||
ee3e279c | 96 | # Only submit the sample, if we received the beginning of it. |
2ab416c2 JH |
97 | if self.start_sample != None: |
98 | self.samplesreceived += 1 | |
99 | self.put(self.start_sample, self.samplenum, self.out_proto, | |
ee3e279c | 100 | ['data', self.data]) |
2ab416c2 | 101 | self.put(self.start_sample, self.samplenum, self.out_ann, |
ee3e279c UH |
102 | [ANN_HEX, ['%s: 0x%08x' % ('L' if self.oldws else 'R', |
103 | self.data)]]) | |
abbbd2ba | 104 | |
ee3e279c | 105 | # Check that the data word was the correct length. |
abbbd2ba JH |
106 | if self.wordlength != -1 and self.wordlength != self.bitcount: |
107 | self.put(self.start_sample, self.samplenum, self.out_ann, | |
108 | [ANN_HEX, ['WARNING: Received a %d-bit word, when a ' | |
109 | '%d-bit word was expected' % (self.bitcount, | |
110 | self.wordlength)]]) | |
111 | ||
112 | self.wordlength = self.bitcount | |
2ab416c2 JH |
113 | |
114 | # Reset decoder state. | |
115 | self.data = 0 | |
116 | self.bitcount = 0 | |
117 | self.start_sample = self.samplenum | |
780a5bee | 118 | |
ee3e279c | 119 | # Save the first sample position. |
780a5bee JH |
120 | if self.first_sample == None: |
121 | self.first_sample = self.samplenum | |
122 | ||
2ab416c2 | 123 | self.oldws = ws |
ee3e279c | 124 |