请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:PROCESSOR-SDK-AM62X 您好!
我正在尝试在 am62x 上测试 i2c 读取和写入。
这是我正在使用的代码。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#define I2C_SLAVE 0x51
typedef struct {
/*
* The I2C Bus number that the device is connected to
*/
int bus;
/*
* The slave address of the I2C device
*/
unsigned int slave_addr;
/*
* The address of the register we want to access
*/
char *reg;
/*
* Number of bytes in the register address
*/
int reg_size;
/*
* Data buffer to transfer with the register
*/
char *buf;
/*
* Number of data bytes to transfer
*/
int buf_size;
} i2c_test_st;
int main(int argc, char **argv)
{
i2c_test_st i2c_test;
int i2c_file;
char reg[1];
char buf[2];
int fail = 0;
printf("\nI2C test!\n");
i2c_file = open("/dev/i2c-0", O_RDWR);
if (i2c_file < 0) {
printf("Open failed\n");
exit(-1);
}
/* Enable the CSI clock */
ioctl(i2c_file, 3, &i2c_test);
i2c_test.bus = 0;
i2c_test.slave_addr = 0x51;
printf("Slave address=0x%x\n\n", i2c_test.slave_addr);
reg[0] = (0x100 >> 8) & 0xff;
reg[1] = 0x100 & 0xff;
buf[1] = 0x4;
buf[0] = 0x0;
i2c_test.reg = reg;
i2c_test.reg_size = 2;
i2c_test.buf = buf;
i2c_test.buf_size = 2;
printf("Data write: buf[0]=%x, buf[1]=%x to reg[0]=%x reg[1]=%x\n", buf[0], buf[1], reg[0], reg[1]);
ioctl(i2c_file, 2, &i2c_test);
buf[0] = 0;
buf[1] = 0;
i2c_test.buf = buf;
ioctl(i2c_file, 1, &i2c_test);
if (i2c_test.buf[0] != 0x0) {
fail = 1;
}
if ((i2c_test.buf[1] & 0x4)!= 0x4) {
fail = 1;
}
printf("Data read: buf[0]=%x, buf[1]=%x from reg=%x\n", buf[0], buf[1], reg[0]);
if (fail == 1) {
printf("\nI2C TEST FAILED\n\n");
} else {
printf("\nI2C TEST PASSED\n\n");
}
/* Disable the CSI clock */
ioctl(i2c_file, 4, &i2c_test);
close(i2c_file);
return 0;
}
但是、当我回读时、我一直得到0。
所有内核配置都是正确的、因为我已经使用 shell 脚本进行了测试、并且能够回读。
以下是脚本。
address=0x01 address2=4 temp=0 data=0x01 bus=0 eeprom=0x51 #Loop through the addresses for (( i=0; i<10; i++ )) do #Increment the address by 1 int_value=$address2 int_value=$((int_value + 1)) address2=$int_value temp=$(printf "%x" $int_value) i2cset -y $bus $eeprom $address 0x$temp $data i i2ctransfer -y 0 w2@0x51 $address 0x$temp r1 >/dev/null