cubieboard A10 A20 I2C设备读写
/** \file ii.c
* LIS3LV02DQ
*
* \version 1.0.0
* \date 2014年03月10日
* \author jiangdou<jiangdouu88@126.com>
*
*
*/
/*
*usage
*dou@ubuntu:~/a10/linshi_chenxu$arm-linux-gnueabi-gcc -o gpio gpio_test.c -static
*
*/
#include <stdio.h>
#include <linux/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#define devic "/dev/i2c-2"
int i2c_read_reg(char *dev, unsigned char *buf, unsigned slave_address, unsigned reg_address, int len)
{
struct i2c_rdwr_ioctl_data work_queue;
unsigned char w_val = reg_address;
int ret;
int fd = open(dev, O_RDWR);
if (!fd) {
printf("Error on opening the device file\n");
return 0;
}
work_queue.nmsgs = 2;
work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct
i2c_msg));
if (!work_queue.msgs) {
printf("Memory alloc error\n");
close(fd);
return 0;
}
ioctl(fd, I2C_TIMEOUT, 3);
ioctl(fd, I2C_RETRIES, 3);
(work_queue.msgs).len = 1;
(work_queue.msgs).addr = slave_address;
(work_queue.msgs).buf = &w_val;
(work_queue.msgs).len = len;
(work_queue.msgs).flags = I2C_M_RD;
(work_queue.msgs).addr = slave_address;
(work_queue.msgs).buf = buf;
ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue);
if (ret < 0) {
printf("Error during I2C_RDWR ioctl with error code: %d\n", ret);
close(fd);
free(work_queue.msgs);
return 0;
} else {
//printf("read salve:%02x reg:%02x\n", slave_address, reg_address);
close(fd);
free(work_queue.msgs);
return len;
}
}
int i2c_write_reg(char *dev, unsigned char *buf, unsigned slave_address, unsigned reg_address, int len)
{
struct i2c_rdwr_ioctl_data work_queue;
unsigned char w_val = reg_address;
unsigned char w_buf;
int ret;
w_buf = reg_address;
int fd = open(dev, O_RDWR);
if (!fd) {
printf("Error on opening the device file\n");
return 0;
}
work_queue.nmsgs = 1;
work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct
i2c_msg));
if (!work_queue.msgs) {
printf("Memory alloc error\n");
close(fd);
return 0;
}
ioctl(fd, I2C_TIMEOUT, 2);
ioctl(fd, I2C_RETRIES, 1);
(work_queue.msgs).len = 1 + len;
(work_queue.msgs).addr = slave_address;
(work_queue.msgs).buf = w_buf;
memcpy(w_buf + 1, buf, len);
ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue);
if (ret < 0) {
printf("Error during I2C_RDWR ioctl with error code: %d\n", ret);
close(fd);
free(work_queue.msgs);
return 0;
} else {
printf("write salve:%02x reg:%02x\n", slave_address, reg_address);
close(fd);
free(work_queue.msgs);
return len;
}
}
int init_LIS3LV02DQ(char *dev)
{
printf("ACC OPEN is 3\n\r");
unsigned char temp1,temp2,temp3;
temp1 = 0x57;
temp2 = 0x81;
temp3 = 0x03;
//寄存器配置
i2c_write_reg(dev, &temp1, 0x1D, 0x20, 1);
i2c_write_reg(dev, &temp2, 0x1D, 0x21, 1);
i2c_write_reg(dev, &temp3, 0x1D, 0x22, 1);
}
int read_x(char *dev) //read x
{
unsigned char tem1,tem2;
int x = 0;
i2c_read_reg(dev, &tem1, 0x1D, 0x28, 1);
i2c_read_reg(dev, &tem2, 0x1D, 0x29, 1);
x = (tem2 << 8) | tem1;
return x;
}
int read_y(char *dev) //read y
{
unsigned char tem1,tem2;
int y = 0;
i2c_read_reg(dev, &tem1, 0x1D, 0x2A, 1);
i2c_read_reg(dev, &tem2, 0x1D, 0x2B, 1);
y = (tem2 << 8) | tem1;
return y;
}
int read_z(char *dev) //read z
{
unsigned char tem1,tem2;
int z = 0;
i2c_read_reg(dev, &tem1, 0x1D, 0x2C, 1);
i2c_read_reg(dev, &tem2, 0x1D, 0x2D, 1);
z = (tem2 << 8) | tem1;
return z;
}
int main(void)
{
int X;
int Y;
int Z;
unsigned int fd;
unsigned char r_val;
fd = open(devic, O_RDWR);
if (!fd) {
printf("Error on opening the device file %s\n", devic);
return 0;
}
init_LIS3LV02DQ(devic);
while(1)
{
X = read_x(devic); //read X_axis
Y = read_y(devic); //read Y_axis //0 -> 65535
Z = read_z(devic); //read Z_axis
printf("X_axis is %d,y_axis is %d,Z_axis is %d\n\r", X,Y,Z);
printf("\n");
usleep(8);
}
return 0;
}
页:
[1]