forked from g4eml/Langstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp23017.c
201 lines (147 loc) · 3.55 KB
/
mcp23017.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
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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>
#include <sys/ioctl.h>
#include "/usr/include/linux/i2c-dev.h"
#include "/usr/include/linux/i2c.h"
#define GPIOA 0
#define GPIOB 1
#define IODIR 0
#define IPOL 1
#define GPINTEN 2
#define DEFVAL 3
#define INTCON 4
#define IOCON 5
#define GPPU 6
#define INTF 7
#define INTCAP 8
#define GPIO 9
#define OLAT 10
#define MCP_REGISTER(r,g) (((r)<<1)|(g)) // For I2C routines
//#####################################################################
//## I2C Access Functions ##
//#####################################################################
static int i2c_fd = -1; //Device node:
static unsigned long i2c_funcs = 0; //Support flags
// Write 8 bits to I2C bus peripheral:
int i2c_write8(int addr,int reg,int byte)
{
struct i2c_rdwr_ioctl_data msgset;
struct i2c_msg iomsgs[1];
char buf[2];
int rc;
buf[0] = (unsigned char) reg; // MCP23017 register no.
buf[1] = (unsigned char) byte; // Byte to write to register
iomsgs[0].addr = (unsigned) addr;
iomsgs[0].flags = 0; // Write
iomsgs[0].buf = buf;
iomsgs[0].len = 2;
msgset.msgs = iomsgs;
msgset.nmsgs = 1;
rc = ioctl(i2c_fd,I2C_RDWR,&msgset);
return rc < 0 ? -1 : 0;
}
// Read 8-bit value from peripheral:
int i2c_read8(int addr,int reg)
{
struct i2c_rdwr_ioctl_data msgset;
struct i2c_msg iomsgs[2];
char buf[1], rbuf[1];
int rc;
buf[0] = (char) reg;
iomsgs[0].addr = iomsgs[1].addr = (unsigned) addr;
iomsgs[0].flags = 0; // Write
iomsgs[0].buf = buf;
iomsgs[0].len = 1;
iomsgs[1].flags = I2C_M_RD; // Read
iomsgs[1].buf = rbuf;
iomsgs[1].len = 1;
msgset.msgs = iomsgs;
msgset.nmsgs = 2;
rc = ioctl(i2c_fd,I2C_RDWR,&msgset);
return rc < 0 ? -1 : ((int)(rbuf[0]) & 0x0FF);
}
// Open I2C bus and check capabilities :
int i2c_init(const char *node)
{
int rc;
i2c_fd = open(node,O_RDWR);
if ( i2c_fd < 0 )
{
return -1;
}
// Make sure the driver supports plain I2C I/O:
rc = ioctl(i2c_fd,I2C_FUNCS,&i2c_funcs);
assert(rc >= 0);
assert(i2c_funcs & I2C_FUNC_I2C);
return 0;
}
/*
* Close the I2C driver :
*/
void i2c_close(void)
{
close(i2c_fd);
i2c_fd = -1;
}
//#####################################################################
//## MCP23017 Functions ##
//#####################################################################
// Write to MCP23017 A or B register:
int mcp23017_writereg(int add,int reg,int AB,int value)
{
int reg_addr = MCP_REGISTER(reg,AB);
int rc;
rc = i2c_write8(add,reg_addr,value);
return rc;
}
// Read 8 bits from port
unsigned mcp23017_readport(int add,int AB)
{
int reg_addr = MCP_REGISTER(GPIO,AB);
return i2c_read8(add,reg_addr);
}
// Write 8-bits port
void mcp23017_writeport(int add,int AB,int value)
{
int reg_addr = MCP_REGISTER(GPIO,AB);
i2c_write8(add,reg_addr,value);
}
int mcp23017_readbit(int add,int AB, int bit)
{
int val;
int mask;
mask=1<<bit;
int reg_addr = MCP_REGISTER(GPIO,AB);
val=i2c_read8(add,reg_addr);
if((val & mask) >0)
{
return 1;
}
else
{
return 0;
}
}
void mcp23017_writebit(int add,int AB,int bit,int value)
{
int val;
int mask;
mask=1<<bit;
int reg_addr = MCP_REGISTER(GPIO,AB);
val= i2c_read8(add,reg_addr);
if(value==0)
{
val=val & (~mask);
}
else
{
val=val | mask;
}
i2c_write8(add,reg_addr,val);
}