]> sigrok.org Git - libsigrokdecode.git/blob - decoders/tdm_audio/pd.py
All PDs: Consistently use singular/plural for annotation classes/rows.
[libsigrokdecode.git] / decoders / tdm_audio / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2019 Ben Dooks <ben.dooks@codethink.co.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
20 import sigrokdecode as srd
21
22 MAX_CHANNELS = 8
23
24 class Decoder(srd.Decoder):
25     api_version = 3
26     id = 'tdm_audio'
27     name = 'TDM audio'
28     longname = 'Time division multiplex audio'
29     desc = 'TDM multi-channel audio protocol.'
30     license = 'gplv2+'
31     inputs = ['logic']
32     outputs = []
33     tags = ['Audio']
34     channels = (
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' },
38     )
39     options = (
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') }
43     )
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))
46
47     def __init__(self):
48         self.reset()
49
50     def reset(self):
51         self.samplerate = None
52         self.channels = MAX_CHANNELS
53         self.channel = 0
54         self.bitdepth = 16
55         self.bitcount = 0
56         self.samplecount = 0
57         self.lastsync = 0
58         self.lastframe = 0
59         self.data = 0
60         self.ss_block = None
61
62     def metdatadata(self, key, value):
63         if key == srd.SRD_CONF_SAMPLERATE:
64             self.samplerate = value
65
66     def start(self):
67         self.out_ann = self.register(srd.OUTPUT_ANN)
68         self.bitdepth = self.options['bps']
69         self.edge = self.options['edge']
70
71     def decode(self):
72         while True:
73             # Wait for edge of clock (sample on rising/falling edge).
74             clock, frame, data = self.wait({0: self.edge[0]})
75
76             self.data = (self.data << 1) | data
77             self.bitcount += 1
78
79             if self.ss_block is not None:
80                 if self.bitcount >= self.bitdepth:
81                     self.bitcount = 0
82                     self.channel += 1
83
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
91                     else:
92                         v = '%08x' % self.data
93
94                     if self.channel < self.channels:
95                         ch = self.channel
96                     else:
97                         ch = 0
98
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)]])
102                     self.data = 0
103                     self.ss_block = self.samplenum
104                     self.samplecount += 1
105
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:
110                 self.channel = 0
111                 self.bitcount = 0
112                 self.data = 0
113                 if self.ss_block is None:
114                     self.ss_block = 0
115
116             self.lastframe = frame