This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF51422 TWI not functioning properly

Hello, i have a strange situation here with TWI module and i have no idea what is happening. I have read a lot of posts here on devzone and the code i am using is also found somewhere on this forum, however in none of the post i found the same behaviour which i am seeing. I have a temperature sensor which is driven via i2c, so for this i am trying to use TWI. Piece of code which i am using is atached in the picture below.image description

If i am letting the code to run, then i hit a break-point at line 283. After i let the code to run again, i hit a break-point at line 286. At line 285 i am sending data to sensor with ret_code = nrf_drv_twi_tx(&p_twi_instance, 0x40, &dummy_data, 1, false); In this case, this is what i am seeing with oscilloscope on SCL and SDA lines: image description

If i remove a break-point at line 283 and allowing the code to run, i hit a break-point at line 286 and this is what i am seeing with oscilloscope in this case: image description

Also, this time i see in TWI1 events, that EVENTS_ERROR is set. Also, in TWI1_ERRORSRC register i have ANACK bit set. After this i have tried to do my own version of twi communication with sensor by modifying TWI1 module registers directly. However, that gives exactly the same results. Putting the break-point between sending data, and then running the code again - communication is seen. If allowing the code to run without any breakpoints, device gives NACK and communication fails.I don't understand what is causing this behaviour. Any help is appreciated. By the way, i am using SoftDevice S110_V8.

@ EDIT: After adding delay after TWI module initialization, and sending data. I don't understand however what is causing small voltage drop just few ms before starting communication?: delay.png

  • The first trace looks good, the sensor ACK the transacation and the data is written on the twi bus. The second trace the sensor NACK the transaction for unknown reason (maybe it's not ready to receive data). The third trace show a small dip in supply voltage some time before the twi transacation, most likely some circuitry is drawing current, this is normal.

    My recommendation is to check that you are using an nRF51 of 3rd revision and update to the latest nRF5 SDK if you haven't already. If the sensor NACK, then add a small delay and retry. Make sure to set breakpoints after the complete transacation is finished (even if it fails), either by using blocking twi calls or use a flag from the event handler. This make it easier to see if the twi driver send the stop condition as expected after an NACK, and is ready for a new transfer.

  • Hello Keneth, thank you for your support. Your comments helped me to solve the problem. It was a timing problem. I missed that my sensor takes about 10ms to do a measurement conversion. Incredible how these obvious things can cause so much trouble.. Shows that still have to learn a lot and to think a lot about doing something new ;) I will update to newest SDK. However i want to ask one more thing. Right now am i missing something, or this TWI module written by nordic only uses TWI1? I don't see a way to change it to TWI0. IS that possible? Thanks again Kenneth, Regards.

  • Typically the twi instance used must be defined when calling nrf_drv_twi_init(), e.g. in the following way:

    static const nrf_drv_twi_t m_twi_mma_7660 = NRF_DRV_TWI_INSTANCE(0);
    

    You will also need to enable/modify the twi instance in nrf_drv_config.h

Related