-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi2cfunc.c
215 lines (191 loc) · 4.36 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
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
/*******************************
* i2cfunc.c v3
* wrapper for I2C functions
* rev 3.1 June 2019 shabaz
*******************************/
#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 <string.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/mman.h>
#include "i2cfunc.h"
typedef struct {
uint32_t C;
uint32_t S;
uint32_t DLEN;
uint32_t A;
uint32_t FIFO;
uint32_t DIV;
uint32_t DEL;
uint32_t CLKT;
} i2c_reg_map_t;
int i2c_set_timeout(int val)
{
int handle;
void* addr;
i2c_reg_map_t *i2c_mem;
handle=open("/dev/mem", O_RDWR | O_SYNC);
if (handle<0)
{
printf("error opening /dev/mem!\n");
return(1);
}
addr=mmap(NULL, 32, PROT_READ | PROT_WRITE, MAP_SHARED, handle, I2C_BASE);
if (addr == MAP_FAILED)
{
printf("error performing mmap!\n");
close(handle);
return(1);
}
i2c_mem=(i2c_reg_map_t *)addr;
i2c_mem->CLKT=val;
munmap(addr, 32);
close(handle);
return(0);
}
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)
{
int ret;
ret = write(handle, buf, length);
if (ret != (int)length)
{
fprintf(stderr, "i2c_write error%d: %s\n", errno, 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) != (int)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);
}