This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

TWI issue migrating SDK8 -> SDK11

I'm migrating an app from SDK8 to SDK11 and I'm getting errors using TWI to communicate with my IMU (Invensense 9250). I first tried it on a nRF52 DK (Eng B) and then went back to my 51 DK to rule out any hardware issues.

I'm using nrf_drv_twi in a very simple manner on both SDKs (blocking mode for SDK11) and I've tried both TWIM and legacy TWI on SDK11.

My read and write functions need to be called from the Invensense MotionDriver library and look like this on SDK8:

int nrf51_i2c_write(unsigned char slave_addr,
					   unsigned char reg_addr,
					   unsigned char length,
                       unsigned char const *data) 
{
	ret_code_t err_code;

	err_code = nrf_drv_twi_tx(&twi, slave_addr, &reg_addr, sizeof(reg_addr), true);
	APP_ERROR_CHECK(err_code);
	err_code = nrf_drv_twi_tx(&twi, slave_addr, data, length, false);
	APP_ERROR_CHECK(err_code);
    
    return err_code;
}

int nrf51_i2c_read(unsigned char slave_addr,
                    unsigned char reg_addr,
                    unsigned char length,
                    unsigned char *data)
{
	ret_code_t err_code;

	err_code = nrf_drv_twi_tx(&twi, slave_addr, &reg_addr, sizeof(reg_addr), true);
	APP_ERROR_CHECK(err_code);
	err_code = nrf_drv_twi_rx(&twi, slave_addr, data, length, false);
	APP_ERROR_CHECK(err_code);
    
    return err_code;
}

And are virtually identical for the SDK11 version, minus the bool flag param on the nrf_drv_twi_rx call.

I've attached Logic captures for both versions, which show a few successful writes, and then an error trying to write to the compass I2C address. The TWI driver returns an internal error (3).

Thanks for any suggestions.

EDIT: attached a basic example project.

LogicCaptures.zip twitest.zip

  • You are receiving a NACK and the function (nrf_drv_twi_tx or nrf_drv_twi_rx) is returning NRF_ERROR_INTERNAL, which is done when no event handler is used. APP_ERROR_CHECK will either reset your chip or go into an infinite while loop depending on if DEBUG is defined, see here. Judging your capture and the problem it looks like it's in an infinite while loop (not doing anything).

    You should add an if sentence to handle NRF_ERROR_INTERNAL from the twi transfer functions or use twi with event handler and check for NACK in this handler.

  • Yes, I defined DEBUG so it would stop in the endless loop and I could get a smaller logic trace. I was not aware that the NRF_ERROR_INTERNAL was expected from a NACK when not using an event handler, thank you for clarifying.

  • I had to go into the function to see what caused it to return NRF_ERROR_INTERNAL. Ideally we should have a better return/error code. I will make a request internally for this.

  • I have made changes to handle the error code, but it is still not working under SDK 11. SDK 8 is still working fine. I have attached a sample project with a brief readme.txt to explain the issue. Any further suggestions would be very helpful, thank you.

  • The project does not compile out of the box, can you upload a project that does?

Related