免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6537 | 回复: 6
打印 上一主题 下一主题

I2C芯片LM75驱动问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-28 15:50 |只看该作者 |倒序浏览
/*----------------------------------------/

   加急!!!!急等高手帮忙......
/----------------------------------------*/
/******************************************************************/
1.环境:
内核:2.6.20
嵌入式平台:AT91ARM9200
i2c芯片:lm75


2.问题描述:

因为2.6的内核,本身就有lm75芯片的驱动支持,所以我就基于这个驱动进行修改就行了,而且2.6.20以后的对at91芯片的i2c支持有。我用一个测试程序对lm75芯片进行读写,写的时候似乎没有问题,但是一旦读的时候就出现“TXRDY timeout“的错误。
具体错误如下:
/********************************************************************************************************/
# ./temp
@_@ filename::/dev/i2c-0

@_@ phy_addr::4c

@_@ start initting ...
@_@ lm75 init over.
@_@ start get temperature ...
read error!
: Connection timed out
# cat /proc/kmsg
<7>i2c_adapter i2c-0: ioctl, cmd=0x703, arg=0x4c
<7>i2c-dev: i2c-0 writing 1 bytes.
<7>i2c_adapter i2c-0: master_xfer[0] W, addr=0x4c, len=1
<7>i2c_adapter i2c-0: at91_xfer: processing 1 messages:
<7>i2c_adapter i2c-0:  #0: writing 1 byte to 0x4c
<7>i2c_adapter i2c-0: TXRDY timeout
<7>i2c-dev: i2c-0 writing 1 bytes.
<7>i2c_adapter i2c-0: master_xfer[0] W, addr=0x4c, len=1
<7>i2c_adapter i2c-0: at91_xfer: processing 1 messages:
<7>i2c_adapter i2c-0:  #0: writing 1 byte to 0x4c
<7>i2c_adapter i2c-0: TXRDY timeout
<7>i2c-dev: i2c-0 writing 1 bytes.
<7>i2c_adapter i2c-0: master_xfer[0] W, addr=0x4c, len=1
<7>i2c_adapter i2c-0: at91_xfer: processing 1 messages:
<7>i2c_adapter i2c-0:  #0: writing 1 byte to 0x4c
<7>i2c_adapter i2c-0: TXRDY timeout
<7>i2c-dev: i2c-0 reading 2 bytes.
<7>i2c_adapter i2c-0: master_xfer[0] R, addr=0x4c, len=2
<7>i2c_adapter i2c-0: at91_xfer: processing 1 messages:
<7>i2c_adapter i2c-0:  #0: reading 2 bytes from 0x4c
<7>i2c_adapter i2c-0: RXRDY timeout
/********************************************************************************************************/
测试程序代码如下:
/************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
//#include <linux/i2c-dev.h>
#include <asm/errno.h>

#include "lm75.h"

#define I2C_SLAVE 0x0703
#define ID_PHY 0x98


/* The LM75 registers */
#define LM75_REG_TEMP       0x00
#define LM75_REG_CONF       0x01
#define LM75_REG_TEMP_HYST  0x02
#define LM75_REG_TEMP_OS    0x03

/* Each client has this additional data */
struct lm75_data {
    unsigned short        temp_input; /* Register values */
    unsigned short        temp_max;
    unsigned short        temp_hyst;
};
static int lm75_init(int file);
static int lm75_get_temp(int file, struct lm75_data * value);
main(int argc, char **argv)
{
        int file;
          int adapter_nr = 0;
          char filename[20];
        struct lm75_data value;
        int  ret;

          sprintf(filename, "/dev/i2c-%d", adapter_nr);
          if ((file = open(filename, O_RDWR)) < 0) {
                perror("No such i2c device!\n");
                    exit(1);
          }
       
        int addr = (ID_PHY) >> 1; /* The I2C address */
        if ((ret = ioctl(file, I2C_SLAVE, addr)) < 0) {
                if(ret ==  -EBUSY)
                        printf("Device busy\n");
                if(ret == -EINVAL)
                        printf("invalid addr\n");
                printf("No such slave device. %d\n ", ret);
                close(file);
                   exit(1);
          }
       
        lm75_init(file);
       
        lm75_get_temp(file, &value);               

        printf("temp_input: %d , temp_max: %d , temp_hyst: %d \n",
                                LM75_TEMP_FROM_REG(value.temp_input),
                                LM75_TEMP_FROM_REG(value.temp_max),
                                LM75_TEMP_FROM_REG(value.temp_hyst) );

        close(file);
}

static int
lm75_init(int file)
{       
        unsigned char buf[2];

        memset(buf, 0, sizeof(buf));
        buf[0] = LM75_REG_CONF;
        write(file, buf , 1);
        buf[0] = 0;
        write(file, buf , 1);       
}

static int
lm75_get_temp(int file, struct lm75_data * value)
{
    int ret = -EIO;
    unsigned char buf[5];
    int i;

    memset(buf, 0, sizeof(buf));
        buf[0] = LM75_REG_TEMP;
while(1){
        write(file, buf , 1);
sleep(1);
}
    ret = read(file, buf, 2);
    if (ret<0){
               perror("read error!\n");
                exit(1);
        }
    value->temp_input = (unsigned short)buf[0]<<8 | buf[1];

        buf[0] = LM75_REG_TEMP_OS;
        write(file, buf , 1);
    ret = read(file, buf, 2);
    if (ret<0){
               perror("read error!\n");
                exit(1);
        }
    value->temp_max = (unsigned short)buf[0]<<8 | buf[1];

        buf[0] = LM75_REG_TEMP_HYST;
        write(file, buf , 1);
    ret = read(file, buf, 2);
    if (ret<0){
               perror("read error!\n");
                exit(1);
        }
    value->temp_hyst = (unsigned short)buf[1]<<8 | buf[0];

    return 0;
}
/************************************************************************************************/

论坛徽章:
5
2 [报告]
发表于 2009-03-28 20:32 |只看该作者
有条件的话, 拿示波器看一下I2C波形吧。

论坛徽章:
0
3 [报告]
发表于 2009-03-28 22:18 |只看该作者

回复 #2 yidou 的帖子

这个肯定有用示波器看过呀,结果sda和scl全是高,不变。现在根据kmsg和printk看了一下,好像测试程序不对,因为测试程序并没有调用到lm75里面的操作函数,但是系统初始化的时候,有对lm75作操作呀,那个时候为什么 也不对呢,现在先改一下lm75的驱动和测试程序,让它真正调用lm75的驱动。

论坛徽章:
5
4 [报告]
发表于 2009-03-28 23:28 |只看该作者
原帖由 ipromiseu 于 2009/3/28 22:18 发表
这个肯定有用示波器看过呀,结果sda和scl全是高,不变。现在根据kmsg和printk看了一下,好像测试程序不对,因为测试程序并没有调用到lm75里面的操作函数,但是系统初始化的时候,有对lm75作操作呀,那个时候为什 ...


能在总线上看到I2C的Start Condition吗?

论坛徽章:
0
5 [报告]
发表于 2009-03-29 02:01 |只看该作者

回复 #4 yidou 的帖子

看不到,就是说SDA线上和SCA线上都没有信号,都是高电平。对了,设备驱动部分对用户接口的 open,read等一些操作可以直接用i2c_dev吗?还需要自己去写吗?

论坛徽章:
0
6 [报告]
发表于 2009-03-30 10:49 |只看该作者
死等,20ms朝上。

论坛徽章:
5
7 [报告]
发表于 2009-03-30 11:41 |只看该作者
原帖由 ipromiseu 于 2009/3/29 02:01 发表
看不到,就是说SDA线上和SCA线上都没有信号,都是高电平。对了,设备驱动部分对用户接口的 open,read等一些操作可以直接用i2c_dev吗?还需要自己去写吗?


驱动中的ioctl, 用i2c_dev提供的.  应用程序这端, 很容易自己实现.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP