当前位置: 技术问答>linux和unix
使用I2CTWI遇到的问题
来源: 互联网 发布时间:2016-10-02
本文导语: 我在uCLinux上使用 I2CTWI作为I2C通信。使用参考程序如下: #include #include #include #include #include #define I2C_SLAVE 0x0703 /* Change slave address */ int main(int argc, char* argv[]) { unsigned char outp_buf[100]; int re...
我在uCLinux上使用 I2CTWI作为I2C通信。使用参考程序如下:
#include
#include
#include
#include
#include
#define I2C_SLAVE 0x0703 /* Change slave address */
int main(int argc, char* argv[])
{
unsigned char outp_buf[100];
int regval, regaddr, rc = 0;
int i2c_fd;
int slave_addr;
slave_addr = 0x3C;
printf("slave addr: 0x%xn", slave_addr);
slave_addr >>= 1;
i2c_fd = open("/dev/i2c-0", O_RDWR);
if(ioctl(i2c_fd, I2C_SLAVE, slave_addr)> 8) & 0xFF;
buf[2] = val & 0xFF;
}
if (write(filehandle, buf, iDataWidth + 1) == iDataWidth + 1)
{
if (iDataWidth == 1)
{
printf("local_cam_set_register: 0x%x 0x%x writtenrn", buf[0], buf[1]);
}
else
{
printf("local_cam_set_register: 0x%x 0x%x 0x%x writtenrn", buf[0], buf[1], buf[2]);
}
return 0;
}
if (iDataWidth == 1)
{
printf("local_cam_set_register: Error sending 0x%02x at 0x%02x, errno = %drn", val, reg_addr, errno);
}
else
{
printf("local_cam_set_register: Error sending 0x%04x at 0x%02x, errno = %drn", val, reg_addr, errno);
}
return (-1);
}
int
local_cam_get_register(int filehandle,
int bus_width,
int reg_addr,
int *val)
{
unsigned char regval = reg_addr; /* Device register to access */
unsigned char buf[10];
int try_count;
buf[0] = regval;
if (write(filehandle, buf, 1) != 1)
{
printf("local_cam_get_register: error sending register address 0x%02x, errno = %drn",
regval, errno);
return -1;
}
if (read(filehandle, buf, bus_width) == bus_width)
{
if (bus_width == 1)
{
*val = buf[0];
}
else
{
*val = buf[0]