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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h> //# isdigit()
#include "lm75.h"
#include "deftypes.h"
extern uint16 devicef;
extern void bus_err(int ern);
extern void print_help(void);
extern uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen);
uchar buf[32];
void lm75_print_all(void)
{
printf(
"============ LM75xx interface ===========\n"
"Registers:\n"
" 00: Temperature data [°C] (reg: 0x00)\n"
" 01: Configuration [hex] (reg: 0x01)\n"
" 02: Tos (Overtemp) [°C] (reg: 0x03)\n"
" 03: Thys (Hysteresis) [°C] (reg: 0x04)\n"
"Configuration:\n"
" tos: Over-Temperature register [-55°C...+125°C]\n"
" thys: Temperature-hysteresis register [-55°C...+125°C]\n"
" conf: Configuration register [hexadecimal]\n"
" sleep: Set idle mode [on/off]\n"
" mode: Comparator/Interrupt mode [comp/int]\n"
" tos_pol: Tos pin polarity (Low/High) [Al/Ah]\n"
" fault_q: Fault queue length [1/2/4]\n"
"\n");
}
// uchar *xchg_data (uchar *buf, uint8 wrlen, uint8 waitlen, uint8 rdlen){ //# [buf] = 32 byte
// if(write(devicef, buf, wrlen) != wrlen){ //# write one byte to device
// bus_err(errno);
// }
// usleep(waitlen*1000); //# Wait 10ms for reading
// if(read(devicef, buf, rdlen) != rdlen) { //# read the result
// bus_err(errno);
// }
// return buf;
// }
static float calc_temp(float offset){ // Calculate temperature
uint16 rawtemp = 0; //# Signed by default
float temp;
buf[0] = 0x00;
buf[1] = 0x00; //# Measurement in the REG[0]
xchg_data(buf,1,0,2); //# Write: 1byte addr, Read: 2byte data, Wait:0, [buf] = 32 byte
rawtemp = (buf[0]*256 + buf[1]) >> 5; //# /x*256 == x << 8/ (>> 5): Temp is 11bit data register
if((rawtemp & 0x400) == 0x400){ //# check if the msb(bit11) is 1 (1024 = 0x400), 2'complement negative number
rawtemp = ~rawtemp + 1; //# 2'complement data
rawtemp = rawtemp & 0x7ff; //# Only the lowest 11bit needed
temp = -1 * rawtemp * 0.125 ;
} else {
temp = rawtemp * 0.125 ; //# 11bit -> 0.125°C or 9bit -> 0.5°C
}
return (temp + offset);
}
static float read_tos(void){ // Over-Temperature Shutdown register
uint16 rawtemp = 0; //# Int16 default signed
buf[0] = 0x03; //# Tos = REG[2]
xchg_data(buf,1,0,2);
rawtemp = buf[0]*256 + buf[1]; // x*256 == x << 8
rawtemp = (rawtemp) >> 7; //# 9bit data
if((rawtemp & 0x100) == 0x100){ //# check MSB if it's a 2'complement number
rawtemp = ~rawtemp + 1 ; //# 2'complement, 2^9 = 512;
rawtemp = rawtemp & 0x1FF; //# lowest 9 bit
return (-1 * rawtemp * 0.5);
} else {
return (rawtemp * 0.5); //# 9bit -> 0.5 celsius
}
}
static float read_thys(void){ // Over-Temp Hysteresis Register
uint16 rawtemp = 0; //# signed
buf[0] = 0x02;
xchg_data(buf,1,0,2); //# read from REG[3]
rawtemp = (buf[0] << 8) + buf[1];
rawtemp = (rawtemp) >> 7; //# 9bit data
if((rawtemp & 256) == 256){ //# 256 = 0x100
rawtemp = ~rawtemp + 1 ; //# 2'complement, 2^9 = 512;
rawtemp = rawtemp & 0x1FF; //# lowest 9 bit
return (-1 * rawtemp * 0.5);
}
return (rawtemp * 0.5); //# 9bit -> 0.5 celsius
}
static uchar read_conf(void){ //Configuration register
buf[0] = 0x01;
xchg_data(buf,1,0,1);
return buf[0];
}
void lm75_read_all(const uchar *opts){ // Print out whole device's data
// if(opts != NULL)
// {
// printf("00:%f\n", calc_temp(strtof(opts, NULL)));
// } else {
printf("00:%f\n", calc_temp(0.0));
// }
printf("01:0x%x\n", read_conf());
printf("02:%f\n", read_thys());
printf("03:%f\n", read_tos());
}
void lm75_read_one(const uchar *opts){ // Prints the selected register's data
uint16 id,i;
uchar temp[256];
if(opts != NULL){ //# Search the comma: ...<01,-11.24>
for(i = 0; i < strlen((char*)opts); i++){
if (*(opts+i) == ','){
break;
} else {
if( !isdigit(*(opts+i)) ){ //# Check the register string
fprintf(stderr, "The Register address must be an integer!\n");
print_help();
lm75_print_all();
exit (EXIT_FAILURE);
}
temp[i] = *(opts+i); //# copy register string
temp[i+1] = '\0';
}
}
id = atoi((char*)temp); //# Convert register to number
strncpy((char*)temp, (char*)opts+i+1, 255); //# Copy remain to temp
switch (id) //# Which register is selected?
{
case 0x00:
printf("%f\n", calc_temp(atof((char*)temp))); //# with the offset
break;
case 0x01:
printf("0x%x\n", read_conf());
break;
case 0x02:
printf("%f\n", read_thys());
break;
case 0x03:
printf("%f\n", read_tos());
break;
default:
print_help();
}
}
}
void lm75_conf_set(const uchar *opts){ // Prints the selected register's data
uint16 i;
uchar temp[256];
if(opts != NULL){
for(i = 0; i < strlen((char*)opts); i++){
if (*(opts+i) == ','){ //# .... conf_set 0x49 thys,-10.5
break;
}
temp[i] = *(opts+i);
temp[i+1] = '\0';
}
if(!strcmp("tos", (char*)temp)){ //# Set the Tos register value
float inp;
uint16 temp;
buf[0] = 0x03;
inp = atof( (char*)(opts+i+1));
if(inp < 0){
temp = 0 - inp/0.5;
temp = ~temp +1; //# 2'complement
buf[1] = (temp >> 1) & 0xFF; //# (x << 7) and (x >> 8) ==> (x >> 1)
buf[2] = ((temp << 7) | 0x7F) & 0xFF; //# |d8,d7,d6,d5|d4,d3,d2,d1|d0,xx,xx,xx|xx,xx,xx,xx|
} else {
temp = inp/0.5;
buf[1] = (temp >> 1) & 0xFF; //# (x << 7) and (x >> 8) ==> (x >> 1)
buf[2] = (temp << 7) & 0xFF;;
}
xchg_data(buf,3,0,2);
}else if(!strcmp("thys", (char*)temp)){ //# Set the Thys reg
float inp;
uint16 temp;
buf[0] = 0x02;
inp = atof( (char*)(opts+i+1));
if(inp < 0){
temp = 0 - inp/0.5;
temp = ~temp +1; //# 2'complement
buf[1] = (temp >> 1) & 0xFF; //# (x << 7) and (x >> 8) ==> (x >> 1)
buf[2] = ((temp << 7) | 0x7F) & 0xFF; //# |d8,d7,d6,d5|d4,d3,d2,d1|d0,xx,xx,xx|xx,xx,xx,xx|
} else {
temp = inp/0.5;
buf[1] = (temp >> 1) & 0xFF; //# (x << 7) and (x >> 8) ==> (x >> 1)
buf[2] = (temp << 7) & 0xFF;;
}
xchg_data(buf,3,0,2);
}else if(!strcmp("conf", (char*)temp)){ //# Set whole conf register
buf[0] = 0x01;
buf[1] = strtol((char*)(opts+i+1),NULL,0);
xchg_data(buf,2,0,1);
}else if(!strcmp("sleep", (char*)temp)){ //# Send the sleep command
buf[0] = 0x01;
xchg_data(buf,1,0,1);
if (!strcmp("on",(char*)opts+i+1))
{
buf[1] = buf[0] | 0x01;
}
else if(!strcmp("off",(char*)opts+i+1))
{
// buf[1] = ~buf[0]; //# XOR = NEG->OR->NEG
// buf[1] = buf[1] | 0b00000001; //# Invert->add->invert
// buf[1] = ~buf[1];
buf[1] = buf[0] ^ 0b00000001; //# XOR to clear the bit
}
else
{
lm75_print_all();
exit(EXIT_FAILURE);
}
buf[0] = 0x01;
xchg_data(buf,2,0,1);
}else if(!strcmp("mode", (char*)temp)){ //# Comparator/Interrupt mode
buf[0] = 0x01;
xchg_data(buf,1,0,1);
if (!strcmp("comp",(char*)opts+i+1))
{
buf[1] = buf[0] ^ 0b00000010; //# XOR to clear
}
else if(!strcmp("int",(char*)opts+i+1))
{
buf[1] = buf[0] | 0b00000010;
}
else
{
lm75_print_all();
exit(EXIT_FAILURE);
}
buf[0] = 0x01;
xchg_data(buf,2,0,1);
}else if(!strcmp("tos_pol", (char*)temp)){ //# Tos polarity
buf[0] = 0x01;
xchg_data(buf,1,0,1);
if (!strcmp("Al",(char*)opts+i+1))
{
buf[1] = buf[0] ^ 0b00000100; //# XOR to clear the bit
}
else if(!strcmp("Ah",(char*)opts+i+1))
{
buf[1] = buf[0] | 0b00000100;
}
else
{
lm75_print_all();
exit(EXIT_FAILURE);
}
buf[0] = 0x01;
xchg_data(buf,2,0,1);
}else if(!strcmp("fault_q", (char*)temp)){ //# Tos Fault Queue
buf[0] = 0x01;
xchg_data(buf,1,0,1);
if (!strcmp("1",(char*)opts+i+1))
{
buf[1] = buf[0] | 0b00011000; //# Add and clear whole section
buf[1] = buf[1] ^ 0b00011000;
}
else if(!strcmp("2",(char*)opts+i+1))
{
buf[1] = buf[0] | 0b00011000; //# Clear first
buf[1] = buf[1] ^ 0b00011000; //# Clear first
buf[1] = buf[1] | 0b00001000; //# Set
}
else if(!strcmp("4",(char*)opts+i+1))
{
buf[1] = buf[0] | 0b00011000; //# Clear first
buf[1] = buf[1] ^ 0b00011000; //# Clear first
buf[1] = buf[1] | 0b00010000; //# Set
}
else
{
lm75_print_all();
exit(EXIT_FAILURE);
}
buf[0] = 0x01;
xchg_data(buf,2,0,1);
} else {
// for(i = 0; i < strlen((char*)opts); i++ ){
// if( !isxdigit(*(opts+i)) || *(opts+i) !='x' || opts[i] != ','){ //# Check the register string
// printf("The Register address, and value must be a hex, or an integer!\n");
print_help();
lm75_print_all();
exit (EXIT_FAILURE);
// }
// }
}
}
}
|