3 * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * On Semiconductor's LM75 Temperature Sensor
30 #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
31 (CFG_EEPROM_PAGE_WRITE_BITS < 1)
32 # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than 1 to use CONFIG_DTT_LM75"
41 #if defined(CFG_I2C_DTT_ADDR)
42 #define DTT_I2C_DEV_CODE CFG_I2C_DTT_ADDR
44 #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
46 #define DTT_READ_TEMP 0x0
47 #define DTT_CONFIG 0x1
48 #define DTT_TEMP_HYST 0x2
49 #define DTT_TEMP_SET 0x3
51 int dtt_read(int sensor, int reg)
56 #ifdef CONFIG_DTT_AD7414
58 * On AD7414 the first value upon bootup is not read correctly.
59 * This is most likely because of the 800ms update time of the
60 * temp register in normal update mode. To get current values
61 * each time we issue the "dtt" command including upon powerup
62 * we switch into one-short mode.
64 * Issue one-shot mode command
66 dtt_write(sensor, DTT_CONFIG, 0x64);
70 * Validate 'reg' param
72 if((reg < 0) || (reg > 3))
76 * Calculate sensor address and register.
78 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
81 * Prepare to handle 2 byte result.
83 if ((reg == DTT_READ_TEMP) ||
84 (reg == DTT_TEMP_HYST) ||
85 (reg == DTT_TEMP_SET))
91 * Now try to read the register.
93 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
97 * Handle 2 byte result.
100 return ((int)((short)data[1] + (((short)data[0]) << 8)));
107 int dtt_write(int sensor, int reg, int val)
113 * Validate 'reg' param
115 if ((reg < 0) || (reg > 3))
119 * Calculate sensor address and register.
121 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
124 * Handle 2 byte values.
126 if ((reg == DTT_READ_TEMP) ||
127 (reg == DTT_TEMP_HYST) ||
128 (reg == DTT_TEMP_SET)) {
130 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
131 data[1] = (char)(val & 0xff);
134 data[0] = (char)(val & 0xff);
138 * Write value to register.
140 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
147 static int _dtt_init(int sensor)
152 * Setup TSET ( trip point ) register
154 val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
155 if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
159 * Setup THYST ( untrip point ) register - Hysteresis
161 val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
162 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
166 * Setup configuraton register
168 #ifdef CONFIG_DTT_AD7414
169 /* config = alert active low and disabled */
172 /* config = 6 sample integration, int mode, active low, and enable */
175 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
185 unsigned char sensors[] = CONFIG_DTT_SENSORS;
186 const char *const header = "DTT: ";
188 for (i = 0; i < sizeof(sensors); i++) {
189 if (_dtt_init(sensors[i]) != 0)
190 printf("%s%d FAILED INIT\n", header, i+1);
192 printf("%s%d is %i C\n", header, i+1,
193 dtt_get_temp(sensors[i]));
199 int dtt_get_temp(int sensor)
201 int const ret = dtt_read(sensor, DTT_READ_TEMP);
204 printf("DTT temperature read failed.\n");
207 return (int)((int16_t) ret / 256);
208 } /* dtt_get_temp() */