2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012 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 # National LM75 (and compatibles) temperature sensor protocol decoder
23 # TODO: Better support for various LM75 compatible devices.
25 import sigrokdecode as srd
27 # LM75 only supports 9 bit resolution, compatible devices usually 9-12 bits.
29 # CONFIG[6:5]: <resolution>
37 # CONFIG[4:3]: <fault tolerance setting>
44 class Decoder(srd.Decoder):
48 longname = 'National LM75'
49 desc = 'National LM75 (and compatibles) temperature sensor.'
55 {'id': 'os', 'name': 'OS', 'desc': 'Overtemperature shutdown'},
56 {'id': 'a0', 'name': 'A0', 'desc': 'I2C slave address input 0'},
57 {'id': 'a1', 'name': 'A1', 'desc': 'I2C slave address input 1'},
58 {'id': 'a2', 'name': 'A2', 'desc': 'I2C slave address input 2'},
61 'sensor': ['Sensor type', 'lm75'],
62 'resolution': ['Resolution', 9], # 9-12 bit, sensor/config dependent
65 ['Celsius', 'Temperature in degrees Celsius'],
66 ['Kelvin', 'Temperature in Kelvin'],
67 ['Text (verbose)', 'Human-readable text (verbose)'],
68 ['Text', 'Human-readable text'],
69 ['Warnings', 'Human-readable warnings'],
72 def __init__(self, **kwargs):
74 self.reg = 0x00 # Currently selected register
78 # self.out_proto = self.register(srd.OUTPUT_PYTHON)
79 self.out_ann = self.register(srd.OUTPUT_ANN)
82 # Helper for annotations which span exactly one I2C packet.
83 self.put(self.ss, self.es, self.out_ann, data)
86 # Helper for annotations which span a block of I2C packets.
87 self.put(self.block_start, self.block_end, self.out_ann, data)
89 def warn_upon_invalid_slave(self, addr):
90 # LM75 and compatible devices have a 7-bit I2C slave address where
91 # the 4 MSBs are fixed to 1001, and the 3 LSBs are configurable.
92 # Thus, valid slave addresses (1001xxx) range from 0x48 to 0x4f.
93 if addr not in range(0x48, 0x4f + 1):
94 s = 'Warning: I2C slave 0x%02x not an LM75 compatible sensor.'
95 self.putx([4, [s % addr]])
97 def output_temperature(self, s, rw):
98 # TODO: Support for resolutions other than 9 bit.
99 before, after = self.databytes[0], (self.databytes[1] >> 7) * 5
100 celsius = float('%d.%d' % (before, after))
101 kelvin = celsius + 273.15
102 self.putb([0, ['%s: %.1f °C' % (s, celsius)]])
103 self.putb([1, ['%s: %.1f K' % (s, kelvin)]])
105 # Warn about the temperature register (0x00) being read-only.
106 if s == 'Temperature' and rw == 'WRITE':
107 s = 'Warning: The temperature register is read-only!'
110 def handle_temperature_reg(self, b, s, rw):
111 # Common helper for the temperature/T_HYST/T_OS registers.
112 if len(self.databytes) == 0:
113 self.block_start = self.ss
114 self.databytes.append(b)
116 self.databytes.append(b)
117 self.block_end = self.es
118 self.output_temperature(s, rw)
121 def handle_reg_0x00(self, b, rw):
122 # Temperature register (16bits, read-only).
123 self.handle_temperature_reg(b, 'Temperature', rw)
125 def handle_reg_0x01(self, b, rw):
126 # Configuration register (8 bits, read/write).
127 # TODO: Bit-exact annotation ranges.
130 tmp = 'normal operation' if (sd == 0) else 'shutdown mode'
131 s = 'SD = %d: %s\n' % (sd, tmp)
132 s2 = 'SD = %s, ' % tmp
134 cmp_int = b & (1 << 1)
135 tmp = 'comparator' if (cmp_int == 0) else 'interrupt'
136 s += 'CMP/INT = %d: %s mode\n' % (cmp_int, tmp)
137 s2 += 'CMP/INT = %s, ' % tmp
140 tmp = 'low' if (pol == 0) else 'high'
141 s += 'POL = %d: OS polarity is active-%s\n' % (pol, tmp)
142 s2 += 'POL = active-%s, ' % tmp
144 bits = (b & ((1 << 4) | (1 << 3))) >> 3
145 s += 'Fault tolerance setting: %d bit(s)\n' % ft[bits]
146 s2 += 'FT = %d' % ft[bits]
148 # Not supported by LM75, but by various compatible devices.
149 if self.options['sensor'] != 'lm75': # TODO
150 bits = (b & ((1 << 6) | (1 << 5))) >> 5
151 s += 'Resolution: %d bits\n' % resolution[bits]
152 s2 += ', resolution = %d' % resolution[bits]
157 def handle_reg_0x02(self, b, rw):
158 # T_HYST register (16 bits, read/write).
159 self.handle_temperature_reg(b, 'T_HYST trip temperature', rw)
161 def handle_reg_0x03(self, b, rw):
162 # T_OS register (16 bits, read/write).
163 self.handle_temperature_reg(b, 'T_OS trip temperature', rw)
165 def decode(self, ss, es, data):
168 # Store the start/end samples of this I2C packet.
169 self.ss, self.es = ss, es
172 if self.state == 'IDLE':
173 # Wait for an I2C START condition.
176 self.state = 'GET SLAVE ADDR'
177 elif self.state == 'GET SLAVE ADDR':
178 # Wait for an address read/write operation.
179 if cmd in ('ADDRESS READ', 'ADDRESS WRITE'):
180 self.warn_upon_invalid_slave(databyte)
181 self.state = cmd[8:] + ' REGS' # READ REGS / WRITE REGS
182 elif self.state in ('READ REGS', 'WRITE REGS'):
183 if cmd in ('DATA READ', 'DATA WRITE'):
184 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
185 handle_reg(databyte, cmd[5:]) # READ / WRITE
190 # self.putx([0, ['Ignoring: %s (data=%s)' % (cmd, databyte)]])
193 raise Exception('Invalid state: %s' % self.state)