]> sigrok.org Git - libsigrokdecode.git/blob - decoders/lm75/pd.py
can: Make sample_point a float option.
[libsigrokdecode.git] / decoders / lm75 / 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 # TODO: Better support for various LM75 compatible devices.
22
23 import sigrokdecode as srd
24
25 # LM75 only supports 9 bit resolution, compatible devices usually 9-12 bits.
26 resolution = {
27     # CONFIG[6:5]: <resolution>
28     0x00: 9,
29     0x01: 10,
30     0x02: 11,
31     0x03: 12,
32 }
33
34 ft = {
35     # CONFIG[4:3]: <fault tolerance setting>
36     0x00: 1,
37     0x01: 2,
38     0x02: 4,
39     0x03: 6,
40 }
41
42 class Decoder(srd.Decoder):
43     api_version = 1
44     id = 'lm75'
45     name = 'LM75'
46     longname = 'National LM75'
47     desc = 'National LM75 (and compatibles) temperature sensor.'
48     license = 'gplv2+'
49     inputs = ['i2c']
50     outputs = ['lm75']
51     optional_probes = (
52         {'id': 'os', 'name': 'OS', 'desc': 'Overtemperature shutdown'},
53         {'id': 'a0', 'name': 'A0', 'desc': 'I²C slave address input 0'},
54         {'id': 'a1', 'name': 'A1', 'desc': 'I²C slave address input 1'},
55         {'id': 'a2', 'name': 'A2', 'desc': 'I²C slave address input 2'},
56     )
57     options = (
58         {'id': 'sensor', 'desc': 'Sensor type', 'default': 'lm75'},
59         {'id': 'resolution', 'desc': 'Resolution', 'default': 9,
60             'values': (9, 10, 11, 12)},
61     )
62     annotations = (
63         ('celsius', 'Temperature in degrees Celsius'),
64         ('kelvin', 'Temperature in Kelvin'),
65         ('text-verbose', 'Human-readable text (verbose)'),
66         ('text', 'Human-readable text'),
67         ('warnings', 'Human-readable warnings'),
68     )
69
70     def __init__(self, **kwargs):
71         self.state = 'IDLE'
72         self.reg = 0x00 # Currently selected register
73         self.databytes = []
74
75     def start(self):
76         # self.out_python = self.register(srd.OUTPUT_PYTHON)
77         self.out_ann = self.register(srd.OUTPUT_ANN)
78
79     def putx(self, data):
80         # Helper for annotations which span exactly one I²C packet.
81         self.put(self.ss, self.es, self.out_ann, data)
82
83     def putb(self, data):
84         # Helper for annotations which span a block of I²C packets.
85         self.put(self.block_start, self.block_end, self.out_ann, data)
86
87     def warn_upon_invalid_slave(self, addr):
88         # LM75 and compatible devices have a 7-bit I²C slave address where
89         # the 4 MSBs are fixed to 1001, and the 3 LSBs are configurable.
90         # Thus, valid slave addresses (1001xxx) range from 0x48 to 0x4f.
91         if addr not in range(0x48, 0x4f + 1):
92             s = 'Warning: I²C slave 0x%02x not an LM75 compatible sensor.'
93             self.putx([4, [s % addr]])
94
95     def output_temperature(self, s, rw):
96         # TODO: Support for resolutions other than 9 bit.
97         before, after = self.databytes[0], (self.databytes[1] >> 7) * 5
98         celsius = float('%d.%d' % (before, after))
99         kelvin = celsius + 273.15
100         self.putb([0, ['%s: %.1f °C' % (s, celsius)]])
101         self.putb([1, ['%s: %.1f K' % (s, kelvin)]])
102
103         # Warn about the temperature register (0x00) being read-only.
104         if s == 'Temperature' and rw == 'WRITE':
105             s = 'Warning: The temperature register is read-only!'
106             self.putb([4, [s]])
107
108     def handle_temperature_reg(self, b, s, rw):
109         # Common helper for the temperature/T_HYST/T_OS registers.
110         if len(self.databytes) == 0:
111             self.block_start = self.ss
112             self.databytes.append(b)
113             return
114         self.databytes.append(b)
115         self.block_end = self.es
116         self.output_temperature(s, rw)
117         self.databytes = []
118
119     def handle_reg_0x00(self, b, rw):
120         # Temperature register (16bits, read-only).
121         self.handle_temperature_reg(b, 'Temperature', rw)
122
123     def handle_reg_0x01(self, b, rw):
124         # Configuration register (8 bits, read/write).
125         # TODO: Bit-exact annotation ranges.
126
127         sd = b & (1 << 0)
128         tmp = 'normal operation' if (sd == 0) else 'shutdown mode'
129         s = 'SD = %d: %s\n' % (sd, tmp)
130         s2 = 'SD = %s, ' % tmp
131
132         cmp_int = b & (1 << 1)
133         tmp = 'comparator' if (cmp_int == 0) else 'interrupt'
134         s += 'CMP/INT = %d: %s mode\n' % (cmp_int, tmp)
135         s2 += 'CMP/INT = %s, ' % tmp
136
137         pol = b & (1 << 2)
138         tmp = 'low' if (pol == 0) else 'high'
139         s += 'POL = %d: OS polarity is active-%s\n' % (pol, tmp)
140         s2 += 'POL = active-%s, ' % tmp
141
142         bits = (b & ((1 << 4) | (1 << 3))) >> 3
143         s += 'Fault tolerance setting: %d bit(s)\n' % ft[bits]
144         s2 += 'FT = %d' % ft[bits]
145
146         # Not supported by LM75, but by various compatible devices.
147         if self.options['sensor'] != 'lm75': # TODO
148             bits = (b & ((1 << 6) | (1 << 5))) >> 5
149             s += 'Resolution: %d bits\n' % resolution[bits]
150             s2 += ', resolution = %d' % resolution[bits]
151
152         self.putx([2, [s]])
153         self.putx([3, [s2]])
154
155     def handle_reg_0x02(self, b, rw):
156         # T_HYST register (16 bits, read/write).
157         self.handle_temperature_reg(b, 'T_HYST trip temperature', rw)
158
159     def handle_reg_0x03(self, b, rw):
160         # T_OS register (16 bits, read/write).
161         self.handle_temperature_reg(b, 'T_OS trip temperature', rw)
162
163     def decode(self, ss, es, data):
164         cmd, databyte = data
165
166         # Store the start/end samples of this I²C packet.
167         self.ss, self.es = ss, es
168
169         # State machine.
170         if self.state == 'IDLE':
171             # Wait for an I²C START condition.
172             if cmd != 'START':
173                 return
174             self.state = 'GET SLAVE ADDR'
175         elif self.state == 'GET SLAVE ADDR':
176             # Wait for an address read/write operation.
177             if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
178                 self.warn_upon_invalid_slave(databyte)
179                 self.state = cmd[8:] + ' REGS' # READ REGS / WRITE REGS
180         elif self.state in ('READ REGS', 'WRITE REGS'):
181             if cmd in ('DATA READ', 'DATA WRITE'):
182                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
183                 handle_reg(databyte, cmd[5:]) # READ / WRITE
184             elif cmd == 'STOP':
185                 # TODO: Any output?
186                 self.state = 'IDLE'
187             else:
188                 # self.putx([0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]])
189                 pass
190         else:
191             raise Exception('Invalid state: %s' % self.state)
192