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