]>
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' | |
33 | desc = 'I2S is an electrical serial bus interface standard used ' \ | |
34 | 'for connecting digital audio devices together.' | |
35 | license = 'gplv2+' | |
36 | inputs = ['logic'] | |
37 | outputs = ['i2s'] | |
38 | probes = [ | |
39 | {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'}, | |
40 | {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'}, | |
41 | {'id': 'sd', 'name': 'SD', 'desc': 'Serial Data line'}, | |
42 | ] | |
43 | annotations = [ | |
44 | ['ASCII', 'Annotations in ASCII format'], | |
45 | ] | |
46 | ||
47 | def __init__(self, **kwargs): | |
48 | self.oldsck = 1 | |
49 | self.oldws = 1 | |
50 | self.bitcount = 0 | |
51 | self.data = 0 | |
52 | self.samplesreceived = 0 | |
780a5bee | 53 | self.first_sample = None |
2ab416c2 JH |
54 | self.start_sample = None |
55 | self.samplenum = -1 | |
abbbd2ba | 56 | self.wordlength = -1 |
2ab416c2 JH |
57 | |
58 | def start(self, metadata): | |
780a5bee | 59 | self.samplerate = metadata['samplerate'] |
2ab416c2 JH |
60 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2s') |
61 | self.out_ann = self.add(srd.OUTPUT_ANN, 'i2s') | |
62 | ||
63 | def report(self): | |
780a5bee JH |
64 | |
65 | # Calculate the sample rate | |
66 | samplerate = '?' | |
67 | if self.start_sample != None and \ | |
68 | self.first_sample != None and \ | |
69 | self.start_sample > self.first_sample: | |
70 | samplerate = "%d" % (self.samplesreceived * | |
71 | self.samplerate / (self.start_sample - | |
72 | self.first_sample)) | |
73 | ||
74 | return 'I2S: %d %d-bit samples received at %sHz' % \ | |
75 | (self.samplesreceived, self.wordlength, samplerate) | |
2ab416c2 JH |
76 | |
77 | def decode(self, ss, es, data): | |
78 | for samplenum, (sck, ws, sd) in data: | |
79 | ||
80 | # Ignore sample if the bit clock hasn't changed. | |
81 | if sck == self.oldsck: | |
82 | continue | |
83 | ||
84 | self.oldsck = sck | |
85 | if sck == 0: # Ignore the falling clock edge | |
86 | continue | |
87 | ||
88 | self.data = (self.data << 1) | sd | |
89 | self.bitcount += 1 | |
90 | ||
91 | # This was not the LSB unless WS has flipped | |
92 | if ws == self.oldws: | |
93 | continue | |
94 | ||
95 | # Only submit the sample, if we received the beginning of it | |
96 | if self.start_sample != None: | |
97 | self.samplesreceived += 1 | |
98 | self.put(self.start_sample, self.samplenum, self.out_proto, | |
99 | ['data', self.data]) | |
100 | self.put(self.start_sample, self.samplenum, self.out_ann, | |
abbbd2ba JH |
101 | [ANN_HEX, ['%s: 0x%08x' % ('L' if self.oldws else 'R', |
102 | self.data)]]) | |
103 | ||
104 | # Check that the data word was the correct length | |
105 | if self.wordlength != -1 and self.wordlength != self.bitcount: | |
106 | self.put(self.start_sample, self.samplenum, self.out_ann, | |
107 | [ANN_HEX, ['WARNING: Received a %d-bit word, when a ' | |
108 | '%d-bit word was expected' % (self.bitcount, | |
109 | self.wordlength)]]) | |
110 | ||
111 | self.wordlength = self.bitcount | |
2ab416c2 JH |
112 | |
113 | # Reset decoder state. | |
114 | self.data = 0 | |
115 | self.bitcount = 0 | |
116 | self.start_sample = self.samplenum | |
780a5bee JH |
117 | |
118 | # Save the first sample position | |
119 | if self.first_sample == None: | |
120 | self.first_sample = self.samplenum | |
121 | ||
2ab416c2 JH |
122 | self.oldws = ws |
123 |