I2C EEPROM:可以写入但只能读取0xFF [英] I2C EEPROM : can write but read only 0xFF
问题描述
我目前正在研究i.MX6(Android BSP)和24C08WP EEPROM之间的I2C通信.
I am currently working on an I2C communication between an i.MX6 (Android BSP) and an 24C08WP EEPROM.
我正在i.MX6上运行一个以前在Linux下NDK下编译的二进制文件.
I'm running on the i.MX6 a binary previously compiled under an NDK under Linux.
借助 i2cdetect
工具,我检测到连接到i.MX6的I2C总线(地址 0x50
)的NTAG 5组件.
I detect the NTAG 5 component connected to the I2C bus (address 0x50
) of the i.MX6 thanks to an i2cdetect
tool.
使用以下代码,我可以执行写操作,可以使用Arduino板和I2C读操作进行检查.
With the following code, I can perform write operation, which I can check using an Arduino board and I2C read operation.
但是,当我在i.MX6下的用户空间中执行读取操作时,我只会得到 0xFF
值.
However, when I perform read operations in user space under the i.MX6, I only get the 0xFF
value.
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include "board.h"
#include "debug_tools.h"
#include "boardselection.h"
int main(void) {
int file;
int adapter_nr = 1; /* probably dynamically determined */
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
file = open(filename, O_RDWR);
if (file < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
int addr = 0x50; /* The I2C address */
if (ioctl(file, I2C_SLAVE, addr) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
uint8_t reg = 0x00;
uint8_t data_w[4] = {0x00, 0x00, 0x00, 0x00};
data_w[0] = reg;
data_w[1] = 0x01;
data_w[2] = 0x02;
data_w[3] = 0x03;
/* Write the register */
if (write(file, data_w, 4) != 4)
{
perror("Failed to write to the i2c bus");
exit(1);
}
usleep(2000000);
uint8_t data_r[4] = {0x00, 0x00, 0x00, 0x00};
if (read(file, data_r, 3) != 3) {
/* ERROR HANDLING: i2c transaction failed */
perror("Failed to read register value");
exit(1);
}
/* data_r[0] contains the read byte */
printf("%X %X %X\n", data_r[0], data_r[1], data_r[2]);
return 0;
}
你能帮我吗?
此线程几乎描述了关于接收0xFF值的同样问题.
This thread describes almost the same problem about receiving 0xFF value.
推荐答案
As @Andrew Cottrell said in this thread: "To read from your I2C device, assuming it uses a one-byte register, write a buffer of one byte (the register address) then read a buffer of one or more bytes (the value at that register and subsequent registers)."
因此正确的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include "board.h"
#include "debug_tools.h"
#include "boardselection.h"
int main(void) {
int file;
int adapter_nr = 1; /* probably dynamically determined */
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
file = open(filename, O_RDWR);
if (file < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
int addr = 0x50; /* The I2C address */
if (ioctl(file, I2C_SLAVE, addr) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
exit(1);
}
uint8_t reg = 0x00;
uint8_t data_w[4] = {0x00, 0x00, 0x00, 0x00};
data_w[0] = reg;
data_w[1] = 0x44;
/* Write the register */
if (write(file, data_w, 2) != 2)
{
perror("Failed to write to the i2c bus");
exit(1);
}
usleep(1000000);
uint8_t data_r[4] = {0x00, 0x00, 0x00, 0x00};
if (write(file, ®, 1) != 1)
{
perror("Failed to write to the i2c bus");
exit(1);
}
if (read(file, data_r, 1) != 1) {
/* ERROR HANDLING: i2c transaction failed */
perror("Failed to read register value");
exit(1);
}
/* data_r[0] contains the read byte: 0x44 */
printf("%02X\n", data_r[0]);
return 0;
}
N.B .:如果不使用 usleep()
在两次写操作之间等待,则第二次写操作可能会失败.
N.B.: If you do not use usleep()
to wait between two write operations, the second write operation might fail.
这篇关于I2C EEPROM:可以写入但只能读取0xFF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!