forked from zodiac1111/i2c-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c-adxl345-dbg.c
305 lines (296 loc) · 6.69 KB
/
i2c-adxl345-dbg.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// i2c 应用层调试,读写挂载在i2c总线上的eeprom
// 设置从设备,mini2440 参考其给的例子,完全在应用层
// 驱动层到 /dev/i2c/0 这个设备的过程完全没有涉及,
// 对于eeprom配合原始的i2c -w 和 i2c -r 检查结果
//
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
// i2c
#include <unistd.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include "adxl345.h"
//切换 EEPROM 则1 其他则测试桩
//切换 DEV_CLASS 指定三种测试方案
//1 自带的eeprom驱动
//2 stub测试桩驱动
//3 利用s3c2440平台i2c驱动,指定adxl345的,即在应用层实现驱动
//通用的i2c使用2440的平台驱动
#define DEV_CLASS 3
#if DEV_CLASS==1
#define ADDRESS 0x50 // eeprom设备地址
#define DEV "/dev/i2c/0"
#elif DEV_CLASS==2
#define ADDRESS 0x1D // stub测试桩地址
#define DEV "/dev/i2c/1"
#elif DEV_CLASS==3
#define ADDRESS 0x53 // adxl地址证实
#define DEV "/dev/i2c/0"
#endif
#define CHECK_I2C_FUNC( var, label ) \
do { if(0 == (var & label)) { \
fprintf(stderr, "\nError: " \
#label " function is required. Program halted.\n\n"); \
return 1; } \
} while(0);
struct Pos{
short x;
short y;
short z;
};
__u8 adxl_read(int fd, __u8 reg);
int adxl_write(int fd, __u8 reg, __u8 val);
__u16 adxl_read_byte(int fd, __u8 reg);
int adxl_read_six_byte(int fd, __u8 reg,struct Pos *pos);
int print_regs(int fd, int from, int to);
int guixyz(char name, int val);
// 主函数
int main(int argc, char* argv[])
{
int fd;
int ret = 0;
int funcs;
int i;
struct Pos pos;
// 1 打开 ,从设备打开
if (argc==2) {
fd = open(argv[1], O_RDWR);
} else {
fd = open(DEV, O_RDWR);
}
if (fd<0) {
perror("打开:");
printf("fd=%d\n", fd);
return -1;
}
// 2 设置从设备地址
ret = ioctl(fd, I2C_SLAVE, ADDRESS);
if (ret<0) {
perror("ioctl:");
printf("i2c_slave=%d\n", I2C_SLAVE);
goto CLOSE;
}
#if 1
//读取器件id
ret = adxl_read(fd, REG_DEVID);
if (ret<0) {
perror("器件id");
}
printf("器件ID[0x%X]=0x%X\n", REG_DEVID, ret);
#endif
#if 0
//其他寄存器测试
printf("所有寄存器读取测试:\n");
print_regs(fd, 0x1E, 0x39);
#endif
#if 1
//adxl_write(fd, DATA_FORMAT, 0b0000);
adxl_write(fd, POWER_CTL, 0x28);
//adxl_write(fd, BW_RATE, 0x0B);
//adxl_write(fd, INT_ENABLE, 0x00);
#endif
#if 1
__s16 x,z,y;
for (;;) {
//x = adxl_read_byte(fd, DATAX0);
//y = adxl_read_byte(fd, DATAY0);
//z = adxl_read_byte(fd, DATAZ0);
//printf("x=%5d y=%5d z=%5d\n", x,y,z);
adxl_read_six_byte(fd, DATAX0,&pos);
printf("x=%5d y=%5d z=%5d\n", pos.x,pos.y,pos.z);
printf("\n\n");
usleep(1000*1000);
}
#endif
#if 0
int x, y, z;
int x0, x1, y0, y1, z0, z1;
for (;;) {
x0 = adxl_read(fd, DATAX0);
x1 = adxl_read(fd, DATAX1);
y0 = adxl_read(fd, DATAY0);
y1 = adxl_read(fd, DATAY1);
z0 = adxl_read(fd, DATAZ0);
z1 = adxl_read(fd, DATAZ1);
x = x0;
y = y0;
z = z0;
guixyz('x', x);
guixyz('y', y);
guixyz('z', z);
printf("\n");
//printf("x0:%03d x1:%03d \n",x0,x1);
//printf("y0:%03d y1:%03d \n",y0,y1);
//printf("z0:%03d z1:%03d \n",z0,z1);
//printf("x=%03d y=%03d z=%03d \n",x,y,z);
usleep(100*1000);
printf("\f");
}
#endif
CLOSE:
close(fd);
return 0;
}
int guixyz(char name, int val)
{
int i;
printf("%c:%4d", name, val);
for (i = -128; i<127; i += 2) {
if (i==0) {
printf("o");
continue;
}
if (val<i) {
printf("-");
} else {
printf("=");
}
}
printf("\n");
return 0;
}
int adxl_write(int fd, __u8 reg, __u8 val)
{
union i2c_smbus_data data;
struct i2c_smbus_ioctl_data args;
int ret;
data.byte = val; //数据
args.read_write = I2C_SMBUS_WRITE; //读
args.command = reg; //命令?
args.size = I2C_SMBUS_BYTE_DATA; //大小一比特
args.data = &data; //保存数据
ret = ioctl(fd, I2C_SMBUS, &args);
if (ret!=0) { //非零错误
printf("写入ioctl错误");
return -1;
}
return data.byte;
}
__u8 adxl_read(int fd, __u8 reg)
{
union i2c_smbus_data data;
struct i2c_smbus_ioctl_data args;
int ret;
#if 0
args.read_write = I2C_SMBUS_WRITE; //写
args.command = reg; //地址
args.size = I2C_SMBUS_BYTE; //大小一比特
args.data = NULL; //没有数据
ret = ioctl(fd, I2C_SMBUS, &args);
if (ret<0) {
printf("r<0 r=%d\n", ret);
perror("ioctl");
}
#endif
//再访问,读
args.read_write = I2C_SMBUS_READ; //读
args.command = reg; //命令?
args.size = I2C_SMBUS_BYTE_DATA; //大小一比特
args.data = &data; //保存数据
ret = ioctl(fd, I2C_SMBUS, &args);
if (ret!=0) { //非零错误
printf("读取ioctl错误");
return -1;
}
return data.byte;
}
int adxl_read_six_byte(int fd, __u8 reg,struct Pos *pos)
{
union i2c_smbus_data data;
struct i2c_smbus_ioctl_data args;
int ret;
int len=6;//读取字节长度
#if 0
args.read_write = I2C_SMBUS_WRITE; //写
args.command = 6; //地址
args.size = I2C_SMBUS_BYTE; //大小一比特
args.data = NULL; //没有数据
ret = ioctl(fd, I2C_SMBUS, &args);
if (ret<0) {
printf("r<0 r=%d\n", ret);
perror("ioctl");
}
#endif
#if 1
//读
data.byte=len; //读取的长度从这里传进去
args.read_write = I2C_SMBUS_READ; //读
args.command = reg; //开始地址
args.size = I2C_SMBUS_I2C_BLOCK_DATA; //2字节
args.data = &data; //保存数据
ret = ioctl(fd, I2C_SMBUS, &args);
if (ret!=0) { //非零错误
printf("读取ioctl错误");
return -1;
}
#endif
#if 0
int i;
for (i = 0; i<7; i++) {
printf(" %02X ", data.block[i]);
fflush(stdout);
}
#endif
memcpy(pos,&data.block[1],len);
return 0;
}
unsigned short adxl_read_byte(int fd, __u8 reg)
{
union i2c_smbus_data data;
struct i2c_smbus_ioctl_data args;
int ret;
#if 0
args.read_write = I2C_SMBUS_WRITE; //写
args.command = reg; //地址
args.size = I2C_SMBUS_BYTE; //大小一比特
args.data = NULL; //没有数据
ret = ioctl(fd, I2C_SMBUS, &args);
if (ret<0) {
printf("r<0 r=%d\n", ret);
perror("ioctl");
}
#endif
#if 1
//读
args.read_write = I2C_SMBUS_READ; //读
args.command = reg; //开始地址
args.size = I2C_SMBUS_WORD_DATA; //2字节
args.data = &data; //保存数据
ret = ioctl(fd, I2C_SMBUS, &args);
if (ret!=0) { //非零错误
printf("读取ioctl错误");
return -1;
}
#endif
#if 0
int i;
for (i = 0; i<2; i++) {
printf(" %02X ", data.block[i]);
fflush(stdout);
}
#endif
return data.word;
}
int print_regs(int fd, int from, int to)
{
int i;
int ret;
for (i = from; i<=to; i++) {
ret = adxl_read(fd, i);
if (ret<0) {
perror("读取测试寄存器");
}
printf("[0x%02X]=0x%02X\t", i, ret);
if ((i-from+1)%4==0) {
printf("\n");
}
}
printf("\n");
return 0;
}