HI @giuliomoro . I've been using your code to drive a OLED display that includes an i2C library from Vinay Divakar. That works great just writing via i2C. When I try to use the same library to read via i2C, I can't manage to get the read register function to return data unless I modify it..
Original code in i2C.c
int i2c_read_register(int fd, unsigned char read_addr, unsigned char *read_data)
{
int ret = 0;
if(i2c_write(fd, read_addr) == -1)
{
perror("I2C: Failed to write |");
return -1;
}
ret = read(fd, &read_data, I2C_ONE_BYTE);
if(ret == -1)
perror("I2C: Failed to read |");
if(ret == 0)
perror("I2C: End of FILE |");
return(ret);
}
I believe this only passes a copy of the read_data pointer to the function, so external to this function *read_data doesn't change. I changed the code to pass a 'pointer to a pointer' and then *read_data does change outside of the function. following this
int i2c_read_register(int fd, unsigned char read_addr, unsigned char **read_data)
{
int ret = 0;
if(i2c_write(fd, read_addr) == -1)
{
perror("I2C: Failed to write |");
return -1;
}
ret = read(fd, *read_data, I2C_ONE_BYTE);
if(ret == -1)
perror("I2C: Failed to rread |");
if(ret == 0)
perror("I2C: End of FILE |");
return(ret);
}
Most likely I am failing to grasp some fundamental coding concept. On the other hand, if this particular i2c code library has some problems, is there a preferred set of i2c commands that I should be using?