gpt4 book ai didi

c - I2C 读取返回不正确的值

转载 作者:太空宇宙 更新时间:2023-11-03 23:46:48 25 4
gpt4 key购买 nike

我正在尝试像使用 i2cget 一样读取 i2c 值,但在其中一种情况下它返回了错误的值。

i2cget -y 0 0x57 0x40 返回 0x57
i2cget -y 0 0x3b 0x09 返回 0x86

当我使用 #define I2C_ADDR 0x57buffer[0] = 0x40 运行我的程序时,我的程序返回 0x57

但是当我使用#define I2C_ADDR 0x3bbuffer[0] = 0x09 运行我的程序时,我的程序返回0x00

这是我的代码:

// #define I2C_ADDR 0x57
#define I2C_ADDR 0x3b

// #define I2C_REG 0x40
#define I2C_REG 0x09

int main(int argc, char **argv) {

char buffer[1];
buffer[0] = I2C_REG;

int fd;

// Get i2c File Descriptor
if((fd = open("/dev/i2c-0", O_RDWR)) >= 0){

// Set i2c Block Address
if((ioctl(fd, I2C_SLAVE, I2C_ADDR)) >= 0) {

// Set i2c Register Address
write(fd, buffer, 1);

// Read data at Register into buffer
read(fd, buffer, 1);

// Close fd
close(fd);

// Print Result
printf("0x%02x\n", buffer[0]);

} else {
// ioctl error
printf("ioctl error: %s\n", strerror(errno));
}
} else {
// file error
printf("Error opening file: %s\n", strerror(errno));
}

return 0;
}

我在 i2cget -y 0x3b 0x09 和我的程序上运行了 strace。这是一段输出,显示了 2 个读数的不同之处。

i2cget:

open("/dev/i2c-0", O_RDWR|O_LARGEFILE)  = 3
ioctl(3, 0x705, 0xbeae5bf8) = 0
ioctl(3, 0x703, 0x3b) = 0
ioctl(3, 0x720, 0xbeae5b4c) = 0
close(3) = 0

我的程序:

open("/dev/i2c-0", O_RDWR|O_LARGEFILE)  = 3
ioctl(3, 0x703, 0x3b) = 0
write(3, "\t", 1) = 1
read(3, "\0", 1) = 1
close(3)

最佳答案

i2cgetstrace我研究了0x720,发现它是I2C_SMBUS的值。然后我在 buildroot 中找到了 i2cget 的源代码。

C 代码中没有使用 this,但使用了函数 i2c_smbus_read_byte_data 。我将其添加到我的代码中并运行它,但由于函数未定义而无法运行。我将 i2c-dev.h 文件从 buildroot 复制到我的文件夹,并将 #include 更改为包含本地文件并运行,并输出正确的数据。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "i2c-dev.h"

#define I2C_ADDR 0x3b
#define I2C_REG 0x09

// Prints Value of /dev/i2c-0 at Block 0x3b Register 0x09
int main(int argc, char **argv) {
int fd, res;

if ((fd = open("/dev/i2c-0", O_RDWR)) >= 0) {

ioctl(fd, I2C_SLAVE, I2C_ADDR);

res = i2c_smbus_read_byte_data(fd, I2C_REG);

close(fd);

printf("Value - 0x%02x\n", res);
} else {
printf("Error opening file: %s\n", strerror(errno));
}

return 0;
}

关于c - I2C 读取返回不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30644118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com