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

Blocking & Non-Blocking operations on TWI

Hello,

 

I am using nRF52832, SDK_15.3.0, S132 SoftDevice and Segger for flashing the image. I am using ‘ble_app_blinky’.

I connected my sensor to nRF52832 DK over TWI. When ‘twi_scanner’ example code loaded I received the address of the slave device.

Just to crosscheck I ported ‘twi_scanner’ as is into ‘ble_app_blinky’. Even here it worked fine.

 

1) But later I have gone through ‘twi_senor’ example and changed

From     :  nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);

To          :  nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL);

 When ‘twi_handler’ added, I get success for all 127 address. I am not sure why this happened.

    for (address = 1; address <= TWI_ADDRESSES; address++)
    {
        err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
        if (err_code == NRF_SUCCESS)
        {
            detected_device = true;
            NRF_LOG_INFO("TWI device detected at address 0x%x.", address);
        }
        NRF_LOG_FLUSH();
    }

2) Under ‘nrf_drv_twi_init’ for event handler it is mentioned as “If NULL, blocking mode is enabled”. What does it mean.

Thanks & Regards

Vishnu Beema

  • When ‘twi_handler’ added, I get success for all 127 address. I am not sure why this happened.

     What does your twi_handler function look like? 

    Under ‘nrf_drv_twi_init’ for event handler it is mentioned as “If NULL, blocking mode is enabled”. What does it mean

    "Blocking communication means that the MCU stalls until the byte is transferred from the data register. In the code, this is usually implemented as checking of the status bit in the “while” loop. However, this approach might not be applicable for some time-critical applications"

    The TWI Scanner uses blocking mode. To enable non-blocking mode, you can add a twi_handler. 

    Please see the TWI master documentation on basic usage.

  • Thank you for your inputs.

    1)  twi_handler() is same as in 'twi_sensor' example.

    2) If 'twi_scanner' example is blocking mode, then can I assume 'twi_sensor' as non-blocking mode. But still due to 'while (m_xfer_done == false)' whether 'twi_sensor' code will behave as a blocking mode. Please correct me if I am wrong.

    Thanks & Regards

    Vishnu Beema

  • Just to clarify, as I just realized you wrote " I have gone through ‘twi_senor’ example ":

    You have ported the twi_scanner example into ble_blinky and configured this as the twi_sensor example including the twi_handler?

  • Yes, in ble_app_blinky, I mixed both 'twi_scanner' to read the address and 'twi_sensor' to add twi handler.

  • beemavishnu said:
    Yes, in ble_app_blinky, I mixed both 'twi_scanner' to read the address and 'twi_sensor' to add twi handler.

     Ok, thanks! 

    beemavishnu said:
    If 'twi_scanner' example is blocking mode, then can I assume 'twi_sensor' as non-blocking mode. But still due to 'while (m_xfer_done == false)' whether 'twi_sensor' code will behave as a blocking mode. Please correct me if I am wrong.

     TWI_sensor is considered as a non-blocking example since it uses a twi_handler, at least the twi driver is configured as non-blocking. However, I agree that the example is confusing when adding the flag m_xfer_done, when polling this flag the example could potentially be blocking.

    Hope this clears some confusion. 

    Kind regards,
    Øyvind

Related