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

power optimal twi tx/rx

Hi - I am wondering what is the power optimal way to read/write with the TWI module. I do a tx and rx with the TWI interface. I have something like this, from the examples to read data from a i2c supported device:

// read from i2c device

uint16_t timeout = 10000;
nrf_drv_twi_tx();

while((!twi_tx_done) && --timeout); // twi_tx_done is in the twi_handler for NRF_DRV_TWI_XFER_TX event
twi_tx_done = false;

timeout = 10000;
nrf_drv_twi_rx();
while((!twi_rx_done) && --timeout); // twi_rx_done is in the twi_handler for NRF_DRV_TWI_XFER_RX event
twi_rx_done = false;

I am not sure if this is the most optimal way of waiting, since it's in a busy loop. Is there a better way to write this?

  • Thanks awneil.

    I have expanded on my question in my reply to simonr. My example is simple where I want to read a register. So What would be my options instead of waiting(using delay) for the twi handler to return an event to indicate a successful read.

  • Hi

    What example is your application based on? Do you have an event handler that the callback goes to after you've called a TWI event? If you do have a callback, you can just wait for that using the idle_state_handle() function, similar to what's done in many of our examples. If this callback is in another file than your main.c file, you can forward it to main.c in order to make it easier.

    Best regards,

    Simon

  • I am referring to twi _sensor where a twi_handler callback is used. The example there has a while wait loop until "m_xfer_done" is set . So it becomes a busy loop. Is there a better way to implement it without busy-looping? Also, what would happen if I get an address nack or data nack.  Things start getting messier once I start having a flag for each state(like rx/tx /addr nack/ data nack). 

    I think the SDK I am using doesn't yet have the twi manager. 

  • Hi

    What SDK version are you using? If you're using a very old one (before SDK v.14.x) I'd suggest you update it due to implementations of various bug fixes, as well as more and updated example projects.

    An address/data nack should not cause any issues in our TWI examples, you'll just have to resend the transfer. You can take a look at this thread, where a power optimization method is proposed by using the app_timer.

    Best regards,

    Simon

Related