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