]> sigrok.org Git - libsigrokdecode.git/blob - decoders/tlc5620/pd.py
6e7c454df13d30c872eea856a54b9e58821e56d3
[libsigrokdecode.git] / decoders / tlc5620 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 import sigrokdecode as srd
22
23 dacs = {
24     0: 'DACA',
25     1: 'DACB',
26     2: 'DACC',
27     3: 'DACD',
28 }
29
30 class Decoder(srd.Decoder):
31     api_version = 2
32     id = 'tlc5620'
33     name = 'TI TLC5620'
34     longname = 'Texas Instruments TLC5620'
35     desc = 'Texas Instruments TLC5620 8-bit quad DAC.'
36     license = 'gplv2+'
37     inputs = ['logic']
38     outputs = ['tlc5620']
39     channels = (
40         {'id': 'clk', 'name': 'CLK', 'desc': 'Serial interface clock'},
41         {'id': 'data', 'name': 'DATA', 'desc': 'Serial interface data'},
42     )
43     optional_channels = (
44         {'id': 'load', 'name': 'LOAD', 'desc': 'Serial interface load control'},
45         {'id': 'ldac', 'name': 'LDAC', 'desc': 'Load DAC'},
46     )
47     annotations = (
48         ('dac-select', 'DAC select'),
49         ('gain', 'Gain'),
50         ('value', 'DAC value'),
51         ('data-latch', 'Data latch point'),
52         ('ldac-fall', 'LDAC falling edge'),
53     )
54
55     def __init__(self, **kwargs):
56         self.oldpins = self.oldclk = self.oldload = self.oldldac = None
57         self.datapin = None
58         self.bits = []
59         self.ss_dac = self.es_dac = 0
60         self.ss_gain = self.es_gain = 0
61         self.ss_value = self.es_value = 0
62         self.dac_select = self.gain = self.dac_value = None
63
64     def start(self):
65         self.out_ann = self.register(srd.OUTPUT_ANN)
66
67     def handle_11bits(self):
68         s = ''.join(str(i) for i in self.bits[:2])
69         self.dac_select = s = dacs[int(s, 2)]
70         self.put(self.ss_dac, self.es_dac, self.out_ann,
71                  [0, ['DAC select: %s' % s, 'DAC sel: %s' % s,
72                       'DAC: %s' % s, 'D: %s' % s, s, s[3]]])
73
74         self.gain = g = 1 + self.bits[2]
75         self.put(self.ss_gain, self.es_gain, self.out_ann,
76                  [1, ['Gain: x%d' % g, 'G: x%d' % g, 'x%d' % g]])
77
78         s = ''.join(str(i) for i in self.bits[3:])
79         self.dac_value = v = int(s, 2)
80         self.put(self.ss_value, self.es_value, self.out_ann,
81                  [2, ['DAC value: %d' % v, 'Value: %d' % v, 'Val: %d' % v,
82                       'V: %d' % v, '%d' % v]])
83
84     def handle_falling_edge_load(self):
85         s, v, g = self.dac_select, self.dac_value, self.gain
86         self.put(self.samplenum, self.samplenum, self.out_ann,
87                  [3, ['Setting %s value to %d (x%d gain)' % (s, v, g),
88                       '%s=%d (x%d gain)' % (s, v, g)]])
89
90     def handle_falling_edge_ldac(self):
91         self.put(self.samplenum, self.samplenum, self.out_ann,
92                  [4, ['Falling edge on LDAC pin', 'LDAC fall', 'LDAC']])
93
94     def handle_new_dac_bit(self):
95         self.bits.append(self.datapin)
96
97         # Wait until we have read 11 bits, then parse them.
98         l, s = len(self.bits), self.samplenum
99         if l == 1:
100             self.ss_dac = s
101         elif l == 2:
102             self.es_dac = self.ss_gain = s
103         elif l == 3:
104             self.es_gain = self.ss_value = s
105         elif l == 11:
106             self.es_value = s
107             self.handle_11bits()
108             self.bits = []
109
110     def decode(self, ss, es, data):
111         for (self.samplenum, pins) in data:
112
113             # Ignore identical samples early on (for performance reasons).
114             if self.oldpins == pins:
115                 continue
116             self.oldpins, (clk, self.datapin, load, ldac) = pins, pins
117
118             # DATA is shifted in the DAC on the falling CLK edge (MSB-first).
119             # A falling edge of LOAD will latch the data.
120
121             if self.oldload == 1 and load == 0:
122                 self.handle_falling_edge_load()
123             if self.oldldac == 1 and ldac == 0:
124                 self.handle_falling_edge_ldac()
125             if self.oldclk == 1 and clk == 0:
126                 self.handle_new_dac_bit()
127
128             self.oldclk = clk
129             self.oldload = load
130             self.oldldac = ldac
131