forked from ckolivas/cgminer
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtm_i2c.c
140 lines (120 loc) · 2.92 KB
/
tm_i2c.c
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
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "tm_i2c.h"
#include "bitfury-config.h"
float tm_i2c_Data2Temp(unsigned int ans) {
float t = ans;
return (t / 1023.0 * 3.3 * 2-2.73) * 100.0;
}
float tm_i2c_Data2Core(unsigned int ans) {
float t = ans;
return t / 1023.0 * 3.3;
}
int tm_i2c_init() {
if ((tm_i2c_fd = open("/dev/i2c-1", O_RDWR)) < 0)
return 1;
else
return 0;
}
void tm_i2c_close() {
close(tm_i2c_fd);
}
unsigned int tm_i2c_req_slot(unsigned char slot, unsigned char cmd, unsigned int data) {
return(tm_i2c_req(tm_i2c_fd, ((TM_ADDR >> 1) + slot), cmd, data));
}
unsigned int tm_i2c_req(int fd, unsigned char addr, unsigned char cmd, unsigned int data) {
int i;
unsigned char buf[16];
struct i2c_msg msg;
tm_struct *tm = (tm_struct *) buf;
struct i2c_rdwr_ioctl_data msg_rdwr;
unsigned int ret;
//printf("REQ from %02X cmd: %02X\n", addr, cmd);
tm->cmd = cmd;
tm->data_lsb = data & 0xFF;
tm->data_msb = (data & 0xFF00) >> 8;
/* Write CMD */
msg.addr = addr;
msg.flags = 0;
msg.len = 3;
msg.buf = buf;
msg_rdwr.msgs = &msg;
msg_rdwr.nmsgs = 1;
if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
// perror("ioctl error");
return -1;
}
/* Read result */
msg.addr = addr;
msg.flags = I2C_M_RD;
msg.len = 3;
msg.buf = buf;
msg_rdwr.msgs = &msg;
msg_rdwr.nmsgs = 1;
if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
// perror("ioctl error");
return -1;
}
//hexdump(buf, 10);
ret = (tm->data_msb << 8) + tm->data_lsb;
if (tm->cmd == cmd) return ret;
return 0;
}
int tm_i2c_detect(unsigned char slot) {
#ifdef BITFURY_METABANK
if (slot < 0 || slot > 31) return 0;
return tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE0, 0);
#else
return 0;
#endif
}
float tm_i2c_getcore0(unsigned char slot) {
#ifdef BITFURY_METABANK
if (slot < 0 || slot > 31) return 0;
return tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE0, 0));
#else
return 0;
#endif
}
float tm_i2c_getcore1(unsigned char slot) {
#ifdef BITFURY_METABANK
if (slot < 0 || slot > 31) return 0;
return tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE1, 0));
#else
return 0;
#endif
}
float tm_i2c_gettemp(unsigned char slot) {
#ifdef BITFURY_METABANK
if (slot < 0 || slot > 31) return 0;
return tm_i2c_Data2Temp(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_TEMP, 0));
#else
return 0;
#endif
}
void tm_i2c_set_oe(unsigned char slot) {
#ifdef BITFURY_METABANK
if (slot < 0 || slot > 31) return;
tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_SET_OE, 0);
#else
return;
#endif
}
void tm_i2c_clear_oe(unsigned char slot) {
#ifdef BITFURY_METABANK
if (slot < 0 || slot > 31) return;
tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_SET_OE, 1);
#else
return;
#endif
}
unsigned char tm_i2c_slot2addr(unsigned char slot) {
#ifdef BITFURY_METABANK
if (slot < 0 || slot > 31) return 0;
return ((TM_ADDR >> 1) + slot);
#else
return 0;
#endif
}