I2C EEPROM : 可写但只读 0xFF
I2C EEPROM : can write but read only 0xFF
我目前正在研究 i.MX6(Android BSP)和 24C08WP EEPROM 之间的 I2C 通信。
我 运行 在 i.MX6 上是一个二进制文件,之前是在 Linux 下的 NDK 下编译的。
借助 i2cdetect
工具,我检测到连接到 i.MX6 的 I2C 总线(地址 0x50
)的 NTAG 5 组件。
使用以下代码,我可以执行写入操作,我可以使用 Arduino 板和 I2C 读取操作进行检查。
但是,当我在i.MX6下对用户space执行读取操作时,我只得到0xFF
值。
这是我的代码:
#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 值的几乎相同的问题。
正如@Andrew Cottrell 在 中所说:“要从您的 I2C 设备读取,假设它使用一个字节的寄存器,写入一个字节的缓冲区(寄存器地址)然后读取一个或多个字节的缓冲区(该寄存器和后续寄存器中的值)。
所以正确的代码如下:
#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()
等待,第二次写操作可能会失败。
我目前正在研究 i.MX6(Android BSP)和 24C08WP EEPROM 之间的 I2C 通信。
我 运行 在 i.MX6 上是一个二进制文件,之前是在 Linux 下的 NDK 下编译的。
借助 i2cdetect
工具,我检测到连接到 i.MX6 的 I2C 总线(地址 0x50
)的 NTAG 5 组件。
使用以下代码,我可以执行写入操作,我可以使用 Arduino 板和 I2C 读取操作进行检查。
但是,当我在i.MX6下对用户space执行读取操作时,我只得到0xFF
值。
这是我的代码:
#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;
}
你能帮帮我吗?
正如@Andrew Cottrell 在
所以正确的代码如下:
#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()
等待,第二次写操作可能会失败。