lm73.c
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* (C) Copyright 2007-2008
* Larry Johnson, lrj@acm.org
*
* based on dtt/lm75.c which is ...
*
* (C) Copyright 2001
* Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
*
* SPDX-License-Identifier: GPL-2.0+
*/
/*
* National Semiconductor LM73 Temperature Sensor
*/
#include <common.h>
#include <i2c.h>
#include <dtt.h>
/*
* Device code
*/
#define DTT_I2C_DEV_CODE 0x48 /* National Semi's LM73 device */
#define DTT_READ_TEMP 0x0
#define DTT_CONFIG 0x1
#define DTT_TEMP_HIGH 0x2
#define DTT_TEMP_LOW 0x3
#define DTT_CONTROL 0x4
#define DTT_ID 0x7
int dtt_read(int const sensor, int const reg)
{
int dlen;
uint8_t data[2];
/*
* Validate 'reg' param and get register size.
*/
switch (reg) {
case DTT_CONFIG:
case DTT_CONTROL:
dlen = 1;
break;
case DTT_READ_TEMP:
case DTT_TEMP_HIGH:
case DTT_TEMP_LOW:
case DTT_ID:
dlen = 2;
break;
default:
return -1;
}
/*
* Try to read the register at the calculated sensor address.
*/
if (0 !=
i2c_read(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data, dlen))
return -1;
/*
* Handle 2 byte result.
*/
if (2 == dlen)
return (int)((unsigned)data[0] << 8 | (unsigned)data[1]);
return (int)data[0];
} /* dtt_read() */
int dtt_write(int const sensor, int const reg, int const val)
{
int dlen;
uint8_t data[2];
/*
* Validate 'reg' param and handle register size
*/
switch (reg) {
case DTT_CONFIG:
case DTT_CONTROL:
dlen = 1;
data[0] = (uint8_t) val;
break;
case DTT_TEMP_HIGH:
case DTT_TEMP_LOW:
dlen = 2;
data[0] = (uint8_t) (val >> 8); /* MSB first */
data[1] = (uint8_t) val;
break;
default:
return -1;
}
/*
* Write value to register at the calculated sensor address.
*/
return 0 != i2c_write(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data,
dlen);
} /* dtt_write() */
int dtt_init_one(int const sensor)
{
int val;
/*
* Validate the Identification register
*/
if (0x0190 != dtt_read(sensor, DTT_ID))
return -1;
/*
* Setup THIGH (upper-limit) and TLOW (lower-limit) registers
*/
val = CONFIG_SYS_DTT_MAX_TEMP << 7;
if (dtt_write(sensor, DTT_TEMP_HIGH, val))
return -1;
val = CONFIG_SYS_DTT_MIN_TEMP << 7;
if (dtt_write(sensor, DTT_TEMP_LOW, val))
return -1;
/*
* Setup configuraton register
*/
/* config = alert active low, disabled, and reset */
val = 0x64;
if (dtt_write(sensor, DTT_CONFIG, val))
return -1;
/*
* Setup control/status register
*/
/* control = temp resolution 0.25C */
val = 0x00;
if (dtt_write(sensor, DTT_CONTROL, val))
return -1;
dtt_read(sensor, DTT_CONTROL); /* clear temperature flags */
return 0;
} /* dtt_init_one() */
int dtt_get_temp(int const sensor)
{
int const ret = dtt_read(sensor, DTT_READ_TEMP);
if (ret < 0) {
printf("DTT temperature read failed.\n");
return 0;
}
return (int)((int16_t) ret + 0x0040) >> 7;
} /* dtt_get_temp() */