i2c_write_read_dt function issue

Hi,

I am checking the i2c protocol with nRF Connect SDK and Zephyr through VSCode.

I created a very simple example where I push a button on the nRF52840 DK and I get the ID of an IMU sensor which is 0xEA.

When I use the i2c_write_dt() and i2c_read_dt() functions, I get the ID as expected (first code):

LED TOGGLE
Who Am I: EA

                uint8_t wArray[1] = {0x00}; // Register address to read from
                uint8_t rBuff = 0x00;       // Buffer to store the read value
                uint8_t a = 0;
            
                a = i2c_write_dt(&m_i2c, wArray, 1);
                if (a != 0) {
                 printk("I2C write failed with error %d\n", a);
                }

                a = i2c_read_dt(&m_i2c, &rBuff, 1);
                if (a != 0) {
                     printk("I2C read failed with error %d\n", a);
                }
                printk("Who Am I: %X\n", rBuff);

when I use the i2c_write_read_dt(), I get an error from the function

LED TOGGLE
I2C error 251
Who Am I: 0

                uint8_t wBuff[1] = {0x00};
                uint8_t rBuff = 0x00;
                uint8_t a = 0;

                a = i2c_write_read_dt(&m_i2c, wBuff, sizeof(wBuff), &rBuff, sizeof(rBuff));
                if(a!= 0){printk("I2C error %d\n", a);}
                printk("Who Am I: %X\n", rBuff[0]);

Shouldn't the two cases work the same way? Am I missing something?

Thank you!

Parents
  • Hello,

    I am sorry for the late reply. I didn't get to the bottom of this today, but I will continue to look into it tomorrow. 

    NB: The return value is probably not 251, but -5. The return value that you store in a is not an uint8_t. It should be an "int". If you want to investigate, you can look into ncs\zephyr\drivers\i2c\i2c_nrfx_twim.c, and look for the function i2c_nrfx_twim_transfer(). I guess this is the one that returns -5 (-EIO). I guess it is the call to nrfx_twim_xfer() on line 136 (at least this line in NCS v2.6.1), that returns something != NRFX_SUCCESS and != NRFX_ERROR_BUSY, causing the function to return -EIO. Can you try to debug the nrfx_twim_xfer function to see what it does?

    Please check that the "sizeof(wBuff)" and "sizeof(rBuff)" are returning what you expect them to return by printing them in the log.

    Best regards,

    Edvin

Reply
  • Hello,

    I am sorry for the late reply. I didn't get to the bottom of this today, but I will continue to look into it tomorrow. 

    NB: The return value is probably not 251, but -5. The return value that you store in a is not an uint8_t. It should be an "int". If you want to investigate, you can look into ncs\zephyr\drivers\i2c\i2c_nrfx_twim.c, and look for the function i2c_nrfx_twim_transfer(). I guess this is the one that returns -5 (-EIO). I guess it is the call to nrfx_twim_xfer() on line 136 (at least this line in NCS v2.6.1), that returns something != NRFX_SUCCESS and != NRFX_ERROR_BUSY, causing the function to return -EIO. Can you try to debug the nrfx_twim_xfer function to see what it does?

    Please check that the "sizeof(wBuff)" and "sizeof(rBuff)" are returning what you expect them to return by printing them in the log.

    Best regards,

    Edvin

Children
Related