]> sigrok.org Git - libsigrokdecode.git/blob - decoders/tlc5620/pd.py
e086c4d811dd527659437b6a0744a60409ce07cf
[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, 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         ('bit', 'Bit'),
54         ('reg-write', 'Register write'),
55         ('voltage-update', 'Voltage update'),
56         ('voltage-update-all', 'Voltage update (all DACs)'),
57         ('invalid-cmd', 'Invalid command'),
58     )
59     annotation_rows = (
60         ('bits', 'Bits', (5,)),
61         ('fields', 'Fields', (0, 1, 2)),
62         ('registers', 'Registers', (6, 7)),
63         ('voltage-updates', 'Voltage updates', (8,)),
64         ('events', 'Events', (3, 4)),
65         ('errors', 'Errors', (9,)),
66     )
67
68     def __init__(self, **kwargs):
69         self.oldpins = self.oldclk = self.oldload = self.oldldac = None
70         self.bits = []
71         self.ss_dac_first = None
72         self.ss_dac = self.es_dac = 0
73         self.ss_gain = self.es_gain = 0
74         self.ss_value = self.es_value = 0
75         self.dac_select = self.gain = self.dac_value = None
76         self.dacval = {'A': '?', 'B': '?', 'C': '?', 'D': '?'}
77
78     def start(self):
79         self.out_ann = self.register(srd.OUTPUT_ANN)
80
81     def handle_11bits(self):
82         # Only look at the last 11 bits, the rest is ignored by the TLC5620.
83         if len(self.bits) > 11:
84             self.bits = self.bits[-11:]
85
86         # If there are less than 11 bits, something is probably wrong.
87         if len(self.bits) < 11:
88             ss, es = self.samplenum, self.samplenum
89             if len(self.bits) >= 2:
90                 ss = self.bits[0][1]
91                 es = self.bits[-1][1] + (self.bits[1][1] - self.bits[0][1])
92             self.put(ss, es, self.out_ann, [9, ['Command too short']])
93             self.bits = []
94             return False
95
96         self.ss_dac = self.bits[0][1]
97         self.es_dac = self.ss_gain = self.bits[2][1]
98         self.es_gain = self.ss_value = self.bits[3][1]
99         self.clock_width = self.es_gain - self.ss_gain
100         self.es_value = self.bits[10][1] + self.clock_width # Guessed.
101
102         if self.ss_dac_first is None:
103             self.ss_dac_first = self.ss_dac
104
105         s = ''.join(str(i[0]) for i in self.bits[:2])
106         self.dac_select = s = dacs[int(s, 2)]
107         self.put(self.ss_dac, self.es_dac, self.out_ann,
108                  [0, ['DAC select: %s' % s, 'DAC sel: %s' % s,
109                       'DAC: %s' % s, 'D: %s' % s, s, s[3]]])
110
111         self.gain = g = 1 + self.bits[2][0]
112         self.put(self.ss_gain, self.es_gain, self.out_ann,
113                  [1, ['Gain: x%d' % g, 'G: x%d' % g, 'x%d' % g]])
114
115         s = ''.join(str(i[0]) for i in self.bits[3:])
116         self.dac_value = v = int(s, 2)
117         self.put(self.ss_value, self.es_value, self.out_ann,
118                  [2, ['DAC value: %d' % v, 'Value: %d' % v, 'Val: %d' % v,
119                       'V: %d' % v, '%d' % v]])
120
121         # Emit an annotation for each bit.
122         for i in range(1, 11):
123             self.put(self.bits[i - 1][1], self.bits[i][1], self.out_ann,
124                      [5, [str(self.bits[i - 1][0])]])
125         self.put(self.bits[10][1], self.bits[10][1] + self.clock_width,
126                  self.out_ann, [5, [str(self.bits[10][0])]])
127
128         self.bits = []
129
130         return True
131
132     def handle_falling_edge_load(self):
133         if not self.handle_11bits():
134             return
135         s, v, g = self.dac_select, self.dac_value, self.gain
136         self.put(self.samplenum, self.samplenum, self.out_ann,
137                  [3, ['Falling edge on LOAD', 'LOAD fall', 'F']])
138         if self.ldac == 0:
139             # If LDAC is low, the voltage is set immediately.
140             self.put(self.ss_dac, self.es_value, self.out_ann,
141                      [7, ['Setting %s voltage to %d (x%d gain)' % (s, v, g),
142                           '%s=%d (x%d gain)' % (s, v, g)]])
143         else:
144             # If LDAC is high, the voltage is not set immediately, but rather
145             # stored in a register. When LDAC goes low all four DAC voltages
146             # (DAC A/B/C/D) will be set at the same time.
147             self.put(self.ss_dac, self.es_value, self.out_ann,
148                      [6, ['Setting %s register value to %d (x%d gain)' % \
149                           (s, v, g), '%s=%d (x%d gain)' % (s, v, g)]])
150         # Save the last value the respective DAC was set to.
151         self.dacval[self.dac_select[-1]] = str(self.dac_value)
152
153     def handle_falling_edge_ldac(self):
154         self.put(self.samplenum, self.samplenum, self.out_ann,
155                  [4, ['Falling edge on LDAC', 'LDAC fall', 'LDAC', 'L']])
156
157         # Don't emit any annotations if we didn't see any register writes.
158         if self.ss_dac_first is None:
159             return
160
161         s = ''.join(['DAC%s=%s ' % (d, self.dacval[d]) for d in 'ABCD']).strip()
162         self.put(self.ss_dac_first, self.samplenum, self.out_ann,
163                  [8, ['Updating voltages: %s' % s, s, s.replace('DAC', '')]])
164         self.ss_dac_first = None
165
166     def handle_new_dac_bit(self):
167         self.bits.append([self.datapin, self.samplenum])
168
169     def decode(self, ss, es, data):
170         for (self.samplenum, pins) in data:
171
172             # Ignore identical samples early on (for performance reasons).
173             if self.oldpins == pins:
174                 continue
175             self.oldpins, (clk, self.datapin, load, ldac) = pins, pins
176             self.ldac = ldac
177
178             # DATA is shifted in the DAC on the falling CLK edge (MSB-first).
179             # A falling edge of LOAD will latch the data.
180
181             if self.oldload == 1 and load == 0:
182                 self.handle_falling_edge_load()
183             if self.oldldac == 1 and ldac == 0:
184                 self.handle_falling_edge_ldac()
185             if self.oldclk == 1 and clk == 0:
186                 self.handle_new_dac_bit()
187
188             self.oldclk = clk
189             self.oldload = load
190             self.oldldac = ldac