]> sigrok.org Git - libsigrokdecode.git/blame - decoders/ltc242x/pd.py
ltc242x: Use plural for annotation rows, avoid duplicates.
[libsigrokdecode.git] / decoders / ltc242x / pd.py
CommitLineData
e0a46d7c
TP
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2020 Analog Devices Inc.
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 3 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
20import sigrokdecode as srd
21
22channel_format = ['Channel %d', 'Ch %d', '%d']
23input_voltage_format = ['%fV', '%fV', '%.6fV', '%.2fV']
24
25class Decoder(srd.Decoder):
26 api_version = 3
27 id = 'ltc242x'
28 name = 'LTC242x'
29 longname = 'Linear Technology LTC242x'
30 desc = 'Linear Technology LTC2421/LTC2422 1-/2-channel 20-bit ADC.'
31 license = 'gplv2+'
32 inputs = ['spi']
33 outputs = []
a3b9585c 34 tags = ['IC', 'Analog/digital']
e0a46d7c
TP
35 annotations = (
36 ('channel', 'Channel'),
37 ('input', 'Input voltage'),
38 )
39 annotation_rows = (
da5d46fb
UH
40 ('channels', 'Channels', (0,)),
41 ('inputs', 'Input voltages', (1,)),
e0a46d7c
TP
42 )
43 options = (
2b61ff3d 44 {'id': 'vref', 'desc': 'Reference voltage (V)', 'default': 1.5},
e0a46d7c
TP
45 )
46
47 def __init__(self):
48 self.reset()
49
50 def reset(self):
51 self.data = 0
52 self.ss, self.es = 0, 0
53
54 def start(self):
55 self.out_ann = self.register(srd.OUTPUT_ANN)
56
57 def handle_channel(self, data):
58 channel = (data & (1 << 22)) >> 22
59 ann = []
60 for format in channel_format:
61 ann.append(format % channel)
62
63 self.put(self.ss, self.es, self.out_ann, [0, ann])
64
65 def handle_input_voltage(self, data):
66 input_voltage = data & 0x3FFFFF
67 input_voltage = -(2**21 - input_voltage)
2b61ff3d 68 input_voltage = (input_voltage / 0xfffff) * self.options['vref']
e0a46d7c
TP
69 ann = []
70 for format in input_voltage_format:
71 ann.append(format % input_voltage)
72
73 self.put(self.ss, self.es, self.out_ann, [1, ann])
74
75 def decode(self, ss, es, data):
76 ptype = data[0]
77
78 if ptype == 'CS-CHANGE':
79 cs_old, cs_new = data[1:]
80 if cs_old is not None and cs_old == 0 and cs_new == 1:
81 self.es = es
82 self.data >>= 1
83 self.handle_channel(self.data)
84 self.handle_input_voltage(self.data)
85
86 self.data = 0
87 elif cs_old is not None and cs_old == 1 and cs_new == 0:
88 self.ss = ss
89
90 elif ptype == 'BITS':
91 miso = data[2]
92 for bit in reversed(miso):
93 self.data = self.data | bit[0]
94
95 self.data <<= 1