Accessing 16-bit registers through i2c

Hello, I am currently working with VEML6035 light sensor and so far i had a hard time working with its 16-bit registers. 

The code that i wrote:

uint8_t i2c_handler::write_16bit(uint8_t registerAddress, uint16_t data){
    uint8_t buff[3];
    buff[0] = registerAddress;
    buff[1] = data & 0xFF;
    buff[2] = (data >> 8) & 0xFF;
    uint8_t ret;
    ret = i2c_write_dt(_handler, buff,3);
    if (ret != 0) {
        printk("Failed to write to 16-bit register of I2C device at Reg. %x (error %d)\n", registerAddress,ret);
        return ret;
    }
    return 0;
}
uint8_t i2c_handler::read_16bit(uint8_t registerAddress, uint16_t* dataDestination){
    uint8_t ret;
    uint8_t data[2];
    ret = i2c_write_read_dt(_handler, &registerAddress, 1, data, 2);
    if (ret != 0) {
        printk("Failed to write/read 16-bit register of I2C device at Reg. %x  (error %d)\n", registerAddress,ret);
        return ret;
    }
    *dataDestination = (data[1]<<8) | data[0];
    printk("Data from read: %x \r\n",*dataDestination);
    return 0;
}

overlay file:

&i2c0 {
    status = "okay";
    clock-frequency = <I2C_BITRATE_STANDARD>;
    veml6035: veml6035@29{
        compatible = "i2c-device";
        reg = <0x29>;
        label = "VEML6035 Ambient Light Sensor";
    };
};

I can't get that to work. Returns error 251 on write/read. Is that a proper way of setting up writing/reading of 16-bit registers?

Parents
  • Hello,

    To debug i2c issues, do take an logic analyzer trace so you can observe the data, and compare with the figure you showed. This will tell much about the problem, e.g. is there pull-up, is the address right, is read/write set, is the 16-bit address correct MSB/LSB, is the data ACK or NACK, is start and restart correct etc.

    Kenneth

Reply
  • Hello,

    To debug i2c issues, do take an logic analyzer trace so you can observe the data, and compare with the figure you showed. This will tell much about the problem, e.g. is there pull-up, is the address right, is read/write set, is the 16-bit address correct MSB/LSB, is the data ACK or NACK, is start and restart correct etc.

    Kenneth

Children
No Data
Related