你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下i2c与时钟芯片PCF8563的通信

[日期:2013-01-25] 来源:Linux社区  作者:hdw10 [字体:大 中 小]

执行 hwclock -r 后 也可以看到:


[ 2821.618046] here is pcf8563_get_datetime ,next func is i2c_transfer!
[ 2821.618058] octeon_i2c_xfer: num = 2
[ 2821.618067] octeon_i2c_xfer:msgs[0].addr = 51, msgs[0].flags = 0, msgs[0].len = 1
[ 2821.618079] octeon_i2c_xfer:msgs[0].buf[0] = 0
[ 2821.618090] octeon_i2c_xfer:msgs[1].addr = 51, msgs[1].flags = 1, msgs[1].len = 13
[ 2821.618102] octeon_i2c_xfer:msgs[1].buf[1] = 0
[ 2821.618112] octeon_i2c_write:data[0](msgs->buf[]) = 0, target(addr) = 51,  length = 1, phase = 0
[ 2821.618240] octeon_i2c_write: data[0] = 0
[ 2821.618348] octeon_i2c_read:data[0](msgs->buf[0]) = 0,target(addr) = 51, length = 13,phase = 1
[ 2821.618578] octeon_i2c_read: data[0] =  8
[ 2821.618685] octeon_i2c_read: data[1] =  0
[ 2821.618792] octeon_i2c_read: data[2] =  8
[ 2821.619329] octeon_i2c_read: data[7] =  7
[ 2821.619440] octeon_i2c_read: data[8] =  12
[ 2821.619547] octeon_i2c_read: data[9] =  a0
[ 2821.619655] octeon_i2c_read: data[10] =  84
[ 2821.619763] octeon_i2c_read: data[11] =  b2
[ 2821.619871] octeon_i2c_read: data[12] =  b5
[ 2821.619885] here come back pcf8563_get_datetime ,the received data :  secs=8, mins=4, hours=15,mday=6, mon=6, year=112,wday=5


宗上,可以定下结论:

在执行关于时间的读操作时 ,使用了 pcf8563_get_datetime --> octeon_i2c_read 

执行关于时间啊的写操作是,使用了pcf8563_set_datetime  ---> octeon_i2c_simple_write


在octeon_i2c_xfer() 中 进入不同读写函数的条件是就利用 msg中的 flags,len.

 

我们仿照pcf8563_get_datetime()中的代码:

 struct i2c_msg msgs[] = {
                { client->addr, 0, 1, buf },    /* setup read ptr */
                { client->addr, I2C_M_RD, 13, buf },    /* read status + date */
        }; 


写设计一个我们自定义的数据结构,这个数据结构为struct i2c_rdwr_ioctl_data, 代码如下:

struct i2c_rdwr_ioctl_data {
        struct i2c_msg __user *msgs;    /* pointers to i2c_msgs */
        __u32 nmsgs;                    /* number of i2c_msgs */
};

struct i2c_msg代码如下:


struct i2c_msg {
        __u16 addr;    /* slave address                        */
        __u16 flags;
#define I2C_M_TEN              0x0010  /* this is a ten bit chip address */
#define I2C_M_RD                0x0001  /* read data, from slave to master */
#define I2C_M_NOSTART          0x4000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_REV_DIR_ADDR      0x2000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK        0x1000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NO_RD_ACK        0x0800  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_RECV_LEN          0x0400  /* length will be first received byte */
        __u16 len;              /* msg length                          */
        __u8 *buf;              /* pointer to msg data                  */
};

 

我们定义一个i2c_read_data(),参考pcf8563_get_datetime,核心内容如下:
{
   
    struct i2c_rdwr_ioctl_data *data;
    data->nmsgs = 2;

    data->msgs[0].addr = addr;
    data->msgs[0].flags = 0;
    data->msgs[0].len = 1;
    data->msgs[0].buf[0] = offset;

    data->msgs[1].addr = addr;
    data->msgs[1].flags = I2C_M_RD;
    data->msgs[1].len = 13;           
    data->msgs[1].buf[0] = 0;

    ioctl(fd, I2C_RDWR, (unsigned long)data) ;

}


pcf8563_set_datetime中使用的i2c_master_send(){
..
    msg.addr = client->addr;
        msg.flags = client->flags & I2C_M_TEN;
        msg.len = count;
        msg.buf = (char *)buf;

    ret = i2c_transfer(adap, &msg, 1);
..
    }

参考以上,我们定义一个i2c_write_data(),核心代码如下:
{
    struct i2c_rdwr_ioctl_data *data;

    data->nmsgs = 1;


        data->msgs[0].addr = addr;
        data->msgs[0].flags = 0;
        data->msgs[0].len = 2;
        data->msgs[0].buf[0] = offset;
        data->msgs[0].buf[1] = val;


    ioctl(fd, I2C_RDWR, (unsigned long)data) ;

 

}


通过i2cctl函数将我们包装好的数据结构发送出去。

linux
相关资讯       Linux I2C  PCF8563 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款