2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012-2015 Uwe Hermann <uwe@hermann-uwe.de>
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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 import sigrokdecode as srd
30 class Decoder(srd.Decoder):
34 longname = 'Texas Instruments TLC5620'
35 desc = 'Texas Instruments TLC5620 8-bit quad DAC.'
40 {'id': 'clk', 'name': 'CLK', 'desc': 'Serial interface clock'},
41 {'id': 'data', 'name': 'DATA', 'desc': 'Serial interface data'},
44 {'id': 'load', 'name': 'LOAD', 'desc': 'Serial interface load control'},
45 {'id': 'ldac', 'name': 'LDAC', 'desc': 'Load DAC'},
48 {'id': 'vref_a', 'desc': 'Reference voltage DACA (V)', 'default': 3.3},
49 {'id': 'vref_b', 'desc': 'Reference voltage DACB (V)', 'default': 3.3},
50 {'id': 'vref_c', 'desc': 'Reference voltage DACC (V)', 'default': 3.3},
51 {'id': 'vref_d', 'desc': 'Reference voltage DACD (V)', 'default': 3.3},
54 ('dac-select', 'DAC select'),
56 ('value', 'DAC value'),
57 ('data-latch', 'Data latch point'),
58 ('ldac-fall', 'LDAC falling edge'),
60 ('reg-write', 'Register write'),
61 ('voltage-update', 'Voltage update'),
62 ('voltage-update-all', 'Voltage update (all DACs)'),
63 ('invalid-cmd', 'Invalid command'),
66 ('bits', 'Bits', (5,)),
67 ('fields', 'Fields', (0, 1, 2)),
68 ('registers', 'Registers', (6, 7)),
69 ('voltage-updates', 'Voltage updates', (8,)),
70 ('events', 'Events', (3, 4)),
71 ('errors', 'Errors', (9,)),
74 def __init__(self, **kwargs):
75 self.oldpins = self.oldclk = self.oldload = self.oldldac = None
77 self.ss_dac_first = None
78 self.ss_dac = self.es_dac = 0
79 self.ss_gain = self.es_gain = 0
80 self.ss_value = self.es_value = 0
81 self.dac_select = self.gain = self.dac_value = None
82 self.dacval = {'A': '?', 'B': '?', 'C': '?', 'D': '?'}
83 self.gains = {'A': '?', 'B': '?', 'C': '?', 'D': '?'}
86 self.out_ann = self.register(srd.OUTPUT_ANN)
88 def handle_11bits(self):
89 # Only look at the last 11 bits, the rest is ignored by the TLC5620.
90 if len(self.bits) > 11:
91 self.bits = self.bits[-11:]
93 # If there are less than 11 bits, something is probably wrong.
94 if len(self.bits) < 11:
95 ss, es = self.samplenum, self.samplenum
96 if len(self.bits) >= 2:
98 es = self.bits[-1][1] + (self.bits[1][1] - self.bits[0][1])
99 self.put(ss, es, self.out_ann, [9, ['Command too short']])
103 self.ss_dac = self.bits[0][1]
104 self.es_dac = self.ss_gain = self.bits[2][1]
105 self.es_gain = self.ss_value = self.bits[3][1]
106 self.clock_width = self.es_gain - self.ss_gain
107 self.es_value = self.bits[10][1] + self.clock_width # Guessed.
109 if self.ss_dac_first is None:
110 self.ss_dac_first = self.ss_dac
112 s = ''.join(str(i[0]) for i in self.bits[:2])
113 self.dac_select = s = dacs[int(s, 2)]
114 self.put(self.ss_dac, self.es_dac, self.out_ann,
115 [0, ['DAC select: %s' % s, 'DAC sel: %s' % s,
116 'DAC: %s' % s, 'D: %s' % s, s, s[3]]])
118 self.gain = g = 1 + self.bits[2][0]
119 self.put(self.ss_gain, self.es_gain, self.out_ann,
120 [1, ['Gain: x%d' % g, 'G: x%d' % g, 'x%d' % g]])
122 s = ''.join(str(i[0]) for i in self.bits[3:])
123 self.dac_value = v = int(s, 2)
124 self.put(self.ss_value, self.es_value, self.out_ann,
125 [2, ['DAC value: %d' % v, 'Value: %d' % v, 'Val: %d' % v,
126 'V: %d' % v, '%d' % v]])
128 # Emit an annotation for each bit.
129 for i in range(1, 11):
130 self.put(self.bits[i - 1][1], self.bits[i][1], self.out_ann,
131 [5, [str(self.bits[i - 1][0])]])
132 self.put(self.bits[10][1], self.bits[10][1] + self.clock_width,
133 self.out_ann, [5, [str(self.bits[10][0])]])
139 def handle_falling_edge_load(self):
140 if not self.handle_11bits():
142 s, v, g = self.dac_select, self.dac_value, self.gain
143 self.put(self.samplenum, self.samplenum, self.out_ann,
144 [3, ['Falling edge on LOAD', 'LOAD fall', 'F']])
145 vref = self.options['vref_%s' % self.dac_select[3].lower()]
146 v = '%.2fV' % (vref * (v / 256) * self.gain)
148 # If LDAC is low, the voltage is set immediately.
149 self.put(self.ss_dac, self.es_value, self.out_ann,
150 [7, ['Setting %s voltage to %s' % (s, v),
153 # If LDAC is high, the voltage is not set immediately, but rather
154 # stored in a register. When LDAC goes low all four DAC voltages
155 # (DAC A/B/C/D) will be set at the same time.
156 self.put(self.ss_dac, self.es_value, self.out_ann,
157 [6, ['Setting %s register value to %s' % \
158 (s, v), '%s=%s' % (s, v)]])
159 # Save the last value the respective DAC was set to.
160 self.dacval[self.dac_select[-1]] = str(self.dac_value)
161 self.gains[self.dac_select[-1]] = self.gain
163 def handle_falling_edge_ldac(self):
164 self.put(self.samplenum, self.samplenum, self.out_ann,
165 [4, ['Falling edge on LDAC', 'LDAC fall', 'LDAC', 'L']])
167 # Don't emit any annotations if we didn't see any register writes.
168 if self.ss_dac_first is None:
171 # Calculate voltages based on Vref and the per-DAC gain.
173 for key, val in self.dacval.items():
177 vref = self.options['vref_%s' % key.lower()]
178 v = vref * (int(val) / 256) * self.gains[key]
179 dacval[key] = '%.2fV' % v
181 s = ''.join(['DAC%s=%s ' % (d, dacval[d]) for d in 'ABCD']).strip()
182 self.put(self.ss_dac_first, self.samplenum, self.out_ann,
183 [8, ['Updating voltages: %s' % s, s, s.replace('DAC', '')]])
184 self.ss_dac_first = None
186 def handle_new_dac_bit(self):
187 self.bits.append([self.datapin, self.samplenum])
189 def decode(self, ss, es, data):
190 for (self.samplenum, pins) in data:
192 # Ignore identical samples early on (for performance reasons).
193 if self.oldpins == pins:
195 self.oldpins, (clk, self.datapin, load, ldac) = pins, pins
198 # DATA is shifted in the DAC on the falling CLK edge (MSB-first).
199 # A falling edge of LOAD will latch the data.
201 if self.oldload == 1 and load == 0:
202 self.handle_falling_edge_load()
203 if self.oldldac == 1 and ldac == 0:
204 self.handle_falling_edge_ldac()
205 if self.oldclk == 1 and clk == 0:
206 self.handle_new_dac_bit()