]> sigrok.org Git - libsigrokdecode.git/blob - decoders/tlc5620/pd.py
all decoders: introduce a reset() method
[libsigrokdecode.git] / decoders / tlc5620 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2012-2015 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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 dacs = {
23     0: 'DACA',
24     1: 'DACB',
25     2: 'DACC',
26     3: 'DACD',
27 }
28
29 class Decoder(srd.Decoder):
30     api_version = 3
31     id = 'tlc5620'
32     name = 'TI TLC5620'
33     longname = 'Texas Instruments TLC5620'
34     desc = 'Texas Instruments TLC5620 8-bit quad DAC.'
35     license = 'gplv2+'
36     inputs = ['logic']
37     outputs = ['tlc5620']
38     channels = (
39         {'id': 'clk', 'name': 'CLK', 'desc': 'Serial interface clock'},
40         {'id': 'data', 'name': 'DATA', 'desc': 'Serial interface data'},
41     )
42     optional_channels = (
43         {'id': 'load', 'name': 'LOAD', 'desc': 'Serial interface load control'},
44         {'id': 'ldac', 'name': 'LDAC', 'desc': 'Load DAC'},
45     )
46     options = (
47         {'id': 'vref_a', 'desc': 'Reference voltage DACA (V)', 'default': 3.3},
48         {'id': 'vref_b', 'desc': 'Reference voltage DACB (V)', 'default': 3.3},
49         {'id': 'vref_c', 'desc': 'Reference voltage DACC (V)', 'default': 3.3},
50         {'id': 'vref_d', 'desc': 'Reference voltage DACD (V)', 'default': 3.3},
51     )
52     annotations = (
53         ('dac-select', 'DAC select'),
54         ('gain', 'Gain'),
55         ('value', 'DAC value'),
56         ('data-latch', 'Data latch point'),
57         ('ldac-fall', 'LDAC falling edge'),
58         ('bit', 'Bit'),
59         ('reg-write', 'Register write'),
60         ('voltage-update', 'Voltage update'),
61         ('voltage-update-all', 'Voltage update (all DACs)'),
62         ('invalid-cmd', 'Invalid command'),
63     )
64     annotation_rows = (
65         ('bits', 'Bits', (5,)),
66         ('fields', 'Fields', (0, 1, 2)),
67         ('registers', 'Registers', (6, 7)),
68         ('voltage-updates', 'Voltage updates', (8,)),
69         ('events', 'Events', (3, 4)),
70         ('errors', 'Errors', (9,)),
71     )
72
73     def __init__(self):
74         self.reset()
75
76     def reset(self):
77         self.bits = []
78         self.ss_dac_first = None
79         self.ss_dac = self.es_dac = 0
80         self.ss_gain = self.es_gain = 0
81         self.ss_value = self.es_value = 0
82         self.dac_select = self.gain = self.dac_value = None
83         self.dacval = {'A': '?', 'B': '?', 'C': '?', 'D': '?'}
84         self.gains = {'A': '?', 'B': '?', 'C': '?', 'D': '?'}
85
86     def start(self):
87         self.out_ann = self.register(srd.OUTPUT_ANN)
88
89     def handle_11bits(self):
90         # Only look at the last 11 bits, the rest is ignored by the TLC5620.
91         if len(self.bits) > 11:
92             self.bits = self.bits[-11:]
93
94         # If there are less than 11 bits, something is probably wrong.
95         if len(self.bits) < 11:
96             ss, es = self.samplenum, self.samplenum
97             if len(self.bits) >= 2:
98                 ss = self.bits[0][1]
99                 es = self.bits[-1][1] + (self.bits[1][1] - self.bits[0][1])
100             self.put(ss, es, self.out_ann, [9, ['Command too short']])
101             self.bits = []
102             return False
103
104         self.ss_dac = self.bits[0][1]
105         self.es_dac = self.ss_gain = self.bits[2][1]
106         self.es_gain = self.ss_value = self.bits[3][1]
107         self.clock_width = self.es_gain - self.ss_gain
108         self.es_value = self.bits[10][1] + self.clock_width # Guessed.
109
110         if self.ss_dac_first is None:
111             self.ss_dac_first = self.ss_dac
112
113         s = ''.join(str(i[0]) for i in self.bits[:2])
114         self.dac_select = s = dacs[int(s, 2)]
115         self.put(self.ss_dac, self.es_dac, self.out_ann,
116                  [0, ['DAC select: %s' % s, 'DAC sel: %s' % s,
117                       'DAC: %s' % s, 'D: %s' % s, s, s[3]]])
118
119         self.gain = g = 1 + self.bits[2][0]
120         self.put(self.ss_gain, self.es_gain, self.out_ann,
121                  [1, ['Gain: x%d' % g, 'G: x%d' % g, 'x%d' % g]])
122
123         s = ''.join(str(i[0]) for i in self.bits[3:])
124         self.dac_value = v = int(s, 2)
125         self.put(self.ss_value, self.es_value, self.out_ann,
126                  [2, ['DAC value: %d' % v, 'Value: %d' % v, 'Val: %d' % v,
127                       'V: %d' % v, '%d' % v]])
128
129         # Emit an annotation for each bit.
130         for i in range(1, 11):
131             self.put(self.bits[i - 1][1], self.bits[i][1], self.out_ann,
132                      [5, [str(self.bits[i - 1][0])]])
133         self.put(self.bits[10][1], self.bits[10][1] + self.clock_width,
134                  self.out_ann, [5, [str(self.bits[10][0])]])
135
136         self.bits = []
137
138         return True
139
140     def handle_falling_edge_load(self):
141         if not self.handle_11bits():
142             return
143         s, v, g = self.dac_select, self.dac_value, self.gain
144         self.put(self.samplenum, self.samplenum, self.out_ann,
145                  [3, ['Falling edge on LOAD', 'LOAD fall', 'F']])
146         vref = self.options['vref_%s' % self.dac_select[3].lower()]
147         v = '%.2fV' % (vref * (v / 256) * self.gain)
148         if self.ldac == 0:
149             # If LDAC is low, the voltage is set immediately.
150             self.put(self.ss_dac, self.es_value, self.out_ann,
151                      [7, ['Setting %s voltage to %s' % (s, v),
152                           '%s=%s' % (s, v)]])
153         else:
154             # If LDAC is high, the voltage is not set immediately, but rather
155             # stored in a register. When LDAC goes low all four DAC voltages
156             # (DAC A/B/C/D) will be set at the same time.
157             self.put(self.ss_dac, self.es_value, self.out_ann,
158                      [6, ['Setting %s register value to %s' % \
159                           (s, v), '%s=%s' % (s, v)]])
160         # Save the last value the respective DAC was set to.
161         self.dacval[self.dac_select[-1]] = str(self.dac_value)
162         self.gains[self.dac_select[-1]] = self.gain
163
164     def handle_falling_edge_ldac(self):
165         self.put(self.samplenum, self.samplenum, self.out_ann,
166                  [4, ['Falling edge on LDAC', 'LDAC fall', 'LDAC', 'L']])
167
168         # Don't emit any annotations if we didn't see any register writes.
169         if self.ss_dac_first is None:
170             return
171
172         # Calculate voltages based on Vref and the per-DAC gain.
173         dacval = {}
174         for key, val in self.dacval.items():
175             if val == '?':
176                 dacval[key] = '?'
177             else:
178                 vref = self.options['vref_%s' % key.lower()]
179                 v = vref * (int(val) / 256) * self.gains[key]
180                 dacval[key] = '%.2fV' % v
181
182         s = ''.join(['DAC%s=%s ' % (d, dacval[d]) for d in 'ABCD']).strip()
183         self.put(self.ss_dac_first, self.samplenum, self.out_ann,
184                  [8, ['Updating voltages: %s' % s, s, s.replace('DAC', '')]])
185         self.ss_dac_first = None
186
187     def handle_new_dac_bit(self, datapin):
188         self.bits.append([datapin, self.samplenum])
189
190     def decode(self):
191         while True:
192             # DATA is shifted in the DAC on the falling CLK edge (MSB-first).
193             # A falling edge of LOAD will latch the data.
194
195             # Wait for one (or multiple) of the following conditions:
196             #   a) Falling edge on CLK, and/or
197             #   b) Falling edge on LOAD, and/or
198             #   b) Falling edge on LDAC
199             pins = self.wait([{0: 'f'}, {2: 'f'}, {3: 'f'}])
200             self.ldac = pins[3]
201
202             # Handle those conditions (one or more) that matched this time.
203             if self.matched[0]:
204                 self.handle_new_dac_bit(pins[1])
205             if self.matched[1]:
206                 self.handle_falling_edge_load()
207             if self.matched[2]:
208                 self.handle_falling_edge_ldac()