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