zoukankan      html  css  js  c++  java
  • tiny6410在I2c用户态中的程序设计eeprom

    在读写的过程中,发现写数据成功但是读取数据却失败,猜测是因为iic的读写操作过快,故在写操作后给一定的延迟,进而读写成功。

    代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    #include <linux/types.h>

    struct i2c_msg {
     unsigned short addr; /* slave address   */
     unsigned short flags;
     unsigned short len;  /* msg length    */
     unsigned char *buf;  /* pointer to msg data   */
    };

    struct i2c_rdwr_ioctl_data {
     struct i2c_msg *msgs; /* pointers to i2c_msgs */
     unsigned int nmsgs;   /* number of i2c_msgs */
    };

    #define I2C_RDWR 0x0707 /* Combined R/W transfer (one STOP only) */

    int main()
    {
     int fd;
     struct i2c_rdwr_ioctl_data eeprom_data;
     //1. 打开设备文件
     fd = open( "/dev/i2c-0", O_RDWR );
     if( fd == -1 )
     {
      printf("error open failed "); 
      return ;
     }
     
     //2. 构造写数据到eeprom的消息
     eeprom_data.msgs = (struct i2c_msg *)malloc( 2*sizeof(struct i2c_msg) );
     
     eeprom_data.nmsgs = 1;
     eeprom_data.msgs[0].len = 2;
     eeprom_data.msgs[0].addr = 0x50;
     eeprom_data.msgs[0].flags = 0;
     eeprom_data.msgs[0].buf = (unsigned char *)malloc(2);
     eeprom_data.msgs[0].buf[0] = 0x10;
     eeprom_data.msgs[0].buf[1] = 0x60;
     
     //3. 使用ioctl写入数据
     if( 0>ioctl( fd, I2C_RDWR, (unsigned long)&eeprom_data))
     {
      printf("error write ");
      return ;
     }
     sleep(1);
     //4. 构造从eeprom读取数据的消息
     eeprom_data.nmsgs = 2;
     eeprom_data.msgs[0].len = 1;
     eeprom_data.msgs[0].addr = 0x50;
     eeprom_data.msgs[0].flags = 0;
     eeprom_data.msgs[0].buf[0] = 0x10;
     
     eeprom_data.msgs[1].len = 1;
     eeprom_data.msgs[1].addr = 0x50;
     eeprom_data.msgs[1].flags = 1;
     eeprom_data.msgs[1].buf = (unsigned char *)malloc(2);
     eeprom_data.msgs[1].buf[0] = 0;
     //5. 使用ioctl读出数据
     if( -1 == ioctl( fd, I2C_RDWR, (unsigned long)&eeprom_data))
     {
      printf("error read ");
      return ;
     }
     
     printf("buff[0]=%x ",eeprom_data.msgs[1].buf[0]);
     
     //6. 关闭设备
     close(fd);
     
    }

  • 相关阅读:
    C# listbox鼠标选择改变改行颜色的另一种方便方法
    非专业码农 JAVA学习笔记 4 java继承和多态
    转:Java学习笔记之方法重载,动态方法调度和抽象类
    非专业码农 JAVA学习笔记 3 抽象、封装和类(2)
    使用bootstrap简单制作Tab切换页
    转载:CSS从大图中抠取小图完整教程(background-position应用)
    xhEditor 整理用法
    SCADA开源项目lite版本
    ImageSharp源码详解之JPEG压缩原理(3)DCT变换
    ImageSharp源码详解之JPEG压缩原理(4)熵编码
  • 原文地址:https://www.cnblogs.com/ynxf/p/5284342.html
Copyright © 2011-2022 走看看