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

TWI timeout - SDK11

Hi,

I'm using the nrf_drv_twi driver (master) to communicate with a number of IO components, such as LEDs, buttons etc.

If my IO component does not respond (why is not relevant to this question, but it does...), the code hangs (nrf_drv_twi.c, line 450)

while (twi_transfer(p_twi, &p_cb->error, &p_cb->bytes_transferred, (uint8_t *)p_data, length, no_stop))
        {}

How would one add a timeout?

  • Modifyexisting code?
  • Use the driver in non-blocking mode?
  • Wait for Nordic to add this feature?

Thank you Matthias

  • We ended up doing this (using embOS):

    - in your read/write function ----------------------------------------

    if (nrf_drv_spi_transfer (&mSpi, setup, 3, NULL, 0) != NRF_SUCCESS)
    {
        nrf_gpio_pin_set (PIN_FRAM_CS);
        return (NRF_ERROR_INTERNAL);
    }

    if (! transferCompleted (DEVICE_TIMEOUT))
    {
        nrf_gpio_pin_set (PIN_FRAM_CS);
        return (NRF_ERROR_TIMEOUT);
    }

    - here is yout transferCompleted function -------------------------------

    static bool transferCompleted (int ms)
    {
        bool tc = (OS_EVENT_WaitTimed (&mTCevent, ms) == 0);

        return (tc); /* Return true to indicate successful transfer */
    }

    - here is what the respective interrupt service routine does ----------

    static void onTransferComplete (nrf_drv_spi_evt_t const * p_event, void * p_context)
    {
        OS_EVENT_Set (&mTCevent); /* All bytes transferred */
                                                          /* --> signal main thread */
    } /* (see transferCompleted ()) */

  • seems like a good solution. The whole point of open source is to add/modify features to your liking, which you did :)

Related