rk platform connects NT3H2111_2211 on i2c; I want to write data to NT3H2111_2211 via i2c in user mode application, but it cannot write; The read data always looks like the following:
Is there read/write protection? What should I configure or do before writing data to NT3H2111_2211 via i2c? What should I configure or do before reading data from NT3H2111_2211 via i2c?
Development board :rk3568
Here's the demo, shown below
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#define I2C_FILE_NAME "/dev/i2c-3"
#define I2C_ADDR 0x55
int fd;
int i2c_open()
{
fd = open(I2C_FILE_NAME, O_RDWR);
if(fd < 0){
perror("Unable to open i2c controlfile");
return 1;
}
printf("open %s success\n", I2C_FILE_NAME);
return 0;
}
int i2c_write(int fd, unsigned char dev_addr, unsigned char reg_addr, unsigned charval)
{
unsigned char buf[2];
struct i2c_rdwr_ioctl_data data;
struct i2c_msg messages;
buf[0] = reg_addr;
buf[1] = charval;
messages.addr = dev_addr; //device address
messages.flags = 0; //write
messages.len = 2;
messages.buf = buf; //data address
data.msgs = &messages;
data.nmsgs = 1;
if(ioctl(fd, I2C_RDWR, &data) < 0){
printf("write ioctl err\n");
return 1;
} else {
//printf("write 0x%x success\n",charval);
}
usleep(1000);
return 1;
}
int i2c_read(int fd, unsigned char addr, unsigned char reg, unsigned char *val)
{
struct i2c_rdwr_ioctl_data data;
struct i2c_msg messages[2];
messages[0].addr = addr; //device address
messages[0].flags = 0; //write
messages[0].len = sizeof(reg);
messages[0].buf = ® //data address
messages[1].addr = addr; //device address
messages[1].flags = I2C_M_RD; //read
messages[1].len = sizeof(val);
messages[1].buf = val;
data.msgs = messages;
data.nmsgs = 2;
if(ioctl(fd, I2C_RDWR, &data) < 0){
printf("read ioctl err\n");
return 1;
}
return 0;
}
int main()
{
unsigned int i;
unsigned char buf[128];
unsigned char val[] = {
0xAA, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0xE1, 0x10, 0x6D, 0x00,
0x03, 0x5F, 0x91, 0x02,
0x35, 0x53, 0x70, 0x91,
0x01, 0x14, 0x54, 0x02,
0x65, 0x6E, 0x4E, 0x54,
0x41, 0x47, 0x20, 0x49,
0x32, 0x43, 0x20, 0x45,
0x58, 0x50, 0x4C, 0x4F,
0x52, 0x45, 0x52, 0x51,
0x01, 0x19, 0x55, 0x01,
0x6E, 0x78, 0x70, 0x2E,
0x63, 0x6F, 0x6D, 0x2F,
0x64, 0x65, 0x6D, 0x6F,
0x62, 0x6F, 0x61, 0x72,
0x64, 0x2F, 0x4F, 0x4D,
0x35, 0x35, 0x36, 0x39,
0x54, 0x0F, 0x13, 0x61,
0x6E, 0x64, 0x72, 0x6F,
0x69, 0x64, 0x2E, 0x63,
0x6F, 0x6D, 0x3A, 0x70,
0x6B, 0x67, 0x63, 0x6F,
0x6D, 0x2E, 0x6E, 0x78,
0x70, 0x2E, 0x6E, 0x74,
0x61, 0x67, 0x69, 0x32,
0x63, 0x64, 0x65, 0x6D,
0x6F, 0xFE, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
i2c_open();
memset(buf, 0, 128);
for(i = 0; i < 0xef; i++){
if(i2c_read(fd, I2C_ADDR, i,&buf[i])){
printf("Unable to get register!\n");
}
}
for(i=0; i< 0xef;i++) {
printf("buf[%d]=-0x%02x ",i,buf[i]);
if (!(i % 9))
printf("\n");
}
printf("---------write-------------before--------\n");
printf("************write************begin************\n");
for(i = 0; i < sizeof(val); i++) {
i2c_write(fd, I2C_ADDR, i, val[i]);
}
printf("************write************end************\n");
printf("---------write-------------after----to read----\n");
memset(buf, 0, 128);
for(i = 0; i < 0xef; i++){
if(i2c_read(fd, I2C_ADDR, i,&buf[i])){
printf("Unable to get register!\n");
}
}
for(i=0; i< 0xef;i++) {
printf("[%d]=0x%02x ",i,buf[i]);
if (!(i % 9))
printf("\n");
}
close(fd);
return 0;
}