- 论坛徽章:
- 0
|
/*----------------------------------------/
加急!!!!急等高手帮忙......
/----------------------------------------*/
/******************************************************************/
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;
}
/************************************************************************************************/ |
|