i2c_smbus_write_byte_data not working on the Raspberry Pi 3

100 Views Asked by At

I am trying to access the mpu6050 with a C program in userspace and I have 2 helper functions to let this take place:

unsigned char readMPU(unsigned char reg) {
    unsigned char res;

    /* Using SMBus command */
    res = i2c_smbus_read_byte_data(file, reg);
    if (res < 0) {
        printf("Reading From: %X failed\n", reg);
    } else {
        //printf("%d", res);
        return res;
    }
}

unsigned char writeMPU(unsigned char reg, unsigned char val) {
    unsigned char res;

    /* Using SMBus command */
    printf("%x %x \n", reg, val);
    res = i2c_smbus_write_byte_data(file, reg, val);
    if (res < 0) {
        printf("Writing: %X at register %X failed\n", val, reg);
    } else {
        return res;
    }
}

Over here in the writeMPU function I notice that the sensors values aren't being modified. When the mpu6050 starts the register 0x6b needs to be set to 0. I am able to set the register to 0 via i2cset, but I can’t set it to 0 using the writeMPU function.

Has someone dealt with this problem before? The kernel docs provide no further help. This is how I am calling it:

writeMPU(0x6b, 0x00);

I expect the register value to change, which i can do manually using i2cset, and the read function works perfectly. I can't get the write to work.

0

There are 0 best solutions below