0

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.

0andriy
  • 4,183
  • 1
  • 24
  • 37
  • 1
    Checking if an `unsigned char` is less than `0` isn't going to do anything useful. – pmacfarlane Jul 31 '23 at 10:44
  • Why are you reinventing a wheel? https://elixir.bootlin.com/linux/latest/source/drivers/iio/imu/inv_mpu6050 is the driver, use it! – 0andriy Jul 31 '23 at 13:17
  • If a function has a return type other than `void` then all paths through the function must return a value. Your `writeMPU` and `readMPU` functions do not return a value on error. (The exception to the rule is the `main()` function.) – Ian Abbott Jul 31 '23 at 13:23

0 Answers0