-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path12_rp-i2c-max7311.c
139 lines (121 loc) · 4.23 KB
/
12_rp-i2c-max7311.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 "12_rp-i2c-max7311.h"
#include "12_i2c.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned long g_sleep_time = 50 * 1000;
char g_I2C_address = 0;
char getDefaultAddress();
int initController(const char *i2c_dev_path, char address){
_Bool state = 1;
char value = 0xAA;
// setup reset relay
state = (write_to_i2c(i2c_dev_path , address , 0x02, value , 0) == 0) & state;
state = (write_to_i2c(i2c_dev_path , address , 0x03, value , 0) == 0) & state;
usleep(g_sleep_time);
value = 0x00;
// setup null level on all out ports
state = (write_to_i2c(i2c_dev_path , address , 0x02, value , 0) == 0) & state;
state = (write_to_i2c(i2c_dev_path , address , 0x03, value , 0) == 0) & state;
// setup all port in outgoing mode
value = 0x00;
state = (write_to_i2c(i2c_dev_path , address , 0x06, value , 0) == 0) & state;
state = (write_to_i2c(i2c_dev_path , address , 0x07, value , 0) == 0) & state;
// check all regs
state = (read_from_i2c(i2c_dev_path , address , 0x02, &value , 0) == 0) & state;
state = (value == 0x00) & state;
state = (read_from_i2c(i2c_dev_path , address , 0x03, &value , 0) == 0) & state;
state = (value == 0x00) & state;
state = (read_from_i2c(i2c_dev_path , address , 0x06, &value , 0) == 0) & state;
state = (value == 0x00) & state;
state = (read_from_i2c(i2c_dev_path , address , 0x07, &value , 0) == 0) & state;
state = (value == 0x00) & state;
return state;
}
int rp_initController(){
return initController(DEFAULT_DEV_PATH, getDefaultAddress());
}
char getDefaultAddress(){
if (g_I2C_address) return g_I2C_address;
g_I2C_address = MAX7311_DEFAULT_ADDRESS_1_1;
FILE *modelF = popen("i2cdetect -y -r 0 | grep 20: | cut -f2 -d:", "r");
char model[128];
fread(&model, sizeof(char), 128, modelF);
char *found = strstr(model, "21");
if(found){
g_I2C_address = MAX7311_DEFAULT_ADDRESS_1_2;
}
return g_I2C_address;
}
int setPIN_GROUP(unsigned short pin_group,int state){
return setPIN_GROUP_EX(DEFAULT_DEV_PATH, getDefaultAddress(), pin_group, state);
}
int setPIN_GROUP_EX(const char *i2c_dev_path, char address, unsigned short pin_group,int state){
char value = 0;
char flag = 0;
char reg_addr = 0x02;
if (pin_group > 0xFF) {
reg_addr = 0x03;
pin_group = pin_group >> 8;
}
if (read_from_i2c(i2c_dev_path , address , reg_addr, &value , 0) == -1)
return -1;
if (state == 0) flag = 0xAA;
if (state == 1) flag = 0x55;
value = ((value & ~pin_group) | (pin_group & flag));
if (write_to_i2c(i2c_dev_path , address , reg_addr, value , 0) == -1)
return -1;
return 0;
}
int rp_setAC_DC(char port,char mode){
switch(port){
case RP_MAX7311_IN2:
if (setPIN_GROUP(PIN_K1,mode) == -1) return -1;
usleep(g_sleep_time);
if (setPIN_GROUP(PIN_K1,-1) == -1) return -1;
break;
case RP_MAX7311_IN1:
if (setPIN_GROUP(PIN_K2,mode) == -1) return -1;
usleep(g_sleep_time);
if (setPIN_GROUP(PIN_K2,-1) == -1) return -1;
break;
default:
return -1;
}
return 0;
}
int rp_setAttenuator(char port,char mode){
switch(port){
case RP_MAX7311_IN2:
if (setPIN_GROUP(PIN_K3,mode) == -1) return -1;
usleep(g_sleep_time);
if (setPIN_GROUP(PIN_K3,-1) == -1) return -1;
break;
case RP_MAX7311_IN1:
if (setPIN_GROUP(PIN_K4,mode) == -1) return -1;
usleep(g_sleep_time);
if (setPIN_GROUP(PIN_K4,-1) == -1) return -1;
break;
default:
return -1;
}
return 0;
}
int rp_setGainOut(char port,char mode){
switch(port){
case RP_MAX7311_OUT2:
if (setPIN_GROUP(PIN_K5,mode) == -1) return -1;
usleep(g_sleep_time);
if (setPIN_GROUP(PIN_K5,-1) == -1) return -1;
break;
case RP_MAX7311_OUT1:
if (setPIN_GROUP(PIN_K6,mode) == -1) return -1;
usleep(g_sleep_time);
if (setPIN_GROUP(PIN_K6,-1) == -1) return -1;
break;
default:
return -1;
}
return 0;
}