CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 7003|回复: 0

cubieboard A10 A20 I2C设备读写

[复制链接]
发表于 2014-12-8 19:29:49 | 显示全部楼层 |阅读模式
  1. /*
  2. * \file        ii.c
  3. * LIS3LV02DQ
  4. *
  5. * \version     1.0.0
  6. * \date        2014年03月10日
  7. * \author      jiangdou  <jiangdouu88@126.com>
  8. *
  9. *
  10. */

  11. /*
  12. *usage
  13. *dou@ubuntu:~/a10/linshi_chenxu$  arm-linux-gnueabi-gcc -o gpio gpio_test.c -static

  14. *
  15. */

  16. #include <stdio.h>  
  17. #include <linux/types.h>  
  18. #include <fcntl.h>  
  19. #include <unistd.h>  
  20. #include <stdlib.h>  
  21. #include <sys/types.h>  
  22. #include <sys/ioctl.h>  
  23. #include <errno.h>  
  24. #include <assert.h>  
  25. #include <string.h>  
  26. #include <linux/i2c.h>  
  27. #include <linux/i2c-dev.h>
  28. #define        devic        "/dev/i2c-2"

  29.   
  30. int i2c_read_reg(char *dev, unsigned char *buf, unsigned slave_address, unsigned reg_address, int len)  
  31. {  
  32.     struct i2c_rdwr_ioctl_data work_queue;  
  33.     unsigned char w_val = reg_address;  
  34.     int ret;  
  35.   
  36.     int fd = open(dev, O_RDWR);  
  37.     if (!fd) {  
  38.         printf("Error on opening the device file\n");  
  39.         return 0;  
  40.     }  
  41.   
  42.     work_queue.nmsgs = 2;  
  43.     work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct  
  44.             i2c_msg));  
  45.     if (!work_queue.msgs) {  
  46.         printf("Memory alloc error\n");  
  47.         close(fd);  
  48.         return 0;  
  49.     }  
  50.   
  51.     ioctl(fd, I2C_TIMEOUT, 3);  
  52.     ioctl(fd, I2C_RETRIES, 3);  
  53.   
  54.     (work_queue.msgs[0]).len = 1;  
  55.     (work_queue.msgs[0]).addr = slave_address;  
  56.     (work_queue.msgs[0]).buf = &w_val;  
  57.   
  58.     (work_queue.msgs[1]).len = len;  
  59.     (work_queue.msgs[1]).flags = I2C_M_RD;  
  60.     (work_queue.msgs[1]).addr = slave_address;  
  61.     (work_queue.msgs[1]).buf = buf;  
  62.   
  63.     ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue);  
  64.     if (ret < 0) {  
  65.         printf("Error during I2C_RDWR ioctl with error code: %d\n", ret);  
  66.         close(fd);  
  67.         free(work_queue.msgs);  
  68.         return 0;  
  69.     } else {  
  70.         //printf("read salve:%02x reg:%02x\n", slave_address, reg_address);  
  71.         close(fd);  
  72.         free(work_queue.msgs);  
  73.         return len;  
  74.     }  
  75. }  
  76.   
  77. int i2c_write_reg(char *dev, unsigned char *buf, unsigned slave_address, unsigned reg_address, int len)  
  78. {  
  79.     struct i2c_rdwr_ioctl_data work_queue;  
  80.     unsigned char w_val = reg_address;  
  81.     unsigned char w_buf[len+1];  
  82.     int ret;  
  83.   
  84.     w_buf[0] = reg_address;  
  85.   
  86.     int fd = open(dev, O_RDWR);  
  87.     if (!fd) {  
  88.         printf("Error on opening the device file\n");  
  89.         return 0;  
  90.     }  
  91.   
  92.     work_queue.nmsgs = 1;  
  93.     work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct  
  94.             i2c_msg));  
  95.     if (!work_queue.msgs) {  
  96.         printf("Memory alloc error\n");  
  97.         close(fd);  
  98.         return 0;  
  99.     }  
  100.   
  101.     ioctl(fd, I2C_TIMEOUT, 2);  
  102.     ioctl(fd, I2C_RETRIES, 1);  
  103.   
  104.     (work_queue.msgs[0]).len = 1 + len;  
  105.     (work_queue.msgs[0]).addr = slave_address;  
  106.     (work_queue.msgs[0]).buf = w_buf;  
  107.   
  108.     memcpy(w_buf + 1, buf, len);  
  109.   
  110.     ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue);  
  111.     if (ret < 0) {  
  112.         printf("Error during I2C_RDWR ioctl with error code: %d\n", ret);  
  113.         close(fd);  
  114.         free(work_queue.msgs);  
  115.         return 0;  
  116.     } else {  
  117.         printf("write salve:%02x reg:%02x\n", slave_address, reg_address);  
  118.         close(fd);  
  119.         free(work_queue.msgs);  
  120.         return len;  
  121.     }  
  122. }  
  123.        
  124.        
  125. int init_LIS3LV02DQ(char *dev)
  126. {
  127.         printf("ACC OPEN is 3\n\r");
  128.         unsigned char temp1,temp2,temp3;
  129.         temp1 = 0x57;
  130.         temp2 = 0x81;
  131.         temp3 = 0x03;
  132.         //寄存器配置
  133.         i2c_write_reg(dev, &temp1, 0x1D, 0x20, 1);
  134.         i2c_write_reg(dev, &temp2, 0x1D, 0x21, 1);
  135.         i2c_write_reg(dev, &temp3, 0x1D, 0x22, 1);
  136.          
  137. }

  138. int read_x(char *dev)                //read x
  139. {       
  140.         unsigned char tem1,tem2;
  141.         int x = 0;
  142.         i2c_read_reg(dev, &tem1, 0x1D, 0x28, 1);
  143.        
  144.         i2c_read_reg(dev, &tem2, 0x1D, 0x29, 1);
  145.         x = (tem2 << 8) | tem1;
  146.         return x;
  147. }

  148. int read_y(char *dev)                //read y
  149. {
  150.         unsigned char tem1,tem2;
  151.         int y = 0;
  152.         i2c_read_reg(dev, &tem1, 0x1D, 0x2A, 1);
  153.         i2c_read_reg(dev, &tem2, 0x1D, 0x2B, 1);
  154.         y = (tem2 << 8) | tem1;

  155.         return y;

  156. }

  157. int read_z(char *dev)                //read z
  158. {       
  159.         unsigned char tem1,tem2;
  160.         int z = 0;
  161.         i2c_read_reg(dev, &tem1, 0x1D, 0x2C, 1);
  162.        
  163.         i2c_read_reg(dev, &tem2, 0x1D, 0x2D, 1);
  164.         z = (tem2 << 8) | tem1;
  165.         return z;


  166. }

  167. int main(void)

  168. {
  169.         int X;
  170.         int Y;
  171.         int Z;
  172.        
  173.         unsigned int fd;  
  174.     unsigned char r_val;  
  175.        
  176.         fd = open(devic, O_RDWR);  
  177.   
  178.     if (!fd) {  
  179.         printf("Error on opening the device file %s\n", devic);  
  180.         return 0;  
  181.     }  
  182.        
  183.         init_LIS3LV02DQ(devic);
  184.         while(1)
  185.         {
  186.                
  187.                 X = read_x(devic);        //read X_axis
  188.                 Y = read_y(devic);        //read Y_axis        //0 -> 65535
  189.                 Z = read_z(devic);        //read Z_axis
  190.                
  191.                 printf("X_axis is %d,y_axis is %d,Z_axis is %d\n\r", X,Y,Z);
  192.          
  193.                 printf("\n");
  194.                 usleep(8);
  195.         }
  196.        

  197.         return 0;
  198. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-5-9 00:57 , Processed in 0.019874 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部