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

TWI: NRF52832 not sleeping while waiting for callback

Hello everyone,

I have a problem with the power consumption of the NRF52 during TWI operations with tw0 HTU21DF temperature and humidity sensors.

I am using NRF52832 with SDK 15.0.0 and softdevice S132 V6.00  on a custom board.

I use 2 different twi interfaces for 2 separate sensors, and I use non blocking operations; twi readings are periodical and triggered by a timer.

All readings are correct, the callback are called normally, but the problem I get is that reading from both sensors consumes about 10 mA for 100 ms,

which in my opinion is an indication that the cpu does not go properly to sleep while waiting for the callbacks. 

Here is my code for init of the twi interfaces 

/**@brief Initialization of the TWI interfaces 
*/
void twi_init (void)
{
    ret_code_t err_code;

    err_code = nrf_drv_twi_init(&m_twi_one, &twi_one_config, twi_OneCallback, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi_one);


    err_code = nrf_drv_twi_init(&m_twi_two, &twi_two_config, twi_TwoCallback, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi_two);

}

Here is the cycle for reading humidity and temperature as required by the sensor datasheet (I report only one of the functions, the other is identical)

static void read_twi_one_data()
{

    ret_code_t err_code;
    twi_one_done = false;
    err_code = nrf_drv_twi_tx(&m_twi_one, HTU21DF_I2CADDR, htu21cmd_ReadHum, sizeof (htu21cmd_ReadHum), false);
    APP_ERROR_CHECK(err_code);
    do {
      idle_state_handle();
    }while(!twi_one_done);


    twi_one_done = false;
    err_code = nrf_drv_twi_rx(&m_twi_one, HTU21DF_I2CADDR, twi_oneHumidity, sizeof(twi_oneHumidity));
    do {
      idle_state_handle();
    }while(!twi_one_done);
        twi_one_done = false;
    err_code = nrf_drv_twi_tx(&m_twi_one, HTU21DF_I2CADDR, htu21cmd_ReadTemp, sizeof (htu21cmd_ReadTemp), false);
    do {
      idle_state_handle();
    }while(!twi_one_done);
        twi_one_done = false;
    err_code = nrf_drv_twi_rx(&m_twi_one, HTU21DF_I2CADDR, twi_oneTemperature, sizeof(twi_oneTemperature));
    do {
      idle_state_handle();
    }while(!twi_one_done);
        APP_ERROR_CHECK(err_code);


}

And finally here is the code managing the callback where I simply put the flag to true to exit from the while cycle

void twi_OneCallback(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    switch (p_event->type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            twi_one_done = true;
            break;
        default:
            break;
    }
}

At the end of the operations, I uninit and disable the TWIs to save energy.

I read several discussions on the forum, and almost everywhere I found that this is the correct way to handle TWI communications, but it seems to me that the idle_state_handle() function is actually not putting the device to sleep while waiting for the callback.

I activated the EASY_DMA for the TWI.

Any idea would be welcome, because 10mA for 100ms is quite a big consumption for our needs (when sampling at fastest rate, we may need one sample every second, which will mean a consumption of 10mA, for 100 ms every second....).

Thanks in advance.

Parents
  • Hi,

     

    10 mA is quite a lot, even if the CPU is running. What is the expected current draw from the temp-sensors ? From the datasheet, it looks like it should be ~0.5 mA, and since you're polling them synchronously, it should only add 0.5 mA to the average current consumption.

    Q1: Are you doing something else in your program? Like advertising or similar.

    Q2: Do you see this issue if you only perform the TWI operations (and nothing else) in your application?

    Q3: I'd recommend that you check your err_code returns, it might be that one of the calls fails for some reason. Only resetting the flag on evt "NRF_DRV_TWI_EVT_DONE" can give you problems if the I2C slave NACKs a transaction.

     

    Best regards,

    Håkon

Reply
  • Hi,

     

    10 mA is quite a lot, even if the CPU is running. What is the expected current draw from the temp-sensors ? From the datasheet, it looks like it should be ~0.5 mA, and since you're polling them synchronously, it should only add 0.5 mA to the average current consumption.

    Q1: Are you doing something else in your program? Like advertising or similar.

    Q2: Do you see this issue if you only perform the TWI operations (and nothing else) in your application?

    Q3: I'd recommend that you check your err_code returns, it might be that one of the calls fails for some reason. Only resetting the flag on evt "NRF_DRV_TWI_EVT_DONE" can give you problems if the I2C slave NACKs a transaction.

     

    Best regards,

    Håkon

Children
No Data
Related