/*
* \file ii.c
* LIS3LV02DQ
*
* \version 1.0.0
* \date 2014年03月10日
* \author jiangdou <jiangdouu88@126.com>
*
* Copyright (c) 2014 jiangdou. All Rights Reserved.
*
*/
usage
dou@ubuntu:~/a10/linshi_chenxu$ /home/dou/a10/a20/A20-420-V12/lichee/out/linux/common/buildroot/external-toolchain/bin/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.msgs[1]).addr = slave_address;
(work_queue.msgs[1]).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[len+1];
int ret;
w_buf[0] = 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[0]).len = 1 + len;
(work_queue.msgs[0]).addr = slave_address;
(work_queue.msgs[0]).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)
{
// init LIS3LV02DQ status
// i2c_write(0x20,0x57)
// i2c_write(0x21,0x05)
// i2c_write(0x22,0x70)
printf("ACC OPEN is 3\n\r");
unsigned char temp1,temp2,temp3;
temp1 = 0x57;
temp2 = 0x05;
temp3 = 0x70;
i2c_write_reg(dev, &temp1, 0x1D, 0x20, 1);
i2c_write_reg(dev, &temp2, 0x1D, 0x21, 1);
i2c_write_reg(dev, &temp3, 0x1D, 0x22, 1);
//i2c_write_reg(char *dev, unsigned char *buf, unsigned slave_address, unsigned reg_address, int len)
}
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;
//i2c_read_reg(argv[1], &rw_val, slave_address, reg_address, 1);
//printf("Read %s-%x reg %x, read value:%x\n", argv[1], slave_address, reg_address, rw_val);
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;
//i2c_read_reg(argv[1], &rw_val, slave_address, reg_address, 1);
//printf("Read %s-%x reg %x, read value:%x\n", argv[1], slave_address, reg_address, rw_val);
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;
//i2c_read_reg(argv[1], &rw_val, slave_address, reg_address, 1);
//printf("Read %s-%x reg %x, read value:%x\n", argv[1], slave_address, reg_address, rw_val);
return z;
}
int main(void)
{
printf("ACC OPEN\n\r");
int X;
int Y;
int Z;
unsigned int fd;
//unsigned int slave_address, reg_address;
//unsigned r_w;
//unsigned w_val;
unsigned char r_val;
fd = open(devic, O_RDWR);
if (!fd) {
printf("Error on opening the device file %s\n", devic);
return 0;
}
printf("ACC OPEN is 1\n\r");
init_LIS3LV02DQ(devic);
printf("ACC OPEN is 2\n\r");
i2c_read_reg(devic, &r_val, 0x1D, 0x20, 1);
printf("read value:%x\n", r_val);
i2c_read_reg(devic, &r_val, 0x1D, 0x21, 1);
printf("read value:%x\n", r_val);
i2c_read_reg(devic, &r_val, 0x1D, 0x22, 1);
printf("read value:%x\n", r_val);
while(1)
{
X = read_x(devic); //read X_axis
Y = read_y(devic); //read Y_axis
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\r");
usleep(80000);
}
return 0;
}
</pre><pre class="cpp" name="code" snippet_file_name="blog_20140310_1_3820319" code_snippet_id="227558">