-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2cfunc.c
executable file
·175 lines (150 loc) · 3.75 KB
/
i2cfunc.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
/*******************************
* i2cfunc.c v3
* wrapper for I2C functions
*******************************/
#include <stdio.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include "i2cfunc.h"
int i2c_open(unsigned char bus, unsigned char addr)
{
int file;
char filename[16];
sprintf(filename,"/dev/i2c-%d", bus);
if ((file = open(filename,O_RDWR)) < 0)
{
fprintf(stderr, "i2c_open open error: %s\n", strerror(errno));
return(file);
}
if (ioctl(file,I2C_SLAVE,addr) < 0)
{
fprintf(stderr, "i2c_open ioctl error: %s\n", strerror(errno));
return(-1);
}
return(file);
}
int i2c_write(int handle, unsigned char* buf, unsigned int length)
{
if (write(handle, buf, length) != length)
{
fprintf(stderr, "i2c_write error: %s\n", strerror(errno));
return(-1);
}
return(length);
}
int i2c_write_byte(int handle, unsigned char val)
{
if (write(handle, &val, 1) != 1)
{
fprintf(stderr, "i2c_write_byte error: %s\n", strerror(errno));
return(-1);
}
return(1);
}
int i2c_read(int handle, unsigned char* buf, unsigned int length)
{
if (read(handle, buf, length) != length)
{
fprintf(stderr, "i2c_read error: %s\n", strerror(errno));
return(-1);
}
return(length);
}
int i2c_read_byte(int handle, unsigned char* val)
{
if (read(handle, val, 1) != 1)
{
fprintf(stderr, "i2c_read_byte error: %s\n", strerror(errno));
return(-1);
}
return(1);
}
int i2c_close(int handle)
{
if ((close(handle)) != 0)
{
fprintf(stderr, "i2c_close error: %s\n", strerror(errno));
return(-1);
}
return(0);
}
int i2c_write_read(int handle,
unsigned char addr_w, unsigned char *buf_w, unsigned int len_w,
unsigned char addr_r, unsigned char *buf_r, unsigned int len_r)
{
struct i2c_rdwr_ioctl_data msgset;
struct i2c_msg msgs[2];
msgs[0].addr=addr_w;
msgs[0].len=len_w;
msgs[0].flags=0;
msgs[0].buf=buf_w;
msgs[1].addr=addr_r;
msgs[1].len=len_r;
msgs[1].flags=1;
msgs[1].buf=buf_r;
msgset.nmsgs=2;
msgset.msgs=msgs;
if (ioctl(handle,I2C_RDWR,(unsigned long)&msgset)<0)
{
fprintf(stderr, "i2c_write_read error: %s\n",strerror(errno));
return -1;
}
return(len_r);
}
int i2c_write_ignore_nack(int handle,
unsigned char addr_w, unsigned char* buf, unsigned int length)
{
struct i2c_rdwr_ioctl_data msgset;
struct i2c_msg msgs[1];
msgs[0].addr=addr_w;
msgs[0].len=length;
msgs[0].flags=0 | I2C_M_IGNORE_NAK;
msgs[0].buf=buf;
msgset.nmsgs=1;
msgset.msgs=msgs;
if (ioctl(handle,I2C_RDWR,(unsigned long)&msgset)<0)
{
fprintf(stderr, "i2c_write_ignore_nack error: %s\n",strerror(errno));
return -1;
}
return(length);
}
int i2c_read_no_ack(int handle,
unsigned char addr_r, unsigned char* buf, unsigned int length)
{
struct i2c_rdwr_ioctl_data msgset;
struct i2c_msg msgs[1];
msgs[0].addr=addr_r;
msgs[0].len=length;
msgs[0].flags=I2C_M_RD | I2C_M_NO_RD_ACK;
msgs[0].buf=buf;
msgset.nmsgs=1;
msgset.msgs=msgs;
if (ioctl(handle,I2C_RDWR,(unsigned long)&msgset)<0)
{
fprintf(stderr, "i2c_read_no_ack error: %s\n",strerror(errno));
return -1;
}
return(length);
}
int delay_ms(unsigned int msec)
{
int ret;
struct timespec a;
if (msec>999)
{
fprintf(stderr, "delay_ms error: delay value needs to be less than 999\n");
msec=999;
}
a.tv_nsec=((long)(msec))*1E6d;
a.tv_sec=0;
if ((ret = nanosleep(&a, NULL)) != 0)
{
fprintf(stderr, "delay_ms error: %s\n", strerror(errno));
}
return(0);
}