2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2019 Ben Dooks <ben.dooks@codethink.co.uk>
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.
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.
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/>.
20 import sigrokdecode as srd
24 class Decoder(srd.Decoder):
28 longname = 'Time division multiplex audio'
29 desc = 'TDM multi-channel audio protocol.'
35 { 'id': 'clock', 'name': 'Bitclk', 'desc': 'Data bit clock' },
36 { 'id': 'frame', 'name': 'Framesync', 'desc': 'Frame sync' },
37 { 'id': 'data', 'name': 'Data', 'desc': 'Serial data' },
40 {'id': 'bps', 'desc': 'Bits per sample', 'default': 16 },
41 {'id': 'channels', 'desc': 'Channels per frame', 'default': MAX_CHANNELS },
42 {'id': 'edge', 'desc': 'Clock edge to sample on', 'default': 'rising', 'values': ('rising', 'falling') }
44 annotations = tuple(('ch%d' % i, 'Ch%d' % i) for i in range(MAX_CHANNELS))
45 annotation_rows = tuple(('ch%d-vals' % i, 'Ch%d' % i, (i,)) for i in range(MAX_CHANNELS))
51 self.samplerate = None
52 self.channels = MAX_CHANNELS
62 def metdatadata(self, key, value):
63 if key == srd.SRD_CONF_SAMPLERATE:
64 self.samplerate = value
67 self.out_ann = self.register(srd.OUTPUT_ANN)
68 self.bitdepth = self.options['bps']
69 self.edge = self.options['edge']
73 # Wait for edge of clock (sample on rising/falling edge).
74 clock, frame, data = self.wait({0: self.edge[0]})
76 self.data = (self.data << 1) | data
79 if self.ss_block is not None:
80 if self.bitcount >= self.bitdepth:
84 c1 = 'Channel %d' % self.channel
85 c2 = 'C%d' % self.channel
86 c3 = '%d' % self.channel
87 if self.bitdepth <= 8:
88 v = '%02x' % self.data
89 elif self.bitdepth <= 16:
90 v = '%04x' % self.data
92 v = '%08x' % self.data
94 if self.channel < self.channels:
99 self.put(self.ss_block, self.samplenum, self.out_ann,
100 [ch, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
101 '%s: %s' % (c3, v)]])
103 self.ss_block = self.samplenum
104 self.samplecount += 1
106 # Check for new frame.
107 # Note, frame may be a single clock, or active for the first
108 # sample in the frame.
109 if frame != self.lastframe and frame == 1:
113 if self.ss_block is None:
116 self.lastframe = frame