Can't access to i2c with my c program

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


It seems like you’re encountering an issue where your C program is unable to communicate with the I2C temperature sensor, even though i2cdetect can see the sensor. Here are a few steps and considerations to help troubleshoot and resolve the issue:

  1. Verify I2C Device Path:
    Ensure that the I2C device path you are using in your code is correct. Based on your context, it seems like you have tried multiple paths. The correct path should be /dev/i2c-3 if i2cdetect -r -y 3 works. So, you should set:

    #define I2C_DEVICE "/dev/i2c-3"
    
  2. Check Permissions:
    Ensure that your Docker container has the necessary permissions to access the I2C device. You are already using --device-cgroup-rule flags, but you might also want to try running the container with the --privileged flag to rule out any permission issues:

    docker run --rm -it --privileged -v /dev:/dev imagelora
    
  3. Verify I2C Communication in Code:
    Double-check the I2C communication code in your chip_id program. Ensure that the I2C initialization and communication functions are correctly implemented. You might want to add additional debug prints to verify that the I2C device is being opened and accessed correctly.

  4. Test I2C Communication in Isolation:
    Create a simple standalone C program to test I2C communication with the temperature sensor. This can help isolate whether the issue is with the I2C communication code or with the integration in the chip_id program. Here is a basic example:

    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    #include <linux/i2c-dev.h>
    
    #define I2C_DEVICE "/dev/i2c-3"
    #define TEMP_SENSOR_ADDR 0x3B
    
    int main() {
        int file;
        char filename[20];
        snprintf(filename, 19, I2C_DEVICE);
        file = open(filename, O_RDWR);
        if (file < 0) {
            perror("Failed to open the i2c bus");
            return 1;
        }
    
        if (ioctl(file, I2C_SLAVE, TEMP_SENSOR_ADDR) < 0) {
            perror("Failed to acquire bus access and/or talk to slave");
            return 1;
        }
    
        // Add code to read/write to the sensor here
    
        close(file);
        return 0;
    }
    
  5. Check I2C Bus Activity:
    Use an oscilloscope or logic analyzer to monitor the I2C bus activity when running your chip_id program. This can help determine if the program is attempting to communicate with the sensor and if there are any signals on the bus.

  6. Review Dockerfile and Build Process:
    Ensure that your Dockerfile and build process correctly compile and link the I2C communication code. Verify that there are no missing dependencies or incorrect configurations.

If you have tried all the above steps and still face issues, it might be helpful to share more details about the I2C communication code in your chip_id program and any additional logs or error messages you receive.

Please, also check if these links can help you solve your problem:

AM62 Gpio sys/class computation - Driver Lora.